mirror of
https://github.com/zoriya/Bomberman.git
synced 2026-06-02 02:05:25 +00:00
gamepad ecs
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
//
|
||||
// Created by Tom Augier on 2021-05-20.
|
||||
// Edited by Benjamin Henry on 2021-05-20.
|
||||
//
|
||||
|
||||
#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<GamepadComponent>();
|
||||
auto &controllable = entity.getComponent<ControllableComponent>();
|
||||
Gamepad gamepad(gamepadComponent.getID());
|
||||
|
||||
const std::map<Button, bool> keyPressedMap = {
|
||||
{gamepadComponent.keyJump, controllable.jump},
|
||||
{gamepadComponent.keyBomb, controllable.bomb},
|
||||
{gamepadComponent.keyPause, controllable.pause}
|
||||
};
|
||||
|
||||
for (auto key : keyPressedMap)
|
||||
key.second = gamepad.isPressed(key.first);
|
||||
controllable.moveX = 0;
|
||||
controllable.moveZ = 0;
|
||||
controllable.moveX += gamepad.isPressed(gamepadComponent.keyRight);
|
||||
controllable.moveX -= gamepad.isPressed(gamepadComponent.keyLeft);
|
||||
controllable.moveX += gamepad.isPressed(gamepadComponent.keyUp);
|
||||
controllable.moveX -= gamepad.isPressed(gamepadComponent.keyDown);
|
||||
}
|
||||
}
|
||||
@@ -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 <map>
|
||||
|
||||
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;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user