r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
PlayerDirection.hpp
Go to the documentation of this file.
1///
2/// @file PlayerDirection.hpp
3/// @brief This file contains the player direction system definitions
4/// @namespace gme
5///
6
7#pragma once
8
9#include <cmath>
10#include <numbers>
11#include <ranges>
12
13#include "ECS/Component.hpp"
15#include "ECS/Registry.hpp"
16#include "Utils/Common.hpp"
18
19#ifndef M_PI
20#define M_PI 3.14159265358979323846
21#endif
22
23namespace gme
24{
25 struct AppConfig;
26
28 {
29 public:
30 explicit PlayerDirectionSystem(const int skinIndex) : m_skinIndex(skinIndex) {}
31 ~PlayerDirectionSystem() override = default;
32
37
38 void update(ecs::Registry &registry, float /* dt */) override
39 {
40 for (const auto &entity : registry.getAll<ecs::Player>() | std::views::keys)
41 {
42 const auto *velocity = registry.getComponent<ecs::Velocity>(entity);
43
44 if (auto *rect = registry.getComponent<ecs::Rect>(entity);
45 (velocity != nullptr) && (rect != nullptr))
46 {
47 int frame = 0;
48 float angle = std::atan2(velocity->y, velocity->x);
49 if (std::abs(velocity->x) < 0.1f && std::abs(velocity->y) < 0.1f)
50 {
51 frame = 0;
52 }
53 else
54 {
55 if (angle < 0)
56 {
57 angle += 2.0f * std::numbers::pi_v<float>;
58 }
59 if (angle >= 0 && angle < M_PI / 4)
60 {
61 frame = 0;
62 }
63 else if (angle >= M_PI / 4 && angle < 3 * M_PI / 4)
64 {
65 frame = 1;
66 }
67 else if (angle >= 3 * M_PI / 4 && angle < 5 * M_PI / 4)
68 {
69 frame = 2;
70 }
71 else if (angle >= 5 * M_PI / 4 && angle < 7 * M_PI / 4)
72 {
73 frame = 3;
74 }
75 else
76 {
77 frame = 4;
78 }
79 }
80 int frame_width = static_cast<int>(utl::GameConfig::Player::SPRITE_WIDTH);
81 int frame_height = static_cast<int>(utl::GameConfig::Player::SPRITE_HEIGHT);
82 int frames_per_row = utl::GameConfig::Player::FRAMES_PER_ROW;
83 int frame_x = (frame % frames_per_row) * frame_width;
84 int frame_y = (frame / frames_per_row) * frame_height;
85
86 const int skin_offset = m_skinIndex * static_cast<int>(utl::GameConfig::Player::SPRITE_HEIGHT);
87 frame_y += skin_offset;
88
89 rect->pos_x = static_cast<float>(frame_x);
90 rect->pos_y = static_cast<float>(frame_y);
91 rect->size_x = frame_width;
92 rect->size_y = frame_height;
93 }
94 }
95 }
96
97 private:
99 }; // class PlayerDirectionSystem
100} // 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(const int skinIndex)
PlayerDirectionSystem & operator=(const PlayerDirectionSystem &)=delete
This file contains common definitions and constants.
constexpr float SPRITE_HEIGHT
constexpr float SPRITE_WIDTH
constexpr int FRAMES_PER_ROW