From 6137adf604da1de988ef47f64b34f49e2e0e088c Mon Sep 17 00:00:00 2001 From: ViktorLjung Date: Wed, 21 May 2014 18:30:16 +0200 Subject: [PATCH] Rockets deleted properly --- src/Components/Physics.h | 3 +- src/Components/TankShell.h | 21 ++++++++ src/GameWorld.cpp | 21 +++++--- src/GameWorld.h | 1 + src/Systems/PhysicsSystem.cpp | 26 ++++++---- src/Systems/PhysicsSystem.h | 43 ++++++++++++---- src/Systems/TankSteeringSystem.cpp | 50 ++++++++++++------- src/Systems/TankSteeringSystem.h | 4 ++ vs11/Returngeance/Returngeance.vcxproj | 2 + .../Returngeance/Returngeance.vcxproj.filters | 12 +++-- 10 files changed, 132 insertions(+), 51 deletions(-) create mode 100644 src/Components/TankShell.h diff --git a/src/Components/Physics.h b/src/Components/Physics.h index 35844b5..fe3358f 100644 --- a/src/Components/Physics.h +++ b/src/Components/Physics.h @@ -9,10 +9,11 @@ namespace Components struct Physics : Component { Physics() - : Mass(0.f), Static(false){} + : Mass(1.f), Static(false), Phantom(false){} float Mass; bool Static; + bool Phantom; virtual Physics* Clone() const override { return new Physics(*this); } }; diff --git a/src/Components/TankShell.h b/src/Components/TankShell.h new file mode 100644 index 0000000..6ac7630 --- /dev/null +++ b/src/Components/TankShell.h @@ -0,0 +1,21 @@ +#ifndef Components_TankShell_h__ +#define Components_TankShell_h__ + +#include "Component.h" + + namespace Components +{ + + struct TankShell : Component + { + TankShell() + : Damage(1.0f){ } + + float Damage; + + virtual TankShell* Clone() const override { return new TankShell(*this); } + }; + +} + +#endif // Components_TankShell_h__ diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index e88afbd..18af9e8 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -258,7 +258,8 @@ void GameWorld::Initialize() auto tankSteering = AddComponent(tank); tankSteering->Player = player1; AddComponent(tank); - + auto health = AddComponent(tank); + health->health = 1; { auto shape = CreateEntity(tank); auto transform = AddComponent(shape); @@ -312,7 +313,8 @@ void GameWorld::Initialize() physics->Static = false; auto modelComponent = AddComponent(shot); modelComponent->ModelFile = "Models/Placeholders/rocket/Rocket.obj"; - + auto tankShellComponent = AddComponent(shot); + tankShellComponent->Damage = 20.f; { auto shape = CreateEntity(shot); auto transform = AddComponent(shape); @@ -361,7 +363,7 @@ void GameWorld::Initialize() // auto wheelpair = CreateEntity(tank); // SetProperty(wheelpair, "Name", "WheelPair"); // AddComponent(wheelpair, "WheelPairThingy"); - + #pragma region Wheels //Create wheels float wheelOffset = 0.4f; float springLength = 0.3f; @@ -658,6 +660,7 @@ void GameWorld::Initialize() emitterComponent->ParticleTemplate = particleEntity; CommitEntity(particleEntity); } +#pragma endregion CommitEntity(tank); } @@ -677,7 +680,8 @@ void GameWorld::Initialize() auto tankSteering = AddComponent(tank); tankSteering->Player = player2; AddComponent(tank); - + auto health = AddComponent(tank); + health->health = 1; { auto shape = CreateEntity(tank); auto transform = AddComponent(shape); @@ -731,7 +735,8 @@ void GameWorld::Initialize() physics->Static = false; auto modelComponent = AddComponent(shot); modelComponent->ModelFile = "Models/Placeholders/rocket/Rocket.obj"; - + auto tankShellComponent = AddComponent(shot); + tankShellComponent->Damage = 20.f; { auto shape = CreateEntity(shot); auto transform = AddComponent(shape); @@ -780,6 +785,7 @@ void GameWorld::Initialize() // auto wheelpair = CreateEntity(tank); // SetProperty(wheelpair, "Name", "WheelPair"); // AddComponent(wheelpair, "WheelPairThingy"); + #pragma region Wheels //Create wheels float wheelOffset = 0.4f; @@ -1076,7 +1082,7 @@ void GameWorld::Initialize() CommitEntity(particleEntity); } - +#pragma endregion CommitEntity(tank); } @@ -1104,7 +1110,7 @@ void GameWorld::Initialize() CommitEntity(entity); }*/ - for(int i = 0; i < 1; i++) + for(int i = 0; i < 0; i++) { for (int y = 0; y < 15; y++) { @@ -1180,6 +1186,7 @@ void GameWorld::RegisterComponents() m_ComponentFactory.Register([]() { return new Components::Transform(); }); m_ComponentFactory.Register([]() { return new Components::Template(); }); m_ComponentFactory.Register([]() { return new Components::Player(); }); + m_ComponentFactory.Register([]() { return new Components::Health(); }); } void GameWorld::RegisterSystems() diff --git a/src/GameWorld.h b/src/GameWorld.h index 7ea100f..d90e67e 100755 --- a/src/GameWorld.h +++ b/src/GameWorld.h @@ -41,6 +41,7 @@ #include "Components/TowerSteering.h" #include "Components/BarrelSteering.h" #include "Components/Player.h" +#include "Components/Health.h" class GameWorld : public World { diff --git a/src/Systems/PhysicsSystem.cpp b/src/Systems/PhysicsSystem.cpp index 290e24d..04a9b15 100644 --- a/src/Systems/PhysicsSystem.cpp +++ b/src/Systems/PhysicsSystem.cpp @@ -122,6 +122,7 @@ void Systems::PhysicsSystem::RegisterComponents(ComponentFactory* cf) cf->Register([]() { return new Components::MeshShape(); }); cf->Register([]() { return new Components::HingeConstraint(); }); cf->Register([]() { return new Components::WheelPair(); }); + cf->Register([]() { return new Components::TankShell(); }); } void Systems::PhysicsSystem::Update(double dt) @@ -159,7 +160,6 @@ void Systems::PhysicsSystem::Update(double dt) m_PhysicsWorld->unmarkForWrite(); } - } static const double timestep = 1 / 60.0; @@ -580,14 +580,18 @@ bool Systems::PhysicsSystem::OnApplyPointImpulse( const Events::ApplyPointImpuls void Systems::PhysicsSystem::OnEntityRemoved( EntityID entity ) { - // TODO: - /*auto rigidBodyIt = m_RigidBodies.find(entity); - if (rigidBodyIt == m_RigidBodies.end()) - return; - - auto rigidBody = rigidBodyIt->second; - m_RigidBodies.erase(entity); - m_RigidBodyEntities.erase(rigidBody); - rigidBody->removeReference();*/ - + if(m_RigidBodies.find(entity) != m_RigidBodies.end()) + { + m_RigidBodyEntities.erase(m_RigidBodies[entity]); + m_PhysicsWorld->markForWrite(); + m_PhysicsWorld->removeEntity(m_RigidBodies[entity]); + m_PhysicsWorld->unmarkForWrite(); + m_RigidBodies.erase(entity); + } + if(m_Vehicles.find(entity) != m_Vehicles.end()) + { + m_PhysicsWorld->markForWrite(); + m_Vehicles[entity]->removeFromWorld(); + m_PhysicsWorld->unmarkForWrite(); + } } diff --git a/src/Systems/PhysicsSystem.h b/src/Systems/PhysicsSystem.h index f687ab7..5f71187 100644 --- a/src/Systems/PhysicsSystem.h +++ b/src/Systems/PhysicsSystem.h @@ -76,7 +76,8 @@ #include -#include "Components/Model.h" +#include "Components/TankShell.h" +#include namespace Systems { class PhysicsSystem : public System @@ -94,27 +95,47 @@ public: EntityID entity1 = m_PhysicsSystem->m_RigidBodyEntities[event.getBody(0)]; EntityID entity2 = m_PhysicsSystem->m_RigidBodyEntities[event.getBody(1)]; + m_PhysicsSystem->m_PhysicsWorld->unmarkForRead(); Events::Collision e; e.Entity1 = entity1; e.Entity2 = entity2; m_PhysicsSystem->EventBroker->Publish(e); - - auto modelComponent = m_PhysicsSystem->m_World->GetComponent(entity1); - - if (modelComponent && modelComponent->ModelFile == "Models/Placeholders/rocket/Rocket.obj" && entity2 == 6) - { - m_PhysicsSystem->m_PhysicsWorld->markForWrite(); - m_PhysicsSystem->m_RigidBodies[entity1]->setPosition(hkVector4(2000, -2000, 2000)); - m_PhysicsSystem->m_PhysicsWorld->unmarkForWrite(); - } + m_PhysicsSystem->m_PhysicsWorld->markForRead(); } private: Systems::PhysicsSystem* m_PhysicsSystem; }; - friend class MyCollisionResolution; + /*class PhantomCallbackShape: public hkpPhantomCallbackShape + { + public: + + PhantomCallbackShape(Systems::PhysicsSystem* physicsSystem) + : m_PhysicsSystem(physicsSystem) { } + + virtual void phantomEnterEvent( const hkpCollidable* collidableA, const hkpCollidable* collidableB, const hkpCollisionInput& env ) + { + Events::ApplyPointImpulse e; + e.Entity = m_PhysicsSystem->m_RigidBodyEntities[hkpGetRigidBody(collidableB)]; + e.Position = HKVECTOR4_TO_GLMVEC3(hkpGetRigidBody(collidableB)->getPosition()); + + glm::vec3 impulse; + impulse = HKVECTOR4_TO_GLMVEC3(hkpGetRigidBody(collidableA)->getPosition()) - HKVECTOR4_TO_GLMVEC3(hkpGetRigidBody(collidableB)->getPosition()); + e.Impulse = impulse; + } + + virtual void phantomLeaveEvent( const hkpCollidable* collidableA, const hkpCollidable* collidableB ) + { + + } + + private: + Systems::PhysicsSystem* m_PhysicsSystem; + }; + friend class PhantomCallbackShape; + */ PhysicsSystem(World* world, std::shared_ptr<::EventBroker> eventBroker) : System(world, eventBroker) { } diff --git a/src/Systems/TankSteeringSystem.cpp b/src/Systems/TankSteeringSystem.cpp index 5502b35..db98e97 100644 --- a/src/Systems/TankSteeringSystem.cpp +++ b/src/Systems/TankSteeringSystem.cpp @@ -97,26 +97,40 @@ void Systems::TankSteeringSystem::UpdateEntity(double dt, EntityID entity, Entit bool Systems::TankSteeringSystem::OnCollision( const Events::Collision &e ) { - // TODO: - auto modelComponent = m_World->GetComponent(e.Entity1); - - if (modelComponent && modelComponent->ModelFile == "Models/Placeholders/rocket/Rocket.obj") + if(m_World->ValidEntity(e.Entity1) && m_World->ValidEntity(e.Entity1)) { - auto transform = m_World->GetComponent(e.Entity1); - m_World->GetSystem()->CreateExplosion( - transform->Position, - 1, - 60, - "Textures/Sprites/NewtonTreeDeleteASAPPlease.png", - glm::angleAxis(glm::pi()/2, glm::vec3(1,0,0)), - 40, - glm::pi(), - 0.5f - ); - //m_World->RemoveEntity(e.Entity1); - } + auto tankShell1 = m_World->GetComponent(e.Entity1); + auto tankShell2 = m_World->GetComponent(e.Entity2); + - //LOG_INFO("Ent1 %i Ent2 %i", e.Entity1, e.Entity2); + if(tankShell1) + { + auto transform = m_World->GetComponent(e.Entity1); + m_World->GetSystem()->CreateExplosion(transform->Position, 1, 60, "Textures/Sprites/NewtonTreeDeleteASAPPlease.png", glm::angleAxis(glm::pi()/2, glm::vec3(1,0,0)), 40, glm::pi(), 0.5f); + + auto health2 = m_World->GetComponent(e.Entity2); + if(health2) + { + health2->health -= tankShell1->Damage; + } + + m_World->RemoveEntity(e.Entity1); + } + + if(tankShell2) + { + auto transform = m_World->GetComponent(e.Entity2); + m_World->GetSystem()->CreateExplosion(transform->Position, 1, 60, "Textures/Sprites/NewtonTreeDeleteASAPPlease.png", glm::angleAxis(glm::pi()/2, glm::vec3(1,0,0)), 40, glm::pi(), 0.5f); + + auto health1 = m_World->GetComponent(e.Entity2); + if(health1) + { + health1->health -= tankShell1->Damage; + } + + m_World->RemoveEntity(e.Entity2); + } + } return true; } diff --git a/src/Systems/TankSteeringSystem.h b/src/Systems/TankSteeringSystem.h index 5a64617..bde07ab 100644 --- a/src/Systems/TankSteeringSystem.h +++ b/src/Systems/TankSteeringSystem.h @@ -18,6 +18,10 @@ #include "Systems/ParticleSystem.h" #include "InputController.h" +#include "Components/Health.h" +#include "Components/TankShell.h" +#include "Components/SphereShape.h" + namespace Systems { diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index 9270387..7a1c955 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -129,6 +129,7 @@ + @@ -146,6 +147,7 @@ + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index 4463763..5dc5595 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -376,9 +376,6 @@ Physics\Components - - Physics\Components - Input\Events @@ -400,6 +397,15 @@ Physics\Events + + Gameplay\Components + + + Gameplay\Components + + + Physics\Components +