From 2aa51c3e90a7dcdeeb7071df5d50307050a7dd47 Mon Sep 17 00:00:00 2001 From: ViktorLjung Date: Mon, 12 May 2014 14:04:55 +0200 Subject: [PATCH] Tank sterring event-based --- src/Components/TankSteering.h | 14 +++ src/Events/TankSteer.h | 19 ++++ src/GameWorld.cpp | 21 +++-- src/GameWorld.h | 1 + src/Systems/FreeSteeringSystem.cpp | 16 ++-- src/Systems/InputSystem.cpp | 11 +-- src/Systems/InputSystem.h | 2 +- src/Systems/PhysicsSystem.cpp | 88 +++++-------------- src/Systems/PhysicsSystem.h | 5 ++ src/Systems/TankSteeringSystem.cpp | 87 ++++++++++++++++++ src/Systems/TankSteeringSystem.h | 45 ++++++++++ vs11/Returngeance/Returngeance.vcxproj | 4 + .../Returngeance/Returngeance.vcxproj.filters | 19 +++- 13 files changed, 244 insertions(+), 88 deletions(-) create mode 100644 src/Components/TankSteering.h create mode 100644 src/Events/TankSteer.h create mode 100644 src/Systems/TankSteeringSystem.cpp create mode 100644 src/Systems/TankSteeringSystem.h diff --git a/src/Components/TankSteering.h b/src/Components/TankSteering.h new file mode 100644 index 0000000..963d81c --- /dev/null +++ b/src/Components/TankSteering.h @@ -0,0 +1,14 @@ +#ifndef TankSteering_h__ +#define TankSteering_h__ + +#include "Component.h" + +namespace Components +{ + struct TankSteering : Component + { + + }; +} + +#endif // TankSteering_h__ \ No newline at end of file diff --git a/src/Events/TankSteer.h b/src/Events/TankSteer.h new file mode 100644 index 0000000..3ee6dc9 --- /dev/null +++ b/src/Events/TankSteer.h @@ -0,0 +1,19 @@ +#ifndef Events_TankSteer_h__ +#define Events_TankSteer_h__ +#include "Entity.h" +#include "EventBroker.h" + +namespace Events +{ + +struct TankSteer : Event +{ + EntityID Entity; + float PositionX; + float PositionY; + bool Handbrake; +}; + +} + +#endif // Events_TankSteer_h__ \ No newline at end of file diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 4744912..979376b 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -12,7 +12,9 @@ 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_SPACE, "+handbrake"); + + BindKey(GLFW_KEY_Q, "+up"); BindKey(GLFW_KEY_LEFT_CONTROL, "+down"); BindKey(GLFW_KEY_LEFT_ALT, "+slow"); BindKey(GLFW_KEY_LEFT_SHIFT, "+fast"); @@ -20,6 +22,12 @@ void GameWorld::Initialize() BindMouseButton(GLFW_MOUSE_BUTTON_2, "+attack2"); BindMouseButton(GLFW_MOUSE_BUTTON_3, "+attack3"); + + BindKey(GLFW_KEY_UP, "+cam_forward"); + BindKey(GLFW_KEY_DOWN, "+cam_backward"); + BindKey(GLFW_KEY_LEFT, "+cam_right"); + BindKey(GLFW_KEY_RIGHT, "+cam_left"); + RegisterComponents(); { @@ -214,7 +222,7 @@ void GameWorld::Initialize() physics->Static = false; auto vehicle = AddComponent(tank, "Vehicle"); vehicle->MaxTorque = 5200.f; - + AddComponent(tank, "TankSteering"); AddComponent(tank, "Input"); { @@ -542,11 +550,11 @@ void GameWorld::RegisterSystems() //m_SystemFactory.Register("CollisionSystem", [this]() { return new Systems::CollisionSystem(this); }); ////m_SystemFactory.Register("ParticleSystem", [this]() { return new Systems::ParticleSystem(this); }); //m_SystemFactory.Register("PlayerSystem", [this]() { return new Systems::PlayerSystem(this); }); - m_SystemFactory.Register("FreeSteeringSystem", [this]() { return new Systems::FreeSteeringSystem(this); }); - m_SystemFactory.Register("SoundSystem", [this]() { return new Systems::SoundSystem(this); }); - m_SystemFactory.Register("PhysicsSystem", [this]() { return new Systems::PhysicsSystem(this); }); - m_SystemFactory.Register("RenderSystem", [this]() { return new Systems::RenderSystem(this, m_Renderer); }); m_SystemFactory.Register("FreeSteeringSystem", [this]() { return new Systems::FreeSteeringSystem(this, m_EventBroker); }); + m_SystemFactory.Register("TankSteeringSystem", [this]() { return new Systems::TankSteeringSystem(this, m_EventBroker); }); + m_SystemFactory.Register("SoundSystem", [this]() { return new Systems::SoundSystem(this, m_EventBroker); }); + m_SystemFactory.Register("PhysicsSystem", [this]() { return new Systems::PhysicsSystem(this, m_EventBroker); }); + m_SystemFactory.Register("RenderSystem", [this]() { return new Systems::RenderSystem(this, m_EventBroker, m_Renderer); }); } void GameWorld::AddSystems() @@ -559,6 +567,7 @@ void GameWorld::AddSystems() ////AddSystem("ParticleSystem"); //AddSystem("PlayerSystem"); AddSystem("FreeSteeringSystem"); + AddSystem("TankSteeringSystem"); AddSystem("SoundSystem"); AddSystem("PhysicsSystem"); AddSystem("RenderSystem"); diff --git a/src/GameWorld.h b/src/GameWorld.h index 6c03eb7..6e9c9e4 100755 --- a/src/GameWorld.h +++ b/src/GameWorld.h @@ -12,6 +12,7 @@ //#include "Systems/ParticleSystem.h" //#include "Systems/PlayerSystem.h" #include "Systems/FreeSteeringSystem.h" +#include "Systems/TankSteeringSystem.h" #include "Systems/RenderSystem.h" #include "Systems/SoundSystem.h" #include "Systems/PhysicsSystem.h" diff --git a/src/Systems/FreeSteeringSystem.cpp b/src/Systems/FreeSteeringSystem.cpp index b382b23..8373c80 100755 --- a/src/Systems/FreeSteeringSystem.cpp +++ b/src/Systems/FreeSteeringSystem.cpp @@ -38,35 +38,35 @@ void Systems::FreeSteeringSystem::UpdateEntity(double dt, EntityID entity, Entit bool Systems::FreeSteeringSystem::FreeSteeringInputController::OnCommand(const Events::InputCommand &event) { // Movement - if (event.Command == "+forward") + if (event.Command == "+cam_forward") { Movement.z += -1.f; } - else if (event.Command == "-forward") + else if (event.Command == "-cam_forward") { Movement.z -= -1.f; } - else if (event.Command == "+backward") + else if (event.Command == "+cam_backward") { Movement.z += 1.f; } - else if (event.Command == "-backward") + else if (event.Command == "-cam_backward") { Movement.z -= 1.f; } - else if (event.Command == "+right") + else if (event.Command == "+cam_right") { Movement.x += 1.f; } - else if (event.Command == "-right") + else if (event.Command == "-cam_right") { Movement.x -= 1.f; } - else if (event.Command == "+left") + else if (event.Command == "+cam_left") { Movement.x += -1.f; } - else if (event.Command == "-left") + else if (event.Command == "-cam_left") { Movement.x -= -1.f; } diff --git a/src/Systems/InputSystem.cpp b/src/Systems/InputSystem.cpp index 3c9fdfd..18a21aa 100755 --- a/src/Systems/InputSystem.cpp +++ b/src/Systems/InputSystem.cpp @@ -44,7 +44,7 @@ bool Systems::InputSystem::OnKeyDown(const Events::KeyDown &event) auto bindingIt = m_KeyBindings.find(event.KeyCode); if (bindingIt != m_KeyBindings.end()) { - PublishCommand(0, bindingIt->second, false); + PublishCommand(0, bindingIt->second, 1.f, false); } return true; @@ -55,7 +55,7 @@ bool Systems::InputSystem::OnKeyUp(const Events::KeyUp &event) auto bindingIt = m_KeyBindings.find(event.KeyCode); if (bindingIt != m_KeyBindings.end()) { - PublishCommand(0, bindingIt->second, true); + PublishCommand(0, bindingIt->second, 1.f, true); } return true; @@ -66,7 +66,7 @@ bool Systems::InputSystem::OnMousePress(const Events::MousePress &event) auto bindingIt = m_MouseButtonBindings.find(event.Button); if (bindingIt != m_MouseButtonBindings.end()) { - PublishCommand(0, bindingIt->second, false); + PublishCommand(0, bindingIt->second, 1.f, false); } return true; @@ -77,7 +77,7 @@ bool Systems::InputSystem::OnMouseRelease(const Events::MouseRelease &event) auto bindingIt = m_MouseButtonBindings.find(event.Button); if (bindingIt != m_MouseButtonBindings.end()) { - PublishCommand(0, bindingIt->second, true); + PublishCommand(0, bindingIt->second, 1.f, true); } return true; @@ -113,7 +113,7 @@ bool Systems::InputSystem::OnBindMouseButton(const Events::BindMouseButton &even return true; } -void Systems::InputSystem::PublishCommand(int playerID, std::string command, bool release /*= false*/) +void Systems::InputSystem::PublishCommand(int playerID, std::string command, float value, bool release /*= false*/) { if (release && command.at(0) == '+') { @@ -123,6 +123,7 @@ void Systems::InputSystem::PublishCommand(int playerID, std::string command, boo Events::InputCommand e; e.PlayerID = playerID; e.Command = command; + e.Value = value; EventBroker->Publish(e); LOG_DEBUG("Input: Published command %s for player %i", e.Command.c_str(), playerID); diff --git a/src/Systems/InputSystem.h b/src/Systems/InputSystem.h index c56cccf..f912de0 100755 --- a/src/Systems/InputSystem.h +++ b/src/Systems/InputSystem.h @@ -48,7 +48,7 @@ private: EventRelay m_EBindMouseButton; bool OnBindMouseButton(const Events::BindMouseButton &event); - void PublishCommand(int playerID, std::string command, bool release = false); + void PublishCommand(int playerID, std::string command, float value, bool release = false); }; } diff --git a/src/Systems/PhysicsSystem.cpp b/src/Systems/PhysicsSystem.cpp index 2000a3c..5fad34c 100644 --- a/src/Systems/PhysicsSystem.cpp +++ b/src/Systems/PhysicsSystem.cpp @@ -28,6 +28,9 @@ void Systems::PhysicsSystem::Initialize() { m_Accumulator = 0; + + // Events + EVENT_SUBSCRIBE_MEMBER(m_ETankSteer, &Systems::PhysicsSystem::OnTankSteer); hkMemorySystem::FrameInfo finfo(6000 * 1024); // Allocate 6MB of Physics solver buffer hkMemoryRouter* memoryRouter = hkMemoryInitUtil::initDefault(hkMallocAllocator::m_defaultMallocAllocator, finfo); @@ -219,73 +222,7 @@ void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID p } } - // HACK: Vehicle test-controls - auto vehicleComponent = m_World->GetComponent(entity, "Vehicle"); - auto inputComponent = m_World->GetComponent(entity, "Input"); - if (vehicleComponent && inputComponent && m_Vehicles.find(entity) != m_Vehicles.end() && m_RigidBodies.find(entity) != m_RigidBodies.end()) - { - m_PhysicsWorld->markForWrite(); - hkpVehicleDriverInputAnalogStatus* deviceStatus = (hkpVehicleDriverInputAnalogStatus*)m_Vehicles[entity]->m_deviceStatus; - - if(inputComponent->KeyState[GLFW_KEY_UP] != 0) - { - deviceStatus->m_positionY += inputComponent->KeyState[GLFW_KEY_UP] * -1 * 1.f * dt; - } - else if (inputComponent->KeyState[GLFW_KEY_DOWN] != 0) - { - deviceStatus->m_positionY += inputComponent->KeyState[GLFW_KEY_DOWN] * 1 * 1.f * dt; - deviceStatus->m_reverseButtonPressed = true; - } - else - { - deviceStatus->m_positionY = 0; - } - - if(deviceStatus->m_positionY > 1) - deviceStatus->m_positionY = 1; - else if(deviceStatus->m_positionY < -1) - deviceStatus->m_positionY = -1; - - float turningSpeed = 3.f; - - if(inputComponent->KeyState[GLFW_KEY_LEFT] != 0 || inputComponent->KeyState[GLFW_KEY_RIGHT] != 0) - { - deviceStatus->m_positionX += inputComponent->KeyState[GLFW_KEY_LEFT] * -1 * turningSpeed * dt + inputComponent->KeyState[GLFW_KEY_RIGHT] * 1 * turningSpeed * dt; - } - else - { - if(deviceStatus->m_positionX > 0) - { - deviceStatus->m_positionX += -1 * (turningSpeed*2) *dt; - } - else if(deviceStatus->m_positionX < 0) - { - deviceStatus->m_positionX += 1 * (turningSpeed*2) * dt; - } - - if (deviceStatus->m_positionX > -0.1 && deviceStatus->m_positionX < 0.1) - { - deviceStatus->m_positionX = 0.f; - } - } - - if(deviceStatus->m_positionX > 1) - deviceStatus->m_positionX = 1; - else if(deviceStatus->m_positionX < -1) - deviceStatus->m_positionX = -1; - - deviceStatus->m_handbrakeButtonPressed = inputComponent->KeyState[GLFW_KEY_RIGHT_CONTROL]; - - if(inputComponent->KeyState[GLFW_KEY_R] && !inputComponent->LastKeyState[GLFW_KEY_R]) - { - transformComponent->Position = transformComponent->Position + glm::vec3(0, 5, 0); - transformComponent->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); - m_RigidBodies[entity]->setLinearVelocity(hkVector4(0, 0, 0)); - m_RigidBodies[entity]->setAngularVelocity(hkVector4(0, 0, 0)); - } - - m_PhysicsWorld->unmarkForWrite(); - } + } void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) @@ -626,3 +563,20 @@ const hkVector4& Systems::PhysicsSystem::ConvertScale(glm::vec3 glmScale) return hkVector4(glmScale.x, glmScale.y, glmScale.z); } +bool Systems::PhysicsSystem::OnTankSteer(const Events::TankSteer &event) +{ + auto vehicleComponent = m_World->GetComponent(event.Entity, "Vehicle"); + auto inputComponent = m_World->GetComponent(event.Entity, "Input"); + if (vehicleComponent && inputComponent && m_Vehicles.find(event.Entity) != m_Vehicles.end() && m_RigidBodies.find(event.Entity) != m_RigidBodies.end()) + { + m_PhysicsWorld->markForWrite(); + hkpVehicleDriverInputAnalogStatus* deviceStatus = (hkpVehicleDriverInputAnalogStatus*)m_Vehicles[event.Entity]->m_deviceStatus; + deviceStatus->m_positionX = event.PositionX; + deviceStatus->m_positionY = event.PositionY; + deviceStatus->m_handbrakeButtonPressed = event.Handbrake; + m_PhysicsWorld->unmarkForWrite(); + } + + return true; +} + diff --git a/src/Systems/PhysicsSystem.h b/src/Systems/PhysicsSystem.h index 7592a7b..b8fc262 100644 --- a/src/Systems/PhysicsSystem.h +++ b/src/Systems/PhysicsSystem.h @@ -12,6 +12,7 @@ #include "Components/MeshShape.h" #include "Components/HingeConstraint.h" #include "Components/WheelPair.h" +#include "Events/TankSteer.h" #include "OBJ.h" // Math and base include @@ -79,6 +80,10 @@ private: double m_Accumulator; hkpWorld* m_PhysicsWorld; + // Events + EventRelay m_ETankSteer; + bool OnTankSteer(const Events::TankSteer &event); + void SetUpPhysicsState(EntityID entity, EntityID parent); void TearDownPhysicsState(EntityID entity, EntityID parent); diff --git a/src/Systems/TankSteeringSystem.cpp b/src/Systems/TankSteeringSystem.cpp new file mode 100644 index 0000000..f0a4e30 --- /dev/null +++ b/src/Systems/TankSteeringSystem.cpp @@ -0,0 +1,87 @@ +#include "PrecompiledHeader.h" +#include "TankSteeringSystem.h" +#include "World.h" + +void Systems::TankSteeringSystem::RegisterComponents( ComponentFactory* cf ) +{ + cf->Register("TankSteering", []() { return new Components::TankSteering(); }); +} + +void Systems::TankSteeringSystem::Initialize() +{ + m_InputController = std::unique_ptr(new TankSteeringInputController(EventBroker)); + m_InputController->PositionX = 0; + m_InputController->PositionY = 0; + m_InputController->Handbrake = false; +} + +void Systems::TankSteeringSystem::Update(double dt) +{ + +} + +void Systems::TankSteeringSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) +{ + auto tankSteeringComponent = m_World->GetComponent(entity, "TankSteering"); + if(tankSteeringComponent) + { + Events::TankSteer e; + e.Entity = entity; + e.PositionX = m_InputController->PositionX; + e.PositionY = m_InputController->PositionY; + e.Handbrake = m_InputController->Handbrake; + EventBroker->Publish(e); + } +} + +bool Systems::TankSteeringSystem::TankSteeringInputController::OnCommand(const Events::InputCommand &event) +{ + float val = boost::any_cast(event.Value); + if (event.Command == "+right") + { + PositionX += val; + } + else if (event.Command == "-right") + { + PositionX -= val; + } + else if (event.Command == "+left") + { + PositionX += -val; + } + else if (event.Command == "-left") + { + PositionX -= -val; + } + else if (event.Command == "+forward") + { + PositionY += -val; + } + else if (event.Command == "-forward") + { + PositionY -= -val; + } + else if (event.Command == "+backward") + { + PositionY += val; + } + else if (event.Command == "-backward") + { + PositionY -= val; + } + + else if (event.Command == "+handbrake") + { + Handbrake = true; + } + else if (event.Command == "-handbrake") + { + Handbrake = false; + } + return true; +} + +bool Systems::TankSteeringSystem::TankSteeringInputController::OnMouseMove( const Events::MouseMove &event ) +{ + return false; +} diff --git a/src/Systems/TankSteeringSystem.h b/src/Systems/TankSteeringSystem.h new file mode 100644 index 0000000..5c7a02e --- /dev/null +++ b/src/Systems/TankSteeringSystem.h @@ -0,0 +1,45 @@ +#include + +#include "System.h" +#include "Events/TankSteer.h" +#include "Components/Transform.h" +#include "Components/TankSteering.h" +#include "Components/Vehicle.h" +#include "InputController.h" + +namespace Systems +{ + + class TankSteeringSystem : public System + { + public: + TankSteeringSystem(World* world, std::shared_ptr<::EventBroker> eventBroker) + : 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 TankSteeringInputController; + std::unique_ptr m_InputController; + }; + + class TankSteeringSystem::TankSteeringInputController : InputController + { + public: + TankSteeringInputController(std::shared_ptr<::EventBroker> eventBroker) + : InputController(eventBroker) { } + + float PositionY; + float PositionX; + bool Handbrake; + + protected: + virtual bool OnCommand(const Events::InputCommand &event); + virtual bool OnMouseMove(const Events::MouseMove &event); + }; + +} \ No newline at end of file diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index ac4c36b..47fff50 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -115,6 +115,7 @@ + @@ -138,6 +139,7 @@ + @@ -155,6 +157,7 @@ + @@ -179,6 +182,7 @@ + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index 169f797..8b1f1a3 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -57,6 +57,9 @@ Input + + Physics\Systems + @@ -128,6 +131,9 @@ {ee125b77-b275-4841-abc9-374957a89916} + + {42ae084f-ade8-402c-87ba-f03f6b846bc8} + @@ -228,7 +234,6 @@ Audio - Physics\Components @@ -305,6 +310,18 @@ GUI + + Physics + + + Physics\Components + + + Physics\Events + + + Physics\Systems +