mirror of
https://github.com/wesnoth/wesnoth
synced 2025-05-04 09:16:55 +00:00
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:
parent
c5dd72d4c5
commit
4646c92f08
18
src/map.cpp
18
src/map.cpp
@ -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();
|
||||
|
@ -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);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user