Commit 3f708722 authored by louiz’'s avatar louiz’

Introduce the concept of direction, to be able to orient the WorldSprites

parent aacf27d1
#ifndef DIRECTION_HPP_INCLUDED
#define DIRECTION_HPP_INCLUDED
enum class Direction: unsigned short
{
E, SE, S, SW, W, NW, N, NE,
count
};
#endif /* DIRECTION_HPP_INCLUDED */
...@@ -10,6 +10,7 @@ const ComponentType Mobility::component_type; ...@@ -10,6 +10,7 @@ const ComponentType Mobility::component_type;
Mobility::Mobility(const Fix16& speed): Mobility::Mobility(const Fix16& speed):
speed(speed) speed(speed)
last_movement{},
{ {
} }
...@@ -62,6 +63,7 @@ void Mobility::follow_path(Path& path, World* world, Location* location) ...@@ -62,6 +63,7 @@ void Mobility::follow_path(Path& path, World* world, Location* location)
log_debug("blocked"); log_debug("blocked");
return; return;
} }
this->last_movement = movement;
location->position() = after_movement; location->position() = after_movement;
} }
...@@ -94,5 +96,40 @@ Fix16 Mobility::move_towards(const Position& goal, Location* location, Fix16 spe ...@@ -94,5 +96,40 @@ Fix16 Mobility::move_towards(const Position& goal, Location* location, Fix16 spe
Fix16 Mobility::get_angle() const Fix16 Mobility::get_angle() const
{ {
return this->last_movement.angle(); auto res = this->last_movement.angle();
if (res >= 0)
return res;
else
return 360 + res;
}
Direction Mobility::get_direction() const
{
auto angle = this->get_angle();
static const auto wide = 22.5_fix;
if (angle >= (360 - wide) ||
angle < (0 + wide))
return Direction::E;
else if (angle >= (45 - wide) &&
angle < (45 + wide))
return Direction::SE;
else if (angle >= (90 - wide) &&
angle < (90 + wide))
return Direction::S;
else if (angle >= (135 - wide) &&
angle < (135 + wide))
return Direction::SW;
else if (angle >= (180 - wide) &&
angle < (180 + wide))
return Direction::W;
else if (angle >= (225 - wide) &&
angle < (225 + wide))
return Direction::NW;
else if (angle >= (270 - wide) &&
angle < (270 + wide))
return Direction::N;
else if (angle >= (315 - wide) &&
angle < (315 + wide))
return Direction::NE;
assert(false);
} }
#ifndef MOBILITY_COMPONENT_HPP_INCLUDED #ifndef MOBILITY_COMPONENT_HPP_INCLUDED
#define MOBILITY_COMPONENT_HPP_INCLUDED #define MOBILITY_COMPONENT_HPP_INCLUDED
#include <world/direction.hpp>
#include <world/components.hpp> #include <world/components.hpp>
#include <fixmath/fix16.hpp> #include <fixmath/fix16.hpp>
#include <world/path.hpp> #include <world/path.hpp>
...@@ -38,7 +39,11 @@ public: ...@@ -38,7 +39,11 @@ public:
* movement done. * movement done.
*/ */
Fix16 get_angle() const; Fix16 get_angle() const;
/**
* Get the one of the 8 cardinal directions, based on the last movement’s
* angle
*/
Direction get_direction() const;
private: private:
Fix16 speed; Fix16 speed;
Vec2 last_movement; Vec2 last_movement;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment