Complete roguelike tutorial using C++ and libtcod - part 5: preparing for combat
Revision as of 15:04, 6 October 2015 by Joel Pera (talk | contribs) (pasted →Don't step on my shoes)
Complete roguelike tutorial using C++ and libtcod -originally written by Jice Text in this tutorial was released under the Creative Commons Attribution-ShareAlike 3.0 Unported and the GNU Free Documentation License (unversioned, with no invariant sections, front-cover texts, or back-cover texts) on 2015-09-21. |
---|
|
In this part, we'll do some refactoring to prepare the monster bashing. This includes detecting actor collisions (an actor trying to step on a tile where there already is an actor) and properly handling game turns.
Don't step on my shoes
First, we want to detect when the player tries to walk on a Tile occupied by another actor as this is the basis for melee combat. We keep the Map::isWall function but we add a Map::canWalk that handles both walls and actors.
In Map.hpp :
bool canWalk(int x, int y) const;
And the implementation in Map.cpp :
bool Map::canWalk(int x, int y) const { if (isWall(x,y)) { // this is a wall return false; } for (Actor **iterator=engine.actors.begin(); iterator!=engine.actors.end();iterator++) { Actor *actor=*iterator; if ( actor->x == x && actor->y == y ) { // there is an actor there. cannot walk return false; } } return true; }
First, the function checks if there is a wall. Else, it scans the actor list and returns false if there is an actor at given position.