Skip to content

Commit

Permalink
CLIENT-GAME: Fix compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
TTENSHII committed Sep 27, 2023
1 parent 73c2484 commit abf6147
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 238 deletions.
270 changes: 91 additions & 179 deletions src/Client/Systems/ClientSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
#include "ClientSystems.hpp"
#include <iostream>
#include "CustomTypes.hpp"
#include "Registry.hpp"
#include "Raylib.hpp"
#include "Registry.hpp"

namespace Systems {

void GraphicSystems::rectRenderer(std::size_t /*unused*/)
{
Registry::components<Types::Position> arrPosition =
Expand All @@ -29,17 +30,19 @@ namespace Systems {
Types::Position &position = positionIt->value();
Types::RectangleShape &rectangle = rectIt->value();

float x = (position.x * static_cast<float>(Raylib::getScreenWidth()))
/ denominator;
float y = (position.y * static_cast<float>(Raylib::getScreenHeight()))
/ denominator;
float x =
(position.x * static_cast<float>(Raylib::getScreenWidth()))
/ denominator;
float y =
(position.y * static_cast<float>(Raylib::getScreenHeight()))
/ denominator;

float width =
(rectangle.width * static_cast<float>(Raylib::getScreenWidth()))
/ denominator;
float height =
(rectangle.height * static_cast<float>(Raylib::getScreenHeight()))
/ denominator;
float width = (rectangle.width
* static_cast<float>(Raylib::getScreenWidth()))
/ denominator;
float height = (rectangle.height
* static_cast<float>(Raylib::getScreenHeight()))
/ denominator;

DrawRectangle(
static_cast<int>(x),
Expand All @@ -53,74 +56,94 @@ namespace Systems {
}
}

void EventsSystems::playerMovement(std::size_t /*unused*/)
{
Registry::components<Types::Position> arrPosition =
Registry::getInstance().getComponents<Types::Position>();

Registry::components<Types::Player> arrPlayer =
Registry::getInstance().getComponents<Types::Player>();

auto positionIt = arrPosition.begin();
auto playerIt = arrPlayer.begin();

while (positionIt != arrPosition.end() && playerIt != arrPlayer.end()) {
if (playerIt->has_value() && positionIt->has_value()
&& playerIt->value().isMine) {
if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_RIGHT)) {
positionIt->value().x += 1;
}
if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_LEFT)) {
positionIt->value().x -= 1;
}
if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_UP)) {
positionIt->value().y -= 1;
}
if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_DOWN)) {
positionIt->value().y += 1;
}
}
positionIt++;
playerIt++;
}
}

static void
drawSpriteWithoutRect(Types::Position &position, Types::Sprite &sprite)
drawSpriteWithoutRect(Types::Position &position, Raylib::Sprite &sprite)
{
float scale = 1.0F;
float rotation = 0;
auto tint = WHITE;
Vector2 spritePos = {0, 0};
const float denominator = 100.0;
float scale = 1.0F;
float rotation = 0;
Raylib::Color tint = Raylib::White;
Raylib::Vector2 spritePos = {0, 0};
const float denominator = 100.0;

float x =
(position.x * static_cast<float>(GetScreenWidth())) / denominator;
float y =
(position.y * static_cast<float>(GetScreenHeight())) / denominator;
float x = (position.x * static_cast<float>(Raylib::getScreenWidth()))
/ denominator;
float y = (position.y * static_cast<float>(Raylib::getScreenHeight()))
/ denominator;

scale = (sprite.width * static_cast<float>(GetScreenWidth()))
/ denominator / static_cast<float>(sprite.sprite.width);
scale =
(sprite.getWidth() * static_cast<float>(Raylib::getScreenWidth()))
/ denominator / static_cast<float>(sprite.getTextureWidth());
spritePos = {x, y};

while (positionIt != arrPosition.end() && playerIt != arrPlayer.end()) {
if (playerIt->has_value() && positionIt->has_value()
&& playerIt->value().isMine) {
if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_RIGHT)) {
positionIt->value().x += 1;
}
if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_LEFT)) {
positionIt->value().x -= 1;
}
if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_UP)) {
positionIt->value().y -= 1;
}
if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_DOWN)) {
positionIt->value().y += 1;
DrawTextureEx(sprite.sprite, spritePos, rotation, scale, tint);
sprite.drawEx(spritePos, rotation, scale, tint);
}

static void drawSpriteWithRect(
Types::Position &position,
Types::Sprite &sprite,
Raylib::Sprite &sprite,
Types::Rect &rect)
{
Vector2 origin = {0, 0};
Raylib::Vector2 origin = {0, 0};
float rotation = 0;
auto tint = WHITE;
Raylib::Color tint = Raylib::White;
const float denominator = 100.0;

float x =
(position.x * static_cast<float>(GetScreenWidth())) / denominator;
float y =
(position.y * static_cast<float>(GetScreenHeight())) / denominator;
float x = (position.x * static_cast<float>(Raylib::getScreenWidth()))
/ denominator;
float y = (position.y * static_cast<float>(Raylib::getScreenHeight()))
/ denominator;

float width =
(sprite.width * static_cast<float>(GetScreenWidth())) / denominator;
float height = (sprite.height * static_cast<float>(GetScreenHeight()))
(sprite.getWidth() * static_cast<float>(Raylib::getScreenWidth()))
/ denominator;
float height =
(sprite.getHeight() * static_cast<float>(Raylib::getScreenHeight()))
/ denominator;

DrawTexturePro(
sprite.sprite,
Rectangle(rect.x, rect.y, rect.width, rect.height),
Rectangle(x, y, width, height),
sprite.drawPro(
Raylib::Rectangle(rect.x, rect.y, rect.width, rect.height),
Raylib::Rectangle(x, y, width, height),
origin,
rotation,
tint);
}

void GraphicSystems::spriteRenderer(std::size_t /*unused*/)
{
Registry::components<Types::Sprite> arrSprite =
Registry::getInstance().getComponents<Types::Sprite>();
Registry::components<Raylib::Sprite> arrSprite =
Registry::getInstance().getComponents<Raylib::Sprite>();
Registry::components<Types::Rect> arrRect =
Registry::getInstance().getComponents<Types::Rect>();
Registry::components<Types::Position> arrPosition =
Expand Down Expand Up @@ -162,79 +185,23 @@ namespace Systems {
}
}

static void
drawSpriteWithoutRect(Types::Position &position, Raylib::Sprite &sprite)
{
float scale = 1.0F;
float rotation = 0;
Raylib::Color tint = Raylib::White;
Raylib::Vector2 spritePos = {0, 0};
const float denominator = 100.0;

float x = (position.x * static_cast<float>(Raylib::getScreenWidth())) / denominator;
float y =
(position.y * static_cast<float>(Raylib::getScreenHeight())) / denominator;

scale = (sprite.getWidth() * static_cast<float>(Raylib::getScreenWidth())) / denominator
/ static_cast<float>(sprite.getTextureWidth());
spritePos = {x, y};

sprite.drawEx(spritePos, rotation, scale, tint);
}

static void drawSpriteWithRect(
Types::Position &position,
Raylib::Sprite &sprite,
Types::Rect &rect)
void GraphicSystems::musicPlayer(std::size_t /*unused*/)
{
Raylib::Vector2 origin = {0, 0};
float rotation = 0;
Raylib::Color tint = Raylib::White;
const float denominator = 100.0;

float x = (position.x * static_cast<float>(Raylib::getScreenWidth())) / denominator;
float y =
(position.y * static_cast<float>(Raylib::getScreenHeight())) / denominator;

float width =
(sprite.getWidth() * static_cast<float>(Raylib::getScreenWidth())) / denominator;
float height =
(sprite.getHeight() * static_cast<float>(Raylib::getScreenHeight())) / denominator;

sprite.drawPro(
Raylib::Rectangle(rect.x, rect.y, rect.width, rect.height),
Raylib::Rectangle(x, y, width, height),
origin,
rotation,
tint);
}
Registry::components<Types::MusicStream> arrMusics =
Registry::getInstance().getComponents<Types::MusicStream>();

void GraphicSystems::spriteRenderer(std::size_t /*unused*/)
{
Registry::components<Raylib::Sprite> arrSprite =
Registry::getInstance().getComponents<Raylib::Sprite>();
Registry::components<Types::Rect> arrRect =
Registry::getInstance().getComponents<Types::Rect>();
Registry::components<Types::Position> arrPosition =
Registry::getInstance().getComponents<Types::Position>();

auto positionIt = arrPosition.begin();
auto spriteIt = arrSprite.begin();
auto rectIt = arrRect.begin();

while (positionIt != arrPosition.end() && spriteIt != arrSprite.end()) {
if (positionIt->has_value() && spriteIt->has_value()
&& rectIt->has_value()) {
drawSpriteWithRect(
positionIt->value(),
spriteIt->value(),
rectIt->value());
} else if (positionIt->has_value() && spriteIt->has_value()) {
drawSpriteWithoutRect(positionIt->value(), spriteIt->value());
for (auto &music : arrMusics) {
if (!music.has_value()) {
continue;
}
if (music.value().needToPlay) {
PlayMusicStream(music.value().music);
music.value().needToPlay = false;
music.value().isPlaying = true;
}
if (music.value().isPlaying) {
UpdateMusicStream(music.value().music);
}
positionIt++;
spriteIt++;
rectIt++;
}
}

Expand Down Expand Up @@ -278,67 +245,12 @@ namespace Systems {
}
}

void EventsSystems::playerMovement(std::size_t /*unused*/)
{
Registry::components<Types::Position> arrPosition =
Registry::getInstance().getComponents<Types::Position>();

Registry::components<Types::Player> arrPlayer =
Registry::getInstance().getComponents<Types::Player>();

auto positionIt = arrPosition.begin();
auto playerIt = arrPlayer.begin();

while (positionIt != arrPosition.end() && playerIt != arrPlayer.end()) {
if (playerIt->has_value() && positionIt->has_value()
&& playerIt->value().isMine) {
if (IsKeyDown(KEY_RIGHT)) {
positionIt->value().x += 1;
}
if (IsKeyDown(KEY_LEFT)) {
positionIt->value().x -= 1;
}
if (IsKeyDown(KEY_UP)) {
positionIt->value().y -= 1;
}
if (IsKeyDown(KEY_DOWN)) {
positionIt->value().y += 1;
}
}
positionIt++;
playerIt++;
}
}

void GraphicSystems::musicPlayer(std::size_t /*unused*/)
{
Registry::components<Types::MusicStream> arrMusics =
Registry::getInstance().getComponents<Types::MusicStream>();

for (auto &music : arrMusics) {
if (!music.has_value()) {
continue;
}
if (music.value().needToPlay) {
PlayMusicStream(music.value().music);
music.value().needToPlay = false;
music.value().isPlaying = true;
}
if (music.value().isPlaying) {
UpdateMusicStream(music.value().music);
}
}
}

const std::vector<std::function<void(std::size_t)>>
GraphicSystems::graphicSystems {
rectRenderer,
spriteRenderer,
textRenderer,
musicPlayer,
soundEffectPlayer
};
soundEffectPlayer};

const std::vector<std::function<void(std::size_t)>>
EventsSystems::eventSystems {playerMovement};
} // namespace Systems
5 changes: 5 additions & 0 deletions src/Client/Systems/CustomTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ namespace Types {
float height;
};

struct Position {
float x;
float y;
};

struct SoundEffect {
SoundEffect(std::string soundPath)
: sound(LoadSound(soundPath.c_str())),
Expand Down
Loading

0 comments on commit abf6147

Please sign in to comment.