// Copyright (c) 2023 Dominic Masters // // This software is released under the MIT License. // https://opensource.org/licenses/MIT #include "assert/assert.hpp" #include "util/Flag.hpp" #include "game/Game.hpp" #include "scene/Scene.hpp" using namespace Dawn; void SceneComponent::init(const std::shared_ptr item) { assertFlagOff( state, SceneComponentState::INITIALIZED, "Component already initialized before" ); Flag::turnOn(state, SceneComponentState::INITIALIZED); this->events.clear(); this->item = item; this->onInit(); } void SceneComponent::dispose() { assertFlagOn( state, SceneComponentState::INITIALIZED, "Component not initialized" ); Flag::turnOn(state, SceneComponentState::DISPOSED); for(auto &event : this->events) { event(); } this->events.clear(); this->onDispose(); this->item.reset(); } bool_t SceneComponent::isInitialized() { return Flag::isOn(state, SceneComponentState::INITIALIZED); } std::shared_ptr SceneComponent::getItem() { auto item = this->item.lock(); assertNotNull(item, "SceneItem has unloaded?"); return item; } std::shared_ptr SceneComponent::getScene() { return this->getItem()->getScene(); } std::shared_ptr SceneComponent::getGame() { return this->getScene()->getGame(); } void SceneComponent::load(const SceneComponentLoadContext &context) { // Override this method to load data from a JSON object. } SceneComponent::~SceneComponent() { if(Flag::isOn(state, SceneComponentState::INITIALIZED)) { assertFlagOn( state, SceneComponentState::DISPOSED, "Component not disposed before destruction" ); } }