SDK: Add LifetimeComponent and LifetimeSystem
This commit is contained in:
parent
713a70dd10
commit
793c5abfe3
|
|
@ -251,6 +251,7 @@ Nazara Development Kit:
|
||||||
- Added possibility of disabling synchronization between PhysicsComponent2D and NodeComponent
|
- Added possibility of disabling synchronization between PhysicsComponent2D and NodeComponent
|
||||||
- Fixed GraphicsComponent not invalidating render queue on material change (causing crashes or visual errors)
|
- Fixed GraphicsComponent not invalidating render queue on material change (causing crashes or visual errors)
|
||||||
- Added CollisionComponent2D::SetGeomOffset and CollisionComponent2D::Recenter
|
- Added CollisionComponent2D::SetGeomOffset and CollisionComponent2D::Recenter
|
||||||
|
- Added LifetimeComponent and LifetimeSystem
|
||||||
|
|
||||||
# 0.4:
|
# 0.4:
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,40 @@
|
||||||
|
// Copyright (C) 2017 Jérôme Leclercq
|
||||||
|
// This file is part of the "Nazara Development Kit"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifndef NDK_COMPONENTS_LIFETIMECOMPONENT_HPP
|
||||||
|
#define NDK_COMPONENTS_LIFETIMECOMPONENT_HPP
|
||||||
|
|
||||||
|
#include <NDK/Component.hpp>
|
||||||
|
|
||||||
|
namespace Ndk
|
||||||
|
{
|
||||||
|
class LifetimeComponent;
|
||||||
|
|
||||||
|
using LifetimeComponentHandle = Nz::ObjectHandle<LifetimeComponent>;
|
||||||
|
|
||||||
|
class NDK_API LifetimeComponent : public Component<LifetimeComponent>
|
||||||
|
{
|
||||||
|
friend class LifetimeSystem;
|
||||||
|
|
||||||
|
public:
|
||||||
|
inline LifetimeComponent(float lifetime);
|
||||||
|
LifetimeComponent(const LifetimeComponent&) = default;
|
||||||
|
~LifetimeComponent() = default;
|
||||||
|
|
||||||
|
inline float GetRemainingTime() const;
|
||||||
|
|
||||||
|
static ComponentIndex componentIndex;
|
||||||
|
|
||||||
|
private:
|
||||||
|
inline bool UpdateLifetime(float elapsedTime);
|
||||||
|
|
||||||
|
float m_lifetime;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <NDK/Components/LifetimeComponent.inl>
|
||||||
|
|
||||||
|
#endif // NDK_COMPONENTS_LIFETIMECOMPONENT_HPP
|
||||||
|
|
@ -0,0 +1,24 @@
|
||||||
|
// Copyright (C) 2017 Jérôme Leclercq
|
||||||
|
// This file is part of the "Nazara Development Kit"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||||
|
|
||||||
|
#include <NDK/Components/LifetimeComponent.hpp>
|
||||||
|
|
||||||
|
namespace Ndk
|
||||||
|
{
|
||||||
|
inline LifetimeComponent::LifetimeComponent(float lifetime) :
|
||||||
|
m_lifetime(lifetime)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
inline float Ndk::LifetimeComponent::GetRemainingTime() const
|
||||||
|
{
|
||||||
|
return m_lifetime;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool LifetimeComponent::UpdateLifetime(float elapsedTime)
|
||||||
|
{
|
||||||
|
m_lifetime -= elapsedTime;
|
||||||
|
return m_lifetime < 0.f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
// Copyright (C) 2017 Jérôme Leclercq
|
||||||
|
// This file is part of the "Nazara Development Kit"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifndef NDK_SYSTEMS_LIFETIMESYSTEM_HPP
|
||||||
|
#define NDK_SYSTEMS_LIFETIMESYSTEM_HPP
|
||||||
|
|
||||||
|
#include <NDK/System.hpp>
|
||||||
|
|
||||||
|
namespace Ndk
|
||||||
|
{
|
||||||
|
class NDK_API LifetimeSystem : public System<LifetimeSystem>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
LifetimeSystem();
|
||||||
|
~LifetimeSystem() = default;
|
||||||
|
|
||||||
|
static SystemIndex systemIndex;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void OnUpdate(float elapsedTime) override;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <NDK/Systems/LifetimeSystem.inl>
|
||||||
|
|
||||||
|
#endif // NDK_SYSTEMS_LIFETIMESYSTEM_HPP
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
// Copyright (C) 2017 Jérôme Leclercq
|
||||||
|
// This file is part of the "Nazara Development Kit"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
// Copyright (C) 2017 Jérôme Leclercq
|
||||||
|
// This file is part of the "Nazara Development Kit"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||||
|
|
||||||
|
#include <NDK/Components/LifetimeComponent.hpp>
|
||||||
|
|
||||||
|
namespace Ndk
|
||||||
|
{
|
||||||
|
ComponentIndex LifetimeComponent::componentIndex;
|
||||||
|
}
|
||||||
|
|
@ -17,11 +17,13 @@
|
||||||
#include <NDK/BaseSystem.hpp>
|
#include <NDK/BaseSystem.hpp>
|
||||||
#include <NDK/Components/CollisionComponent2D.hpp>
|
#include <NDK/Components/CollisionComponent2D.hpp>
|
||||||
#include <NDK/Components/CollisionComponent3D.hpp>
|
#include <NDK/Components/CollisionComponent3D.hpp>
|
||||||
|
#include <NDK/Components/LifetimeComponent.hpp>
|
||||||
#include <NDK/Components/NodeComponent.hpp>
|
#include <NDK/Components/NodeComponent.hpp>
|
||||||
#include <NDK/Components/PhysicsComponent2D.hpp>
|
#include <NDK/Components/PhysicsComponent2D.hpp>
|
||||||
#include <NDK/Components/PhysicsComponent3D.hpp>
|
#include <NDK/Components/PhysicsComponent3D.hpp>
|
||||||
#include <NDK/Components/VelocityComponent.hpp>
|
#include <NDK/Components/VelocityComponent.hpp>
|
||||||
#include <NDK/Components/ConstraintComponent2D.hpp>
|
#include <NDK/Components/ConstraintComponent2D.hpp>
|
||||||
|
#include <NDK/Systems/LifetimeSystem.hpp>
|
||||||
#include <NDK/Systems/PhysicsSystem2D.hpp>
|
#include <NDK/Systems/PhysicsSystem2D.hpp>
|
||||||
#include <NDK/Systems/PhysicsSystem3D.hpp>
|
#include <NDK/Systems/PhysicsSystem3D.hpp>
|
||||||
#include <NDK/Systems/VelocitySystem.hpp>
|
#include <NDK/Systems/VelocitySystem.hpp>
|
||||||
|
|
@ -88,6 +90,7 @@ namespace Ndk
|
||||||
// Shared components
|
// Shared components
|
||||||
InitializeComponent<CollisionComponent2D>("NdkColl2");
|
InitializeComponent<CollisionComponent2D>("NdkColl2");
|
||||||
InitializeComponent<CollisionComponent3D>("NdkColl3");
|
InitializeComponent<CollisionComponent3D>("NdkColl3");
|
||||||
|
InitializeComponent<LifetimeComponent>("NdkLiftm");
|
||||||
InitializeComponent<NodeComponent>("NdkNode");
|
InitializeComponent<NodeComponent>("NdkNode");
|
||||||
InitializeComponent<PhysicsComponent2D>("NdkPhys2");
|
InitializeComponent<PhysicsComponent2D>("NdkPhys2");
|
||||||
InitializeComponent<PhysicsComponent3D>("NdkPhys3");
|
InitializeComponent<PhysicsComponent3D>("NdkPhys3");
|
||||||
|
|
@ -110,6 +113,7 @@ namespace Ndk
|
||||||
BaseSystem::Initialize();
|
BaseSystem::Initialize();
|
||||||
|
|
||||||
// Shared systems
|
// Shared systems
|
||||||
|
InitializeSystem<LifetimeSystem>();
|
||||||
InitializeSystem<PhysicsSystem2D>();
|
InitializeSystem<PhysicsSystem2D>();
|
||||||
InitializeSystem<PhysicsSystem3D>();
|
InitializeSystem<PhysicsSystem3D>();
|
||||||
InitializeSystem<VelocitySystem>();
|
InitializeSystem<VelocitySystem>();
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
// Copyright (C) 2017 Jérôme Leclercq
|
||||||
|
// This file is part of the "Nazara Development Kit"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||||
|
|
||||||
|
#include <NDK/Systems/LifetimeSystem.hpp>
|
||||||
|
#include <NDK/Components/LifetimeComponent.hpp>
|
||||||
|
|
||||||
|
namespace Ndk
|
||||||
|
{
|
||||||
|
LifetimeSystem::LifetimeSystem()
|
||||||
|
{
|
||||||
|
Requires<LifetimeComponent>();
|
||||||
|
}
|
||||||
|
|
||||||
|
void LifetimeSystem::OnUpdate(float elapsedTime)
|
||||||
|
{
|
||||||
|
for (const Ndk::EntityHandle& entity : GetEntities())
|
||||||
|
{
|
||||||
|
auto& lifetime = entity->GetComponent<LifetimeComponent>();
|
||||||
|
|
||||||
|
if (lifetime.UpdateLifetime(elapsedTime))
|
||||||
|
entity->Kill();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SystemIndex LifetimeSystem::systemIndex;
|
||||||
|
}
|
||||||
|
|
@ -6,6 +6,7 @@
|
||||||
#include <Nazara/Core/Clock.hpp>
|
#include <Nazara/Core/Clock.hpp>
|
||||||
#include <Nazara/Core/Error.hpp>
|
#include <Nazara/Core/Error.hpp>
|
||||||
#include <NDK/BaseComponent.hpp>
|
#include <NDK/BaseComponent.hpp>
|
||||||
|
#include <NDK/Systems/LifetimeSystem.hpp>
|
||||||
#include <NDK/Systems/PhysicsSystem2D.hpp>
|
#include <NDK/Systems/PhysicsSystem2D.hpp>
|
||||||
#include <NDK/Systems/PhysicsSystem3D.hpp>
|
#include <NDK/Systems/PhysicsSystem3D.hpp>
|
||||||
#include <NDK/Systems/VelocitySystem.hpp>
|
#include <NDK/Systems/VelocitySystem.hpp>
|
||||||
|
|
@ -43,6 +44,7 @@ namespace Ndk
|
||||||
|
|
||||||
void World::AddDefaultSystems()
|
void World::AddDefaultSystems()
|
||||||
{
|
{
|
||||||
|
AddSystem<LifetimeSystem>();
|
||||||
AddSystem<PhysicsSystem2D>();
|
AddSystem<PhysicsSystem2D>();
|
||||||
AddSystem<PhysicsSystem3D>();
|
AddSystem<PhysicsSystem3D>();
|
||||||
AddSystem<VelocitySystem>();
|
AddSystem<VelocitySystem>();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue