-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Im the process of moving all time-related stuff to callbacks
- Loading branch information
L3nn0x
committed
Jul 15, 2018
1 parent
71fe2c7
commit 632f3e8
Showing
10 changed files
with
95 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#pragma once | ||
|
||
#include "system.h" | ||
|
||
namespace Systems { | ||
|
||
class CallbackSystem : public System { | ||
public: | ||
CallbackSystem(SystemManager &manager); | ||
virtual ~CallbackSystem() = default; | ||
|
||
virtual void update(EntityManager &es, std::chrono::milliseconds dt) override; | ||
|
||
template <typename Func> | ||
static void add_callback(Entity e, std::chrono::milliseconds time_to_update, Func&& callback) { | ||
if (!e) return; | ||
if (!e.component<Callback>()) e.assign<Callback>(); | ||
e.component<Callback>()->callbacks_.emplace_back(std::forward<Func>(callback), time_to_update); | ||
} | ||
}; | ||
|
||
} // namespace Systems |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include "systems/callbacksystem.h" | ||
#include <stack> | ||
|
||
using namespace Systems; | ||
using namespace RoseCommon; | ||
|
||
CallbackSystem::CallbackSystem(SystemManager &manager) : System(manager) {} | ||
|
||
void CallbackSystem::update(EntityManager &es, std::chrono::milliseconds dt) { | ||
for (Entity e : es.entities_with_components<Callback>()) { | ||
auto call = e.component<Callback>(); | ||
std::stack<std::vector<CallbackImp>::const_iterator> to_delete; | ||
for (auto it = call->callbacks_.begin(); it != call->callbacks_.end(); ++it) { | ||
it->time_elapsed_ += dt; | ||
if (it->time_elapsed_ >= it->time_to_update_) { | ||
if (!(it->callback_)(manager_)) | ||
to_delete.push(it); | ||
else | ||
it->time_elapsed_ = 0ms; | ||
} | ||
} | ||
while (to_delete.size()) { | ||
call->callbacks_.erase(to_delete.top()); | ||
to_delete.pop(); | ||
} | ||
if (!call->callbacks_.size()) { | ||
e.remove<Callback>(); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
#include <chrono> | ||
#include <functional> | ||
|
||
namespace { | ||
using namespace std::chrono_literals; | ||
} | ||
|
||
class SystemManager; | ||
|
||
struct CallbackImp { | ||
CallbackImp(std::function<bool(SystemManager&)>&& callback, std::chrono::milliseconds update): | ||
callback_(std::move(callback)), | ||
time_to_update_(update) {} | ||
|
||
std::function<bool(SystemManager&)> callback_; | ||
std::chrono::milliseconds time_to_update_; | ||
std::chrono::milliseconds time_elapsed_ = 0ms; | ||
}; | ||
|
||
struct Callback { | ||
std::vector<CallbackImp> callbacks_; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,16 @@ | ||
#pragma once | ||
|
||
#include <chrono> | ||
|
||
namespace { // try to avoid polluting the global namespace | ||
using namespace std::chrono_literals; | ||
} | ||
|
||
struct Spawner { | ||
Spawner(int mob_id, int mob_count, int spawner_limit, std::chrono::seconds spawner_interval, int spawner_range) : | ||
Spawner(int mob_id, int mob_count, int spawner_limit, int spawner_range) : | ||
mob_id_(mob_id), | ||
total_on_map_(mob_count), | ||
total_once_(spawner_limit), | ||
interval_(spawner_interval), | ||
range_(spawner_range) | ||
{} | ||
|
||
int mob_id_; | ||
int total_on_map_; | ||
int total_once_; | ||
int current_total_ = 0; | ||
std::chrono::seconds interval_; | ||
std::chrono::milliseconds until_update_ = 0ms; | ||
int range_; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters