Skip to content

Instantly share code, notes, and snippets.

@frostney
Created November 24, 2013 14:04
Show Gist options
  • Save frostney/7627609 to your computer and use it in GitHub Desktop.
Save frostney/7627609 to your computer and use it in GitHub Desktop.
Game engine experiment in CoffeeScript
###
This a small 1h experiment on creating a game environment which should look similar to a templating engine or QML.
This is just a mockup and may be developed into a game engine in the near future.
###
# Create a new game instance (It would be cool if you wouldn't need an instance and could just write "Game")
new Game ->
# Create a scene within the game
@scene ->
# Create an entity within the scene and set properties
@entity
name: 'Background'
image: 'background.png'
# Create an entity, but use a function instead which allows more control
@entity ->
@x = 50
@y = 100
@image = 'monster.png'
@on 'update', => @x = @x + 10
# Start everything up
@run()
###
Well, you've seen my vision in the above snippet
How far did I get in about an hour?
* Game and Scene class
* Implemented class can either have a function or an object to describe the object
* Scenes are correctly bound to the Game instance
* Game#scene also allows for a scene instance as a parameter
###
class Base
constructor: (@parent, descriptor) ->
if typeof descriptor is 'function'
descriptor.call @
else
# TODO: Don't overwrite prototype methods
@[key] = value for key, value of descriptor
log: ->
class Scene extends Base
class Game extends Base
constructor: (descriptor) ->
@scenes = []
super null, descriptor
scene: (obj) ->
scene = if obj instanceof Scene
obj.parent = @
obj
else
new Scene @, obj
@scenes.push scene
new Game ->
@scene ->
@frostney
Copy link
Author

This experiment will be developed into a real project, see: https://github.com/freezedev/chione

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment