2019-01-20 13:21:43 +01:00
|
|
|
#pragma once
|
2019-01-20 22:26:18 +01:00
|
|
|
#include "utils/concurrent_list.hpp"
|
2019-01-20 13:21:43 +01:00
|
|
|
#include "entity.hpp"
|
|
|
|
#include "event.hpp"
|
|
|
|
|
|
|
|
namespace game
|
|
|
|
{
|
|
|
|
namespace scripting
|
|
|
|
{
|
|
|
|
class context;
|
|
|
|
|
2019-01-20 22:26:18 +01:00
|
|
|
class event_listener_handle
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
unsigned long long id = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class event_listener final : public event_listener_handle
|
2019-01-20 13:21:43 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
std::string event = {};
|
|
|
|
unsigned int entity_id = 0;
|
|
|
|
std::function<void(const std::vector<chaiscript::Boxed_Value>&)> callback = {};
|
|
|
|
bool is_volatile = false;
|
|
|
|
};
|
|
|
|
|
2019-01-20 22:26:18 +01:00
|
|
|
class generic_event_listener final : public event_listener_handle
|
2019-01-20 13:21:43 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
std::string event = {};
|
|
|
|
std::function<void(const entity&, const std::vector<chaiscript::Boxed_Value>&)> callback = {};
|
|
|
|
bool is_volatile = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
class event_handler final
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit event_handler(context* context);
|
|
|
|
|
|
|
|
void dispatch(event* event);
|
|
|
|
|
2019-01-20 22:26:18 +01:00
|
|
|
event_listener_handle add_event_listener(event_listener listener);
|
|
|
|
event_listener_handle add_event_listener(generic_event_listener listener);
|
2019-01-20 13:21:43 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
context* context_;
|
2019-01-20 22:26:18 +01:00
|
|
|
std::atomic_int64_t current_listener_id_ = 0;
|
|
|
|
|
|
|
|
utils::concurrent_list<event_listener> event_listeners_;
|
|
|
|
utils::concurrent_list<generic_event_listener> generic_event_listeners_;
|
2019-01-20 13:21:43 +01:00
|
|
|
|
|
|
|
void dispatch_to_specific_listeners(event* event, const std::vector<chaiscript::Boxed_Value>& arguments);
|
|
|
|
void dispatch_to_generic_listeners(event* event, const std::vector<chaiscript::Boxed_Value>& arguments);
|
|
|
|
|
2019-01-20 22:26:18 +01:00
|
|
|
void remove(const event_listener_handle& handle);
|
2019-01-20 13:21:43 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|