From ce5b62cf6d2b9de188d1e93817d7842681d1cb44 Mon Sep 17 00:00:00 2001 From: ViktorLjung Date: Tue, 13 May 2014 03:32:58 +0200 Subject: [PATCH] =?UTF-8?q?Shots=20fired!=20=EF=BC=88=20=EF=BE=9F=D0=94?= =?UTF-8?q?=EF=BE=9F=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets | 2 +- src/Components/BarrelSteering.h | 7 +- src/Components/Shot.h | 17 ----- src/Components/TowerSteering.h | 5 +- src/GameWorld.cpp | 64 ++++++++++--------- src/GameWorld.h | 1 - src/Systems/PhysicsSystem.cpp | 7 +- src/Systems/TankSteeringSystem.cpp | 28 ++++---- src/Systems/TankSteeringSystem.h | 5 +- src/World.cpp | 4 ++ vs11/Returngeance/Returngeance.vcxproj | 1 - .../Returngeance/Returngeance.vcxproj.filters | 3 - 12 files changed, 70 insertions(+), 74 deletions(-) delete mode 100644 src/Components/Shot.h diff --git a/assets b/assets index 05b3d22..15ac025 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 05b3d22b01131512cc370dacd507a22e88c46e57 +Subproject commit 15ac02523ad374b4aaf60a16db89b1934de7f303 diff --git a/src/Components/BarrelSteering.h b/src/Components/BarrelSteering.h index f3ee943..1cd71be 100644 --- a/src/Components/BarrelSteering.h +++ b/src/Components/BarrelSteering.h @@ -9,10 +9,13 @@ namespace Components struct BarrelSteering : Component { BarrelSteering() - : Velocity(1.f), Axis(glm::vec3(0,1,0)){ } - float Velocity; + : TurnSpeed(1.f), Axis(glm::vec3(0,1,0)){ } + float TurnSpeed; glm::vec3 Axis; EntityID ShotTemplate; + float ShotSpeed; + + virtual BarrelSteering* Clone() const override { return new BarrelSteering(*this); } }; } diff --git a/src/Components/Shot.h b/src/Components/Shot.h deleted file mode 100644 index 324a9c5..0000000 --- a/src/Components/Shot.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef Shot_h__ -#define Shot_h__ - -#include "Component.h" - -namespace Components -{ - - struct Shot : Component - { - Shot(); - float Speed; - }; - -} - -#endif // Shot_h__ \ No newline at end of file diff --git a/src/Components/TowerSteering.h b/src/Components/TowerSteering.h index 38a2320..f4f72b9 100644 --- a/src/Components/TowerSteering.h +++ b/src/Components/TowerSteering.h @@ -9,10 +9,11 @@ struct TowerSteering : Component { TowerSteering() - : Velocity(1.f), Axis(glm::vec3(0,1,0)){ } + : TurnSpeed(1.f), Axis(glm::vec3(0,1,0)){ } - float Velocity; + float TurnSpeed; glm::vec3 Axis; + virtual TowerSteering* Clone() const override { return new TowerSteering(*this); } }; } diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index c3f653e..6368764 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -221,7 +221,7 @@ void GameWorld::Initialize() auto tank = CreateEntity(); auto transform = AddComponent(tank, "Transform"); transform->Position = glm::vec3(0, 5, 0); - transform->Orientation = glm::angleAxis(glm::pi()/2, glm::vec3(0, 1, 0)); + //transform->Orientation = glm::angleAxis(0.f, glm::vec3(0, 1, 0)); auto physics = AddComponent(tank, "Physics"); physics->Mass = 45000; physics->Static = false; @@ -260,7 +260,7 @@ void GameWorld::Initialize() model->ModelFile = "Models/Tank/Fix/Top.obj"; auto towerSteering = AddComponent(tower, "TowerSteering"); towerSteering->Axis = glm::vec3(0.f, 1.f, 0.f); - towerSteering->Velocity = glm::pi()/4.f; + towerSteering->TurnSpeed = glm::pi()/4.f; { auto barrel = CreateEntity(tower); auto transform = AddComponent(barrel, "Transform"); @@ -269,21 +269,23 @@ void GameWorld::Initialize() model->ModelFile = "Models/Tank/Fix/Barrel.obj"; auto barrelSteering = AddComponent(barrel, "BarrelSteering"); barrelSteering->Axis = glm::vec3(1.f, 0.f, 0.f); - barrelSteering->Velocity = glm::pi()/4.f; + barrelSteering->TurnSpeed = glm::pi()/4.f; + barrelSteering->ShotSpeed = 70.f; { auto shot = CreateEntity(barrel); auto transform = AddComponent(shot, "Transform"); - transform->Position = glm::vec3(0.35f, 1.f, -2.f); - auto shotComponent = AddComponent(shot, "Shot"); - shotComponent->Speed = 5; + transform->Position = glm::vec3(0.35f, 0.f, -2.f); + transform->Orientation = glm::angleAxis(-glm::pi()/2.f, glm::vec3(1, 0, 0)); + transform->Scale = glm::vec3(3.f); + AddComponent(shot, "Template"); auto physics = AddComponent(shot, "Physics"); physics->Mass = 10.f; physics->Static = false; auto modelComponent = AddComponent(shot, "Model"); - modelComponent->ModelFile = "Models/Placeholders/PhysicsTest/Cube.obj"; + modelComponent->ModelFile = "Models/Placeholders/rocket/Rocket.obj"; { - auto shape = CreateEntity(tank); + auto shape = CreateEntity(shot); auto transform = AddComponent(shape, "Transform"); auto boxShape = AddComponent(shape, "BoxShape"); boxShape->Width = 0.5f; @@ -501,7 +503,7 @@ void GameWorld::Initialize() } CommitEntity(wheel); - auto entity = CreateEntity(tank); + /*auto entity = CreateEntity(tank); auto transformComponent = AddComponent(entity, "Transform"); transformComponent->Position = glm::vec3(2,-1.7,2.0); transformComponent->Scale = glm::vec3(3,3,3); @@ -523,7 +525,7 @@ void GameWorld::Initialize() spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png"; emitterComponent->ParticleTemplate = particleEntity; - CommitEntity(particleEntity); + CommitEntity(particleEntity);*/ } { @@ -585,29 +587,29 @@ void GameWorld::Initialize() } CommitEntity(wheel); - auto entity = CreateEntity(tank); - auto transformComponent = AddComponent(entity, "Transform"); - transformComponent->Position = glm::vec3(-2,-1.7,2.0); - transformComponent->Scale = glm::vec3(3,3,3); - transformComponent->Orientation = glm::angleAxis(glm::pi()/2, glm::vec3(1,0,0)); - auto emitterComponent = AddComponent(entity, "ParticleEmitter"); - emitterComponent->SpawnCount = 2; - emitterComponent->SpawnFrequency = 0.005; - emitterComponent->SpreadAngle = glm::pi(); - emitterComponent->UseGoalVelocity = false; - emitterComponent->LifeTime = 0.5; - //emitterComponent->AngularVelocitySpectrum.push_back(glm::pi() / 100); - emitterComponent->ScaleSpectrum.push_back(glm::vec3(0.05)); - CommitEntity(entity); + //auto entity = CreateEntity(tank); + //auto transformComponent = AddComponent(entity, "Transform"); + //transformComponent->Position = glm::vec3(-2,-1.7,2.0); + //transformComponent->Scale = glm::vec3(3,3,3); + //transformComponent->Orientation = glm::angleAxis(glm::pi()/2, glm::vec3(1,0,0)); + //auto emitterComponent = AddComponent(entity, "ParticleEmitter"); + //emitterComponent->SpawnCount = 2; + //emitterComponent->SpawnFrequency = 0.005; + //emitterComponent->SpreadAngle = glm::pi(); + //emitterComponent->UseGoalVelocity = false; + //emitterComponent->LifeTime = 0.5; + ////emitterComponent->AngularVelocitySpectrum.push_back(glm::pi() / 100); + //emitterComponent->ScaleSpectrum.push_back(glm::vec3(0.05)); + //CommitEntity(entity); - auto particleEntity = CreateEntity(entity); - auto TEMP = AddComponent(particleEntity, "Transform"); - TEMP->Scale = glm::vec3(0); - auto spriteComponent = AddComponent(particleEntity, "Sprite"); - spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png"; - emitterComponent->ParticleTemplate = particleEntity; + //auto particleEntity = CreateEntity(entity); + //auto TEMP = AddComponent(particleEntity, "Transform"); + //TEMP->Scale = glm::vec3(0); + //auto spriteComponent = AddComponent(particleEntity, "Sprite"); + //spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png"; + //emitterComponent->ParticleTemplate = particleEntity; - CommitEntity(particleEntity); + //CommitEntity(particleEntity); } CommitEntity(tank); diff --git a/src/GameWorld.h b/src/GameWorld.h index 27a36dd..f884e2f 100755 --- a/src/GameWorld.h +++ b/src/GameWorld.h @@ -38,7 +38,6 @@ #include "Components/TankSteering.h" #include "Components/TowerSteering.h" #include "Components/BarrelSteering.h" -#include "Components/Shot.h" class GameWorld : public World { diff --git a/src/Systems/PhysicsSystem.cpp b/src/Systems/PhysicsSystem.cpp index 5e4a71f..8efa4bb 100644 --- a/src/Systems/PhysicsSystem.cpp +++ b/src/Systems/PhysicsSystem.cpp @@ -31,7 +31,7 @@ void Systems::PhysicsSystem::Initialize() // Events EVENT_SUBSCRIBE_MEMBER(m_ETankSteer, &Systems::PhysicsSystem::OnTankSteer); - EVENT_SUBSCRIBE_MEMBER(m_ETankSteer, &Systems::PhysicsSystem::OnSetVelocity); + EVENT_SUBSCRIBE_MEMBER(m_ESetVelocity, &Systems::PhysicsSystem::OnSetVelocity); hkMemorySystem::FrameInfo finfo(6000 * 1024); // Allocate 6MB of Physics solver buffer hkMemoryRouter* memoryRouter = hkMemoryInitUtil::initDefault(hkMallocAllocator::m_defaultMallocAllocator, finfo); @@ -583,5 +583,8 @@ bool Systems::PhysicsSystem::OnTankSteer(const Events::TankSteer &event) bool Systems::PhysicsSystem::OnSetVelocity( const Events::SetVelocity &event ) { - + m_PhysicsWorld->markForWrite(); + m_RigidBodies[event.Entity]->setLinearVelocity(ConvertPosition(event.Velocity)); + m_PhysicsWorld->unmarkForWrite(); + return true; } diff --git a/src/Systems/TankSteeringSystem.cpp b/src/Systems/TankSteeringSystem.cpp index 5e6c025..11f83b6 100644 --- a/src/Systems/TankSteeringSystem.cpp +++ b/src/Systems/TankSteeringSystem.cpp @@ -38,7 +38,7 @@ void Systems::TankSteeringSystem::UpdateEntity(double dt, EntityID entity, Entit if(towerSteeringComponent) { auto transformComponent = m_World->GetComponent(entity, "Transform"); - glm::quat orientation = glm::angleAxis(towerSteeringComponent->Velocity * m_TowerInputController->TowerDirection * (float)dt, towerSteeringComponent->Axis); + glm::quat orientation = glm::angleAxis(towerSteeringComponent->TurnSpeed * m_TowerInputController->TowerDirection * (float)dt, towerSteeringComponent->Axis); transformComponent->Orientation *= orientation; } @@ -46,21 +46,25 @@ void Systems::TankSteeringSystem::UpdateEntity(double dt, EntityID entity, Entit if(barrelSteeringComponent) { auto transformComponent = m_World->GetComponent(entity, "Transform"); - glm::quat orientation = glm::angleAxis(barrelSteeringComponent->Velocity * m_TowerInputController->BarrelDirection * (float)dt, barrelSteeringComponent->Axis); + auto absoluteTransform = m_World->GetSystem("TransformSystem")->AbsoluteTransform(entity); + glm::quat orientation = glm::angleAxis(barrelSteeringComponent->TurnSpeed * m_TowerInputController->BarrelDirection * (float)dt, barrelSteeringComponent->Axis); transformComponent->Orientation *= orientation; - } - auto shotComponent = m_World->GetComponent(entity, "Shot"); - if(shotComponent) - { - if(m_TowerInputController->shoot) + if(m_TowerInputController->Shoot && m_TimeSinceLastShot[entity] > 1.0) { - auto absoluteOrientation = m_World->GetSystem("TransformSystem")->AbsoluteOrientation(entity); + EntityID clone = m_World->CloneEntity(barrelSteeringComponent->ShotTemplate); + auto templateAbsoluteTransform = m_World->GetSystem("TransformSystem")->AbsoluteTransform(barrelSteeringComponent->ShotTemplate); + auto cloneTransform = m_World->GetComponent(clone, "Transform"); + cloneTransform->Position = templateAbsoluteTransform.Position; + cloneTransform->Orientation = absoluteTransform.Orientation * cloneTransform->Orientation; Events::SetVelocity e; - e.Entity = entity; - e.Velocity = absoluteOrientation * (glm::vec3(0.f, 0.f, 1.f) * shotComponent->Speed * (float)dt); + e.Entity = clone; + e.Velocity = absoluteTransform.Orientation * (glm::vec3(0.f, 0.f, -1.f) * barrelSteeringComponent->ShotSpeed); EventBroker->Publish(e); + m_TimeSinceLastShot[entity] = 0; } + + m_TimeSinceLastShot[entity] += dt; } } @@ -74,7 +78,7 @@ void Systems::TankSteeringSystem::TowerSteeringInputController::Update( double d { TowerDirection = m_TowerDirection; BarrelDirection = m_BarrelDirection; - shoot = m_Shoot; + Shoot = m_Shoot; } bool Systems::TankSteeringSystem::TankSteeringInputController::OnCommand(const Events::InputCommand &event) @@ -111,7 +115,7 @@ bool Systems::TankSteeringSystem::TowerSteeringInputController::OnCommand( const else if (event.Command == "shoot") { - shoot = (bool)val; + m_Shoot = val > 0; } return true; } diff --git a/src/Systems/TankSteeringSystem.h b/src/Systems/TankSteeringSystem.h index 1622063..d264325 100644 --- a/src/Systems/TankSteeringSystem.h +++ b/src/Systems/TankSteeringSystem.h @@ -8,7 +8,6 @@ #include "Components/TowerSteering.h" #include "Components/BarrelSteering.h" #include "Components/Vehicle.h" -#include "Components/Shot.h" #include "Systems/TransformSystem.h" #include "InputController.h" @@ -32,6 +31,8 @@ namespace Systems std::unique_ptr m_TankInputController; class TowerSteeringInputController; std::unique_ptr m_TowerInputController; + + std::map m_TimeSinceLastShot; }; class TankSteeringSystem::TankSteeringInputController : InputController @@ -76,7 +77,7 @@ namespace Systems float TowerDirection; float BarrelDirection; - bool shoot; + bool Shoot; void Update(double dt); protected: virtual bool OnCommand(const Events::InputCommand &event); diff --git a/src/World.cpp b/src/World.cpp index 01713ab..c84a2bd 100755 --- a/src/World.cpp +++ b/src/World.cpp @@ -170,6 +170,8 @@ EntityID World::CloneEntity(EntityID entity, EntityID parent /* = 0 */) for (auto pair : m_EntityComponents[entity]) { auto type = pair.first; + if (type == "Template") + continue; auto component = std::shared_ptr(pair.second->Clone()); if (component != nullptr) { @@ -186,6 +188,8 @@ EntityID World::CloneEntity(EntityID entity, EntityID parent /* = 0 */) } } + CommitEntity(clone); + return clone; } diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index 8d2c9e7..297bb80 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -139,7 +139,6 @@ - diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index f3d89c8..b58c65d 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -337,9 +337,6 @@ Physics\Components - - Physics\Components - Physics\Events