Kaniku (Japanese: かにく) means flesh of a coconut.
Model is the main source of information about game objects. All data flow must come through models.
Models are able to emit events. Views, other models, etc. are allowed to emit events on behalf of the model.
Method to emit event:
playeremit'died'killer: 'red pacman ghost'killerLevel: 10
Method to subscribe to events:
Example of model:
@defaultsx: 0 # the view will update this variabley: 0timeAlive: 0timeAliveScoreFactor: 100@useUpdates:@setTimeAlive@getTimeAlive + dt@computed 'score'depends: 'x''timeAlive''timeAliveScoreFactor': ->@getX + @getTimeAlive * @getTimeAliveScoreFactor
defaults static method sets initial values of model and also generates getters and setters for all listed variables.
So it's important to list variables even with null initial values to let getters and setters to be automatically generated.
Getters and setters are available both method-style (
m.setX(1)) and property-style (
m.x = 1).
Generated setters emit event
You can define computed variables via
computed static method. This method makes change event of this computed variable to be emited on depending variable change. Also it creates property alias for getter.
useUpdates method requests controller to call model's
update method on every frame. The first argument of
update method is time between frames in seconds.
To get defaults of model you can use method
getDefaults which is available both for class and for instances. To get all variables of model (including computed) you can use method
View provides interface between model and real world. Usually views are cocos2d-x Node objects which render game objects on the user's screen.
There are no kaniku class to extend from.
Views don't communicate with each other. They communicate through models.
Controller is something like glue between all components of the game. It creates models and views and links them with each other.
Controllers are derived from cocos2d-x Scene class.
Updaters do some global work which may affect multiple views and models. Updaters are called each frame.
Updater can be any object which has
update method (for example,
kaniku.Updater) or it can be just a function.
: ->@playerModel = x: 0y: 100 # you can override initial values@addModel@playerModel@npcModel =@npcModelsetPlayer@playerModel@addModel@npcModel: ->uiLayer =scoreView = # suppose it extends from cc.LabelscoreViewsetPlayer@playerModeluiLayeraddChildscoreView@addChilduiLayer2 # cc.Scene methodgameWorldLayer =playerView =# Suppose this object call physics engine internally and then# update player model with new coodinates.## Score view will receive event that something was changed,# model will compute new score and then# score view will finally update the label on screen using model data.playerViewsetPlayer@playerModelgameWorldLayeraddChildplayerView@addChildgameWorldLayer1: ->@addUpdater@playeron 'change:x'generateGameWorldAfterx