Add basic controls components and system

KeyboardComponents/System + ControllableComponents/ControllableSystem
This commit is contained in:
TrueBabyChaise
2021-05-20 17:59:34 +02:00
parent da7694eb0b
commit 7377301c4e
8 changed files with 244 additions and 0 deletions
@@ -0,0 +1,46 @@
//
// Created by Tom Augier on 2021-05-20.
// Edited by Benjamin Henry on 2021-05-20.
//
#pragma once
#include "lib/wal/sources/Component/Component.hpp"
#include "lib/wal/sources/Entity/Entity.hpp"
namespace BBM
{
class ControllableComponent : public WAL::Component
{
private:
bool _up;
bool _down;
bool _left;
bool _right;
bool _jump;
bool _bomb;
bool _pause;
public:
//! @inherit
WAL::Component *clone(WAL::Entity &entity) const override;
//! @brief A component can't be instantiated, it should be derived.
explicit ControllableComponent(WAL::Entity &entity);
//! @brief Constructor
ControllableComponent(WAL::Entity &entity, unsigned int maxBombCount);
//! @brief A component can't be instantiated, it should be derived.
ControllableComponent(const ControllableComponent &) = default;
//! @brief default destructor
~ControllableComponent() override = default;
//! @brief A component can't be assigned
ControllableComponent &operator=(const ControllableComponent &) = delete;
friend class KeyboardSystem;
friend class ControllableSystem;
};
}
@@ -0,0 +1,14 @@
//
// 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)
{}
} // namespace BMM
@@ -0,0 +1,52 @@
//
// Created by Tom Augier on 2021-05-20.
// Edited by Benjamin Henry on 2021-05-20.
//
#pragma once
#include "lib/wal/sources/Component/Component.hpp"
#include "lib/wal/sources/Entity/Entity.hpp"
namespace BBM
{
class KeyboardComponent : public WAL::Component
{
public:
//! @brief jump key
int keyJump;
//! @brief bomb key
int keyBomb;
//! @brief pause key
int keyPause;
//! @brief move right key
int keyRight;
//! @brief move left key
int keyLeft;
//! @brief move up key
int keyUp;
//! @brief move down key
int keyDown;
//! @inherit
WAL::Component *clone(WAL::Entity &entity) const override;
//! @brief A component can't be instantiated, it should be derived.
explicit KeyboardComponent(WAL::Entity &entity);
//! @brief Constructor
KeyboardComponent(WAL::Entity &entity, unsigned int maxBombCount);
//! @brief A component can't be instantiated, it should be derived.
KeyboardComponent(const KeyboardComponent &) = default;
//! @brief default destructor
~KeyboardComponent() override = default;
//! @brief A component can't be assigned
KeyboardComponent &operator=(const KeyboardComponent &) = delete;
friend class KeyboardSystem;
};
}
@@ -0,0 +1,34 @@
//
// Created by Tom Augier on 2021-05-20.
// Edited by Benjamin Henry on 2021-05-20.
//
#include "ControllableSystem.hpp"
#include "lib/wal/sources/Component/Movable/MovableComponent.hpp"
#include "sources/Component/Controllable/ControllableComponent.hpp"
#include "lib/wal/sources/Entity/Entity.hpp"
namespace BBM
{
const std::type_info &ControllableSystem::getComponent() const
{
return typeid(ControllableComponent);
}
void ControllableSystem::onFixedUpdate(WAL::Entity &entity)
{
auto &controllable= entity.getComponent<ControllableComponent>();
auto &movable= entity.getComponent<WAL::MovableComponent>();
if (controllable._left)
movable.addForce(WAL::Vector3f(-1, 0, 0));
if (controllable._right)
movable.addForce(WAL::Vector3f(1, 0, 0));
if (controllable._down)
movable.addForce(WAL::Vector3f(0, 0, -1));
if (controllable._up)
movable.addForce(WAL::Vector3f(0, 0, 1));
if (controllable._jump)
movable.addForce(WAL::Vector3f(0, 1, 0));
}
}
@@ -0,0 +1,30 @@
//
// Created by Tom Augier on 2021-05-20.
// Edited by Benjamin Henry on 2021-05-20.
//
#pragma once
#include "lib/wal/sources/System/System.hpp"
namespace BBM
{
//! @brief A system to handle Controllable entities.
class ControllableSystem : public WAL::System
{
public:
//! @inherit
const std::type_info &getComponent() const override;
//! @inherit
void onFixedUpdate(WAL::Entity &entity) override;
//! @brief A default constructor
ControllableSystem() = default;
//! @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;
};
}
@@ -0,0 +1,38 @@
//
// Created by Tom Augier on 2021-05-20.
// Edited by Benjamin Henry on 2021-05-20.
//
#include "KeyboardSystem.hpp"
#include "sources/Component/Keyboard/KeyboardComponent.hpp"
#include "sources/Component/Controllable/ControllableComponent.hpp"
#include "lib/wal/sources/Entity/Entity.hpp"
namespace BBM
{
const std::type_info &KeyboardSystem::getComponent() const
{
return typeid(KeyboardComponent);
}
void KeyboardSystem::onFixedUpdate(WAL::Entity &entity)
{
auto &keyboard = entity.getComponent<KeyboardComponent>();
auto &controllable= entity.getComponent<ControllableComponent>();
if (RAY::IsKeyPressed(keyboard.keyRight))
controllable._right = true;
if (RAY::IsKeyPressed(keyboard.keyLeft))
controllable._left = true;
if (RAY::IsKeyPressed(keyboard.keyUp))
controllable._up = true;
if (RAY::IsKeyPressed(keyboard.keyDown))
controllable._down = true;
if (RAY::IsKeyPressed(keyboard.keyBomb))
controllable._bomb = true;
if (RAY::IsKeyPressed(keyboard.keyJump))
controllable._jump = true;
if (RAY::IsKeyPressed(keyboard.keyPause))
controllable._pause = true;
}
}
@@ -0,0 +1,30 @@
//
// Created by Tom Augier on 2021-05-20.
// Edited by Benjamin Henry on 2021-05-20.
//
#pragma once
#include "lib/wal/sources/System/System.hpp"
namespace BBM
{
//! @brief A system to handle keyboard entities.
class KeyboardSystem : public WAL::System
{
public:
//! @inherit
const std::type_info &getComponent() const override;
//! @inherit
void onFixedUpdate(WAL::Entity &entity) override;
//! @brief A default constructor
KeyboardSystem() = default;
//! @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;
};
}