More Progress
This commit is contained in:
15
src/dawn/scene/CMakeLists.txt
Normal file
15
src/dawn/scene/CMakeLists.txt
Normal file
@ -0,0 +1,15 @@
|
||||
# Copyright (c) 2022 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
Scene.cpp
|
||||
SceneItem.cpp
|
||||
SceneItemComponent.cpp
|
||||
)
|
||||
|
||||
# Subdirs
|
||||
add_subdirectory(components)
|
44
src/dawn/scene/Scene.cpp
Normal file
44
src/dawn/scene/Scene.cpp
Normal file
@ -0,0 +1,44 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "Scene.hpp"
|
||||
#include "game/DawnGame.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
Scene::Scene(std::weak_ptr<DawnGame> game) {
|
||||
this->game = game;
|
||||
this->nextId = 0;
|
||||
}
|
||||
|
||||
void Scene::update() {
|
||||
// Finsh adding scene items that were trying to add from the last frame.
|
||||
auto it = this->itemsNotInitialized.begin();
|
||||
while(it != this->itemsNotInitialized.end()) {
|
||||
this->items[it->first] = it->second;
|
||||
it++;
|
||||
it->second->init();
|
||||
}
|
||||
this->itemsNotInitialized.clear();
|
||||
|
||||
// TODO: Cleanup old scene items
|
||||
|
||||
// TODO: Tick scene items(?)
|
||||
}
|
||||
|
||||
std::shared_ptr<SceneItem> Scene::createSceneItem() {
|
||||
sceneitemid_t id = this->nextId++;
|
||||
auto item = std::make_shared<SceneItem>(weak_from_this(), id);
|
||||
this->itemsNotInitialized[id] = item;
|
||||
return item;
|
||||
}
|
||||
|
||||
void Scene::removeSceneItem(SceneItem *item) {
|
||||
|
||||
}
|
||||
|
||||
Scene::~Scene() {
|
||||
|
||||
}
|
33
src/dawn/scene/Scene.hpp
Normal file
33
src/dawn/scene/Scene.hpp
Normal file
@ -0,0 +1,33 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "SceneItem.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class DawnGame;
|
||||
|
||||
class Scene :
|
||||
public std::enable_shared_from_this<Scene>
|
||||
{
|
||||
private:
|
||||
sceneitemid_t nextId;
|
||||
std::map<sceneitemid_t,std::shared_ptr<SceneItem>> items;
|
||||
std::map<sceneitemid_t,std::shared_ptr<SceneItem>> itemsNotInitialized;
|
||||
|
||||
public:
|
||||
std::weak_ptr<DawnGame> game;
|
||||
|
||||
Scene(std::weak_ptr<DawnGame> game);
|
||||
|
||||
void update();
|
||||
|
||||
std::shared_ptr<SceneItem> createSceneItem();
|
||||
|
||||
void removeSceneItem(SceneItem *item);
|
||||
|
||||
~Scene();
|
||||
};
|
||||
}
|
22
src/dawn/scene/SceneItem.cpp
Normal file
22
src/dawn/scene/SceneItem.cpp
Normal file
@ -0,0 +1,22 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "SceneItem.hpp"
|
||||
#include "Scene.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
SceneItem::SceneItem(std::weak_ptr<Scene>, sceneitemid_t id) {
|
||||
this->scene = scene;
|
||||
this->id = id;
|
||||
}
|
||||
|
||||
void SceneItem::init() {
|
||||
|
||||
}
|
||||
|
||||
SceneItem::~SceneItem() {
|
||||
|
||||
}
|
47
src/dawn/scene/SceneItem.hpp
Normal file
47
src/dawn/scene/SceneItem.hpp
Normal file
@ -0,0 +1,47 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "SceneItemComponent.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
typedef int32_t sceneitemid_t;
|
||||
|
||||
class Scene;
|
||||
|
||||
class SceneItem {
|
||||
private:
|
||||
std::vector<std::shared_ptr<SceneItemComponent>> components;
|
||||
|
||||
public:
|
||||
std::weak_ptr<Scene> scene;
|
||||
sceneitemid_t id;
|
||||
|
||||
SceneItem(std::weak_ptr<Scene> scene, sceneitemid_t id);
|
||||
|
||||
void init();
|
||||
|
||||
template<class T>
|
||||
std::shared_ptr<T> addComponent() {
|
||||
auto component = std::make_unique<T>(this);
|
||||
this->components.push_back(component);
|
||||
return component;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
std::shared_ptr<T> getComponent() {
|
||||
auto it = this->components.begin();
|
||||
while(it != this->components.end()) {
|
||||
std::shared_ptr<T> castedAs = dynamic_cast<std::shared_ptr<T>>(*it);
|
||||
if(castedAs != nullptr) return castedAs;
|
||||
it++;
|
||||
continue;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
~SceneItem();
|
||||
};
|
||||
}
|
17
src/dawn/scene/SceneItemComponent.cpp
Normal file
17
src/dawn/scene/SceneItemComponent.cpp
Normal file
@ -0,0 +1,17 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "SceneItemComponent.hpp"
|
||||
#include "SceneItem.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
SceneItemComponent::SceneItemComponent(std::weak_ptr<SceneItem> item) {
|
||||
this->item = item;
|
||||
}
|
||||
|
||||
SceneItemComponent::~SceneItemComponent() {
|
||||
|
||||
}
|
22
src/dawn/scene/SceneItemComponent.hpp
Normal file
22
src/dawn/scene/SceneItemComponent.hpp
Normal file
@ -0,0 +1,22 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "dawnlibs.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class SceneItem;
|
||||
|
||||
class SceneItemComponent {
|
||||
public:
|
||||
std::weak_ptr<SceneItem> item;
|
||||
|
||||
SceneItemComponent(std::weak_ptr<SceneItem> item);
|
||||
|
||||
virtual void start() = 0;
|
||||
|
||||
virtual ~SceneItemComponent();
|
||||
};
|
||||
}
|
10
src/dawn/scene/components/CMakeLists.txt
Normal file
10
src/dawn/scene/components/CMakeLists.txt
Normal file
@ -0,0 +1,10 @@
|
||||
# Copyright (c) 2022 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
DummyComponent.cpp
|
||||
)
|
12
src/dawn/scene/components/DummyComponent.cpp
Normal file
12
src/dawn/scene/components/DummyComponent.cpp
Normal file
@ -0,0 +1,12 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "DummyComponent.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
void DummyComponent::start() {
|
||||
std::cout << "Dummy Component Start" << std::endl;
|
||||
}
|
18
src/dawn/scene/components/DummyComponent.hpp
Normal file
18
src/dawn/scene/components/DummyComponent.hpp
Normal file
@ -0,0 +1,18 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "scene/SceneItemComponent.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class DummyComponent : public SceneItemComponent {
|
||||
public:
|
||||
DummyComponent(std::weak_ptr<SceneItem> item) : SceneItemComponent(item) {
|
||||
std::cout << "Dummy Component Initialized" << std::endl;
|
||||
};
|
||||
|
||||
void start() override;
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user