#pragma once #include "../manager/ComponentManager.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(Args &&... args) { Log::logf(std::forward(args)...); } template void Script::subscribe_internal(const EventHandler & callback, event_channel_t channel) { Mediator & mediator = this->mediator; EventManager & mgr = mediator.event_manager; subscription_t listener = mgr.subscribe( [this, callback](const EventType & data) -> bool { bool & active = this->active; if (!active) return false; 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 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