#pragma once #include "../manager/ComponentManager.h" #include "../manager/ReplayManager.h" #include "BehaviorScript.h" #include "Script.h" namespace crepe { template T & Script::get_component() const { using namespace std; RefVector all_components = this->get_components(); if (all_components.size() < 1) throw runtime_error( format("Script: no component found with type = {}", typeid(T).name())); return all_components.back().get(); } template RefVector Script::get_components() const { return this->get_components_by_id(this->game_object_id); } template void Script::logf(const Log::Level & level, std::format_string fmt, Args &&... args) { Log::logf(level, fmt, std::forward(args)...); } template void Script::logf(std::format_string fmt, Args &&... args) { Log::logf(fmt, std::forward(args)...); } template void Script::subscribe_internal(const EventHandler & callback, event_channel_t channel) { EventManager & mgr = this->mediator->event_manager; subscription_t listener = mgr.subscribe( [this, callback](const EventType & data) -> bool { // check if (parent) BehaviorScript component is active bool & active = this->active; if (!active) return false; // check if replay manager is playing (if initialized) try { ReplayManager & replay = this->mediator->replay_manager; if (replay.get_state() == ReplayManager::PLAYING) return false; } catch (const std::runtime_error &) {} // call user-provided callback return callback(data); }, channel); this->listeners.push_back(listener); } template void Script::subscribe(const EventHandler & callback, event_channel_t channel) { this->subscribe_internal(callback, channel); } template void Script::subscribe(const EventHandler & callback) { this->subscribe_internal(callback, EventManager::CHANNEL_ALL); } template void Script::trigger_event(const EventType & event, event_channel_t channel) { EventManager & mgr = this->mediator->event_manager; mgr.trigger_event(event, channel); } template void Script::queue_event(const EventType & event, event_channel_t channel) { EventManager & mgr = this->mediator->event_manager; mgr.queue_event(event, channel); } template RefVector Script::get_components_by_id(game_object_id_t id) const { Mediator & mediator = this->mediator; ComponentManager & mgr = mediator.component_manager; return mgr.get_components_by_id(id); } template RefVector Script::get_components_by_name(const std::string & name) const { Mediator & mediator = this->mediator; ComponentManager & mgr = mediator.component_manager; return mgr.get_components_by_name(name); } template RefVector Script::get_components_by_tag(const std::string & tag) const { Mediator & mediator = this->mediator; ComponentManager & mgr = mediator.component_manager; return mgr.get_components_by_tag(tag); } } // namespace crepe