r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
PlayerDirection.hpp
Go to the documentation of this file.
1///
2/// @file Systems.hpp
3/// @brief This file contains the system definitions
4/// @namespace cli
5///
6
7#pragma once
8
9#include <cmath>
10
11#include "Client/GameConfig.hpp"
12#include "ECS/Component.hpp"
13#include "ECS/Registry.hpp"
14
15#ifndef M_PI
16#define M_PI 3.14159265358979323846
17#endif
18
19namespace cli
20{
21
23 {
24 public:
25 explicit PlayerDirectionSystem() = default;
26 ~PlayerDirectionSystem() override = default;
27
32
33 void update(ecs::Registry &registry, float /* dt */) override
34 {
35 for (auto &[entity, player] : registry.getAll<ecs::Player>())
36 {
37 const auto *velocity = registry.getComponent<ecs::Velocity>(entity);
38 auto *rect = registry.getComponent<ecs::Rect>(entity);
39
40 if (velocity && rect)
41 {
42 int frame = 0;
43 float angle = std::atan2(velocity->y, velocity->x);
44 if (std::abs(velocity->x) < 0.1f && std::abs(velocity->y) < 0.1f)
45 {
46 return;
47 }
48 if (angle < 0)
49 angle += 2.0f * static_cast<float>(M_PI);
50 if (angle >= 0 && angle < M_PI / 4)
51 frame = 0; // Droite
52 else if (angle >= M_PI / 4 && angle < 3 * M_PI / 4)
53 frame = 1; // Haut
54 else if (angle >= 3 * M_PI / 4 && angle < 5 * M_PI / 4)
55 frame = 2; // Gauche
56 else if (angle >= 5 * M_PI / 4 && angle < 7 * M_PI / 4)
57 frame = 3; // Bas
58 else
59 frame = 4; // Droite (retour)
60 int frame_width = static_cast<int>(GameConfig::Player::SPRITE_WIDTH);
61 int frame_height = static_cast<int>(GameConfig::Player::SPRITE_HEIGHT);
62 int frames_per_row = GameConfig::Player::FRAMES_PER_ROW;
63 int frame_x = (frame % frames_per_row) * frame_width;
64 int frame_y = (frame / frames_per_row) * frame_height;
65
66 rect->pos_x = static_cast<float>(frame_x);
67 rect->pos_y = static_cast<float>(frame_y);
68 rect->size_x = frame_width;
69 rect->size_y = frame_height;
70 }
71 }
72 }
73 }; // class PlayerDirectionSystem
74
75} // namespace cli
This file contains the component definitions.
Configuration constants for the game.
#define M_PI
This file contains the Registry class declaration.
PlayerDirectionSystem(PlayerDirectionSystem &&)=delete
PlayerDirectionSystem(const PlayerDirectionSystem &)=delete
void update(ecs::Registry &registry, float) override
PlayerDirectionSystem & operator=(PlayerDirectionSystem &&)=delete
~PlayerDirectionSystem() override=default
PlayerDirectionSystem & operator=(const PlayerDirectionSystem &)=delete
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
constexpr float SPRITE_WIDTH
constexpr int FRAMES_PER_ROW
constexpr float SPRITE_HEIGHT