Tank sterring event-based

This commit is contained in:
ViktorLjung
2014-05-12 14:04:55 +02:00
parent c8a77d5c3d
commit 2aa51c3e90
13 changed files with 244 additions and 88 deletions
+14
View File
@@ -0,0 +1,14 @@
#ifndef TankSteering_h__
#define TankSteering_h__
#include "Component.h"
namespace Components
{
struct TankSteering : Component
{
};
}
#endif // TankSteering_h__
+19
View File
@@ -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__
+15 -6
View File
@@ -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<Components::Vehicle>(tank, "Vehicle");
vehicle->MaxTorque = 5200.f;
AddComponent<Components::TankSteering>(tank, "TankSteering");
AddComponent<Components::Input>(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");
+1
View File
@@ -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"
+8 -8
View File
@@ -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;
}
+6 -5
View File
@@ -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);
+1 -1
View File
@@ -48,7 +48,7 @@ private:
EventRelay<Events::BindMouseButton> 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);
};
}
+21 -67
View File
@@ -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<Components::Vehicle>(entity, "Vehicle");
auto inputComponent = m_World->GetComponent<Components::Input>(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<Components::Vehicle>(event.Entity, "Vehicle");
auto inputComponent = m_World->GetComponent<Components::Input>(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;
}
+5
View File
@@ -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<Events::TankSteer> m_ETankSteer;
bool OnTankSteer(const Events::TankSteer &event);
void SetUpPhysicsState(EntityID entity, EntityID parent);
void TearDownPhysicsState(EntityID entity, EntityID parent);
+87
View File
@@ -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<TankSteeringInputController>(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<Components::TankSteering>(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<float>(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;
}
+45
View File
@@ -0,0 +1,45 @@
#include <array>
#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<TankSteeringInputController> 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);
};
}
+4
View File
@@ -115,6 +115,7 @@
<ClCompile Include="..\..\src\Systems\PhysicsSystem.cpp" />
<ClCompile Include="..\..\src\Systems\RenderSystem.cpp" />
<ClCompile Include="..\..\src\Systems\SoundSystem.cpp" />
<ClCompile Include="..\..\src\Systems\TankSteeringSystem.cpp" />
<ClCompile Include="..\..\src\Systems\TransformSystem.cpp" />
<ClCompile Include="..\..\src\Texture.cpp" />
<ClCompile Include="..\..\src\World.cpp" />
@@ -138,6 +139,7 @@
<ClInclude Include="..\..\src\Components\SoundEmitter.h" />
<ClInclude Include="..\..\src\Components\SphereShape.h" />
<ClInclude Include="..\..\src\Components\Sprite.h" />
<ClInclude Include="..\..\src\Components\TankSteering.h" />
<ClInclude Include="..\..\src\Components\Template.h" />
<ClInclude Include="..\..\src\Components\Transform.h" />
<ClInclude Include="..\..\src\Components\Vehicle.h" />
@@ -155,6 +157,7 @@
<ClInclude Include="..\..\src\Events\MousePress.h" />
<ClInclude Include="..\..\src\Events\MouseRelease.h" />
<ClInclude Include="..\..\src\Events\PlaySound.h" />
<ClInclude Include="..\..\src\Events\TankSteer.h" />
<ClInclude Include="..\..\src\Factory.h" />
<ClInclude Include="..\..\src\GameWorld.h" />
<ClInclude Include="..\..\src\GUI\Frame.h" />
@@ -179,6 +182,7 @@
<ClInclude Include="..\..\src\Systems\PhysicsSystem.h" />
<ClInclude Include="..\..\src\Systems\RenderSystem.h" />
<ClInclude Include="..\..\src\Systems\SoundSystem.h" />
<ClInclude Include="..\..\src\Systems\TankSteeringSystem.h" />
<ClInclude Include="..\..\src\Systems\TransformSystem.h" />
<ClInclude Include="..\..\src\Texture.h" />
<ClInclude Include="..\..\src\Util\GLError.h" />
+18 -1
View File
@@ -57,6 +57,9 @@
<ClCompile Include="..\..\src\InputManager.cpp">
<Filter>Input</Filter>
</ClCompile>
<ClCompile Include="..\..\src\Systems\TankSteeringSystem.cpp">
<Filter>Physics\Systems</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Filter Include="Util">
@@ -128,6 +131,9 @@
<Filter Include="Input\Events">
<UniqueIdentifier>{ee125b77-b275-4841-abc9-374957a89916}</UniqueIdentifier>
</Filter>
<Filter Include="Physics\Events">
<UniqueIdentifier>{42ae084f-ade8-402c-87ba-f03f6b846bc8}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\World.h" />
@@ -228,7 +234,6 @@
<ClInclude Include="..\..\src\Sound.h">
<Filter>Audio</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Physics\VehicleSetup.h" />
<ClInclude Include="..\..\src\Components\Vehicle.h">
<Filter>Physics\Components</Filter>
</ClInclude>
@@ -305,6 +310,18 @@
<ClInclude Include="..\..\src\GUI\Viewport.h">
<Filter>GUI</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Physics\VehicleSetup.h">
<Filter>Physics</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Components\TankSteering.h">
<Filter>Physics\Components</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Events\TankSteer.h">
<Filter>Physics\Events</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Systems\TankSteeringSystem.h">
<Filter>Physics\Systems</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\src\Shaders\AABB.frag.glsl">