Since we've already covered Tiled in an earlier chapter, we shall skip straight to the contents of the TMX file for a typical level. So, open up level_01.tmx
and you should see something like this:
This particular TMX file has a tileset with a tile of size 90
pixels. The tileset is designed in such a way that you can create any path that you wish by simply placing the appropriate tile.
Now you can see in the Layers window that there are two layers defined: an object layer named
PathObjects
and a tile layer named EnemyPath
. The PathObjects
object layer will provide us with every point where there is a change in the enemy's direction, represented by objects titled WalkPoint_0
, WalkPoint_1
, WalkPoint_2
, and so on.
In addition to this, the object layer also defines another object titled Pumpkin
that is nothing but the screen position of the pumpkin for this level. Let's now briefly glance at the CreateWalkPoints
function from GameWorld.cpp
where the CCTMXTiledMap
object is queried for the enemy's path:
void GameWorld::CreateWalkPoints() { // parse the list of objects CCTMXObjectGroup* object_group = tiled_map_->objectGroupNamed("PathObjects"); CCArray* objects = object_group->getObjects(); int num_objects = objects->count(); CCDictionary* object = NULL; for(int i = 0; i < num_objects; ++i) { object = (CCDictionary*)(objects->objectAtIndex(i)); // save each WalkPoint's position for enemies to use if(strstr(object->valueForKey("name")->getCString(), "WalkPoint") != NULL) { enemy_walk_points_.push_back(ccp( object->valueForKey("x")->floatValue(), object->valueForKey("y")->floatValue())); } } num_enemy_walk_points_ = enemy_walk_points_.size(); }
At the beginning of the function, we query tiled_map_
; that is nothing but a member of class GameWorld
and is a reference to the CCTMXTiledMap
object. We get the object group titled PathObjects
and consequently loop through each object within it.
All that we actually need from these objects is their position, and we take care of that by storing these positions in a vector named enemy_walk_points_
. When an enemy is spawned or starts walking, it is passed this vector so it knows exactly what path it needs to follow.
With these sections covered, we have data to create towers and enemies. We have even written code to create a level, store the path that the enemies must follow, as well as create the waves in which they are to attack. We can now focus on the behavior of the towers and enemies by defining the Tower
and Enemy
classes, respectively.
18.191.168.203