mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2024-12-26 15:33:34 +00:00
switch to unique_ptrs for callback timers
This commit is contained in:
parent
83065dfb6f
commit
451f7e76d7
@ -787,23 +787,21 @@ CppScripts::Script* Entity::GetScript() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Entity::Update(const float deltaTime) {
|
void Entity::Update(const float deltaTime) {
|
||||||
auto namedTimerItr = std::remove_if(m_Timers.begin(), m_Timers.end(), [this, &deltaTime](EntityTimer* timer) {
|
auto namedTimerItr = std::remove_if(m_Timers.begin(), m_Timers.end(), [this, &deltaTime](const std::unique_ptr<EntityTimer>& timer) {
|
||||||
timer->Update(deltaTime);
|
timer->Update(deltaTime);
|
||||||
if (timer->GetTime() <= 0) {
|
if (timer->GetTime() <= 0) {
|
||||||
GetScript()->OnTimerDone(this, timer->GetName());
|
GetScript()->OnTimerDone(this, timer->GetName());
|
||||||
TriggerEvent(eTriggerEventType::TIMER_DONE, this);
|
TriggerEvent(eTriggerEventType::TIMER_DONE, this);
|
||||||
delete timer;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
m_Timers.erase(namedTimerItr, m_Timers.end());
|
m_Timers.erase(namedTimerItr, m_Timers.end());
|
||||||
|
|
||||||
auto callbackTimerItr = std::remove_if(m_CallbackTimers.begin(), m_CallbackTimers.end(), [this, &deltaTime](EntityCallbackTimer* timer) {
|
auto callbackTimerItr = std::remove_if(m_CallbackTimers.begin(), m_CallbackTimers.end(), [this, &deltaTime](const std::unique_ptr<EntityCallbackTimer>& timer) {
|
||||||
timer->Update(deltaTime);
|
timer->Update(deltaTime);
|
||||||
if (timer->GetTime() <= 0) {
|
if (timer->GetTime() <= 0) {
|
||||||
timer->ExecuteCallback();
|
timer->ExecuteCallback();
|
||||||
delete timer;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -811,8 +809,12 @@ void Entity::Update(const float deltaTime) {
|
|||||||
m_CallbackTimers.erase(callbackTimerItr, m_CallbackTimers.end());
|
m_CallbackTimers.erase(callbackTimerItr, m_CallbackTimers.end());
|
||||||
|
|
||||||
// Add pending timers to the list of timers so they start next tick.
|
// Add pending timers to the list of timers so they start next tick.
|
||||||
if (m_PendingTimers.size() > 0) {
|
if (!m_PendingTimers.empty()) {
|
||||||
m_Timers.insert(m_Timers.end(), m_PendingTimers.begin(), m_PendingTimers.end());
|
// unique_ptrs are not CopyConstructible. Must move an rvalue.
|
||||||
|
this->m_Timers.reserve(m_Timers.size() + m_PendingTimers.size());
|
||||||
|
for (auto& timer : m_PendingTimers) {
|
||||||
|
this->m_Timers.push_back(std::move(timer));
|
||||||
|
}
|
||||||
m_PendingTimers.clear();
|
m_PendingTimers.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1163,24 +1165,21 @@ void Entity::RemoveParent() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Entity::AddTimer(const std::string& name, float time) {
|
void Entity::AddTimer(const std::string& name, float time) {
|
||||||
m_PendingTimers.push_back(new EntityTimer(name, time));
|
m_PendingTimers.emplace_back(std::make_unique<EntityTimer>(name, time));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Entity::AddCallbackTimer(const float time, const std::function<void()>& callback) {
|
void Entity::AddCallbackTimer(const float time, const std::function<void()>& callback) {
|
||||||
m_CallbackTimers.push_back(new EntityCallbackTimer(time, callback));
|
m_CallbackTimers.emplace_back(std::make_unique<EntityCallbackTimer>(time, callback));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Entity::HasTimer(const std::string& name) {
|
bool Entity::HasTimer(const std::string& name) {
|
||||||
auto possibleTimer = std::find_if(m_Timers.begin(), m_Timers.end(), [name](EntityTimer* timer) {
|
auto possibleTimer = std::find_if(m_Timers.begin(), m_Timers.end(), [name](const std::unique_ptr<EntityTimer>& timer) {
|
||||||
return timer->GetName() == name;
|
return timer->GetName() == name;
|
||||||
});
|
});
|
||||||
return possibleTimer != m_Timers.end();
|
return possibleTimer != m_Timers.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Entity::CancelCallbackTimers() {
|
void Entity::CancelCallbackTimers() {
|
||||||
std::for_each(m_CallbackTimers.begin(), m_CallbackTimers.end(), [](EntityCallbackTimer* timer) {
|
|
||||||
delete timer;
|
|
||||||
});
|
|
||||||
m_CallbackTimers.clear();
|
m_CallbackTimers.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1191,27 +1190,14 @@ void Entity::ScheduleKillAfterUpdate(Entity* murderer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Entity::CancelTimer(const std::string& name) {
|
void Entity::CancelTimer(const std::string& name) {
|
||||||
auto toErase = std::remove_if(m_Timers.begin(), m_Timers.end(), [&name](EntityTimer* timer) {
|
auto toErase = std::remove_if(m_Timers.begin(), m_Timers.end(), [&name](const std::unique_ptr<EntityTimer>& timer) {
|
||||||
if (timer->GetName() == name) {
|
return timer->GetName() == name;
|
||||||
delete timer;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
});
|
});
|
||||||
m_Timers.erase(m_Timers.begin(), toErase);
|
m_Timers.erase(m_Timers.begin(), toErase);
|
||||||
}
|
}
|
||||||
// ### LEFT OFF HERE ###
|
// ### LEFT OFF HERE ###
|
||||||
void Entity::CancelAllTimers() {
|
void Entity::CancelAllTimers() {
|
||||||
for (auto* timer : m_Timers) {
|
|
||||||
delete timer;
|
|
||||||
}
|
|
||||||
|
|
||||||
m_Timers.clear();
|
m_Timers.clear();
|
||||||
|
|
||||||
for (auto* callBackTimer : m_CallbackTimers) {
|
|
||||||
delete callBackTimer;
|
|
||||||
}
|
|
||||||
|
|
||||||
m_CallbackTimers.clear();
|
m_CallbackTimers.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -338,9 +338,9 @@ protected:
|
|||||||
std::vector<std::function<void(Entity*)>> m_PhantomCollisionCallbacks;
|
std::vector<std::function<void(Entity*)>> m_PhantomCollisionCallbacks;
|
||||||
|
|
||||||
std::unordered_map<eReplicaComponentType, ComponentPtr> m_Components;
|
std::unordered_map<eReplicaComponentType, ComponentPtr> m_Components;
|
||||||
std::vector<EntityTimer*> m_Timers;
|
std::vector<std::unique_ptr<EntityTimer>> m_Timers;
|
||||||
std::vector<EntityTimer*> m_PendingTimers;
|
std::vector<std::unique_ptr<EntityTimer>> m_PendingTimers;
|
||||||
std::vector<EntityCallbackTimer*> m_CallbackTimers;
|
std::vector<std::unique_ptr<EntityCallbackTimer>> m_CallbackTimers;
|
||||||
|
|
||||||
bool m_ShouldDestroyAfterUpdate;
|
bool m_ShouldDestroyAfterUpdate;
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "EntityCallbackTimer.h"
|
#include "EntityCallbackTimer.h"
|
||||||
|
|
||||||
EntityCallbackTimer::EntityCallbackTimer(float time, std::function<void()> callback) {
|
EntityCallbackTimer::EntityCallbackTimer(const float& time, const std::function<void()>& callback) {
|
||||||
m_Time = time;
|
m_Time = time;
|
||||||
m_Callback = callback;
|
m_Callback = callback;
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
class EntityCallbackTimer {
|
class EntityCallbackTimer {
|
||||||
public:
|
public:
|
||||||
EntityCallbackTimer(float time, std::function<void()> callback);
|
EntityCallbackTimer(const float& time, const std::function<void()>& callback);
|
||||||
~EntityCallbackTimer();
|
~EntityCallbackTimer();
|
||||||
|
|
||||||
void ExecuteCallback();
|
void ExecuteCallback();
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "EntityTimer.h"
|
#include "EntityTimer.h"
|
||||||
|
|
||||||
EntityTimer::EntityTimer(std::string name, float time) {
|
EntityTimer::EntityTimer(const std::string& name, const float& time) {
|
||||||
m_Name = name;
|
m_Name = name;
|
||||||
m_Time = time;
|
m_Time = time;
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
class EntityTimer {
|
class EntityTimer {
|
||||||
public:
|
public:
|
||||||
EntityTimer(std::string name, float time);
|
EntityTimer(const std::string& name, const float& time);
|
||||||
~EntityTimer();
|
~EntityTimer();
|
||||||
|
|
||||||
std::string GetName();
|
std::string GetName();
|
||||||
|
Loading…
Reference in New Issue
Block a user