Skip to content

Commit

Permalink
Im the process of moving all time-related stuff to callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
L3nn0x committed Jul 15, 2018
1 parent 71fe2c7 commit 632f3e8
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 78 deletions.
4 changes: 2 additions & 2 deletions src/map/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ SET( SOURCES_HEADER
include/systems/inventorysystem.h
include/systems/partysystem.h
include/systems/mapsystem.h
include/systems/spawnersystem.h
include/systems/callbacksystem.h
)

SET( SOURCES_MAIN
Expand All @@ -47,7 +47,7 @@ SET( SOURCES_MAIN
src/systems/inventorysystem.cpp
src/systems/partysystem.cpp
src/systems/mapsystem.cpp
src/systems/spawnersystem.cpp
src/systems/callbacksystem.cpp
)

add_executable(${PROJECT} ${SOURCES_MAIN} ${SOURCES_HEADER})
Expand Down
22 changes: 22 additions & 0 deletions src/map/include/systems/callbacksystem.h
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
25 changes: 0 additions & 25 deletions src/map/include/systems/luasystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,8 @@ class LuaSystem : public System {

virtual void update(EntityManager&, std::chrono::milliseconds) override {}

/*void unregisterEntity(Entity e) {
auto it = std::find(callbacks_.begin(), callbacks_.end(), Callback{e});
if (it != callbacks_.end())
callbacks_.erase(it);
}
virtual void update(EntityManager&, double dt) {
for (auto &it : callbacks_) {
it.dt += dt;
if (it.dt >= it.timeout) {
it.dt = 0.f;
}
}
}*/

private:
sol::state state_;

/*struct Callback {
Entity e;
std::string name{};
double timeout = 0;
double dt = 0;
bool operator==(const Callback& c) const { return e == c.e; }
};
std::vector<Callback> callbacks_;*/
};

}
15 changes: 0 additions & 15 deletions src/map/include/systems/spawnersystem.h

This file was deleted.

16 changes: 13 additions & 3 deletions src/map/src/entitysystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "systems/movementsystem.h"
#include "systems/partysystem.h"
#include "systems/updatesystem.h"
#include "systems/spawnersystem.h"
#include "systems/callbacksystem.h"

#include "srv_npcchar.h"

Expand All @@ -26,7 +26,7 @@ EntitySystem::EntitySystem(CMapServer *server) : systemManager_(*this), server_(
systemManager_.add<Systems::PartySystem>();
systemManager_.add<Systems::MapSystem>();
systemManager_.add<Systems::LuaSystem>();
systemManager_.add<Systems::SpawnerSystem>();
systemManager_.add<Systems::CallbackSystem>();
}

EntityManager& EntitySystem::getEntityManager() { return entityManager_; }
Expand Down Expand Up @@ -333,7 +333,17 @@ Entity EntitySystem::create_spawner(std::string alias, int mob_id, int mob_count
e.assign<BasicInfo>(es.id_manager_.get_free_id());
auto pos = e.assign<Position>(x * 100, y * 100, map_id, 0);
pos->z_ = static_cast<uint16_t>(z);
e.assign<Spawner>(mob_id, mob_count, spawner_limit, std::chrono::seconds(spawner_interval), spawner_range * 100);
e.assign<Spawner>(mob_id, mob_count, spawner_limit, spawner_range * 100);
Systems::CallbackSystem::add_callback(e, std::chrono::seconds(spawner_interval), [e](SystemManager& manager) mutable {
auto spawner = e.component<Spawner>();
if (spawner->current_total_ < spawner->total_on_map_) {
Entity mob = manager.buildMob(e);
manager.send(e, CMapServer::eSendType::NEARBY,
makePacket<ePacketType::PAKWC_MOB_CHAR>(mob));
++spawner->current_total_;
}
return true;
});
})};
std::lock_guard<std::mutex> lock(access_);
create_commands_.emplace_back(std::move(ptr));
Expand Down
30 changes: 30 additions & 0 deletions src/map/src/systems/callbacksystem.cpp
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>();
}
}
}
22 changes: 0 additions & 22 deletions src/map/src/systems/spawnersystem.cpp

This file was deleted.

25 changes: 25 additions & 0 deletions src/rosecommon/include/components/callbacks.h
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_;
};
11 changes: 1 addition & 10 deletions src/rosecommon/include/components/spawner.h
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_;
};
3 changes: 2 additions & 1 deletion src/rosecommon/include/entitycomponents.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "components/npc.h"
#include "components/warpgate.h"
#include "components/spawner.h"
#include "components/callbacks.h"

#include "entityapi.h"
#include "item.h"
Expand All @@ -55,7 +56,7 @@ using EntityManager = entityx::EntityX<entityx::DefaultStorage, entityx::Feature
CharacterGraphics, Position, StatusEffects, Skills, Hotbar, Destination,
RidingItems, BulletItems, Inventory, Party, Wishlist, Quests,
RoseCommon::EntityAPI, RoseCommon::Item, Npc, Warpgate,
Spawner>;
Spawner, Callback>;

using Entity = EntityManager::Entity;

Expand Down

0 comments on commit 632f3e8

Please sign in to comment.