Last active
August 29, 2015 13:59
-
-
Save frostney/10783806 to your computer and use it in GitHub Desktop.
Prototyping GameObject-/Behavior-Relationship
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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