diff --git a/src/demo/Demo.cpp b/src/demo/Demo.cpp index 8cf98462..53699289 100644 --- a/src/demo/Demo.cpp +++ b/src/demo/Demo.cpp @@ -11,6 +11,7 @@ #include "components/Position.hpp" #include "components/Size.hpp" #include "components/Velocity.hpp" +#include "components/Weapon.hpp" #include "systems/Movement.hpp" #include "systems/PositionLogger.hpp" #ifdef CLIENT_COMPILATION_MODE @@ -20,7 +21,7 @@ #include "components/EnemyAI.hpp" #include "systems/Draw.hpp" #include "systems/HandleSFMLEvents.hpp" - #include "systems/HandleSFMLMovements.hpp" + #include "systems/HandleSFMLKeys.hpp" #include "systems/ManageClientEvents.hpp" #endif @@ -33,23 +34,24 @@ ecs::World getGameWorld(ecs::Engine &engine) world.registry.addComponent(player, {5, 5}); world.registry.addComponent(player, {2, 2}); world.registry.addComponent(player, {1, 0}); - world.addSystem(ecs::systems::positionLogger); + world.registry.addComponent(player, {100, 1}); + // world.addSystem(ecs::systems::positionLogger); world.addSystem(ecs::systems::movement); #ifdef CLIENT_COMPILATION_MODE + std::filesystem::path playerPath = ecs::crossPlatformPath("src", "demo", "assets", "textures", "players.gif"); world.registry.addComponent( player, {sf::Keyboard::Z, sf::Keyboard::Q, sf::Keyboard::S, sf::Keyboard::D}); - world.registry.addComponent( - player, {"src/demo/assets/textures/players.gif", {1, 1, 32, 16}}); + world.registry.addComponent(player, {playerPath, {1, 1, 32, 16}}); + world.registry.addComponent(player, ecs::component::Shootable(sf::Keyboard::Space)); ecs::Entity enemy = world.registry.spawn_entity(); world.registry.addComponent(enemy, {500, 500}); world.registry.addComponent(enemy, {5, 5}); world.registry.addComponent(enemy, {}); - world.registry.addComponent( - enemy, {"src/demo/assets/textures/players.gif", {1, 18, 32, 16}}); + world.registry.addComponent(enemy, {playerPath, {1, 18, 32, 16}}); world.addSystem(ecs::systems::handleSFMLEvents); - world.addSystem(ecs::systems::handleSFMLMovements); + world.addSystem(ecs::systems::handleSFMLKeys); world.addSystem(ecs::systems::manageClientEvents); world.addSystem(ecs::systems::draw); diff --git a/src/ecs/AssetManager.hpp b/src/ecs/AssetManager.hpp new file mode 100644 index 00000000..385190a9 --- /dev/null +++ b/src/ecs/AssetManager.hpp @@ -0,0 +1,20 @@ +/* +** EPITECH PROJECT, 2022 +** RTYPE +** File description: +** AssetManager +*/ + +#pragma once + +#include + +namespace ecs +{ + inline std::filesystem::path crossPlatformPath(std::filesystem::path path) { return path; } + template + std::filesystem::path crossPlatformPath(std::filesystem::path path, std::string_view next, Args... args) + { + return crossPlatformPath(path.append(next), args...); + } +} // namespace ecs diff --git a/src/ecs/Constant.hpp b/src/ecs/Constant.hpp index 3d274c6f..56cbb4eb 100644 --- a/src/ecs/Constant.hpp +++ b/src/ecs/Constant.hpp @@ -12,6 +12,15 @@ namespace ecs { + namespace constant + { + using chrono = std::chrono::high_resolution_clock; + using chronoDuration = std::chrono::duration; + + static const std::size_t npos = -1; + static short mapWidth = 1920; + static short mapHeight = 1080; + } // namespace constant static const std::size_t npos = -1; enum PacketType { diff --git a/src/ecs/Engine.hpp b/src/ecs/Engine.hpp index 743e3034..3c19ae1a 100644 --- a/src/ecs/Engine.hpp +++ b/src/ecs/Engine.hpp @@ -1,21 +1,31 @@ /* - * File: Engine.hpp - * Project: ecs - * File Created: Tuesday, 4th October 2022 6:33:43 pm - * Author: Aurèle Nicolas (aurele.nicolas@epitech.eu) - * ----- - * Last Modified: Wednesday, 5th October 2022 2:05:52 pm - * Modified By: Aurèle Nicolas (aurele.nicolas@epitech.eu>) - * ----- - * Copyright 2022 - 2022 Your Company, Your Company - */ +** EPITECH PROJECT, 2022 +** RTYPE +** File description: +** Engine +*/ #pragma once +#include #include +#include "AssetManager.hpp" #include "Engine.hpp" #include "Registry.hpp" #include "World.hpp" +#include "components/Controllable.hpp" +#include "components/Drawable.hpp" +#include "components/EnemyAI.hpp" +#include "components/Position.hpp" +#include "components/Shootable.hpp" +#include "components/Size.hpp" +#include "components/Velocity.hpp" +#include "systems/Draw.hpp" +#include "systems/HandleSFMLEvents.hpp" +#include "systems/HandleSFMLKeys.hpp" +#include "systems/ManageClientEvents.hpp" +#include "systems/Movement.hpp" +#include "systems/PositionLogger.hpp" namespace ecs { @@ -52,8 +62,9 @@ namespace ecs /** * Set the world given as parameter to release mode * @param world The world you want to set to release mode + * @param worldSwitchReady Used to prevents of automatic world's switch */ - void setWaitingWorld(ecs::World world, bool worldSwitchReady = true) + void setWaitingWorld(const ecs::World &world, bool worldSwitchReady = true) { if (_waitingWorld) _waitingWorld.release(); diff --git a/src/ecs/Event.hpp b/src/ecs/Event.hpp index b65a888e..d3fcc538 100644 --- a/src/ecs/Event.hpp +++ b/src/ecs/Event.hpp @@ -1,14 +1,9 @@ /* - * File: Event.hpp - * Project: ecs - * File Created: Tuesday, 4th October 2022 7:31:30 pm - * Author: Aurèle Nicolas (aurele.nicolas@epitech.eu) - * ----- - * Last Modified: Wednesday, 5th October 2022 2:01:45 pm - * Modified By: Aurèle Nicolas (aurele.nicolas@epitech.eu>) - * ----- - * Copyright 2022 - 2022 Your Company, Your Company - */ +** EPITECH PROJECT, 2022 +** RTYPE +** File description: +** Event +*/ #pragma once diff --git a/src/ecs/Registry.hpp b/src/ecs/Registry.hpp index 29cb5631..28f4cabf 100644 --- a/src/ecs/Registry.hpp +++ b/src/ecs/Registry.hpp @@ -92,7 +92,7 @@ namespace ecs Entity entityFromIndex(std::size_t idx) { if (idx >= _lastEntity || std::find(_entitiesBin.begin(), _entitiesBin.end(), idx) != _entitiesBin.end()) - return Entity(ecs::npos); + return Entity(ecs::constant::npos); return Entity(idx); } diff --git a/src/ecs/SparseArray.hpp b/src/ecs/SparseArray.hpp index efa04522..72783031 100644 --- a/src/ecs/SparseArray.hpp +++ b/src/ecs/SparseArray.hpp @@ -85,8 +85,8 @@ namespace ecs referenceType insertAt(sizeType pos, Component const &c) { try { - if (pos >= _data.capacity()) - _data.resize(pos); + if (pos > _data.capacity()) + _data.resize(pos, std::nullopt); _data.emplace(_data.begin() + pos, c); } catch (std::exception &e) { std::cout << e.what() << std::endl; @@ -97,8 +97,8 @@ namespace ecs referenceType insertAt(sizeType pos, Component &&c) { try { - if (pos >= _data.capacity()) - _data.resize(pos + 1); + if (pos > _data.capacity()) + _data.resize(pos, std::nullopt); _data.emplace(_data.begin() + pos, std::move(c)); } catch (std::exception &e) { std::cout << e.what() << std::endl; @@ -109,7 +109,7 @@ namespace ecs template referenceType emplaceAt(sizeType pos, Params &&...args) { if (pos >= _data.capacity()) - _data.resize(pos + 1); + _data.resize(pos, std::nullopt); _data.emplace(_data.begin() + pos, Component(args...)); return _data[pos]; } @@ -126,12 +126,12 @@ namespace ecs { std::size_t cpt = 0; if (!c.has_value()) - return ecs::npos; + return ecs::constant::npos; for (valueType iterator = _data.begin(); iterator != _data.end(); iterator++, cpt++) if (iterator.has_value() && iterator.value() == c.value()) { return cpt; } - return ecs::npos; + return ecs::constant::npos; } private: diff --git a/src/ecs/World.hpp b/src/ecs/World.hpp index dcf4517e..98487e01 100644 --- a/src/ecs/World.hpp +++ b/src/ecs/World.hpp @@ -1,21 +1,15 @@ /* - * File: World.hpp - * Project: ecs - * File Created: Tuesday, 4th October 2022 6:33:43 pm - * Author: Aurèle Nicolas (aurele.nicolas@epitech.eu) - * ----- - * Last Modified: Wednesday, 5th October 2022 1:57:31 pm - * Modified By: Aurèle Nicolas (aurele.nicolas@epitech.eu>) - * ----- - * Copyright 2022 - 2022 Your Company, Your Company - */ +** EPITECH PROJECT, 2022 +** RTYPE +** File description: +** World +*/ #pragma once #include #include "Event.hpp" #include "Registry.hpp" -#include "SFML/Graphics.hpp" #include "utils/Window.hpp" namespace ecs diff --git a/src/ecs/components/CMakeLists.txt b/src/ecs/components/CMakeLists.txt index 3982f78b..d107d757 100644 --- a/src/ecs/components/CMakeLists.txt +++ b/src/ecs/components/CMakeLists.txt @@ -5,9 +5,13 @@ set(COMPONENTS_SRC ${SRCROOT}/Controllable.hpp ${SRCROOT}/Drawable.hpp ${SRCROOT}/EnemyAI.hpp + ${SRCROOT}/Health.hpp ${SRCROOT}/Position.hpp + ${SRCROOT}/Projectile.hpp + ${SRCROOT}/Shootable.hpp ${SRCROOT}/Size.hpp ${SRCROOT}/Velocity.hpp + ${SRCROOT}/Weapon.hpp ${SRCROOT}/Network.hpp ${SRCROOT}/NetworkId.hpp ${SRCROOT}/EntityType.hpp diff --git a/src/ecs/components/Controllable.hpp b/src/ecs/components/Controllable.hpp index 12ad88b4..87586351 100644 --- a/src/ecs/components/Controllable.hpp +++ b/src/ecs/components/Controllable.hpp @@ -1,14 +1,9 @@ /* - * File: Controllable.hpp - * Project: components - * File Created: Monday, 3rd October 2022 6:17:46 pm - * Author: Aurèle Nicolas (aurele.nicolas@epitech.eu) - * ----- - * Last Modified: Tuesday, 4th October 2022 7:26:15 pm - * Modified By: Aurèle Nicolas (aurele.nicolas@epitech.eu>) - * ----- - * Copyright 2022 - 2022 Your Company, Your Company - */ +** EPITECH PROJECT, 2022 +** RTYPE +** File description: +** Controllable +*/ #pragma once diff --git a/src/ecs/components/Direction.hpp b/src/ecs/components/Direction.hpp index 1ccd3abb..028bf611 100644 --- a/src/ecs/components/Direction.hpp +++ b/src/ecs/components/Direction.hpp @@ -1,14 +1,9 @@ /* - * File: Direction.hpp - * Project: components - * File Created: Tuesday, 4th October 2022 7:10:05 pm - * Author: Aurèle Nicolas (aurele.nicolas@epitech.eu) - * ----- - * Last Modified: Tuesday, 4th October 2022 7:22:19 pm - * Modified By: Aurèle Nicolas (aurele.nicolas@epitech.eu>) - * ----- - * Copyright 2022 - 2022 Your Company, Your Company - */ +** EPITECH PROJECT, 2022 +** RTYPE +** File description: +** Direction +*/ #pragma once diff --git a/src/ecs/components/Drawable.hpp b/src/ecs/components/Drawable.hpp index 6f05f6a0..12f59d0e 100644 --- a/src/ecs/components/Drawable.hpp +++ b/src/ecs/components/Drawable.hpp @@ -1,14 +1,9 @@ /* - * File: Drawable.hpp - * Project: components - * File Created: Monday, 3rd October 2022 6:32:42 pm - * Author: Aurèle Nicolas (aurele.nicolas@epitech.eu) - * ----- - * Last Modified: Wednesday, 5th October 2022 2:09:12 pm - * Modified By: Aurèle Nicolas (aurele.nicolas@epitech.eu>) - * ----- - * Copyright 2022 - 2022 Your Company, Your Company - */ +** EPITECH PROJECT, 2022 +** RTYPE +** File description: +** Drawable +*/ #pragma once @@ -21,10 +16,12 @@ namespace ecs::component * It differs by constructor parameters */ struct Drawable { +#ifdef CLIENT_COMPILATION_MODE explicit Drawable(const std::string &texture) { Texture.loadFromFile(texture); } Drawable(const std::string &texture, sf::IntRect rect) { Texture.loadFromFile(texture, rect); } explicit Drawable(const sf::Texture &texture) : Texture(texture) {} sf::Texture Texture; +#endif }; } // namespace ecs::component diff --git a/src/ecs/components/EnemyAI.hpp b/src/ecs/components/EnemyAI.hpp index 5dbee7fb..b8e1e26d 100644 --- a/src/ecs/components/EnemyAI.hpp +++ b/src/ecs/components/EnemyAI.hpp @@ -1,14 +1,9 @@ /* - * File: EnemyAI.hpp - * Project: components - * File Created: Tuesday, 4th October 2022 6:59:11 pm - * Author: Aurèle Nicolas (aurele.nicolas@epitech.eu) - * ----- - * Last Modified: Tuesday, 4th October 2022 7:26:25 pm - * Modified By: Aurèle Nicolas (aurele.nicolas@epitech.eu>) - * ----- - * Copyright 2022 - 2022 Your Company, Your Company - */ +** EPITECH PROJECT, 2022 +** RTYPE +** File description: +** EnemyAI +*/ #pragma once diff --git a/src/ecs/components/Health.hpp b/src/ecs/components/Health.hpp new file mode 100644 index 00000000..1babe58c --- /dev/null +++ b/src/ecs/components/Health.hpp @@ -0,0 +1,15 @@ +/* +** EPITECH PROJECT, 2022 +** RTYPE +** File description: +** Health +*/ + +#pragma once + +namespace ecs::component +{ + struct Health { + int health; + }; +} // namespace ecs::component diff --git a/src/ecs/components/Position.hpp b/src/ecs/components/Position.hpp index 2e7c675b..c43d847f 100644 --- a/src/ecs/components/Position.hpp +++ b/src/ecs/components/Position.hpp @@ -1,14 +1,9 @@ /* - * File: Position.hpp - * Project: components - * File Created: Monday, 3rd October 2022 3:37:48 pm - * Author: Aurèle Nicolas (aurele.nicolas@epitech.eu) - * ----- - * Last Modified: Tuesday, 4th October 2022 7:26:30 pm - * Modified By: Aurèle Nicolas (aurele.nicolas@epitech.eu>) - * ----- - * Copyright 2022 - 2022 Your Company, Your Company - */ +** EPITECH PROJECT, 2022 +** RTYPE +** File description: +** Position +*/ #pragma once #include diff --git a/src/ecs/components/Projectile.hpp b/src/ecs/components/Projectile.hpp new file mode 100644 index 00000000..80fe1ab2 --- /dev/null +++ b/src/ecs/components/Projectile.hpp @@ -0,0 +1,17 @@ +/* +** EPITECH PROJECT, 2022 +** RTYPE +** File description: +** Projectile +*/ + +#pragma once + +namespace ecs::component +{ + struct Projectile { + Projectile(int damage) : Damage(damage) {} + + int Damage; + }; +} // namespace ecs::component diff --git a/src/ecs/components/Shootable.hpp b/src/ecs/components/Shootable.hpp new file mode 100644 index 00000000..2b43022d --- /dev/null +++ b/src/ecs/components/Shootable.hpp @@ -0,0 +1,33 @@ +/* +** EPITECH PROJECT, 2022 +** RTYPE +** File description: +** Shootable +*/ + +#pragma once + +#include "SFML/Window/Keyboard.hpp" + +namespace ecs::component +{ + /** + * Shootable component define if the entity is able to shoot on other entities. In brief if it can damage another + * one + */ + struct Shootable { + /** + * Default constructor of the component, + * @param shoot Primary shoot sfml key + * @param shootSecondary Secondary shoot sfml key, unknown as default + */ + explicit Shootable(sf::Keyboard::Key shoot, sf::Keyboard::Key shootSecondary = sf::Keyboard::Key::Unknown) + : Shoot(shoot), ShootSecondary(shootSecondary) + { + } + + sf::Keyboard::Key Shoot; + + sf::Keyboard::Key ShootSecondary; + }; +} // namespace ecs::component diff --git a/src/ecs/components/Weapon.hpp b/src/ecs/components/Weapon.hpp new file mode 100644 index 00000000..4acdaa1a --- /dev/null +++ b/src/ecs/components/Weapon.hpp @@ -0,0 +1,27 @@ +/* +** EPITECH PROJECT, 2022 +** RTYPE +** File description: +** Weapon +*/ + +#pragma once + +#include + +namespace ecs::component +{ + struct Weapon { + Weapon(int shootDelay, int damage, bool hasSuper = false, int superDamage = 0, int superLoadingTime = 0) + : ShootDelay(shootDelay * 1000000), Damage(damage), HasSuper(hasSuper), SuperDamage(superDamage), SuperLoadingTime(superLoadingTime), LastShoot(0) + { + } + + int ShootDelay; + int Damage; + int64_t LastShoot; + bool HasSuper; + int SuperDamage; + int SuperLoadingTime; + }; +} // namespace ecs::component diff --git a/src/ecs/systems/CMakeLists.txt b/src/ecs/systems/CMakeLists.txt index 17b584e0..f01a4c65 100644 --- a/src/ecs/systems/CMakeLists.txt +++ b/src/ecs/systems/CMakeLists.txt @@ -4,7 +4,7 @@ set(SRCROOT ${PROJECT_SOURCE_DIR}/src/ecs/systems) set(SYSTEMS_SRC ${SRCROOT}/Draw.hpp ${SRCROOT}/HandleSFMLEvents.hpp - ${SRCROOT}/HandleSFMLMovements.hpp + ${SRCROOT}/HandleSFMLKeys.hpp ${SRCROOT}/ManageClientEvents.hpp ${SRCROOT}/Movement.hpp ${SRCROOT}/PositionLogger.hpp diff --git a/src/ecs/systems/Draw.hpp b/src/ecs/systems/Draw.hpp index 56eb3155..b985d1b0 100644 --- a/src/ecs/systems/Draw.hpp +++ b/src/ecs/systems/Draw.hpp @@ -1,14 +1,9 @@ /* - * File: Draw.hpp - * Project: systems - * File Created: Tuesday, 4th October 2022 6:33:43 pm - * Author: Aurèle Nicolas (aurele.nicolas@epitech.eu) - * ----- - * Last Modified: Wednesday, 5th October 2022 2:07:38 pm - * Modified By: Aurèle Nicolas (aurele.nicolas@epitech.eu>) - * ----- - * Copyright 2022 - 2022 Your Company, Your Company - */ +** EPITECH PROJECT, 2022 +** RTYPE +** File description: +** Draw +*/ #pragma once diff --git a/src/ecs/systems/HandleSFMLEvents.hpp b/src/ecs/systems/HandleSFMLEvents.hpp index 6a8a9749..e8687e32 100644 --- a/src/ecs/systems/HandleSFMLEvents.hpp +++ b/src/ecs/systems/HandleSFMLEvents.hpp @@ -1,27 +1,23 @@ /* - * File: HandleSFMLEvents.hpp - * Project: systems - * File Created: Tuesday, 4th October 2022 7:34:35 pm - * Author: Aurèle Nicolas (aurele.nicolas@epitech.eu) - * ----- - * Last Modified: Wednesday, 5th October 2022 2:07:14 pm - * Modified By: Aurèle Nicolas (aurele.nicolas@epitech.eu>) - * ----- - * Copyright 2022 - 2022 Your Company, Your Company - */ +** EPITECH PROJECT, 2022 +** RTYPE +** File description: +** HandleSFMLEvents +*/ #pragma once #include #include "World.hpp" -#include "components/Controllable.hpp" namespace ecs::systems { /** * Used to manage Sfml events * Currently able to manage the following actions: - * Close the window, KeyPressed + * Close the window + * KeyPressed, in this case, we check if the bind is known from sfml: + * if yes, we had it on world's events' stack, nothing otherwise */ std::function handleSFMLEvents = [](World &world) { #ifdef CLIENT_COMPILATION_MODE @@ -30,7 +26,17 @@ namespace ecs::systems while (world.getWindow().pollEvent(event)) { switch (event.type) { case sf::Event::Closed: world.getWindow().close(); break; - case sf::Event::KeyPressed: break; + case sf::Event::KeyPressed: { + // auto const &shootables = world.registry.getComponents(); + + // for (size_t i = 0; i < shootables.size(); ++i) { + // auto const &shoot = shootables[i]; + // if (shoot) { + // if (event.key.code == shoot.value().Shoot || event.key.code == shoot.value().ShootSecondary) + // world.pushEvent(ecs::Event(ecs::Event::EventType::Shoot)); + // } + // } + } break; default: break; } } diff --git a/src/ecs/systems/HandleSFMLMovements.hpp b/src/ecs/systems/HandleSFMLKeys.hpp similarity index 54% rename from src/ecs/systems/HandleSFMLMovements.hpp rename to src/ecs/systems/HandleSFMLKeys.hpp index 3472451b..6679bb51 100644 --- a/src/ecs/systems/HandleSFMLMovements.hpp +++ b/src/ecs/systems/HandleSFMLKeys.hpp @@ -1,47 +1,53 @@ /* - * File: HandleSFMLMovements.hpp - * Project: systems - * File Created: Tuesday, 4th October 2022 10:11:08 pm - * Author: Aurèle Nicolas (aurele.nicolas@epitech.eu) - * ----- - * Last Modified: Wednesday, 5th October 2022 2:07:45 pm - * Modified By: Aurèle Nicolas (aurele.nicolas@epitech.eu>) - * ----- - * Copyright 2022 - 2022 Your Company, Your Company - */ +** EPITECH PROJECT, 2022 +** RTYPE +** File description: +** HandleSFMLKeys +*/ #pragma once #include #include "World.hpp" #include "components/Controllable.hpp" +#include "components/Shootable.hpp" namespace ecs::systems { /** - * Used to manage every movement ordered by Sfml input by the user + * Used to manage every action ordered by Sfml input by the user * Refer to the Controllable.hpp documentation to learn more about managed input */ - std::function handleSFMLMovements = [](World &world) { + std::function handleSFMLKeys = [](World &world) { +#ifdef CLIENT_COMPILATION_MODE auto const &controllables = world.registry.getComponents(); + auto const &shootables = world.registry.getComponents(); - for (size_t i = 0; i < controllables.size(); ++i) { - auto const &contr = controllables[i]; + for (size_t i = 0; i < controllables.size() && i < shootables.size(); ++i) { + auto &contr = controllables[i]; + auto const &shoot = shootables[i]; if (contr) { if (sf::Keyboard::isKeyPressed(contr.value().MoveUp) || sf::Keyboard::isKeyPressed(contr.value().MoveUpSecondary)) world.pushEvent(ecs::Event(ecs::Event::EventType::MoveUp)); + else if (sf::Keyboard::isKeyPressed(contr.value().MoveDown) + || sf::Keyboard::isKeyPressed(contr.value().MoveDownSecondary)) + world.pushEvent(ecs::Event(ecs::Event::EventType::MoveDown)); if (sf::Keyboard::isKeyPressed(contr.value().MoveLeft) || sf::Keyboard::isKeyPressed(contr.value().MoveLeftSecondary)) world.pushEvent(ecs::Event(ecs::Event::EventType::MoveLeft)); - if (sf::Keyboard::isKeyPressed(contr.value().MoveDown) - || sf::Keyboard::isKeyPressed(contr.value().MoveDownSecondary)) - world.pushEvent(ecs::Event(ecs::Event::EventType::MoveDown)); - if (sf::Keyboard::isKeyPressed(contr.value().MoveRight) + else if (sf::Keyboard::isKeyPressed(contr.value().MoveRight) || sf::Keyboard::isKeyPressed(contr.value().MoveRightSecondary)) world.pushEvent(ecs::Event(ecs::Event::EventType::MoveRight)); + + if (shoot) { + if (sf::Keyboard::isKeyPressed(shoot.value().Shoot) + || sf::Keyboard::isKeyPressed(shoot.value().ShootSecondary)) + world.pushEvent(ecs::Event(ecs::Event::EventType::Shoot)); + } } } +#endif }; } // namespace ecs::systems diff --git a/src/ecs/systems/ManageClientEvents.hpp b/src/ecs/systems/ManageClientEvents.hpp index 5eae5d63..8a69bbd2 100644 --- a/src/ecs/systems/ManageClientEvents.hpp +++ b/src/ecs/systems/ManageClientEvents.hpp @@ -1,14 +1,9 @@ /* - * File: ManageClientEvents.hpp - * Project: systems - * File Created: Tuesday, 4th October 2022 10:17:07 pm - * Author: Aurèle Nicolas (aurele.nicolas@epitech.eu) - * ----- - * Last Modified: Tuesday, 4th October 2022 11:37:18 pm - * Modified By: Aurèle Nicolas (aurele.nicolas@epitech.eu>) - * ----- - * Copyright 2022 - 2022 Your Company, Your Company - */ +** EPITECH PROJECT, 2022 +** RTYPE +** File description: +** ManageClientEvents +*/ #pragma once @@ -17,6 +12,8 @@ #include "World.hpp" #include "components/Controllable.hpp" #include "components/Direction.hpp" +#include "components/Projectile.hpp" +#include "components/Weapon.hpp" namespace ecs::systems { @@ -27,32 +24,73 @@ namespace ecs::systems std::function manageClientEvents = [](World &world) { while (world.getEvent() != ecs::Event::EventType::Null) { +/** + * Region only reserved for player movement. + * Incrementing and decrementing the entity direction is used to manage multi input in the sametime, still caped as + * 1 or -1 + */ #pragma region Player Movement - auto &directions = world.registry.getComponents(); - auto const &controllables = world.registry.getComponents(); - - for (size_t i = 0; i < directions.size() && i < controllables.size(); ++i) { - auto &dir = directions[i]; - auto const &con = controllables[i]; - if (dir && con) { - if (world.getEvent() == ecs::Event::EventType::MoveUp) - dir.value().y -= 1; - else if (world.getEvent() == ecs::Event::EventType::MoveLeft) - dir.value().x -= 1; - else if (world.getEvent() == ecs::Event::EventType::MoveDown) - dir.value().y += 1; - else if (world.getEvent() == ecs::Event::EventType::MoveRight) - dir.value().x += 1; - if (dir.value().x > 0) - dir.value().x = 1; - else if (dir.value().x < 0) - dir.value().x = -1; - if (dir.value().y > 0) - dir.value().y = 1; - else if (dir.value().y < 0) - dir.value().y = -1; + if (world.getEvent() == ecs::Event::EventType::MoveUp || world.getEvent() == ecs::Event::EventType::MoveLeft + || world.getEvent() == ecs::Event::EventType::MoveDown + || world.getEvent() == ecs::Event::EventType::MoveRight) { + auto &directions = world.registry.getComponents(); + auto const &controllables = world.registry.getComponents(); + + for (size_t i = 0; i < directions.size() && i < controllables.size(); ++i) { + auto &dir = directions[i]; + auto const &con = controllables[i]; + if (dir && con) { + if (world.getEvent() == ecs::Event::EventType::MoveUp) + dir.value().y <= 0 ? dir.value().y = -1 : dir.value().y -= 1; + else if (world.getEvent() == ecs::Event::EventType::MoveLeft) + dir.value().x <= 0 ? dir.value().x = -1 : dir.value().x -= 1; + else if (world.getEvent() == ecs::Event::EventType::MoveDown) + dir.value().y >= 0 ? dir.value().y = 1 : dir.value().y += 1; + else if (world.getEvent() == ecs::Event::EventType::MoveRight) + dir.value().x >= 0 ? dir.value().x = 1 : dir.value().x += 1; + } + } + } +#pragma endregion + +/** + * Region only reserved for bullet Shoot + * If the system is called, instantly create a new entity with bullet's component + */ +#pragma region Bullet Shoot + else if (world.getEvent() == ecs::Event::EventType::Shoot) { + auto const &controllables = world.registry.getComponents(); + auto const &positions = world.registry.getComponents(); + auto &weapons = world.registry.getComponents(); + + for (size_t i = 0; i < positions.size() && i < controllables.size(); ++i) { + auto const &pos = positions[i]; + auto const &con = controllables[i]; + auto &weapon = weapons[i]; + if (pos && con && weapon) { + auto elapsed = constant::chrono::now().time_since_epoch().count() - weapon.value().LastShoot; + if (weapon.value().HasSuper && elapsed > weapon.value().SuperLoadingTime) { + weapon.value().LastShoot = constant::chrono::now().time_since_epoch().count(); + // spawn super bullet + } else if (elapsed > weapon.value().ShootDelay) { + weapon.value().LastShoot = constant::chrono::now().time_since_epoch().count(); + ecs::Entity bullet = world.registry.spawn_entity(); + world.registry.addComponent(bullet, {1, 0}); + world.registry.addComponent( + bullet, {pos.value().x, pos.value().y}); + world.registry.addComponent(bullet, {10, 10}); + world.registry.addComponent(bullet, {10, 0}); + world.registry.addComponent(bullet, {weapon.value().Damage}); +#ifdef CLIENT_COMPILATION_MODE + std::filesystem::path playerPath = + ecs::crossPlatformPath("src", "demo", "assets", "textures", "players.gif"); + world.registry.addComponent( + bullet, {playerPath, {5, 5, 1, 1}}); // To be removed with the server/client split +#endif + } + } } - }; + } #pragma endregion world.popEvent(); diff --git a/src/ecs/systems/Movement.hpp b/src/ecs/systems/Movement.hpp index 7235e2b3..f8db1044 100644 --- a/src/ecs/systems/Movement.hpp +++ b/src/ecs/systems/Movement.hpp @@ -1,14 +1,9 @@ /* - * File: Movement.hpp - * Project: systems - * File Created: Tuesday, 4th October 2022 6:33:43 pm - * Author: Aurèle Nicolas (aurele.nicolas@epitech.eu) - * ----- - * Last Modified: Wednesday, 5th October 2022 2:08:02 pm - * Modified By: Aurèle Nicolas (aurele.nicolas@epitech.eu>) - * ----- - * Copyright 2022 - 2022 Your Company, Your Company - */ +** EPITECH PROJECT, 2022 +** RTYPE +** File description: +** Movement +*/ #pragma once @@ -30,22 +25,34 @@ namespace ecs::systems auto &positions = world.registry.getComponents(); auto const &velocities = world.registry.getComponents(); auto &directions = world.registry.getComponents(); +#ifdef CLIENT_COMPILATION_MODE + auto const &controllables = world.registry.getComponents(); +#endif using chrono = std::chrono::high_resolution_clock; + using chronoDuration = std::chrono::duration; static auto clock = chrono::now(); - for (size_t i = 0; i < positions.size() && i < velocities.size(); ++i) { - auto &pos = positions[i]; - auto const &vel = velocities[i]; - auto &dir = directions[i]; - if (pos && vel) { - if (std::chrono::duration(chrono::now() - clock).count() > 10) { - pos.value().x += vel.value().x * (dir.value().x); + if (chronoDuration(chrono::now() - clock).count() > 10) { + for (size_t i = 0; i < positions.size() && i < velocities.size(); ++i) { + auto &pos = positions[i]; + auto const &vel = velocities[i]; + auto &dir = directions[i]; +#ifdef CLIENT_COMPILATION_MODE + auto const &contr = controllables[i]; +#endif + + if (pos && vel) { + pos.value().x += vel.value().x * dir.value().x; pos.value().y += vel.value().y * dir.value().y; - dir.value().x = 0; - dir.value().y = 0; - clock = chrono::now(); +#ifdef CLIENT_COMPILATION_MODE + if (i < controllables.size() && contr) { + dir.value().x = 0; + dir.value().y = 0; + } +#endif } - } - }; + }; + clock = chrono::now(); + } }; } // namespace ecs::systems diff --git a/src/ecs/utils/Window.hpp b/src/ecs/utils/Window.hpp index dc69bfdc..ce8b5cfe 100644 --- a/src/ecs/utils/Window.hpp +++ b/src/ecs/utils/Window.hpp @@ -9,12 +9,13 @@ #ifdef CLIENT_COMPILATION_MODE #include "SFML/Graphics.hpp" + #include "Constant.hpp" namespace ecs::utils { class Window : public sf::RenderWindow { public: - Window() : sf::RenderWindow(sf::VideoMode(800, 400), "r-type") {} + Window() : sf::RenderWindow(sf::VideoMode(ecs::constant::mapWidth, ecs::constant::mapHeight), "r-type") {} // Window(int wSizeWidth = 800, int wSizeHeight = 600, std::string wTitle = "r-type") // : _window(sf::VideoMode(800, 400), "r-type") // {