SPUDevelopers / Naturalize

A Cocos2d-x turn based strategy game.
Other
11 stars 7 forks source link

Add unit class #8

Open Isaac-W opened 9 years ago

Isaac-W commented 9 years ago

Unit class should hold all attributes pertaining to a single unit in play, according to Naturalize documentation. For the most part, I think that at least getting the attributes put in place is a start. Looking at the documentation, the methods can mostly all be implemented, with exception of a move() method, which we will look into splitting up once more of the engine comes along in the next week or so.

Isaac-W commented 9 years ago

Assigned to @sanderzinc to cooperate with @adamcarlson in implementing this.

Isaac-W commented 9 years ago

Okay, so I've determined that all objects visible on the screen should subclass Node. So, the Unit class should inherit from public cocos2d::Node. Then, inside the Unit class, you can have all sorts of Sprites that are members of that class. All you need to do is, inside the init() method (which is called when you create the object), add those Sprites as children of the Unit instance. This will associate those Sprites with the Unit instance (the instance itself is technically invisible because we don't specify any custom drawing code, but its children Sprites will be drawn instead).

Any children of a Node will automatically move and scale along with the parent Node, which means that we can directly operate on the Unit class itself without any knowledge of its children Sprites. For example, I have written some sample code that creates a test lumberjack unit and moves him relative to the map. The TestUnit class inherits from Node, and has a Sprite member variable. This Sprite is what holds and draws the lumberjack image. In the init() code for TestUnit, the lumberjack Sprite is added to TestUnit as a child, meaning that the two form a forever inseparable bond of parent and child. Furthermore, in the main GameScene, I create a TestUnit instance and attach that to the map as another child, meaning that, when the map moves, the TestUnit and by extension the lumberjack Sprite move with it. You can run the test and move the map around to see this in action.

tl;dr Look at commit b4850fa97a99880c44e610e5653549a6726aae6f for the TestUnit, and make sure that the Unit class you're designing also inherits from cocos2d::Node in the same way.