Tank tower and barrel steering working

This commit is contained in:
ViktorLjung
2014-05-12 22:33:53 +02:00
parent 92e9fd1049
commit 38482431a1
17 changed files with 246 additions and 96 deletions
+15 -12
View File
@@ -44,7 +44,11 @@ bool Systems::InputSystem::OnKeyDown(const Events::KeyDown &event)
auto bindingIt = m_KeyBindings.find(event.KeyCode);
if (bindingIt != m_KeyBindings.end())
{
PublishCommand(0, bindingIt->second, 1.f, false);
std::string command;
float value;
std::tie(command, value) = bindingIt->second;
m_KeyBindingValues[command] += value;
PublishCommand(0, command, std::max(-1.f, std::min(m_KeyBindingValues[command], 1.f)));
}
return true;
@@ -55,7 +59,11 @@ bool Systems::InputSystem::OnKeyUp(const Events::KeyUp &event)
auto bindingIt = m_KeyBindings.find(event.KeyCode);
if (bindingIt != m_KeyBindings.end())
{
PublishCommand(0, bindingIt->second, 1.f, true);
std::string command;
float value;
std::tie(command, value) = bindingIt->second;
m_KeyBindingValues[command] -= value;
PublishCommand(0, command, std::max(-1.f, std::min(m_KeyBindingValues[command], 1.f)));
}
return true;
@@ -66,7 +74,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, 1.f, false);
PublishCommand(0, bindingIt->second, 1.f);
}
return true;
@@ -77,7 +85,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, 1.f, true);
PublishCommand(0, bindingIt->second, 1.f);
}
return true;
@@ -91,7 +99,7 @@ bool Systems::InputSystem::OnBindKey(const Events::BindKey &event)
}
else
{
m_KeyBindings[event.KeyCode] = event.Command;
m_KeyBindings[event.KeyCode] = std::make_tuple(event.Command, event.Value);
LOG_DEBUG("Input: Bound key %c to %s", (char)event.KeyCode, event.Command.c_str());
}
@@ -113,18 +121,13 @@ bool Systems::InputSystem::OnBindMouseButton(const Events::BindMouseButton &even
return true;
}
void Systems::InputSystem::PublishCommand(int playerID, std::string command, float value, bool release /*= false*/)
void Systems::InputSystem::PublishCommand(int playerID, std::string command, float value)
{
if (release && command.at(0) == '+')
{
command[0] = '-';
}
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);
LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, playerID);
}
+4 -2
View File
@@ -3,6 +3,7 @@
#include <array>
#include <unordered_map>
#include <boost/any.hpp>
#include "System.h"
#include "Components/Input.h"
@@ -30,7 +31,8 @@ public:
private:
// Input binding tables
std::unordered_map<int, std::string> m_KeyBindings; // GLFW_KEY... -> command string
std::unordered_map<int, std::tuple<std::string, float>> m_KeyBindings; // GLFW_KEY... -> command string & value
std::unordered_map<std::string, float> m_KeyBindingValues; // command string -> command value
std::unordered_map<int, std::string> m_MouseButtonBindings; // GLFW_MOUSE_BUTTON... -> command string
// Input events
@@ -48,7 +50,7 @@ private:
EventRelay<Events::BindMouseButton> m_EBindMouseButton;
bool OnBindMouseButton(const Events::BindMouseButton &event);
void PublishCommand(int playerID, std::string command, float value, bool release = false);
void PublishCommand(int playerID, std::string command, float value);
};
}
+5 -6
View File
@@ -31,7 +31,7 @@ void Systems::PhysicsSystem::Initialize()
// 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);
hkBaseSystem::init(memoryRouter, HavokErrorReport);
@@ -115,7 +115,6 @@ void Systems::PhysicsSystem::RegisterComponents(ComponentFactory* cf)
cf->Register("MeshShape", []() { return new Components::MeshShape(); });
cf->Register("HingeConstraint", []() { return new Components::HingeConstraint(); });
cf->Register("WheelPair", []() { return new Components::WheelPair(); });
}
void Systems::PhysicsSystem::Update(double dt)
@@ -550,7 +549,9 @@ glm::quat Systems::PhysicsSystem::ConvertRotation(const hkQuaternion &hkRotation
const hkQuaternion& Systems::PhysicsSystem::ConvertRotation(glm::quat glmRotation)
{
return hkQuaternion(glmRotation.x, glmRotation.y, glmRotation.z, glmRotation.w);
hkQuaternion quat = hkQuaternion(glmRotation.x, glmRotation.y, glmRotation.z, glmRotation.w);
quat.normalize();
return quat;
}
glm::vec3 Systems::PhysicsSystem::ConvertScale(const hkVector4 &hkScale)
@@ -566,8 +567,7 @@ const hkVector4& Systems::PhysicsSystem::ConvertScale(glm::vec3 glmScale)
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())
if (vehicleComponent && 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;
@@ -579,4 +579,3 @@ bool Systems::PhysicsSystem::OnTankSteer(const Events::TankSteer &event)
return true;
}
+1
View File
@@ -12,6 +12,7 @@
#include "Components/MeshShape.h"
#include "Components/HingeConstraint.h"
#include "Components/WheelPair.h"
#include "Components/TowerSteering.h"
#include "Events/TankSteer.h"
#include "OBJ.h"
+64 -39
View File
@@ -5,19 +5,20 @@
void Systems::TankSteeringSystem::RegisterComponents( ComponentFactory* cf )
{
cf->Register("TankSteering", []() { return new Components::TankSteering(); });
cf->Register("TowerSteering", []() { return new Components::TowerSteering(); });
cf->Register("BarrelSteering", []() { return new Components::BarrelSteering(); });
}
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;
m_TankInputController = std::unique_ptr<TankSteeringInputController>(new TankSteeringInputController(EventBroker));
m_TowerInputController = std::unique_ptr<TowerSteeringInputController>(new TowerSteeringInputController(EventBroker));
}
void Systems::TankSteeringSystem::Update(double dt)
{
m_TankInputController->Update(dt);
m_TowerInputController->Update(dt);
}
void Systems::TankSteeringSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
@@ -27,61 +28,85 @@ void Systems::TankSteeringSystem::UpdateEntity(double dt, EntityID entity, Entit
{
Events::TankSteer e;
e.Entity = entity;
e.PositionX = m_InputController->PositionX;
e.PositionY = m_InputController->PositionY;
e.Handbrake = m_InputController->Handbrake;
e.PositionX = m_TankInputController->PositionX;
e.PositionY = m_TankInputController->PositionY;
e.Handbrake = m_TankInputController->Handbrake;
EventBroker->Publish(e);
}
auto towerSteeringComponent = m_World->GetComponent<Components::TowerSteering>(entity, "TowerSteering");
if(towerSteeringComponent)
{
auto transformComponent = m_World->GetComponent<Components::Transform>(entity, "Transform");
glm::quat orientation = glm::angleAxis(towerSteeringComponent->Velocity * m_TowerInputController->TowerDirection * (float)dt, towerSteeringComponent->Axis);
transformComponent->Orientation *= orientation;
}
auto barrelSteeringComponent = m_World->GetComponent<Components::BarrelSteering>(entity, "BarrelSteering");
if(barrelSteeringComponent)
{
auto transformComponent = m_World->GetComponent<Components::Transform>(entity, "Transform");
glm::quat orientation = glm::angleAxis(barrelSteeringComponent->Velocity * m_TowerInputController->BarrelDirection * (float)dt, barrelSteeringComponent->Axis);
transformComponent->Orientation *= orientation;
}
}
void Systems::TankSteeringSystem::TankSteeringInputController::Update( double dt )
{
PositionX = m_Horizontal;
PositionY = m_Vertical;
}
void Systems::TankSteeringSystem::TowerSteeringInputController::Update( double dt )
{
TowerDirection = m_TowerDirection;
BarrelDirection = m_BarrelDirection;
}
bool Systems::TankSteeringSystem::TankSteeringInputController::OnCommand(const Events::InputCommand &event)
{
float val = boost::any_cast<float>(event.Value);
if (event.Command == "+right")
float val = event.Value;
if (event.Command == "horizontal")
{
PositionX += val;
m_Horizontal = val;
}
else if (event.Command == "-right")
else if (event.Command == "vertical")
{
PositionX -= val;
m_Vertical = val;
}
else if (event.Command == "+left")
else if (event.Command == "handbrake")
{
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;
Handbrake = val;
}
else if (event.Command == "+handbrake")
return true;
}
bool Systems::TankSteeringSystem::TowerSteeringInputController::OnCommand( const Events::InputCommand &event )
{
float val = event.Value;
if(event.Command == "tower_rotation")
{
Handbrake = true;
m_TowerDirection = val;
}
else if (event.Command == "-handbrake")
else if(event.Command == "barrel_rotation")
{
Handbrake = false;
m_BarrelDirection = val;
}
return true;
}
bool Systems::TankSteeringSystem::TowerSteeringInputController::OnMouseMove( const Events::MouseMove &event )
{
return false;
}
bool Systems::TankSteeringSystem::TankSteeringInputController::OnMouseMove( const Events::MouseMove &event )
{
return false;
}
+42 -3
View File
@@ -4,6 +4,8 @@
#include "Events/TankSteer.h"
#include "Components/Transform.h"
#include "Components/TankSteering.h"
#include "Components/TowerSteering.h"
#include "Components/BarrelSteering.h"
#include "Components/Vehicle.h"
#include "InputController.h"
@@ -24,22 +26,59 @@ namespace Systems
private:
class TankSteeringInputController;
std::unique_ptr<TankSteeringInputController> m_InputController;
std::unique_ptr<TankSteeringInputController> m_TankInputController;
class TowerSteeringInputController;
std::unique_ptr<TowerSteeringInputController> m_TowerInputController;
};
class TankSteeringSystem::TankSteeringInputController : InputController
{
public:
TankSteeringInputController(std::shared_ptr<::EventBroker> eventBroker)
: InputController(eventBroker) { }
: InputController(eventBroker)
{
m_Horizontal = 0.f;
m_Vertical = 0.f;
PositionX = 0;
PositionY = 0;
Handbrake = false;
}
float PositionY;
float PositionX;
bool Handbrake;
void Update(double dt);
protected:
virtual bool OnCommand(const Events::InputCommand &event);
virtual bool OnMouseMove(const Events::MouseMove &event);
private:
float m_Horizontal;
float m_Vertical;
};
class TankSteeringSystem::TowerSteeringInputController : InputController
{
public:
TowerSteeringInputController(std::shared_ptr<::EventBroker> eventBroker)
: InputController(eventBroker)
{
m_TowerDirection = 0.f;
m_BarrelDirection = 0.f;
TowerDirection = 0.f;
BarrelDirection = 0.f;
}
float TowerDirection;
float BarrelDirection;
void Update(double dt);
protected:
virtual bool OnCommand(const Events::InputCommand &event);
virtual bool OnMouseMove(const Events::MouseMove &event);
private:
float m_TowerDirection;
float m_BarrelDirection;
};
}