Walls are now destroyable!
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
#include "PrecompiledHeader.h"
|
||||
#include "DamageSystem.h"
|
||||
#include "World.h"
|
||||
|
||||
void Systems::DamageSystem::RegisterComponents( ComponentFactory* cf )
|
||||
{
|
||||
cf->Register<Components::Health>([]() { return new Components::Health(); });
|
||||
@@ -15,8 +14,16 @@ void Systems::DamageSystem::Initialize()
|
||||
|
||||
bool Systems::DamageSystem::OnDamage( const Events::Damage &event )
|
||||
{
|
||||
if(!m_World->ValidEntity(event.Entity))
|
||||
return false;
|
||||
auto health = m_World->GetComponent<Components::Health>(event.Entity);
|
||||
health->Amount -= event.Amount;
|
||||
if(health->Amount <= 0)
|
||||
{
|
||||
Events::OnDead e;
|
||||
e.Entity = event.Entity;
|
||||
EventBroker->Publish(e);
|
||||
}
|
||||
LOG_INFO("Damaged entity %i, Health left: %f", event.Entity, health->Amount);
|
||||
return true;
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "System.h"
|
||||
#include "Components/Health.h"
|
||||
#include "Events/Damage.h"
|
||||
#include "Events/OnDead.h"
|
||||
|
||||
namespace Systems
|
||||
{
|
||||
|
||||
@@ -163,6 +163,10 @@ void Systems::PhysicsSystem::Update(double dt)
|
||||
EntityID entity = pair.first;
|
||||
EntityID parent = pair.second;
|
||||
|
||||
auto templateComponent = m_World->GetComponent<Components::Template>(entity);
|
||||
if(templateComponent)
|
||||
continue;
|
||||
|
||||
if (m_RigidBodies.find(entity) == m_RigidBodies.end())
|
||||
continue;
|
||||
|
||||
@@ -227,6 +231,10 @@ void Systems::PhysicsSystem::Update(double dt)
|
||||
|
||||
void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
|
||||
{
|
||||
auto templateComponent = m_World->GetComponent<Components::Template>(entity);
|
||||
if(templateComponent)
|
||||
return;
|
||||
|
||||
auto transformComponent = m_World->GetComponent<Components::Transform>(entity);
|
||||
if (!transformComponent)
|
||||
return;
|
||||
@@ -312,7 +320,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity )
|
||||
return;
|
||||
}
|
||||
hkpShape* shape;
|
||||
if(! physicsComponent->Static) // Not static
|
||||
if(physicsComponent->MotionType == Components::Physics::MotionTypeEnum::Dynamic)
|
||||
{
|
||||
hkArray<hkpShape*> shapeArray;
|
||||
for (auto &shapeData : m_Shapes[entity])
|
||||
@@ -447,7 +455,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity )
|
||||
rigidBody->removeReference();
|
||||
}
|
||||
}
|
||||
else // Static
|
||||
else if(physicsComponent->MotionType == Components::Physics::MotionTypeEnum::Fixed || physicsComponent->MotionType == Components::Physics::MotionTypeEnum::Keyframed)
|
||||
{
|
||||
// Create the hkpStaticCompoundShape and add the instances.
|
||||
// "meshShape" should not be modified by the user in any way after adding it as an instance.
|
||||
@@ -499,7 +507,15 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity )
|
||||
hkpRigidBodyCinfo rigidBodyInfo;
|
||||
{
|
||||
rigidBodyInfo.m_shape = shape;
|
||||
rigidBodyInfo.m_motionType = hkpMotion::MOTION_FIXED;
|
||||
if(physicsComponent->MotionType == Components::Physics::MotionTypeEnum::Fixed)
|
||||
{
|
||||
rigidBodyInfo.m_motionType = hkpMotion::MOTION_FIXED;
|
||||
}
|
||||
else if(physicsComponent->MotionType == Components::Physics::MotionTypeEnum::Keyframed)
|
||||
{
|
||||
rigidBodyInfo.m_motionType = hkpMotion::MOTION_KEYFRAMED;
|
||||
}
|
||||
|
||||
auto absoluteTransform = m_World->GetSystem<Systems::TransformSystem>()->AbsoluteTransform(entity);
|
||||
hkVector4 position = GLMVEC3_TO_HKVECTOR4(absoluteTransform.Position);
|
||||
hkQuaternion rotation = GLMQUAT_TO_HKQUATERNION(absoluteTransform.Orientation);
|
||||
@@ -816,7 +832,6 @@ void Systems::PhysicsSystem::OnComponentRemoved(EntityID entity, std::string typ
|
||||
m_RigidBodies.erase(entity);
|
||||
m_PhysicsWorld->unmarkForWrite();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -124,6 +124,9 @@ bool Systems::TankSteeringSystem::OnCollision( const Events::Collision &e )
|
||||
return false;
|
||||
}
|
||||
|
||||
if(m_World->GetComponent<Components::Template>(shellEntity))
|
||||
return false;
|
||||
|
||||
auto physicsComponents = m_World->GetComponentsOfType<Components::Physics>();
|
||||
auto shellTransform = m_World->GetComponent<Components::Transform>(shellEntity);
|
||||
//auto otherTransform = m_World->GetComponent<Components::Transform>(otherEntity);
|
||||
@@ -155,8 +158,6 @@ bool Systems::TankSteeringSystem::OnCollision( const Events::Collision &e )
|
||||
EventBroker->Publish(d);
|
||||
}
|
||||
|
||||
|
||||
|
||||
m_World->RemoveEntity(shellEntity);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
#include "PrecompiledHeader.h"
|
||||
#include "WallSystem.h"
|
||||
#include "World.h"
|
||||
|
||||
void Systems::WallSystem::RegisterComponents( ComponentFactory* cf )
|
||||
{
|
||||
cf->Register<Components::Wall>([]() { return new Components::Wall(); });
|
||||
}
|
||||
|
||||
void Systems::WallSystem::Initialize()
|
||||
{
|
||||
EVENT_SUBSCRIBE_MEMBER(m_eDamage, &Systems::WallSystem::Damage);
|
||||
}
|
||||
|
||||
bool Systems::WallSystem::Damage( const Events::Damage &event )
|
||||
{
|
||||
auto wallComponent = m_World->GetComponent<Components::Wall>(event.Entity);
|
||||
if(!wallComponent)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
LOG_INFO("WallSystem::Damage");
|
||||
auto wallhealthcomponent = m_World->GetComponent<Components::Health>(event.Entity);
|
||||
if(wallhealthcomponent->Amount > 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
LOG_INFO("You are dead");
|
||||
auto transformComponent = m_World->GetComponent<Components::Transform>(event.Entity);
|
||||
|
||||
for(auto d : wallComponent->Walldebris)
|
||||
{
|
||||
auto debris = m_World->CloneEntity(d);
|
||||
|
||||
auto transform = m_World->GetComponent<Components::Transform>(debris);
|
||||
transform->Position += transformComponent->Position;
|
||||
|
||||
// DO STUFF! :D
|
||||
float distance = glm::distance(transform->Position, transformComponent->Position);
|
||||
float radius = 5.f;
|
||||
float strength = (1.f - pow(distance / radius, 2)) * 400.f;
|
||||
glm::vec3 direction = glm::normalize(transformComponent->Position - transform->Position);
|
||||
|
||||
Events::ApplyPointImpulse e;
|
||||
e.Entity = debris;
|
||||
e.Impulse = direction * strength;
|
||||
e.Position = transformComponent->Position;
|
||||
EventBroker->Publish(e);
|
||||
|
||||
}
|
||||
|
||||
m_World->RemoveEntity(event.Entity);
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
#ifndef WallSystem_h__
|
||||
#define WallSystem_h__
|
||||
|
||||
|
||||
#include "System.h"
|
||||
#include "Events/OnDead.h"
|
||||
#include "Events/Damage.h"
|
||||
#include "Events/ApplyPointImpulse.h"
|
||||
#include "Components/Model.h"
|
||||
#include "Components/Wall.h"
|
||||
#include "Components/Transform.h"
|
||||
#include "Components/Physics.h"
|
||||
#include "Components/MeshShape.h"
|
||||
#include "Components/Tankshell.h"
|
||||
#include "Components/Health.h"
|
||||
|
||||
|
||||
|
||||
namespace Systems
|
||||
{
|
||||
class WallSystem : public System
|
||||
{
|
||||
public:
|
||||
|
||||
WallSystem(World* world, std::shared_ptr<::EventBroker> eventBroker, std::shared_ptr<::ResourceManager> resourceManager)
|
||||
: System(world, eventBroker, resourceManager) { }
|
||||
|
||||
|
||||
void Initialize() override;
|
||||
void RegisterComponents(ComponentFactory* cf) override;
|
||||
|
||||
|
||||
|
||||
EventRelay<WallSystem, Events::Damage> m_eDamage;
|
||||
bool Damage(const Events::Damage &event);
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
#endif // WallSystem_h__
|
||||
Reference in New Issue
Block a user