Skip to content

Instantly share code, notes, and snippets.

@frostney
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frostney/10783806 to your computer and use it in GitHub Desktop.
Save frostney/10783806 to your computer and use it in GitHub Desktop.
Prototyping GameObject-/Behavior-Relationship
class Behavior
constructor: (descriptor) ->
mixedice [@, @::], new EventMap()
@type = 'Behavior'
@name = "#{@type}-#{Date.now()}"
@children = {}
@parent = null
descriptor.call @
@on 'update', (dt) =>
for key, value of @children
value.update dt
null
addBehavior: (behavior) ->
@children[behavior.name] = behavior
behavior.parent = @
class GameObject
constructor: (descriptor) ->
mixedice [@, @::], new EventMap()
@type = 'GameObject'
@name = "#{@type}-#{Date.now()}"
@children = {}
@parent = null
descriptor.call @
@on 'update', (dt) =>
for key, value of @children
value.update dt
null
addGameObject: (gameObject) ->
@children[gameObject.name] = gameObject
gameObject.parent = @
addBehavior: (behavior) ->
@children[behavior.name] = behavior
behavior.parent = @
class Scene
constructor: (descriptor) ->
mixedice [@, @::], new EventMap()
@type = 'Scene'
@name = "#{@type}-#{Date.now()}"
@children = {}
@parent = null
descriptor.call @
@on 'update', (dt) =>
for key, value of @children
value.update dt
null
addGameObject: (gameObject) ->
@children[gameObject.name] = gameObject
gameObject.parent = @
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment