Enhance get_direction() to allow to do more than one step in a direction

(also support negative number of steps by giving backward direction)
This commit is contained in:
Ali El Gariani 2007-09-22 00:02:14 +00:00
parent c5dd72d4c5
commit 4646c92f08
2 changed files with 13 additions and 8 deletions

View File

@ -218,15 +218,19 @@ gamemap::location& gamemap::location::operator-=(const gamemap::location &a)
}
gamemap::location gamemap::location::get_direction(
gamemap::location::DIRECTION dir) const
gamemap::location::DIRECTION dir, int n) const
{
if (n < 0 ) {
dir = get_opposite_dir(dir);
n = -n;
}
switch(dir) {
case NORTH: return gamemap::location(x,y-1);
case NORTH_EAST: return gamemap::location(x+1,y-is_even(x));
case SOUTH_EAST: return gamemap::location(x+1,y+is_odd(x));
case SOUTH: return gamemap::location(x,y+1);
case SOUTH_WEST: return gamemap::location(x-1,y+is_odd(x));
case NORTH_WEST: return gamemap::location(x-1,y-is_even(x));
case NORTH: return gamemap::location(x, y - n);
case SOUTH: return gamemap::location(x, y + n);
case SOUTH_EAST: return gamemap::location(x + n, y + (n+is_odd(x))/2 );
case SOUTH_WEST: return gamemap::location(x - n, y + (n+is_odd(x))/2 );
case NORTH_EAST: return gamemap::location(x + n, y - (n+is_even(x))/2 );
case NORTH_WEST: return gamemap::location(x - n, y - (n+is_even(x))/2 );
default:
wassert(false);
return gamemap::location();

View File

@ -92,7 +92,8 @@ public:
location operator-(const location &a) const;
location &operator-=(const location &a);
location get_direction(DIRECTION d) const;
// do n step in the direction d
location get_direction(DIRECTION d, int n = 1) const;
DIRECTION get_relative_dir(location loc) const;
static DIRECTION get_opposite_dir(DIRECTION d);