iw5-mod/src/game/scripting/entity.cpp
2019-01-20 22:26:18 +01:00

83 lines
2.2 KiB
C++

#include "std_include.hpp"
#include "context.hpp"
namespace game
{
namespace scripting
{
entity::entity() : entity(nullptr, 0)
{
}
entity::entity(const entity& other) : entity(other.context_, other.entity_id_)
{
}
entity::entity(context* context, const unsigned int entity_id) : context_(context), entity_id_(entity_id)
{
if (this->entity_id_)
{
native::VariableValue value{};
value.type = native::SCRIPT_OBJECT;
value.u.entityId = this->entity_id_;
native::AddRefToValue(&value);
}
}
entity::~entity()
{
if (this->entity_id_)
{
native::RemoveRefToValue(native::SCRIPT_OBJECT, {static_cast<int>(this->entity_id_)});
}
}
event_listener_handle entity::on_notify(const std::string& event,
const std::function<void(const std::vector<chaiscript::Boxed_Value>&)>&
callback,
const bool is_volatile)
const
{
event_listener listener;
listener.event = event;
listener.callback = callback;
listener.entity_id = this->entity_id_;
listener.is_volatile = is_volatile;
return this->context_->get_event_handler()->add_event_listener(listener);
}
unsigned int entity::get_entity_id() const
{
return this->entity_id_;
}
native::scr_entref_t entity::get_entity_reference() const
{
return game::native::Scr_GetEntityIdRef(this->get_entity_id());
}
chaiscript::Boxed_Value entity::call(const std::string& function,
const std::vector<chaiscript::Boxed_Value>& arguments) const
{
return this->context_->get_executer()->call(function, this->get_entity_id(), arguments);
}
void entity::notify(const std::string& event,
const std::vector<chaiscript::Boxed_Value>& arguments) const
{
this->context_->get_executer()->notify(event, this->get_entity_id(), arguments);
}
void entity::set(const std::string& field, const chaiscript::Boxed_Value& value) const
{
this->context_->get_executer()->set_entity_field(field, this->get_entity_id(), value);
}
chaiscript::Boxed_Value entity::get(const std::string& field) const
{
return this->context_->get_executer()->get_entity_field(field, this->get_entity_id());
}
}
}