Skip to content

Commit bd197db

Browse files
committed
feat: enhance Timer functionality and integrate with System class
1 parent 70c3add commit bd197db

File tree

4 files changed

+85
-4
lines changed

4 files changed

+85
-4
lines changed

include/libecs-cpp/System.hpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,44 @@
22

33
#include <string>
44
#include <queue>
5+
#include <functional>
56
#include <libecs-cpp/Timing.hpp>
67

78
namespace ecs
89
{
910
class Container;
1011

12+
class Timer
13+
{
14+
public:
15+
Timer(std::string name,
16+
std::function<void()> callback,
17+
uint32_t interval = 30 /* seconds */,
18+
bool repeat = true)
19+
: Repeat(repeat)
20+
{
21+
this->Name = name;
22+
this->callback = callback;
23+
this->timing.SetFrequency(1000000 * interval); // Convert seconds to microseconds
24+
}
25+
26+
bool CallbackRun()
27+
{
28+
if (this->timing.ShouldUpdate() && this->callback)
29+
{
30+
this->callback();
31+
return true;
32+
}
33+
return false;
34+
}
35+
std::string Name;
36+
bool Repeat;
37+
38+
private:
39+
ecs::Timing timing;
40+
std::function<void()> callback = nullptr;
41+
};
42+
1143
class System
1244
{
1345
public:
@@ -16,6 +48,7 @@ namespace ecs
1648
virtual void Initialize();
1749
virtual void Configure(nlohmann::json config);
1850
virtual void Update() {};
51+
void UpdateSystem();
1952
std::string Handle;
2053
ecs::Container *Container = nullptr;
2154
void MessageSubmit(nlohmann::json);
@@ -24,10 +57,13 @@ namespace ecs
2457
ecs::Timing Timing;
2558
const size_t MessagesWaiting();
2659
const uint32_t DeltaTimeGet();
60+
void TimerClear(std::string name);
61+
void TimerAdd(Timer timer);
2762
protected:
2863
std::queue<nlohmann::json> messages;
2964
std::chrono::steady_clock::time_point LastTime = std::chrono::steady_clock::now();
3065
std::unordered_map<std::string, std::vector<std::string>> ComponentsToDelete;
3166
void ComponentsClear();
67+
std::vector<ecs::Timer> Timers;
3268
};
3369
}

src/Container.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ namespace ecs
114114
for(auto &Handle : this->SystemsGet())
115115
{
116116
if(this->Systems[Handle]->Timing.ShouldUpdate())
117-
this->Systems[Handle]->Update();
117+
this->Systems[Handle]->UpdateSystem();
118118
}
119119
}
120120

src/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pkginclude_HEADERS = \
1212
../include/libecs-cpp/uuid_v4.h \
1313
../include/libecs-cpp/endianness.h \
1414
../include/libecs-cpp/json.hpp \
15-
../include/libecs-cpp/Timing.hpp
15+
../include/libecs-cpp/Timing.hpp
1616

1717
if BUILTIN_UUID
1818
libecs_cpp_la_CXXFLAGS = -std=c++20 -I../include -Werror -Wall -Wno-psabi -mssse3 -mavx2

src/System.cpp

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,42 @@
11
#include <libecs-cpp/Component.hpp>
22
#include <libecs-cpp/ecs.hpp>
3+
#include <iostream>
4+
#include <vector>
35

46
namespace ecs
57
{
68
System::System():
7-
Handle(ecs::Uuid().Get()) {}
9+
Handle(ecs::Uuid().Get())
10+
{
11+
}
812

913
System::System(std::string Handle):
10-
Handle(Handle) {}
14+
Handle(Handle)
15+
{
16+
}
1117

1218
void System::Initialize() {}
1319

20+
void System::UpdateSystem()
21+
{
22+
std::vector<std::string> timersToRemove;
23+
for(auto &timer : this->Timers)
24+
{
25+
if(timer.CallbackRun() && !timer.Repeat)
26+
{
27+
timersToRemove.push_back(timer.Name);
28+
}
29+
}
30+
31+
// Remove timers after iteration to avoid modifying container during iteration
32+
for(const auto &timerName : timersToRemove)
33+
{
34+
this->TimerClear(timerName);
35+
}
36+
37+
this->Update();
38+
}
39+
1440
void System::Configure(nlohmann::json config) {}
1541

1642
void System::MessageSubmit(nlohmann::json message)
@@ -33,6 +59,12 @@ namespace ecs
3359

3460
void System::ComponentsClear()
3561
{
62+
if (!this->Container)
63+
{
64+
std::cout << "Warning: Container is null in ComponentsClear()" << std::endl;
65+
return;
66+
}
67+
3668
for(auto &[type, entities] : this->ComponentsToDelete)
3769
{
3870
for(const auto &entity : entities)
@@ -42,4 +74,17 @@ namespace ecs
4274
}
4375
this->ComponentsToDelete.clear();
4476
}
77+
78+
void System::TimerClear(std::string name)
79+
{
80+
this->Timers.erase(
81+
std::remove_if(this->Timers.begin(), this->Timers.end(),
82+
[&name](const Timer &timer) { return timer.Name == name; }),
83+
this->Timers.end());
84+
}
85+
86+
void System::TimerAdd(Timer timer)
87+
{
88+
this->Timers.push_back(timer);
89+
}
4590
}

0 commit comments

Comments
 (0)