FreeSteeringSystem converted to take input commands
This commit is contained in:
+7
-3
@@ -12,6 +12,10 @@ void GameWorld::Initialize()
|
||||
BindKey(GLFW_KEY_S, "+backward");
|
||||
BindKey(GLFW_KEY_A, "+left");
|
||||
BindKey(GLFW_KEY_D, "+right");
|
||||
BindKey(GLFW_KEY_SPACE, "+up");
|
||||
BindKey(GLFW_KEY_LEFT_CONTROL, "+down");
|
||||
BindKey(GLFW_KEY_LEFT_ALT, "+slow");
|
||||
BindKey(GLFW_KEY_LEFT_SHIFT, "+fast");
|
||||
BindMouseButton(GLFW_MOUSE_BUTTON_1, "+attack");
|
||||
BindMouseButton(GLFW_MOUSE_BUTTON_2, "+attack2");
|
||||
BindMouseButton(GLFW_MOUSE_BUTTON_3, "+attack3");
|
||||
@@ -50,7 +54,7 @@ void GameWorld::Initialize()
|
||||
CommitEntity(ground);
|
||||
}
|
||||
|
||||
/*{
|
||||
{
|
||||
auto TankTest = CreateEntity();
|
||||
auto transform = AddComponent<Components::Transform>(TankTest, "Transform");
|
||||
transform->Position = glm::vec3(1.5f, 0.7f, 5.f);
|
||||
@@ -60,7 +64,7 @@ void GameWorld::Initialize()
|
||||
CommitEntity(TankTest);
|
||||
}
|
||||
|
||||
for(int i = 0; i < 83; i++)
|
||||
/*for(int i = 0; i < 83; i++)
|
||||
{
|
||||
auto light = CreateEntity();
|
||||
auto transform = AddComponent<Components::Transform>(light, "Transform");
|
||||
@@ -122,7 +126,7 @@ void GameWorld::RegisterSystems()
|
||||
{
|
||||
m_SystemFactory.Register("TransformSystem", [this]() { return new Systems::TransformSystem(this, m_EventBroker); });
|
||||
//m_SystemFactory.Register("LevelGenerationSystem", [this]() { return new Systems::LevelGenerationSystem(this); });
|
||||
m_SystemFactory.Register("InputSystem", [this]() { return new Systems::InputSystem(this, m_EventBroker, m_Renderer); });
|
||||
m_SystemFactory.Register("InputSystem", [this]() { return new Systems::InputSystem(this, m_EventBroker); });
|
||||
m_SystemFactory.Register("DebugSystem", [this]() { return new Systems::DebugSystem(this, m_EventBroker); });
|
||||
//m_SystemFactory.Register("CollisionSystem", [this]() { return new Systems::CollisionSystem(this); });
|
||||
////m_SystemFactory.Register("ParticleSystem", [this]() { return new Systems::ParticleSystem(this); });
|
||||
|
||||
@@ -5,26 +5,29 @@
|
||||
|
||||
#include "EventBroker.h"
|
||||
#include "Events/InputCommand.h"
|
||||
#include "Events/MouseMove.h"
|
||||
|
||||
class InputController
|
||||
{
|
||||
public:
|
||||
InputController(std::shared_ptr<::EventBroker> eventBroker)
|
||||
: EventBroker(EventBroker) { Initialize(); }
|
||||
: EventBroker(eventBroker) { Initialize(); }
|
||||
|
||||
virtual void Initialize()
|
||||
{
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &InputController::OnCommand)
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &InputController::OnCommand);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EMouseMove, &InputController::OnMouseMove);
|
||||
}
|
||||
|
||||
virtual bool OnCommand(const Events::InputCommand &event) { }
|
||||
virtual bool OnCommand(const Events::InputCommand &event) { return false; }
|
||||
virtual bool OnMouseMove(const Events::MouseMove &event) { return false; }
|
||||
|
||||
protected:
|
||||
std::shared_ptr<::EventBroker> EventBroker;
|
||||
|
||||
private:
|
||||
EventRelay<Events::InputCommand> m_EInputCommand;
|
||||
|
||||
EventRelay<Events::MouseMove> m_EMouseMove;
|
||||
};
|
||||
|
||||
#endif // InputController_h__
|
||||
|
||||
@@ -7,65 +7,127 @@ void Systems::FreeSteeringSystem::RegisterComponents(ComponentFactory* cf)
|
||||
cf->Register("FreeSteering", []() { return new Components::FreeSteering(); });
|
||||
}
|
||||
|
||||
void Systems::FreeSteeringSystem::Initialize()
|
||||
{
|
||||
m_InputController = std::unique_ptr<FreeSteeringInputController>(new FreeSteeringInputController(EventBroker));
|
||||
}
|
||||
|
||||
void Systems::FreeSteeringSystem::Update(double dt)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
void Systems::FreeSteeringSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
|
||||
{
|
||||
auto steering = m_World->GetComponent<Components::FreeSteering>(entity, "FreeSteering");
|
||||
auto input = m_World->GetComponent<Components::Input>(entity, "Input");
|
||||
if (steering && input)
|
||||
if (steering)
|
||||
{
|
||||
auto transform = m_World->GetComponent<Components::Transform>(entity, "Transform");
|
||||
|
||||
glm::vec3 Camera_Right = glm::vec3(glm::vec4(1, 0, 0, 0) * transform->Orientation);
|
||||
glm::vec3 Camera_Forward = glm::vec3(glm::vec4(0, 0, 1, 0) * transform->Orientation);
|
||||
|
||||
float speed = steering->Speed;
|
||||
if (input->KeyState[GLFW_KEY_LEFT_SHIFT])
|
||||
{
|
||||
speed *= 4.0f;
|
||||
}
|
||||
if (input->KeyState[GLFW_KEY_LEFT_ALT])
|
||||
{
|
||||
speed /= 4.0f;
|
||||
}
|
||||
if (input->KeyState[GLFW_KEY_A])
|
||||
{
|
||||
transform->Position -= Camera_Right * (float)dt * speed;
|
||||
}
|
||||
else if (input->KeyState[GLFW_KEY_D])
|
||||
{
|
||||
transform->Position += Camera_Right * (float)dt * speed;
|
||||
}
|
||||
if (input->KeyState[GLFW_KEY_W])
|
||||
{
|
||||
transform->Position -= Camera_Forward * (float)dt * speed;
|
||||
}
|
||||
if (input->KeyState[GLFW_KEY_S])
|
||||
{
|
||||
transform->Position += Camera_Forward * (float)dt * speed;
|
||||
}
|
||||
if (input->KeyState[GLFW_KEY_SPACE])
|
||||
{
|
||||
transform->Position += glm::vec3(0, 1, 0) * (float)dt * speed;
|
||||
}
|
||||
if (input->KeyState[GLFW_KEY_LEFT_CONTROL])
|
||||
{
|
||||
transform->Position -= glm::vec3(0, 1, 0) * (float)dt * speed;
|
||||
}
|
||||
|
||||
if (input->MouseState[GLFW_MOUSE_BUTTON_LEFT])
|
||||
{
|
||||
// TOUCHING THIS CODE MIGHT CAUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS // spelling tobias :3
|
||||
//---------------------------------------------------------------------
|
||||
transform->Orientation = glm::angleAxis<float>(input->dY / 300.f, glm::vec3(1, 0, 0)) * transform->Orientation;
|
||||
|
||||
transform->Orientation = transform->Orientation * glm::angleAxis<float>(input->dX / 300.f, glm::vec3(0, 1, 0));
|
||||
//---------------------------------------------------------------------
|
||||
// TOUCHING THIS CODE MIGHT CAUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS
|
||||
}
|
||||
glm::vec3 cameraRight = glm::vec3(glm::vec4(1, 0, 0, 0) * m_InputController->Orientation);
|
||||
glm::vec3 cameraForward = glm::vec3(glm::vec4(0, 0, -1, 0) * m_InputController->Orientation);
|
||||
glm::vec3 movement;
|
||||
movement += cameraRight * m_InputController->Movement.x;
|
||||
movement.y += m_InputController->Movement.y;
|
||||
movement += cameraForward * -m_InputController->Movement.z;
|
||||
transform->Position += movement * steering->Speed * m_InputController->SpeedMultiplier * (float)dt;
|
||||
transform->Orientation = m_InputController->Orientation;
|
||||
}
|
||||
}
|
||||
|
||||
bool Systems::FreeSteeringSystem::FreeSteeringInputController::OnCommand(const Events::InputCommand &event)
|
||||
{
|
||||
// Movement
|
||||
if (event.Command == "+forward")
|
||||
{
|
||||
Movement.z += -1.f;
|
||||
}
|
||||
else if (event.Command == "-forward")
|
||||
{
|
||||
Movement.z -= -1.f;
|
||||
}
|
||||
else if (event.Command == "+backward")
|
||||
{
|
||||
Movement.z += 1.f;
|
||||
}
|
||||
else if (event.Command == "-backward")
|
||||
{
|
||||
Movement.z -= 1.f;
|
||||
}
|
||||
else if (event.Command == "+right")
|
||||
{
|
||||
Movement.x += 1.f;
|
||||
}
|
||||
else if (event.Command == "-right")
|
||||
{
|
||||
Movement.x -= 1.f;
|
||||
}
|
||||
else if (event.Command == "+left")
|
||||
{
|
||||
Movement.x += -1.f;
|
||||
}
|
||||
else if (event.Command == "-left")
|
||||
{
|
||||
Movement.x -= -1.f;
|
||||
}
|
||||
else if (event.Command == "+up")
|
||||
{
|
||||
Movement.y += 1.f;
|
||||
}
|
||||
else if (event.Command == "-up")
|
||||
{
|
||||
Movement.y -= 1.f;
|
||||
}
|
||||
else if (event.Command == "+down")
|
||||
{
|
||||
Movement.y += -1.f;
|
||||
}
|
||||
else if (event.Command == "-down")
|
||||
{
|
||||
Movement.y -= -1.f;
|
||||
}
|
||||
|
||||
// Speed
|
||||
else if (event.Command == "+fast")
|
||||
{
|
||||
SpeedMultiplier *= 4.f;
|
||||
}
|
||||
else if (event.Command == "-fast")
|
||||
{
|
||||
SpeedMultiplier /= 4.f;
|
||||
}
|
||||
else if (event.Command == "+slow")
|
||||
{
|
||||
SpeedMultiplier /= 4.f;
|
||||
}
|
||||
else if (event.Command == "-slow")
|
||||
{
|
||||
SpeedMultiplier *= 4.f;
|
||||
}
|
||||
|
||||
// Mouse click
|
||||
else if (event.Command == "+attack")
|
||||
{
|
||||
OrientationActive = true;
|
||||
}
|
||||
else if (event.Command == "-attack")
|
||||
{
|
||||
OrientationActive = false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Systems::FreeSteeringSystem::FreeSteeringInputController::OnMouseMove(const Events::MouseMove &event)
|
||||
{
|
||||
if (OrientationActive)
|
||||
{
|
||||
// TOUCHING THIS CODE MIGHT CAUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS
|
||||
//---------------------------------------------------------------------
|
||||
Orientation = glm::angleAxis<float>(event.DeltaY / 300.f, glm::vec3(1, 0, 0)) * Orientation * glm::angleAxis<float>(event.DeltaX / 300.f, glm::vec3(0, 1, 0));
|
||||
//---------------------------------------------------------------------
|
||||
// TOUCHING THIS CODE MIGHT CAUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -2,11 +2,12 @@
|
||||
|
||||
#include "System.h"
|
||||
#include "Components/Transform.h"
|
||||
#include "Components/Input.h"
|
||||
#include "Components/FreeSteering.h"
|
||||
#include "InputController.h"
|
||||
|
||||
namespace Systems
|
||||
{
|
||||
|
||||
class FreeSteeringSystem : public System
|
||||
{
|
||||
public:
|
||||
@@ -14,8 +15,33 @@ public:
|
||||
: System(world, eventBroker) { }
|
||||
|
||||
void RegisterComponents(ComponentFactory* cf) override;
|
||||
void Initialize() override;
|
||||
|
||||
void Update(double dt) override;
|
||||
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
|
||||
|
||||
private:
|
||||
class FreeSteeringInputController;
|
||||
|
||||
std::unique_ptr<FreeSteeringInputController> m_InputController;
|
||||
};
|
||||
|
||||
class FreeSteeringSystem::FreeSteeringInputController : InputController
|
||||
{
|
||||
public:
|
||||
FreeSteeringInputController(std::shared_ptr<::EventBroker> eventBroker)
|
||||
: InputController(eventBroker)
|
||||
, SpeedMultiplier(1.f)
|
||||
, OrientationActive(false) { }
|
||||
|
||||
glm::vec3 Movement;
|
||||
glm::quat Orientation;
|
||||
float SpeedMultiplier;
|
||||
bool OrientationActive;
|
||||
|
||||
protected:
|
||||
virtual bool OnCommand(const Events::InputCommand &event);
|
||||
virtual bool OnMouseMove(const Events::MouseMove &event);
|
||||
};
|
||||
|
||||
}
|
||||
@@ -5,7 +5,6 @@
|
||||
#include <unordered_map>
|
||||
|
||||
#include "System.h"
|
||||
#include "Renderer.h"
|
||||
#include "Components/Input.h"
|
||||
#include "Events/KeyUp.h"
|
||||
#include "Events/KeyDown.h"
|
||||
@@ -21,9 +20,8 @@ namespace Systems
|
||||
class InputSystem : public System
|
||||
{
|
||||
public:
|
||||
InputSystem(World* world, std::shared_ptr<::EventBroker> eventBroker, std::shared_ptr<Renderer> renderer)
|
||||
: System(world, eventBroker)
|
||||
, m_Renderer(renderer) { }
|
||||
InputSystem(World* world, std::shared_ptr<::EventBroker> eventBroker)
|
||||
: System(world, eventBroker) { }
|
||||
|
||||
void RegisterComponents(ComponentFactory* cf) override;
|
||||
void Initialize() override;
|
||||
@@ -31,8 +29,6 @@ public:
|
||||
void Update(double dt) override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<Renderer> m_Renderer;
|
||||
|
||||
// Input binding tables
|
||||
std::unordered_map<int, std::string> m_KeyBindings; // GLFW_KEY... -> command string
|
||||
std::unordered_map<int, std::string> m_MouseButtonBindings; // GLFW_MOUSE_BUTTON... -> command string
|
||||
|
||||
Reference in New Issue
Block a user