1 #ifndef SPRITE_MOVEMENT_H
2 #define SPRITE_MOVEMENT_H
45 return std::make_shared<SpriteData>(
SpriteData {
69 if(glm::distance(glm::vec2(data->transform->getAbsoluteTranslation()), data->next_position) > glm::length(data->current_velocity * 1.0f / 1000.0f * (
float)delta)) {
70 data->transform->getParent()->translate(data->current_velocity * 1.0f / 1000.0f * (
float)delta);
74 data->transform->getParent()->translate(data->next_position - glm::vec2(data->transform->getAbsoluteTranslation()));
88 data->current_velocity = glm::vec2(0.0,
data->moving_speed);
89 data->next_position = glm::vec2(
data->transform->getAbsoluteTranslation()) + glm::normalize(
data->current_velocity) *
data->move_quantization_in_tiles;
90 data->tile_location += glm::ivec2(0, -1);
92 auto absolute_next_position = glm::vec2(
data->transform->getAbsoluteTranslation()) + glm::normalize(
data->current_velocity) *
data->move_quantization_in_tiles;
123 data->current_velocity = glm::vec2(0.0, 0.0);
124 data->next_position = glm::vec2(
data->transform->getAbsoluteTranslation());
132 if(
data->collision_data->getCollideLevel(
data->tile_location.x - 1,
data->tile_location.y) <
data->current_level) {
140 if(
data->collision_data->getCollideLevel(
data->tile_location.x + 1,
data->tile_location.y) <
data->current_level) {
148 if(
data->collision_data->getCollideLevel(
data->tile_location.x,
data->tile_location.y - 1) <
data->current_level) {
156 if(
data->collision_data->getCollideLevel(
data->tile_location.x,
data->tile_location.y + 1) <
data->current_level) {
175 data->current_velocity = glm::vec2(0.0, -
data->moving_speed);
176 data->next_position = glm::vec2(
data->transform->getAbsoluteTranslation()) + glm::normalize(
data->current_velocity) *
data->move_quantization_in_tiles;
177 data->tile_location += glm::ivec2(0, 1);
179 auto absolute_next_position = glm::vec2(
data->transform->getAbsoluteTranslation()) + glm::normalize(
data->current_velocity) *
data->move_quantization_in_tiles;
210 data->current_velocity = glm::vec2(0.0, 0.0);
211 data->next_position = glm::vec2(
data->transform->getAbsoluteTranslation());
219 if(
data->collision_data->getCollideLevel(
data->tile_location.x - 1,
data->tile_location.y) <
data->current_level) {
227 if(
data->collision_data->getCollideLevel(
data->tile_location.x + 1,
data->tile_location.y) <
data->current_level) {
235 if(
data->collision_data->getCollideLevel(
data->tile_location.x,
data->tile_location.y - 1) <
data->current_level) {
243 if(
data->collision_data->getCollideLevel(
data->tile_location.x,
data->tile_location.y + 1) <
data->current_level) {
262 data->current_velocity = glm::vec2(-
data->moving_speed, 0.0);
263 data->next_position = glm::vec2(
data->transform->getAbsoluteTranslation()) + glm::normalize(
data->current_velocity) *
data->move_quantization_in_tiles;
264 data->tile_location += glm::ivec2(-1, 0);
266 auto absolute_next_position = glm::vec2(
data->transform->getAbsoluteTranslation()) + glm::normalize(
data->current_velocity) *
data->move_quantization_in_tiles;
297 data->current_velocity = glm::vec2(0.0, 0.0);
298 data->next_position = glm::vec2(
data->transform->getAbsoluteTranslation());
306 if(
data->collision_data->getCollideLevel(
data->tile_location.x - 1,
data->tile_location.y) <
data->current_level) {
314 if(
data->collision_data->getCollideLevel(
data->tile_location.x + 1,
data->tile_location.y) <
data->current_level) {
322 if(
data->collision_data->getCollideLevel(
data->tile_location.x,
data->tile_location.y - 1) <
data->current_level) {
330 if(
data->collision_data->getCollideLevel(
data->tile_location.x,
data->tile_location.y + 1) <
data->current_level) {
349 data->current_velocity = glm::vec2(
data->moving_speed, 0.0);
350 data->next_position = glm::vec2(
data->transform->getAbsoluteTranslation()) + glm::normalize(
data->current_velocity) *
data->move_quantization_in_tiles;
351 data->tile_location += glm::ivec2(1, 0);
353 auto absolute_next_position = glm::vec2(
data->transform->getAbsoluteTranslation()) + glm::normalize(
data->current_velocity) *
data->move_quantization_in_tiles;
384 data->current_velocity = glm::vec2(0.0, 0.0);
385 data->next_position = glm::vec2(
data->transform->getAbsoluteTranslation());
393 if(
data->collision_data->getCollideLevel(
data->tile_location.x - 1,
data->tile_location.y) <
data->current_level) {
401 if(
data->collision_data->getCollideLevel(
data->tile_location.x + 1,
data->tile_location.y) <
data->current_level) {
409 if(
data->collision_data->getCollideLevel(
data->tile_location.x,
data->tile_location.y - 1) <
data->current_level) {
417 if(
data->collision_data->getCollideLevel(
data->tile_location.x,
data->tile_location.y + 1) <
data->current_level) {
432 MoveUpReactor, FaceUpReactor, MoveDownReactor, FaceDownReactor,
443 std::unique_ptr<SpriteMovementMotor::SpriteFSM> state_machine;
444 std::shared_ptr<SpriteMovementMotor::SpriteData> data;
447 virtual bool onUpdate(
const double delta)
override;
448 virtual void onStart()
override;
452 virtual void onNotifyNow(std::shared_ptr<Events::Event> event)
override;
467 void addCollisionData(std::shared_ptr<Physics::CollisionData> collision_data);
482 virtual std::
string className() const noexcept override;
483 virtual std::
string to_string() const noexcept override;
486 virtual
void log(el::base::type::ostream_t& os) const override;
virtual bool updateState(const double delta, SpriteInput &t) override
Definition: sprite_movement.h:98
virtual void enterState() override
enterState is called upon triggering of this state
Definition: sprite_movement.h:296
SpriteInput
Sprite input enum.
Definition: sprite_movement.h:25
virtual void onNotifyNow(std::shared_ptr< Events::Event > event) override
When receiving an event that is immediate, onNotifyNow is used. It acts as an interrupt to make sure ...
Definition: sprite_movement.cpp:42
virtual void enterState() override
enterState is called upon triggering of this state
Definition: sprite_movement.h:383
Definition: sprite_movement.h:25
SpriteState
Sprite state enum.
Definition: sprite_movement.h:21
MoveUpReactor(const std::shared_ptr< SpriteData > &data, Events::Subject *subject)
Definition: sprite_movement.h:86
virtual void notify(std::shared_ptr< Event > event)
notify is used to tell observers of an event.
Definition: subject.cpp:13
Definition: sprite_movement.h:25
Base Class for all components.
Definition: component.h:20
static std::shared_ptr< SpriteMoveEvent > create(glm::vec2 &velocity, const glm::vec2 &next_position)
SpriteMoveEvent factory function.
Definition: sprite_move_event.h:35
Definition: sprite_movement.h:21
Definition: sprite_movement.h:21
Class for animation trigger event.
Definition: animation_trigger_event.hpp:13
Definition: sprite_movement.h:21
virtual void enterState() override
enterState is called upon triggering of this state
Definition: sprite_movement.h:174
virtual std::string className() const noexceptoverride
Returns a string representing the class name.
Definition: sprite_movement.cpp:91
virtual void enterState() override
enterState is called upon triggering of this state
Definition: sprite_movement.h:122
FaceUpReactor(const std::shared_ptr< SpriteData > &data, Events::Subject *subject)
Definition: sprite_movement.h:121
Definition: sprite_movement.h:344
Definition: sprite_movement.h:25
glm::ivec2 tile_location
Definition: sprite_movement.h:34
virtual SpriteState react(const SpriteInput transition) override
Definition: sprite_movement.h:367
Class for state reactor.
Definition: fsm.hpp:18
FaceLeftReactor(const std::shared_ptr< SpriteData > &data, Events::Subject *subject)
Definition: sprite_movement.h:295
virtual void enterState() override
enterState is called upon triggering of this state
Definition: sprite_movement.h:209
Events::FSM::FSM< SpriteData, SpriteState, SpriteInput, MoveUpReactor, FaceUpReactor, MoveDownReactor, FaceDownReactor, MoveLeftReactor, FaceLeftReactor, MoveRightReactor, FaceRightReactor > SpriteFSM
Definition: sprite_movement.h:433
constexpr SpriteState getStateType() const noexcept
Definition: fsm.hpp:35
std::shared_ptr< Physics::CollisionData > collision_data
Definition: sprite_movement.h:37
virtual SpriteState react(const SpriteInput transition) override
Definition: sprite_movement.h:280
Class for a subject that an observer would observe for changes.
Definition: subject.h:13
Definition: sprite_movement.h:118
float move_quantization_in_tiles
Definition: sprite_movement.h:31
virtual void onDestroy() override
Called when the engine is shutting down.
Definition: sprite_movement.h:449
Definition: sprite_movement.h:21
virtual std::string to_string() const noexceptoverride
Returns a string representation of the object.
Definition: sprite_movement.cpp:95
glm::vec2 current_velocity
Definition: sprite_movement.h:32
MoveDownReactor(const std::shared_ptr< SpriteData > &data, Events::Subject *subject)
Definition: sprite_movement.h:173
std::shared_ptr< Transform > transform
Definition: sprite_movement.h:36
Definition: sprite_movement.h:170
virtual void handleQueuedEvent(std::shared_ptr< Events::Event > event) override
HandleQueuedEvent allows derived classes to define behaviour for when queuedEvents are received...
Definition: sprite_movement.cpp:46
virtual SpriteState react(const SpriteInput transition) override
Definition: sprite_movement.h:106
void addCollisionData(std::shared_ptr< Physics::CollisionData > collision_data)
Adds a collision data.
Definition: sprite_movement.cpp:19
virtual void enterState() override
enterState is called upon triggering of this state
Definition: sprite_movement.h:348
Definition: sprite_movement.h:17
FaceRightReactor()=delete
FaceDownReactor(const std::shared_ptr< SpriteData > &data, Events::Subject *subject)
Definition: sprite_movement.h:208
FaceRightReactor(const std::shared_ptr< SpriteData > &data, Events::Subject *subject)
Definition: sprite_movement.h:382
static bool updateSprite(const std::shared_ptr< SpriteData > data, const SpriteState current_state, const double delta)
General sprite update for each reactor.
Definition: sprite_movement.h:66
Definition: sprite_movement.h:21
virtual SpriteState react(const SpriteInput transition) override
Definition: sprite_movement.h:390
Definition: sprite_movement.h:21
float moving_speed
Definition: sprite_movement.h:30
virtual void enterState() override
enterState is called upon triggering of this state
Definition: sprite_movement.h:87
virtual void notifyNow(std::shared_ptr< Event > event)
notifyNow is used to tell observers of an event as an interrupt.
Definition: subject.cpp:19
Definition: sprite_movement.h:25
void setMoveQuantization(const float number_of_tiles)
Sets the move quantization.
Definition: sprite_movement.cpp:83
virtual void enterState() override
enterState is called upon triggering of this state
Definition: sprite_movement.h:261
Definition: sprite_movement.h:379
int current_level
Definition: sprite_movement.h:35
glm::vec2 next_position
Definition: sprite_movement.h:33
MoveRightReactor()=delete
Subject * subject
Definition: fsm.hpp:21
Definition: sprite_movement.h:257
Definition: sprite_movement.h:21
virtual bool onUpdate(const double delta) override
Called every engine loop.
Definition: sprite_movement.cpp:30
virtual bool updateState(const double delta, SpriteInput &t) override
Definition: sprite_movement.h:359
MoveLeftReactor(const std::shared_ptr< SpriteData > &data, Events::Subject *subject)
Definition: sprite_movement.h:260
Definition: sprite_movement.h:205
virtual SpriteState react(const SpriteInput transition) override
Definition: sprite_movement.h:216
MoveRightReactor(const std::shared_ptr< SpriteData > &data, Events::Subject *subject)
Definition: sprite_movement.h:347
static std::shared_ptr< SpriteData > defaultSpriteData()
Create a default SpriteData POD.
Definition: sprite_movement.h:44
virtual bool updateState(const double delta, SpriteInput &t) override
Definition: sprite_movement.h:185
virtual unsigned long long getValueForSorting() const noexceptoverride
Gets the value for sorting.
Definition: sprite_movement.cpp:75
virtual void log(el::base::type::ostream_t &os) const override
Definition: sprite_movement.cpp:87
virtual SpriteState react(const SpriteInput transition) override
Definition: sprite_movement.h:303
Sprite data to be exposed for FSM mutation.
Definition: sprite_movement.h:29
virtual void onStart() override
Called when the engine starts and when a new scene is loaded.
Definition: sprite_movement.cpp:23
virtual SpriteState react(const SpriteInput transition) override
Definition: sprite_movement.h:129
virtual bool updateState(const double delta, SpriteInput &t) override
Definition: sprite_movement.h:272
Class for sprite movement.
Definition: sprite_movement.h:441
Definition: sprite_movement.h:84
Definition: sprite_movement.h:25
std::shared_ptr< SpriteData > data
Definition: fsm.hpp:20
Generic Finite State Machine.
Definition: fsm.hpp:78
Definition: sprite_movement.h:21
Definition: sprite_movement.h:292
virtual SpriteState react(const SpriteInput transition) override
Definition: sprite_movement.h:193
void setMovingSpeed(const float speed)
Sets the moving speed.
Definition: sprite_movement.cpp:79