More Progress

This commit is contained in:
2022-10-18 15:23:50 -07:00
parent 0ec2809d88
commit 6f10535962
45 changed files with 7994 additions and 0 deletions

View 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
View 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
View 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();
};
}

View 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() {
}

View 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();
};
}

View 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() {
}

View 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();
};
}

View 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
)

View 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;
}

View 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;
};
}