r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
PlayerDirection.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4#include <utility>
5
6#include "ECS/Component.hpp"
8#include "ECS/Registry.hpp"
10
11#ifndef M_PI
12#define M_PI 3.14159265358979323846
13#endif
14
15namespace gme
16{
17 class PlayerDirectionSystem final : public ecs::ASystem
18 {
19 public:
21 ~PlayerDirectionSystem() override = default;
22
27
28 void update(ecs::Registry &registry, float /* dt */) override
29 {
30 for (auto &pair : registry.getAll<ecs::Player>())
31 {
32 const auto entity = pair.first;
33 const auto *velocity = registry.getComponent<ecs::Velocity>(entity);
34
35 if (auto *rect = registry.getComponent<ecs::Rect>(entity);
36 (velocity != nullptr) && (rect != nullptr))
37 {
38 int frame = 0;
39 float angle = std::atan2(velocity->y, velocity->x);
40
41 const bool isIdle = (std::abs(velocity->x) < 0.1f && std::abs(velocity->y) < 0.1f);
42 if (isIdle)
43 {
44 frame = 0;
45 }
46 else
47 {
48 if (angle < 0)
49 {
50 angle += 2.0f * static_cast<float>(M_PI);
51 }
52 if (angle >= 0 && angle < M_PI / 4)
53 {
54 frame = 0; // droite
55 }
56 else if (angle >= M_PI / 4 && angle < 3 * M_PI / 4)
57 {
58 frame = 1; // bas
59 }
60 else if (angle >= 3 * M_PI / 4 && angle < 5 * M_PI / 4)
61 {
62 frame = 2; // gauche
63 }
64 else if (angle >= 5 * M_PI / 4 && angle < 7 * M_PI / 4)
65 {
66 frame = 3; // haut
67 }
68 else
69 {
70 frame = 4; // wrap
71 }
72 }
73
74 const int frame_x = (frame % utl::GameConfig::Player::FRAMES_PER_ROW) *
76 int frame_y = (frame / utl::GameConfig::Player::FRAMES_PER_ROW) *
78
79 const int current_row = static_cast<int>(
80 rect->pos_y / static_cast<float>(static_cast<int>(utl::GameConfig::Player::SPRITE_HEIGHT)));
81 const int skin_offset = current_row * static_cast<int>(utl::GameConfig::Player::SPRITE_HEIGHT);
82 frame_y = skin_offset + frame_y;
83
84 rect->pos_x = static_cast<float>(frame_x);
85 rect->pos_y = static_cast<float>(frame_y);
86 rect->size_x = static_cast<int>(utl::GameConfig::Player::SPRITE_WIDTH);
87 rect->size_y = static_cast<int>(utl::GameConfig::Player::SPRITE_HEIGHT);
88 }
89 }
90 }
91 };
92} // namespace gme
This file contains the component definitions.
Configuration constants for the multiplayer game.
This file contains the interface for systems.
#define M_PI
This file contains the Registry class declaration.
Abstract class for system.
Definition ISystems.hpp:34
Class for managing entities and their components.
Definition Registry.hpp:25
std::unordered_map< Entity, T > & getAll()
Definition Registry.hpp:77
T * getComponent(Entity e)
Definition Registry.hpp:71
~PlayerDirectionSystem() override=default
PlayerDirectionSystem(PlayerDirectionSystem &&)=delete
PlayerDirectionSystem(const PlayerDirectionSystem &)=delete
void update(ecs::Registry &registry, float) override
PlayerDirectionSystem & operator=(PlayerDirectionSystem &&)=delete
PlayerDirectionSystem & operator=(const PlayerDirectionSystem &)=delete
constexpr float SPRITE_HEIGHT
constexpr float SPRITE_WIDTH
constexpr int FRAMES_PER_ROW