diff --git a/ChangeLog.md b/ChangeLog.md index 3f22ba700..a700b072b 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -251,6 +251,7 @@ Nazara Development Kit: - Added possibility of disabling synchronization between PhysicsComponent2D and NodeComponent - Fixed GraphicsComponent not invalidating render queue on material change (causing crashes or visual errors) - Added CollisionComponent2D::SetGeomOffset and CollisionComponent2D::Recenter +- Added LifetimeComponent and LifetimeSystem # 0.4: diff --git a/SDK/include/NDK/Components/LifetimeComponent.hpp b/SDK/include/NDK/Components/LifetimeComponent.hpp new file mode 100644 index 000000000..72004a90d --- /dev/null +++ b/SDK/include/NDK/Components/LifetimeComponent.hpp @@ -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 + +namespace Ndk +{ + class LifetimeComponent; + + using LifetimeComponentHandle = Nz::ObjectHandle; + + class NDK_API LifetimeComponent : public Component + { + 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 + +#endif // NDK_COMPONENTS_LIFETIMECOMPONENT_HPP diff --git a/SDK/include/NDK/Components/LifetimeComponent.inl b/SDK/include/NDK/Components/LifetimeComponent.inl new file mode 100644 index 000000000..145e160ba --- /dev/null +++ b/SDK/include/NDK/Components/LifetimeComponent.inl @@ -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 + +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; + } +} diff --git a/SDK/include/NDK/Systems/LifetimeSystem.hpp b/SDK/include/NDK/Systems/LifetimeSystem.hpp new file mode 100644 index 000000000..9957c1b53 --- /dev/null +++ b/SDK/include/NDK/Systems/LifetimeSystem.hpp @@ -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 + +namespace Ndk +{ + class NDK_API LifetimeSystem : public System + { + public: + LifetimeSystem(); + ~LifetimeSystem() = default; + + static SystemIndex systemIndex; + + private: + void OnUpdate(float elapsedTime) override; + }; +} + +#include + +#endif // NDK_SYSTEMS_LIFETIMESYSTEM_HPP diff --git a/SDK/include/NDK/Systems/LifetimeSystem.inl b/SDK/include/NDK/Systems/LifetimeSystem.inl new file mode 100644 index 000000000..5302ce8d0 --- /dev/null +++ b/SDK/include/NDK/Systems/LifetimeSystem.inl @@ -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 diff --git a/SDK/src/NDK/Components/LifetimeComponent.cpp b/SDK/src/NDK/Components/LifetimeComponent.cpp new file mode 100644 index 000000000..5ffa6da82 --- /dev/null +++ b/SDK/src/NDK/Components/LifetimeComponent.cpp @@ -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 + +namespace Ndk +{ + ComponentIndex LifetimeComponent::componentIndex; +} diff --git a/SDK/src/NDK/Sdk.cpp b/SDK/src/NDK/Sdk.cpp index 90d07c738..341e6f0d5 100644 --- a/SDK/src/NDK/Sdk.cpp +++ b/SDK/src/NDK/Sdk.cpp @@ -17,11 +17,13 @@ #include #include #include +#include #include #include #include #include #include +#include #include #include #include @@ -88,6 +90,7 @@ namespace Ndk // Shared components InitializeComponent("NdkColl2"); InitializeComponent("NdkColl3"); + InitializeComponent("NdkLiftm"); InitializeComponent("NdkNode"); InitializeComponent("NdkPhys2"); InitializeComponent("NdkPhys3"); @@ -110,6 +113,7 @@ namespace Ndk BaseSystem::Initialize(); // Shared systems + InitializeSystem(); InitializeSystem(); InitializeSystem(); InitializeSystem(); diff --git a/SDK/src/NDK/Systems/LifetimeSystem.cpp b/SDK/src/NDK/Systems/LifetimeSystem.cpp new file mode 100644 index 000000000..d575ebdc4 --- /dev/null +++ b/SDK/src/NDK/Systems/LifetimeSystem.cpp @@ -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 +#include + +namespace Ndk +{ + LifetimeSystem::LifetimeSystem() + { + Requires(); + } + + void LifetimeSystem::OnUpdate(float elapsedTime) + { + for (const Ndk::EntityHandle& entity : GetEntities()) + { + auto& lifetime = entity->GetComponent(); + + if (lifetime.UpdateLifetime(elapsedTime)) + entity->Kill(); + } + } + + SystemIndex LifetimeSystem::systemIndex; +} diff --git a/SDK/src/NDK/World.cpp b/SDK/src/NDK/World.cpp index bfbe0140c..579985297 100644 --- a/SDK/src/NDK/World.cpp +++ b/SDK/src/NDK/World.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -43,6 +44,7 @@ namespace Ndk void World::AddDefaultSystems() { + AddSystem(); AddSystem(); AddSystem(); AddSystem();