diff --git a/src/Components/HelicopterSteering.h b/src/Components/HelicopterSteering.h new file mode 100644 index 0000000..630ec67 --- /dev/null +++ b/src/Components/HelicopterSteering.h @@ -0,0 +1,16 @@ +#ifndef HelicopterSteering_h__ +#define HelicopterSteering_h__ + +#include "Component.h" + +namespace Components +{ + +struct HelicopterSteering : Component +{ + HelicopterSteering* Clone() const override { return new HelicopterSteering(*this); } +}; + +} + +#endif // HelicopterSteering_h__ \ No newline at end of file diff --git a/src/Events/ApplyForce.h b/src/Events/ApplyForce.h new file mode 100644 index 0000000..2008764 --- /dev/null +++ b/src/Events/ApplyForce.h @@ -0,0 +1,18 @@ +#ifndef Events_ApplyForce_h__ +#define Events_ApplyForce_h__ +#include "Entity.h" +#include "EventBroker.h" + +namespace Events +{ + +struct ApplyForce : Event +{ + EntityID Entity; + double DeltaTime; + glm::vec3 Force; +}; + +} + +#endif // Events_ApplyForce_h__ \ No newline at end of file diff --git a/src/GameWorld.h b/src/GameWorld.h index 7235e25..952f002 100755 --- a/src/GameWorld.h +++ b/src/GameWorld.h @@ -13,6 +13,7 @@ //#include "Systems/PlayerSystem.h" #include "Systems/FreeSteeringSystem.h" #include "Systems/TankSteeringSystem.h" +#include "Systems/HelicopterSteeringSystem.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 8a5d638..efbfde1 100755 --- a/src/Systems/FreeSteeringSystem.cpp +++ b/src/Systems/FreeSteeringSystem.cpp @@ -60,27 +60,27 @@ void Systems::FreeSteeringSystem::UpdateEntity(double dt, EntityID entity, Entit bool Systems::FreeSteeringSystem::FreeSteeringInputController::OnCommand(const Events::InputCommand &event) { // Movement - if (event.Command == "vertical") + if (event.Command == "cam_vertical") { Movement.z = -event.Value; } - else if (event.Command == "horizontal") + else if (event.Command == "cam_horizontal") { Movement.x = event.Value; } - else if (event.Command == "normal") + else if (event.Command == "cam_normal") { Movement.y = event.Value; } // Speed - else if (event.Command == "speed") + else if (event.Command == "cam_speed") { SpeedMultiplier = event.Value; } // Mouse click - else if (event.Command == "attack") + else if (event.Command == "cam_attack") { OrientationActive = event.Value > 0; @@ -96,11 +96,11 @@ bool Systems::FreeSteeringSystem::FreeSteeringInputController::OnCommand(const E } } - else if (event.Command == "vertical2") + else if (event.Command == "cam_vertical2") { ControllerOrientation.x = event.Value; } - else if (event.Command == "horizontal2") + else if (event.Command == "cam_horizontal2") { ControllerOrientation.y = -event.Value; } diff --git a/src/Systems/HelicopterSteeringSystem.cpp b/src/Systems/HelicopterSteeringSystem.cpp new file mode 100644 index 0000000..7fd9661 --- /dev/null +++ b/src/Systems/HelicopterSteeringSystem.cpp @@ -0,0 +1,67 @@ +#include "PrecompiledHeader.h" +#include "HelicopterSteeringSystem.h" +#include "World.h" + +void Systems::HelicopterSteeringSystem::RegisterComponents(ComponentFactory* cf) +{ + cf->Register("HelicopterSteering", []() { return new Components::HelicopterSteering(); }); +} + +void Systems::HelicopterSteeringSystem::Initialize() +{ + m_InputController = std::unique_ptr(new HelicopterSteeringInputController(EventBroker)); +} + +void Systems::HelicopterSteeringSystem::Update(double dt) +{ + +} + +void Systems::HelicopterSteeringSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) +{ + auto transform = m_World->GetComponent(entity, "Transform"); + if (!transform) + return; + + auto helicopterComponent = m_World->GetComponent(entity, "HelicopterSteering"); + if (helicopterComponent) + { + glm::vec3 controllerRotationEuler = m_InputController->Rotation * (float)dt; + transform->Orientation *= glm::quat(controllerRotationEuler); + + Events::ApplyForce e; + e.Entity = entity; + e.DeltaTime = dt; + e.Force = glm::normalize(transform->Orientation * glm::vec3(0, 1, 0)) * (m_InputController->Power * 3000.f * 9.82f * 8.f); + EventBroker->Publish(e); + } +} + +bool Systems::HelicopterSteeringSystem::HelicopterSteeringInputController::OnCommand(const Events::InputCommand &event) +{ + if (event.Command == "horizontal") + { + Rotation.z = -event.Value; + } + else if (event.Command == "vertical") + { + Rotation.x = -event.Value; + } + + else if (event.Command == "normal") + { + Power = event.Value; + } + + return true; +} + +bool Systems::HelicopterSteeringSystem::HelicopterSteeringInputController::OnMouseMove(const Events::MouseMove &event) +{ + return true; +} + +void Systems::HelicopterSteeringSystem::HelicopterSteeringInputController::Update(double dt) +{ + +} diff --git a/src/Systems/HelicopterSteeringSystem.h b/src/Systems/HelicopterSteeringSystem.h new file mode 100644 index 0000000..ed91096 --- /dev/null +++ b/src/Systems/HelicopterSteeringSystem.h @@ -0,0 +1,54 @@ +#include "System.h" +#include "Components/Transform.h" +#include "Components/HelicopterSteering.h" +#include "Events/SetVelocity.h" +#include "Events/ApplyForce.h" +#include "InputController.h" + +namespace Systems +{ + +class HelicopterSteeringSystem : public System +{ +public: + HelicopterSteeringSystem(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 HelicopterSteeringInputController; + std::unique_ptr m_InputController; + + std::map m_TimeSinceLastShot; +}; + +class HelicopterSteeringSystem::HelicopterSteeringInputController : InputController +{ +public: + HelicopterSteeringInputController(std::shared_ptr<::EventBroker> eventBroker) + : InputController(eventBroker) + , Power(0.f) + { } + + float Power; + glm::vec3 Rotation; + + void Update(double dt); + +protected: + bool OnCommand(const Events::InputCommand &event) override; + bool OnMouseMove(const Events::MouseMove &event) override; + + +}; + + + + + +} \ No newline at end of file diff --git a/src/Systems/PhysicsSystem.cpp b/src/Systems/PhysicsSystem.cpp index 0b95b3f..c8ccc6d 100644 --- a/src/Systems/PhysicsSystem.cpp +++ b/src/Systems/PhysicsSystem.cpp @@ -34,6 +34,7 @@ void Systems::PhysicsSystem::Initialize() // Events EVENT_SUBSCRIBE_MEMBER(m_ETankSteer, &Systems::PhysicsSystem::OnTankSteer); EVENT_SUBSCRIBE_MEMBER(m_ESetVelocity, &Systems::PhysicsSystem::OnSetVelocity); + EVENT_SUBSCRIBE_MEMBER(m_EApplyForce, &Systems::PhysicsSystem::OnApplyForce); hkMemorySystem::FrameInfo finfo(6000 * 1024); // Allocate 6MB of Physics solver buffer hkMemoryRouter* memoryRouter = hkMemoryInitUtil::initDefault(hkMallocAllocator::m_defaultMallocAllocator, finfo); @@ -164,7 +165,7 @@ void Systems::PhysicsSystem::Update(double dt) m_Accumulator += dt; while (m_Accumulator >= timestep) { - m_PhysicsWorld->stepMultithreaded(m_JobQueue, m_ThreadPool, timestep); + hkpStepResult stepresult = m_PhysicsWorld->stepMultithreaded(m_JobQueue, m_ThreadPool, timestep); //m_PhysicsWorld->stepDeltaTime(timestep); m_Accumulator -= timestep; @@ -176,10 +177,7 @@ void Systems::PhysicsSystem::Update(double dt) // Clear accumulated timer data in this thread and all slave threads hkMonitorStream::getInstance().reset(); m_ThreadPool->clearTimerData(); - } - - - + } } void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) @@ -595,5 +593,15 @@ bool Systems::PhysicsSystem::OnSetVelocity( const Events::SetVelocity &event ) m_PhysicsWorld->markForWrite(); m_RigidBodies[event.Entity]->setLinearVelocity(ConvertPosition(event.Velocity)); m_PhysicsWorld->unmarkForWrite(); + + return true; +} + +bool Systems::PhysicsSystem::OnApplyForce(const Events::ApplyForce &event) +{ + m_PhysicsWorld->markForWrite(); + m_RigidBodies[event.Entity]->applyForce(event.DeltaTime, ConvertPosition(event.Force)); + m_PhysicsWorld->unmarkForWrite(); + return true; } diff --git a/src/Systems/PhysicsSystem.h b/src/Systems/PhysicsSystem.h index 0ff2bda..2e0230d 100644 --- a/src/Systems/PhysicsSystem.h +++ b/src/Systems/PhysicsSystem.h @@ -15,6 +15,7 @@ #include "Components/TowerSteering.h" #include "Events/TankSteer.h" #include "Events/SetVelocity.h" +#include "Events/ApplyForce.h" #include "OBJ.h" // Math and base include @@ -103,9 +104,10 @@ private: // Events EventRelay m_ETankSteer; bool OnTankSteer(const Events::TankSteer &event); - EventRelay m_ESetVelocity; bool OnSetVelocity(const Events::SetVelocity &event); + EventRelay m_EApplyForce; + bool OnApplyForce(const Events::ApplyForce &event); void SetUpPhysicsState(EntityID entity, EntityID parent); void TearDownPhysicsState(EntityID entity, EntityID parent); diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index 01a7d55..250e3db 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -111,6 +111,7 @@ + @@ -132,6 +133,7 @@ + @@ -153,6 +155,7 @@ + @@ -189,6 +192,7 @@ + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index 27e204d..bdab530 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -63,6 +63,7 @@ Physics\Systems + @@ -137,6 +138,21 @@ {42ae084f-ade8-402c-87ba-f03f6b846bc8} + + {5b992473-73e6-485e-8f13-17e0801fb2ca} + + + {8a78be3a-a3c5-4054-a2f1-1456f3fcbab0} + + + {8ccc0abe-5bdd-4656-ab11-5708a39c71ae} + + + {b2416d05-a49e-4fef-a5c4-cd03d8cc2efd} + + + {b34c0c95-a887-4cf4-af24-cf7c1f459aa8} + @@ -358,6 +374,15 @@ Input\Events + + Gameplay\Vehicles\Helicopter\Components + + + Gameplay\Vehicles\Helicopter\Systems + + + Physics\Events +