Core: Add Lifetime component and system
This commit is contained in:
parent
a61ebb7806
commit
99b4397d78
|
|
@ -0,0 +1,34 @@
|
||||||
|
// this file was automatically generated and should not be edited
|
||||||
|
|
||||||
|
/*
|
||||||
|
Nazara Engine - Core module
|
||||||
|
|
||||||
|
Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
this software and associated documentation files (the "Software"), to deal in
|
||||||
|
the Software without restriction, including without limitation the rights to
|
||||||
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||||
|
of the Software, and to permit persons to whom the Software is furnished to do
|
||||||
|
so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifndef NAZARA_CORE_COMPONENTS_HPP
|
||||||
|
#define NAZARA_CORE_COMPONENTS_HPP
|
||||||
|
|
||||||
|
#include <Nazara/Core/Components/LifetimeComponent.hpp>
|
||||||
|
|
||||||
|
#endif // NAZARA_CORE_COMPONENTS_HPP
|
||||||
|
|
@ -0,0 +1,39 @@
|
||||||
|
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||||
|
// This file is part of the "Nazara Engine - Core module"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifndef NAZARA_CORE_COMPONENTS_LIFETIMECOMPONENT_HPP
|
||||||
|
#define NAZARA_CORE_COMPONENTS_LIFETIMECOMPONENT_HPP
|
||||||
|
|
||||||
|
#include <Nazara/Prerequisites.hpp>
|
||||||
|
#include <Nazara/Utility/Config.hpp>
|
||||||
|
|
||||||
|
namespace Nz
|
||||||
|
{
|
||||||
|
class LifetimeComponent
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
inline LifetimeComponent(float lifetime);
|
||||||
|
LifetimeComponent(const LifetimeComponent&) = default;
|
||||||
|
LifetimeComponent(LifetimeComponent&&) = default;
|
||||||
|
~LifetimeComponent() = default;
|
||||||
|
|
||||||
|
inline void DecreaseLifetime(float elapsedTime);
|
||||||
|
|
||||||
|
inline float GetRemainingLifeTime() const;
|
||||||
|
|
||||||
|
inline bool IsAlive() const;
|
||||||
|
|
||||||
|
LifetimeComponent& operator=(const LifetimeComponent&) = default;
|
||||||
|
LifetimeComponent& operator=(LifetimeComponent&&) = default;
|
||||||
|
|
||||||
|
private:
|
||||||
|
float m_remainingLifetime;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <Nazara/Core/Components/LifetimeComponent.inl>
|
||||||
|
|
||||||
|
#endif // NAZARA_CORE_COMPONENTS_LIFETIMECOMPONENT_HPP
|
||||||
|
|
@ -0,0 +1,31 @@
|
||||||
|
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||||
|
// This file is part of the "Nazara Engine - Core module"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
|
#include <Nazara/Core/Components/LifetimeComponent.hpp>
|
||||||
|
#include <Nazara/Core/Debug.hpp>
|
||||||
|
|
||||||
|
namespace Nz
|
||||||
|
{
|
||||||
|
inline LifetimeComponent::LifetimeComponent(float lifetime) :
|
||||||
|
m_remainingLifetime(lifetime)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void LifetimeComponent::DecreaseLifetime(float elapsedTime)
|
||||||
|
{
|
||||||
|
m_remainingLifetime -= elapsedTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline float LifetimeComponent::GetRemainingLifeTime() const
|
||||||
|
{
|
||||||
|
return m_remainingLifetime;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool LifetimeComponent::IsAlive() const
|
||||||
|
{
|
||||||
|
return m_remainingLifetime >= 0.f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <Nazara/Core/DebugOff.hpp>
|
||||||
|
|
@ -0,0 +1,34 @@
|
||||||
|
// this file was automatically generated and should not be edited
|
||||||
|
|
||||||
|
/*
|
||||||
|
Nazara Engine - Core module
|
||||||
|
|
||||||
|
Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
this software and associated documentation files (the "Software"), to deal in
|
||||||
|
the Software without restriction, including without limitation the rights to
|
||||||
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||||
|
of the Software, and to permit persons to whom the Software is furnished to do
|
||||||
|
so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifndef NAZARA_CORE_SYSTEMS_HPP
|
||||||
|
#define NAZARA_CORE_SYSTEMS_HPP
|
||||||
|
|
||||||
|
#include <Nazara/Core/Systems/LifetimeSystem.hpp>
|
||||||
|
|
||||||
|
#endif // NAZARA_CORE_SYSTEMS_HPP
|
||||||
|
|
@ -0,0 +1,41 @@
|
||||||
|
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||||
|
// This file is part of the "Nazara Engine - Core module"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifndef NAZARA_CORE_SYSTEMS_LIFETIMESYSTEM_HPP
|
||||||
|
#define NAZARA_CORE_SYSTEMS_LIFETIMESYSTEM_HPP
|
||||||
|
|
||||||
|
#include <Nazara/Prerequisites.hpp>
|
||||||
|
#include <Nazara/Core/Config.hpp>
|
||||||
|
#include <Nazara/Utils/TypeList.hpp>
|
||||||
|
#include <entt/entt.hpp>
|
||||||
|
|
||||||
|
namespace Nz
|
||||||
|
{
|
||||||
|
class NAZARA_CORE_API LifetimeSystem
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static constexpr bool AllowConcurrent = false;
|
||||||
|
static constexpr Int64 ExecutionOrder = 1'000'000;
|
||||||
|
using Components = TypeList<class LifetimeComponent>;
|
||||||
|
|
||||||
|
inline LifetimeSystem(entt::registry& registry);
|
||||||
|
LifetimeSystem(const LifetimeSystem&) = delete;
|
||||||
|
LifetimeSystem(LifetimeSystem&&) = delete;
|
||||||
|
~LifetimeSystem() = default;
|
||||||
|
|
||||||
|
void Update(float elapsedTime);
|
||||||
|
|
||||||
|
LifetimeSystem& operator=(const LifetimeSystem&) = delete;
|
||||||
|
LifetimeSystem& operator=(LifetimeSystem&&) = delete;
|
||||||
|
|
||||||
|
private:
|
||||||
|
entt::registry& m_registry;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <Nazara/Core/Systems/LifetimeSystem.inl>
|
||||||
|
|
||||||
|
#endif // NAZARA_CORE_SYSTEMS_LIFETIMESYSTEM_HPP
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||||
|
// This file is part of the "Nazara Engine - Core module"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
|
#include <Nazara/Core/Systems/LifetimeSystem.hpp>
|
||||||
|
#include <Nazara/Core/Debug.hpp>
|
||||||
|
|
||||||
|
namespace Nz
|
||||||
|
{
|
||||||
|
inline LifetimeSystem::LifetimeSystem(entt::registry& registry) :
|
||||||
|
m_registry(registry)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <Nazara/Core/DebugOff.hpp>
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||||
|
// This file is part of the "Nazara Engine - Core module"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
|
#include <Nazara/Core/Systems/LifetimeSystem.hpp>
|
||||||
|
#include <Nazara/Core/Components/LifetimeComponent.hpp>
|
||||||
|
#include <Nazara/Core/Debug.hpp>
|
||||||
|
|
||||||
|
namespace Nz
|
||||||
|
{
|
||||||
|
void LifetimeSystem::Update(float elapsedTime)
|
||||||
|
{
|
||||||
|
auto view = m_registry.view<LifetimeComponent>();
|
||||||
|
for (auto [entity, lifetimeComponent] : view.each())
|
||||||
|
{
|
||||||
|
lifetimeComponent.DecreaseLifetime(elapsedTime);
|
||||||
|
if (!lifetimeComponent.IsAlive())
|
||||||
|
m_registry.destroy(entity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue