diff --git a/CMakeLists.txt b/CMakeLists.txt index 7a6f85c7..f43e51f3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,9 +20,31 @@ set(SOURCES sources/Component/Position/PositionComponent.hpp sources/Component/Movable/MovableComponent.cpp sources/Component/Movable/MovableComponent.hpp + sources/Component/Controllable/ControllableComponent.hpp + sources/Component/Controllable/ControllableComponent.cpp + sources/Component/Gamepad/GamepadComponent.cpp + sources/Component/Gamepad/GamepadComponent.hpp + sources/Component/Keyboard/KeyboardComponent.cpp + sources/Component/Keyboard/KeyboardComponent.hpp + sources/Component/Health/HealthComponent.cpp + sources/Component/Health/HealthComponent.hpp sources/System/Movable/MovableSystem.hpp sources/System/Movable/MovableSystem.cpp + sources/System/Controllable/ControllableSystem.cpp + sources/System/Controllable/ControllableSystem.hpp + sources/System/Gamepad/GamepadSystem.cpp + sources/System/Gamepad/GamepadSystem.hpp + sources/System/Health/HealthSystem.cpp + sources/System/Health/HealthSystem.hpp + sources/System/Keyboard/KeyboardSystem.cpp + sources/System/Keyboard/KeyboardSystem.hpp + sources/System/Movable/MovableSystem.cpp + sources/System/Movable/MovableSystem.hpp sources/Models/Vector3.hpp + sources/Component/GridCentered/GridCenteredComponent.cpp + sources/Component/GridCentered/GridCenteredComponent.hpp + sources/System/GridCentered/GridCenteredSystem.cpp + sources/System/GridCentered/GridCenteredSystem.hpp sources/Models/Vector2.hpp sources/Component/Renderer/Drawable3DComponent.hpp sources/Component/Renderer/Drawable2DComponent.hpp @@ -49,7 +71,7 @@ add_executable(unit_tests EXCLUDE_FROM_ALL tests/MainTest.cpp tests/EngineTests.cpp tests/CallbackTest.cpp -) + tests/MoveTests.cpp) target_include_directories(unit_tests PUBLIC sources) target_link_libraries(unit_tests PUBLIC wal ray) diff --git a/lib/Ray/sources/Controllers/Keyboard.hpp b/lib/Ray/sources/Controllers/Keyboard.hpp index be5c54d2..dcb474ce 100644 --- a/lib/Ray/sources/Controllers/Keyboard.hpp +++ b/lib/Ray/sources/Controllers/Keyboard.hpp @@ -1,4 +1,3 @@ - /* ** EPITECH PROJECT, 2021 ** Bomberman diff --git a/lib/Ray/sources/Window.cpp b/lib/Ray/sources/Window.cpp index c30c2575..11b14486 100644 --- a/lib/Ray/sources/Window.cpp +++ b/lib/Ray/sources/Window.cpp @@ -102,7 +102,7 @@ void RAY::Window::setFPS(unsigned int fps) SetTargetFPS(fps); } -void RAY::Window::clear(const RAY::Color &color) +void RAY::Window::clear(RAY::Color color) { ClearBackground(color); } diff --git a/lib/Ray/sources/Window.hpp b/lib/Ray/sources/Window.hpp index 91acd870..b04f60d8 100644 --- a/lib/Ray/sources/Window.hpp +++ b/lib/Ray/sources/Window.hpp @@ -90,7 +90,7 @@ namespace RAY { //! @brief Set background color (framebuffer clear color) //! @param color The color to clear the screen (default: black) - void clear(const Color &color = BLACK); + void clear(Color color = BLACK); //! @brief Different states of the view of the window enum displayState { diff --git a/sources/Component/Controllable/ControllableComponent.cpp b/sources/Component/Controllable/ControllableComponent.cpp new file mode 100644 index 00000000..959d372a --- /dev/null +++ b/sources/Component/Controllable/ControllableComponent.cpp @@ -0,0 +1,17 @@ +// +// Created by Zoe Roux on 5/24/21. +// + +#include "ControllableComponent.hpp" + +namespace BBM +{ + ControllableComponent::ControllableComponent(WAL::Entity &entity) + : WAL::Component(entity) + {} + + WAL::Component *ControllableComponent::clone(WAL::Entity &entity) const + { + return new ControllableComponent(entity); + } +} \ No newline at end of file diff --git a/sources/Component/Controllable/ControllableComponent.hpp b/sources/Component/Controllable/ControllableComponent.hpp new file mode 100644 index 00000000..8376d43f --- /dev/null +++ b/sources/Component/Controllable/ControllableComponent.hpp @@ -0,0 +1,38 @@ +// +// Created by Tom Augier on 2021-05-20. +// Edited by Benjamin Henry on 2021-05-20. +// + +#pragma once + +#include +#include "Component/Component.hpp" +#include "Entity/Entity.hpp" + +namespace BBM +{ + class ControllableComponent : public WAL::Component + { + public: + //! @brief The X and Z abscis of the movement. + Vector2f move; + //! @brief input value for jump + bool jump = false; + //! @brief input value for bomb + bool bomb = false; + //! @brief input value for pause + bool pause = false; + + //! @inherit + WAL::Component *clone(WAL::Entity &entity) const override; + + //! @brief Initialize a new controllable component. + explicit ControllableComponent(WAL::Entity &entity); + //! @brief A Controllable component is copy constructable. + ControllableComponent(const ControllableComponent &) = default; + //! @brief default destructor + ~ControllableComponent() override = default; + //! @brief A Controllable component can't be assigned + ControllableComponent &operator=(const ControllableComponent &) = delete; + }; +} \ No newline at end of file diff --git a/sources/Component/Gamepad/GamepadComponent.cpp b/sources/Component/Gamepad/GamepadComponent.cpp new file mode 100644 index 00000000..0b889b7a --- /dev/null +++ b/sources/Component/Gamepad/GamepadComponent.cpp @@ -0,0 +1,34 @@ +// +// Created by Tom Augier on 2021-05-20. +// Edited by Benjamin Henry on 2021-05-20. +// + +#include "GamepadComponent.hpp" + +namespace BBM +{ + GamepadComponent::GamepadComponent(WAL::Entity &entity) + : WAL::Component(entity), _ID(0) + {} + + GamepadComponent::GamepadComponent(WAL::Entity &entity, int ID) + : WAL::Component(entity), _ID(ID) + {} + + WAL::Component *GamepadComponent::clone(WAL::Entity &entity) const + { + return new GamepadComponent(entity, _ID); + } + + GamepadComponent &GamepadComponent::setID(int ID) + { + this->_ID = ID; + return *this; + } + + int GamepadComponent::getID() const + { + return this->_ID; + } + +} // namespace BMM diff --git a/sources/Component/Gamepad/GamepadComponent.hpp b/sources/Component/Gamepad/GamepadComponent.hpp new file mode 100644 index 00000000..be00a49b --- /dev/null +++ b/sources/Component/Gamepad/GamepadComponent.hpp @@ -0,0 +1,62 @@ +// +// Created by Tom Augier on 2021-05-20. +// Edited by Benjamin Henry on 2021-05-20. +// + +#pragma once + +#include "Controllers/Gamepad.hpp" +#include "Component/Component.hpp" +#include "Entity/Entity.hpp" + +using Button = RAY::Controller::GamePad::Button; +using Gamepad = RAY::Controller::GamePad; + +namespace BBM +{ + class GamepadComponent : public WAL::Component + { + private: + //! @brief Identifier of the gamepad, used to fetch events + int _ID; + public: + //! @brief jump key + Button keyJump = GAMEPAD_BUTTON_RIGHT_FACE_DOWN; + //! @brief bomb key + Button keyBomb = GAMEPAD_BUTTON_RIGHT_FACE_RIGHT; + //! @brief pause key + Button keyPause = GAMEPAD_BUTTON_MIDDLE; + //! @brief move right key + Button keyRight = GAMEPAD_BUTTON_LEFT_FACE_RIGHT; + //! @brief move left key + Button keyLeft = GAMEPAD_BUTTON_LEFT_FACE_LEFT; + //! @brief move up key + Button keyUp = GAMEPAD_BUTTON_LEFT_FACE_UP; + //! @brief move down key + Button keyDown = GAMEPAD_BUTTON_LEFT_FACE_DOWN; + + //! @inherit + WAL::Component *clone(WAL::Entity &entity) const override; + + //! @brief Create a new gampad component using default keys. + explicit GamepadComponent(WAL::Entity &entity); + + //! @brief Create a new PositionComponent at a certain position + GamepadComponent(WAL::Entity &entity, int id); + + //! @brief A Gamepad component is copy constructable. + GamepadComponent(const GamepadComponent &) = default; + + //! @brief default destructor + ~GamepadComponent() override = default; + + //! @brief A Gamepad component can't be assigned + GamepadComponent &operator=(const GamepadComponent &) = delete; + + //! @brief Set the ID of the Gamepad the events must be fetch from; + GamepadComponent &setID(int ID); + + //! @brief Get the ID of the Gamepad the events must be fetch from; + int getID() const; + }; +} \ No newline at end of file diff --git a/sources/Component/GridCentered/GridCenteredComponent.cpp b/sources/Component/GridCentered/GridCenteredComponent.cpp new file mode 100644 index 00000000..d4b333b4 --- /dev/null +++ b/sources/Component/GridCentered/GridCenteredComponent.cpp @@ -0,0 +1,17 @@ +// +// Created by Zoe Roux on 5/24/21. +// + +#include "GridCenteredComponent.hpp" + +namespace BBM +{ + GridCenteredComponent::GridCenteredComponent(WAL::Entity &entity) + : WAL::Component(entity) + {} + + WAL::Component *GridCenteredComponent::clone(WAL::Entity &entity) const + { + return new GridCenteredComponent(entity); + } +} \ No newline at end of file diff --git a/sources/Component/GridCentered/GridCenteredComponent.hpp b/sources/Component/GridCentered/GridCenteredComponent.hpp new file mode 100644 index 00000000..d4de0eed --- /dev/null +++ b/sources/Component/GridCentered/GridCenteredComponent.hpp @@ -0,0 +1,33 @@ +// +// Created by Zoe Roux on 5/24/21. +// + +#pragma once + +#include + +namespace BBM +{ + //! @brief A component to slowly center entities to the middle of their current block. + //! This allow flexibility in their movement will preventing them from getting stuck at every corner. + class GridCenteredComponent : public WAL::Component + { + public: + //! @brief The force factor applied at each frame. + float force = 1; + + //! @inherit + Component *clone(WAL::Entity &entity) const override; + + //! @brief Create a new, default GridCenteredComponent. + //! @param entity The entity attached to this component. + explicit GridCenteredComponent(WAL::Entity &entity); + //! @brief A GridCenteredComponent is copy constructable + //! @param other The other GridCenteredComponent to copy. + GridCenteredComponent(const GridCenteredComponent &other) = default; + //! @brief A default destructor + ~GridCenteredComponent() override = default; + //! @brief A GridCenteredComponent is not assignable + GridCenteredComponent &operator=(const GridCenteredComponent &) = delete; + }; +} diff --git a/sources/Component/Health/HealthComponent.cpp b/sources/Component/Health/HealthComponent.cpp new file mode 100644 index 00000000..fe81fabb --- /dev/null +++ b/sources/Component/Health/HealthComponent.cpp @@ -0,0 +1,43 @@ +// +// Created by Tom Augier on 2021-05-20. +// Edited by Benjamin Henry on 2021-05-20. +// Edited by Louis Auzuret on 2021-05-20. +// + +#include "HealthComponent.hpp" + +namespace BBM +{ + HealthComponent::HealthComponent(WAL::Entity &entity) + : WAL::Component(entity), + _healthPoint() + {} + + HealthComponent::HealthComponent(WAL::Entity &entity, unsigned int healthPoint) + : WAL::Component(entity), + _healthPoint(healthPoint) + {} + + WAL::Component *HealthComponent::clone(WAL::Entity &entity) const + { + return new HealthComponent(entity); + } + + void HealthComponent::addHealthPoint(unsigned int healthPoint) + { + this->_healthPoint += healthPoint; + } + + void HealthComponent::takeDmg(unsigned int damage) + { + if (damage >= this->_healthPoint) { + this->_healthPoint = 0; + } else + this->_healthPoint -= damage; + } + + unsigned int HealthComponent::getHealthPoint(void) const + { + return (this->_healthPoint); + } +} \ No newline at end of file diff --git a/sources/Component/Health/HealthComponent.hpp b/sources/Component/Health/HealthComponent.hpp new file mode 100644 index 00000000..2eadafc5 --- /dev/null +++ b/sources/Component/Health/HealthComponent.hpp @@ -0,0 +1,53 @@ +// +// Created by Tom Augier on 2021-05-20. +// Edited by Benjamin Henry on 2021-05-20. +// Edited by Louis Auzuret on 2021-05-20. +// + +#pragma once + +#include +#include "Component/Component.hpp" +#include "Entity/Entity.hpp" + +namespace BBM +{ + class HealthComponent : public WAL::Component + { + + private: + //! @brief life of an entity + unsigned int _healthPoint; + + public: + //! @brief The callback invoked on this entity's death. + WAL::Callback onDeath; + + //! @brief add health to the entity + void addHealthPoint(unsigned int healthPoint); + + //! @brief reduce health + void takeDmg(unsigned int damage); + + //! @brief return health point of the entity + unsigned int getHealthPoint(void) const; + + //! @inherit + WAL::Component *clone(WAL::Entity &entity) const override; + + //! @brief A Health component can't be instantiated, it should be derived. + explicit HealthComponent(WAL::Entity &entity); + + //! @brief Constructor + HealthComponent(WAL::Entity &entity, unsigned int healthPoint); + + //! @brief A Health component can't be instantiated, it should be derived. + HealthComponent(const HealthComponent &) = default; + + //! @brief default destructor + ~HealthComponent() override = default; + + //! @brief A Health component can't be assigned + HealthComponent &operator=(const HealthComponent &) = delete; + }; +} \ No newline at end of file diff --git a/sources/Component/Keyboard/KeyboardComponent.cpp b/sources/Component/Keyboard/KeyboardComponent.cpp new file mode 100644 index 00000000..de379862 --- /dev/null +++ b/sources/Component/Keyboard/KeyboardComponent.cpp @@ -0,0 +1,19 @@ +// +// Created by Tom Augier on 2021-05-20. +// Edited by Benjamin Henry on 2021-05-20. +// + +#include "KeyboardComponent.hpp" + +namespace BBM +{ + KeyboardComponent::KeyboardComponent(WAL::Entity &entity) + : WAL::Component(entity) + {} + + WAL::Component *KeyboardComponent::clone(WAL::Entity &entity) const + { + return new KeyboardComponent(entity); + } + +} // namespace BMM diff --git a/sources/Component/Keyboard/KeyboardComponent.hpp b/sources/Component/Keyboard/KeyboardComponent.hpp new file mode 100644 index 00000000..bd0f2ab7 --- /dev/null +++ b/sources/Component/Keyboard/KeyboardComponent.hpp @@ -0,0 +1,49 @@ +// +// Created by Tom Augier on 2021-05-20. +// Edited by Benjamin Henry on 2021-05-20. +// + +#pragma once + +#include +#include "Component/Component.hpp" +#include "Entity/Entity.hpp" + +using Key = RAY::Controller::Keyboard::Key; + +namespace BBM +{ + class KeyboardComponent : public WAL::Component + { + public: + //! @brief jump key + Key keyJump = KEY_SPACE; + //! @brief bomb key + Key keyBomb = KEY_E; + //! @brief pause key + Key keyPause = KEY_ESCAPE; + //! @brief move right key + Key keyRight = KEY_A; + //! @brief move left key + Key keyLeft = KEY_D; + //! @brief move up key + Key keyUp = KEY_W; + //! @brief move down key + Key keyDown = KEY_S; + + //! @inherit + WAL::Component *clone(WAL::Entity &entity) const override; + + //! @brief Create a new keyboard component using default keys. + explicit KeyboardComponent(WAL::Entity &entity); + + //! @brief A Keyboard component is copy constructable. + KeyboardComponent(const KeyboardComponent &) = default; + + //! @brief default destructor + ~KeyboardComponent() override = default; + + //! @brief A Keyboard component can't be assigned + KeyboardComponent &operator=(const KeyboardComponent &) = delete; + }; +} \ No newline at end of file diff --git a/sources/Component/Renderer/CameraComponent.cpp b/sources/Component/Renderer/CameraComponent.cpp index 3f6c1239..5934d527 100644 --- a/sources/Component/Renderer/CameraComponent.cpp +++ b/sources/Component/Renderer/CameraComponent.cpp @@ -6,8 +6,9 @@ namespace BBM { - CameraComponent::CameraComponent(WAL::Entity &entity) - : Component(entity) + CameraComponent::CameraComponent(WAL::Entity &entity, Vector3f target) + : Component(entity), + target(target) {} WAL::Component *BBM::CameraComponent::clone(WAL::Entity &entity) const diff --git a/sources/Component/Renderer/CameraComponent.hpp b/sources/Component/Renderer/CameraComponent.hpp index 1aa5cc38..3f8e7fc5 100644 --- a/sources/Component/Renderer/CameraComponent.hpp +++ b/sources/Component/Renderer/CameraComponent.hpp @@ -5,6 +5,7 @@ #pragma once #include +#include namespace BBM { @@ -13,11 +14,14 @@ namespace BBM class CameraComponent : public WAL::Component { public: + //! @brief The camera's target, the cam will look at this position. + Vector3f target; + //! @inherit Component *clone(WAL::Entity &entity) const override; //! @brief Ctor - explicit CameraComponent(WAL::Entity &); + explicit CameraComponent(WAL::Entity &, Vector3f target = Vector3f()); //! @brief A camera component is copy constructable. CameraComponent(const CameraComponent &) = default; //! @brief Default destructor. diff --git a/sources/Map/Map.cpp b/sources/Map/Map.cpp index 77574593..d02b65b4 100644 --- a/sources/Map/Map.cpp +++ b/sources/Map/Map.cpp @@ -90,7 +90,7 @@ namespace BBM { scene->addEntity("Breakable Block") .addComponent(coords) - //.addComponent(1) + .addComponent(1) .addComponent>("assets/wall/breakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/breakable_wall.png")); } diff --git a/sources/Map/Map.hpp b/sources/Map/Map.hpp index 21266877..d519c52d 100644 --- a/sources/Map/Map.hpp +++ b/sources/Map/Map.hpp @@ -17,6 +17,7 @@ #include "Model/Model.hpp" #include "Component/Component.hpp" #include "Component/Position/PositionComponent.hpp" +#include "Component/Health/HealthComponent.hpp" #include "Component/Movable/MovableComponent.hpp" namespace BBM diff --git a/sources/Models/Vector2.hpp b/sources/Models/Vector2.hpp index 07904949..629a11b8 100644 --- a/sources/Models/Vector2.hpp +++ b/sources/Models/Vector2.hpp @@ -129,6 +129,11 @@ namespace BBM { double mag = this->magnitude(); + if (mag == 0) { + this->x = 0; + this->y = 0; + return *this; + } this->x /= mag; this->y /= mag; return *this; @@ -138,6 +143,8 @@ namespace BBM { T mag = this->magnitude(); + if (mag == 0) + return Vector2(); return Vector2(this->x / mag, this->y / mag); } diff --git a/sources/Models/Vector3.hpp b/sources/Models/Vector3.hpp index 5579b382..def6d322 100644 --- a/sources/Models/Vector3.hpp +++ b/sources/Models/Vector3.hpp @@ -136,6 +136,12 @@ namespace BBM { double mag = this->magnitude(); + if (mag == 0) { + this->x = 0; + this->y = 0; + this->z = 0; + return *this; + } this->x /= mag; this->y /= mag; this->z /= mag; @@ -146,6 +152,8 @@ namespace BBM { T mag = this->magnitude(); + if (mag == 0) + return Vector3(); return Vector3(this->x / mag, this->y / mag, this->z / mag); } diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 4266cb24..8c14ad31 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -12,6 +12,12 @@ #include #include #include +#include +#include +#include +#include +#include +#include #include "Models/Vector2.hpp" #include "Component/Renderer/CameraComponent.hpp" #include "Runner.hpp" @@ -33,6 +39,14 @@ namespace BBM engine.shouldClose = true; } + void addSystems(WAL::Wal &wal) + { + wal.addSystem() + .addSystem() + .addSystem() + .addSystem(); + } + void enableRaylib(WAL::Wal &wal) { RAY::TraceLog::setLevel(LOG_WARNING); @@ -41,7 +55,7 @@ namespace BBM wal.addSystem>(); wal.addSystem(window) - .addSystem>(); + .addSystem>(); wal.addSystem(window); } @@ -50,12 +64,18 @@ namespace BBM auto scene = std::make_shared(); scene->addEntity("cube") .addComponent() - .addComponent>(Vector2f(), Vector2f(10, 10), RED); - /* scene->addEntity("player") + .addComponent>(Vector2f(), Vector2f(10, 10), RED) + .addComponent() + .addComponent() + .addComponent();; + scene->addEntity("player") .addComponent() - .addComponent>("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png")); */ + .addComponent>("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png")) + .addComponent() + .addComponent() + .addComponent(); scene->addEntity("camera") - .addComponent(25, 50, 25) + .addComponent(0, 20, -5) .addComponent(); MapGenerator::generateMap(15, 15, rand(), scene); return scene; @@ -64,7 +84,7 @@ namespace BBM int run() { WAL::Wal wal; - wal.addSystem(); + addSystems(wal); enableRaylib(wal); wal.scene = loadGameScene(); diff --git a/sources/System/Controllable/ControllableSystem.cpp b/sources/System/Controllable/ControllableSystem.cpp new file mode 100644 index 00000000..0a9c789c --- /dev/null +++ b/sources/System/Controllable/ControllableSystem.cpp @@ -0,0 +1,30 @@ +// +// Created by Tom Augier on 2021-05-20. +// Edited by Benjamin Henry on 2021-05-20. +// + +#include "ControllableSystem.hpp" +#include "Component/Movable/MovableComponent.hpp" +#include "Component/Controllable/ControllableComponent.hpp" +#include "Entity/Entity.hpp" + +namespace BBM +{ + float ControllableSystem::speed = .25f; + + ControllableSystem::ControllableSystem() + : WAL::System({ + typeid(ControllableComponent), + typeid(MovableComponent) + }) + {} + + void ControllableSystem::onFixedUpdate(WAL::Entity &entity) + { + auto &controllable = entity.getComponent(); + auto &movable = entity.getComponent(); + Vector2f move = controllable.move.normalized() * ControllableSystem::speed; + + movable.addForce(Vector3f(move.x, controllable.jump, move.y)); + } +} \ No newline at end of file diff --git a/sources/System/Controllable/ControllableSystem.hpp b/sources/System/Controllable/ControllableSystem.hpp new file mode 100644 index 00000000..fac4db08 --- /dev/null +++ b/sources/System/Controllable/ControllableSystem.hpp @@ -0,0 +1,31 @@ +// +// Created by Tom Augier on 2021-05-20. +// Edited by Benjamin Henry on 2021-05-20. +// + +#pragma once + +#include "System/System.hpp" + +namespace BBM +{ + //! @brief A system to handle Controllable entities. + class ControllableSystem : public WAL::System + { + public: + //! @brief The speed applied to every controllable entities. + static float speed; + + //! @inherit + void onFixedUpdate(WAL::Entity &entity) override; + + //! @brief A default constructor + ControllableSystem(); + //! @brief A Controllable system is copy constructable + ControllableSystem(const ControllableSystem &) = default; + //! @brief A default destructor + ~ControllableSystem() override = default; + //! @brief A Controllable system is assignable. + ControllableSystem &operator=(const ControllableSystem &) = default; + }; +} diff --git a/sources/System/Gamepad/GamepadSystem.cpp b/sources/System/Gamepad/GamepadSystem.cpp new file mode 100644 index 00000000..5df6bd96 --- /dev/null +++ b/sources/System/Gamepad/GamepadSystem.cpp @@ -0,0 +1,43 @@ +// +// Created by Arthur Jamet on 2021-05-31. +// + +#include "GamepadSystem.hpp" +#include "Component/Gamepad/GamepadComponent.hpp" +#include "Component/Controllable/ControllableComponent.hpp" +#include "Entity/Entity.hpp" +#include "Controllers/Gamepad.hpp" + +using Button = RAY::Controller::GamePad::Button; +using Gamepad = RAY::Controller::GamePad; + +namespace BBM +{ + GamepadSystem::GamepadSystem() + : WAL::System({ + typeid(GamepadComponent), + typeid(ControllableComponent) + }) + {} + + void GamepadSystem::onFixedUpdate(WAL::Entity &entity) + { + const auto &gamepadComponent = entity.getComponent(); + auto &controllable = entity.getComponent(); + Gamepad gamepad(gamepadComponent.getID()); + + const std::map keyPressedMap = { + {gamepadComponent.keyJump, controllable.jump}, + {gamepadComponent.keyBomb, controllable.bomb}, + {gamepadComponent.keyPause, controllable.pause} + }; + + for (auto key : keyPressedMap) + key.second = gamepad.isPressed(key.first); + controllable.move = Vector2f(); + controllable.move.x += gamepad.isPressed(gamepadComponent.keyRight); + controllable.move.x -= gamepad.isPressed(gamepadComponent.keyLeft); + controllable.move.y += gamepad.isPressed(gamepadComponent.keyUp); + controllable.move.y -= gamepad.isPressed(gamepadComponent.keyDown); + } +} \ No newline at end of file diff --git a/sources/System/Gamepad/GamepadSystem.hpp b/sources/System/Gamepad/GamepadSystem.hpp new file mode 100644 index 00000000..9c8c705f --- /dev/null +++ b/sources/System/Gamepad/GamepadSystem.hpp @@ -0,0 +1,28 @@ +// +// Created by Arthur Jamet on 2021-05-31. +// + +#pragma once + +#include "System/System.hpp" +#include + +namespace BBM +{ + //! @brief A system to handle Gamepad entities. + class GamepadSystem : public WAL::System + { + public: + //! @inherit + void onFixedUpdate(WAL::Entity &entity) override; + + //! @brief A default constructor + GamepadSystem(); + //! @brief A Gamepad system is copy constructable + GamepadSystem(const GamepadSystem &) = default; + //! @brief A default destructor + ~GamepadSystem() override = default; + //! @brief A Gamepad system is assignable. + GamepadSystem &operator=(const GamepadSystem &) = default; + }; +} diff --git a/sources/System/GridCentered/GridCenteredSystem.cpp b/sources/System/GridCentered/GridCenteredSystem.cpp new file mode 100644 index 00000000..5dcca1a9 --- /dev/null +++ b/sources/System/GridCentered/GridCenteredSystem.cpp @@ -0,0 +1,25 @@ +// +// Created by Zoe Roux on 5/24/21. +// + +#include "Component/Movable/MovableComponent.hpp" +#include "Component/GridCentered/GridCenteredComponent.hpp" +#include "GridCenteredSystem.hpp" + +namespace BBM +{ + GridCenteredSystem::GridCenteredSystem() + : WAL::System({ + typeid(GridCenteredComponent), + typeid(MovableComponent), +// typeid(PositionComponent) + }) + {} + + void GridCenteredSystem::onFixedUpdate(WAL::Entity &entity) + { + auto &grid = entity.getComponent(); + auto &movement = entity.getComponent(); +// movement.addForce(grid.force * ) + } +} \ No newline at end of file diff --git a/sources/System/GridCentered/GridCenteredSystem.hpp b/sources/System/GridCentered/GridCenteredSystem.hpp new file mode 100644 index 00000000..e84e65fb --- /dev/null +++ b/sources/System/GridCentered/GridCenteredSystem.hpp @@ -0,0 +1,26 @@ +// +// Created by Zoe Roux on 5/24/21. +// + +#pragma once + +#include + +namespace BBM +{ + //! @brief The system handling GridCenteredComponent + class GridCenteredSystem : public WAL::System + { + public: + void onFixedUpdate(WAL::Entity &entity) override; + + //! @brief A default constructor + GridCenteredSystem(); + //! @brief A GridCenteredSystem is copyable. + GridCenteredSystem(const GridCenteredSystem &) = default; + //! @brief A default destructor + ~GridCenteredSystem() override = default; + //! @brief A GridCenteredSystem is assignable + GridCenteredSystem &operator=(const GridCenteredSystem &) = default; + }; +} diff --git a/sources/System/Health/HealthSystem.cpp b/sources/System/Health/HealthSystem.cpp new file mode 100644 index 00000000..90bbfbb9 --- /dev/null +++ b/sources/System/Health/HealthSystem.cpp @@ -0,0 +1,26 @@ +// +// Created by Tom Augier on 2021-05-20. +// Edited by Benjamin Henry on 2021-05-20. +// + +#include "HealthSystem.hpp" +#include "Component/Health/HealthComponent.hpp" +#include "Component/Controllable/ControllableComponent.hpp" +#include "Entity/Entity.hpp" + +namespace BBM +{ + HealthSystem::HealthSystem() + : WAL::System({ + typeid(HealthComponent) + }) + {} + + void HealthSystem::onFixedUpdate(WAL::Entity &entity) + { + auto &health = entity.getComponent(); + + if (health.getHealthPoint() == 0) + health.onDeath(entity); + } +} \ No newline at end of file diff --git a/sources/System/Health/HealthSystem.hpp b/sources/System/Health/HealthSystem.hpp new file mode 100644 index 00000000..15ca2062 --- /dev/null +++ b/sources/System/Health/HealthSystem.hpp @@ -0,0 +1,28 @@ +// +// Created by Tom Augier on 2021-05-20. +// Edited by Benjamin Henry on 2021-05-20. +// + +#pragma once + +#include "System/System.hpp" + +namespace BBM +{ + //! @brief A system to handle Health entities. + class HealthSystem : public WAL::System + { + public: + //! @inherit + void onFixedUpdate(WAL::Entity &entity) override; + + //! @brief A default constructor + HealthSystem(); + //! @brief A Health system is copy constructable + HealthSystem(const HealthSystem &) = default; + //! @brief A default destructor + ~HealthSystem() override = default; + //! @brief A Health system is assignable. + HealthSystem &operator=(const HealthSystem &) = default; + }; +} diff --git a/sources/System/Keyboard/KeyboardSystem.cpp b/sources/System/Keyboard/KeyboardSystem.cpp new file mode 100644 index 00000000..13ad9804 --- /dev/null +++ b/sources/System/Keyboard/KeyboardSystem.cpp @@ -0,0 +1,47 @@ +// +// Created by Tom Augier on 2021-05-20. +// Edited by Benjamin Henry on 2021-05-20. +// + +#include +#include "KeyboardSystem.hpp" +#include "Component/Keyboard/KeyboardComponent.hpp" +#include "Component/Controllable/ControllableComponent.hpp" +#include "Entity/Entity.hpp" +#include "Controllers/Keyboard.hpp" + +using Keyboard = RAY::Controller::Keyboard; + +namespace BBM +{ + KeyboardSystem::KeyboardSystem() + : WAL::System({ + typeid(KeyboardComponent), + typeid(ControllableComponent) + }) + {} + + void KeyboardSystem::onFixedUpdate(WAL::Entity &entity) + { + const auto &keyboard = entity.getComponent(); + auto &controllable = entity.getComponent(); + + const std::map keyPressedMap = { + {keyboard.keyJump, controllable.jump}, + {keyboard.keyBomb, controllable.bomb}, + {keyboard.keyPause, controllable.pause} + }; + + for (auto key : keyPressedMap) + key.second = Keyboard::isDown(key.first); + controllable.move = Vector2f(); + if (Keyboard::isDown(keyboard.keyRight)) + controllable.move.x += 1; + if (Keyboard::isDown(keyboard.keyLeft)) + controllable.move.x -= 1; + if (Keyboard::isDown(keyboard.keyUp)) + controllable.move.y += 1; + if (Keyboard::isDown(keyboard.keyDown)) + controllable.move.y -= 1; + } +} \ No newline at end of file diff --git a/sources/System/Keyboard/KeyboardSystem.hpp b/sources/System/Keyboard/KeyboardSystem.hpp new file mode 100644 index 00000000..f17c5f15 --- /dev/null +++ b/sources/System/Keyboard/KeyboardSystem.hpp @@ -0,0 +1,29 @@ +// +// Created by Tom Augier on 2021-05-20. +// Edited by Benjamin Henry on 2021-05-20. +// + +#pragma once + +#include "System/System.hpp" +#include + +namespace BBM +{ + //! @brief A system to handle keyboard entities. + class KeyboardSystem : public WAL::System + { + public: + //! @inherit + void onFixedUpdate(WAL::Entity &entity) override; + + //! @brief A default constructor + KeyboardSystem(); + //! @brief A keyboard system is copy constructable + KeyboardSystem(const KeyboardSystem &) = default; + //! @brief A default destructor + ~KeyboardSystem() override = default; + //! @brief A keyboard system is assignable. + KeyboardSystem &operator=(const KeyboardSystem &) = default; + }; +} diff --git a/sources/System/Movable/MovableSystem.cpp b/sources/System/Movable/MovableSystem.cpp index 5b57ebba..1c439345 100644 --- a/sources/System/Movable/MovableSystem.cpp +++ b/sources/System/Movable/MovableSystem.cpp @@ -5,7 +5,6 @@ #include "Component/Position/PositionComponent.hpp" #include "MovableSystem.hpp" #include "Component/Movable/MovableComponent.hpp" -#include "Wal.hpp" namespace BBM { @@ -21,8 +20,8 @@ namespace BBM auto &movable = entity.getComponent(); auto &position = entity.getComponent(); - position.position += movable._velocity * WAL::Wal::timestep.count(); - movable._velocity = movable._acceleration * WAL::Wal::timestep.count(); + position.position += movable._velocity; + movable._velocity = movable._acceleration; movable._acceleration = Vector3f(); } } // namespace WAL \ No newline at end of file diff --git a/sources/System/Renderer/RenderScreenSystem.cpp b/sources/System/Renderer/RenderScreenSystem.cpp index 5523ee48..8eba847a 100644 --- a/sources/System/Renderer/RenderScreenSystem.cpp +++ b/sources/System/Renderer/RenderScreenSystem.cpp @@ -27,6 +27,8 @@ namespace BBM void RenderScreenSystem::onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime) { const auto &pos = entity.getComponent(); + const auto &cam = entity.getComponent(); _camera.setPosition(pos.position); + _camera.setTarget(cam.target); } } \ No newline at end of file diff --git a/tests/MoveTests.cpp b/tests/MoveTests.cpp new file mode 100644 index 00000000..cd37d22a --- /dev/null +++ b/tests/MoveTests.cpp @@ -0,0 +1,51 @@ +// +// Created by Zoe Roux on 5/31/21. +// + +#include "Entity/Entity.hpp" +#include "Component/Position/PositionComponent.hpp" +#include "System/Movable/MovableSystem.hpp" +#include "System/Controllable/ControllableSystem.hpp" +#include +#include +#include + +#define private public +#include + +using namespace WAL; +using namespace BBM; + + +TEST_CASE("Move test", "[Component][System]") +{ + Scene scene; + scene.addEntity("player") + .addComponent() + .addComponent() + .addComponent(); + Entity &entity = scene.getEntities()[0]; + + REQUIRE(entity.getComponent().position == Vector3f()); + + entity.getComponent().move = Vector2f(1, 1); + + ControllableSystem controllable; + controllable.onUpdate(entity, std::chrono::nanoseconds(1)); + controllable.onFixedUpdate(entity); + REQUIRE(entity.getComponent()._acceleration.x > 0); + REQUIRE(entity.getComponent()._acceleration.z > 0); + + MovableSystem movable; + movable.onUpdate(entity, std::chrono::nanoseconds(1)); + movable.onFixedUpdate(entity); + REQUIRE(entity.getComponent()._velocity.x > 0); + REQUIRE(entity.getComponent()._velocity.z > 0); + REQUIRE(entity.getComponent()._acceleration.x == 0); + REQUIRE(entity.getComponent()._acceleration.z == 0); + movable.onUpdate(entity, std::chrono::nanoseconds(1)); + movable.onFixedUpdate(entity); + REQUIRE(entity.getComponent().position.x > 0); + REQUIRE(entity.getComponent().position.z > 0); + +} \ No newline at end of file