89 lines
2.0 KiB
C++
89 lines
2.0 KiB
C++
// 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;
|
|
|
|
uint64_t SCENE_COMPONENTS_ACTIVE = 0;
|
|
|
|
void SceneComponent::init(const std::shared_ptr<SceneItem> item) {
|
|
SCENE_COMPONENTS_ACTIVE++;
|
|
|
|
assertFlagOff(
|
|
sceneComponentState,
|
|
SCENE_COMPONENT_STATE_INIT,
|
|
"SceneComponent is already initialized!"
|
|
);
|
|
Flag::turnOn<uint_fast8_t>(
|
|
sceneComponentState,
|
|
SCENE_COMPONENT_STATE_INIT
|
|
);
|
|
this->item = item;
|
|
this->onInit();
|
|
}
|
|
|
|
void SceneComponent::dispose() {
|
|
assertFlagOn(
|
|
sceneComponentState,
|
|
SCENE_COMPONENT_STATE_INIT,
|
|
"SceneComponent is not initialized!"
|
|
);
|
|
assertFlagOff(
|
|
sceneComponentState,
|
|
SCENE_COMPONENT_STATE_DISPOSED,
|
|
"SceneComponent is already disposed!"
|
|
);
|
|
Flag::turnOn<uint_fast8_t>(
|
|
sceneComponentState,
|
|
SCENE_COMPONENT_STATE_DISPOSED
|
|
);
|
|
|
|
// Clear Listeners
|
|
for(auto &listener : this->listeners) listener();
|
|
this->listeners.clear();
|
|
|
|
this->onDispose();
|
|
this->item.reset();
|
|
}
|
|
|
|
std::shared_ptr<SceneItem> SceneComponent::getItem() {
|
|
auto item = this->item.lock();
|
|
assertNotNull(item, "Item cannot be null?");
|
|
return item;
|
|
}
|
|
|
|
std::shared_ptr<Scene> SceneComponent::getScene() {
|
|
auto item = this->getItem();
|
|
auto scene = item->getScene();
|
|
assertNotNull(scene, "Scene cannot be null?");
|
|
return scene;
|
|
}
|
|
|
|
std::shared_ptr<Game> SceneComponent::getGame() {
|
|
auto scene = this->getScene();
|
|
auto game = scene->getGame();
|
|
assertNotNull(game, "Game cannot be null?");
|
|
return game;
|
|
}
|
|
|
|
SceneComponent::~SceneComponent() {
|
|
if(Flag::isOn<uint_fast8_t>(
|
|
sceneComponentState,
|
|
SCENE_COMPONENT_STATE_INIT
|
|
)) {
|
|
assertFlagOn(
|
|
sceneComponentState,
|
|
SCENE_COMPONENT_STATE_DISPOSED,
|
|
"SceneComponent is initialized but was not properly disposed!"
|
|
);
|
|
}
|
|
|
|
this->item.reset();
|
|
SCENE_COMPONENTS_ACTIVE--;
|
|
} |