Scene system roughly how I like

This commit is contained in:
2024-07-11 08:43:50 -05:00
parent 50a0847f53
commit c916e1a6cf
10 changed files with 123 additions and 14 deletions

View File

@ -14,6 +14,7 @@ Game::Game() {
} }
void Game::init() { void Game::init() {
currentScene = std::make_shared<Scene>(weak_from_this()); currentScene = std::make_shared<Scene>(weak_from_this());
auto myItem = currentScene->addSceneItem<SceneItem2D>("myItem"); auto myItem = currentScene->addSceneItem<SceneItem2D>("myItem");
} }
@ -22,6 +23,23 @@ void Game::update() {
} }
std::shared_ptr<Scene> Game::getCurrentScene() {
return currentScene;
}
void Game::setActiveScene(std::shared_ptr<Scene> scene) {
auto previousCurrent = currentScene;
if(previousCurrent != nullptr) {
previousCurrent->inactive();
}
currentScene = scene;
if(currentScene != nullptr) {
currentScene->active();
}
}
Game::~Game() { Game::~Game() {
} }

View File

@ -31,6 +31,20 @@ namespace Dawn {
*/ */
void update(); void update();
/**
* Returns the current active scene.
*
* @return The current active scene.
*/
std::shared_ptr<Scene> getCurrentScene();
/**
* Sets the active scene for the game.
*
* @param scene The scene to set as active.
*/
void setActiveScene(std::shared_ptr<Scene> scene);
/** /**
* Deloads game and all sub managers used by game. * Deloads game and all sub managers used by game.
*/ */

View File

@ -7,10 +7,27 @@
using namespace Dawn; using namespace Dawn;
Scene::Scene(std::weak_ptr<Game> game) : SceneItem(std::weak_ptr<SceneItem>()) { Scene::Scene(std::weak_ptr<Game> game) : SceneItem(
std::weak_ptr<SceneItem>(),
std::enable_shared_from_this<Scene>::weak_from_this()
) {
this->game = game; this->game = game;
} }
void Scene::active() {
auto itItems = items.begin();
}
void Scene::inactive() {
}
bool_t Scene::isActiveScene() {
auto g = game.lock();
if(g == nullptr) return false;
return g->getCurrentScene() == this->getScene();
}
Scene::~Scene() { Scene::~Scene() {
} }

View File

@ -8,10 +8,22 @@
#include "game/Game.hpp" #include "game/Game.hpp"
namespace Dawn { namespace Dawn {
class Scene : public SceneItem { class Scene : public SceneItem, public std::enable_shared_from_this<Scene> {
private: private:
std::weak_ptr<Game> game; std::weak_ptr<Game> game;
/**
* Invoked by the game whenever this scene has been set as the active
* scene.
*/
void active();
/**
* Invoked by the game whenever this scene has been set as the inactive
* scene.
*/
void inactive();
public: public:
/** /**
* Creates a new Scene. * Creates a new Scene.
@ -20,9 +32,18 @@ namespace Dawn {
*/ */
Scene(std::weak_ptr<Game> game); Scene(std::weak_ptr<Game> game);
/**
* Returns true if this scene is the active scene.
*
* @return True if this scene is the active scene.
*/
bool_t isActiveScene();
/** /**
* Destroys this scene and all of its items. * Destroys this scene and all of its items.
*/ */
virtual ~Scene(); virtual ~Scene();
friend class Game;
}; };
} }

View File

@ -8,17 +8,22 @@
using namespace Dawn; using namespace Dawn;
SceneItem::SceneItem( SceneItem::SceneItem(
std::weak_ptr<SceneItem> parent std::weak_ptr<SceneItem> parent,
std::weak_ptr<Scene> scene
) : ) :
parent(parent) parent(parent),
scene(scene)
{ {
} }
std::shared_ptr<SceneItem> SceneItem::getParent() { std::shared_ptr<SceneItem> SceneItem::getParent() {
return parent.lock(); return parent.lock();
} }
std::shared_ptr<Scene> SceneItem::getScene() {
return scene.lock();
}
void SceneItem::removeSceneItem(std::shared_ptr<SceneItem> item) { void SceneItem::removeSceneItem(std::shared_ptr<SceneItem> item) {
// I don't need to go through the trouble of removing items if I'm already // I don't need to go through the trouble of removing items if I'm already
// disposing, since I'll be clearing them all anyway. // disposing, since I'll be clearing them all anyway.

View File

@ -5,23 +5,35 @@
#pragma once #pragma once
#include "dawn.hpp" #include "dawn.hpp"
#include "event/Event.hpp"
namespace Dawn { namespace Dawn {
class Scene;
class SceneItem : public std::enable_shared_from_this<SceneItem> { class SceneItem : public std::enable_shared_from_this<SceneItem> {
private: private:
std::weak_ptr<SceneItem> parent; std::weak_ptr<SceneItem> parent;
std::vector<std::shared_ptr<SceneItem>> items; std::weak_ptr<Scene> scene;
bool_t isDisposing = false; bool_t isDisposing = false;
protected:
std::vector<std::shared_ptr<SceneItem>> items;
public: public:
std::string name; std::string name;
Event<> onEnter;
Event<> onLeave;
/** /**
* Creates a new SceneItem. * Creates a new SceneItem.
* *
* @param parent The parent SceneItem, or nullptr if root (unlikely). * @param parent The parent SceneItem, or nullptr if root (unlikely).
* @param scene The Scene this SceneItem belongs to.
*/ */
SceneItem(std::weak_ptr<SceneItem> parent); SceneItem(
std::weak_ptr<SceneItem> parent,
std::weak_ptr<Scene> scene
);
/** /**
* Adds a SceneItem as a child of this SceneItem. * Adds a SceneItem as a child of this SceneItem.
@ -31,13 +43,14 @@ namespace Dawn {
*/ */
template<typename C> template<typename C>
std::shared_ptr<C> addSceneItem(const std::string &name = "") { std::shared_ptr<C> addSceneItem(const std::string &name = "") {
auto item = std::make_shared<C>(weak_from_this()); auto item = std::make_shared<C>(weak_from_this(), scene);
auto asItem = std::dynamic_pointer_cast<SceneItem>(item); auto asItem = std::dynamic_pointer_cast<SceneItem>(item);
if(asItem == nullptr) { if(asItem == nullptr) {
throw std::runtime_error("SceneItem must be a SceneItem."); throw std::runtime_error("SceneItem must be a SceneItem.");
} }
asItem->name = name; asItem->name = name;
items.push_back(asItem); items.push_back(asItem);
return item; return item;
} }
@ -80,9 +93,18 @@ namespace Dawn {
*/ */
std::shared_ptr<SceneItem> getParent(); std::shared_ptr<SceneItem> getParent();
/**
* Gets the Scene this SceneItem belongs to.
*
* @return The Scene this SceneItem belongs to.
*/
std::shared_ptr<Scene> getScene();
/** /**
* Destroys this SceneItem. * Destroys this SceneItem.
*/ */
virtual ~SceneItem(); virtual ~SceneItem();
friend class Scene;
}; };
} }

View File

@ -7,8 +7,11 @@
using namespace Dawn; using namespace Dawn;
SceneItem2D::SceneItem2D(std::weak_ptr<SceneItem> parent) : SceneItem2D::SceneItem2D(
SceneItem(parent) std::weak_ptr<SceneItem> parent,
std::weak_ptr<Scene> scene
) :
SceneItem(parent, scene)
{ {
} }

View File

@ -20,7 +20,10 @@ namespace Dawn {
* *
* @param parent The parent item. * @param parent The parent item.
*/ */
SceneItem2D(std::weak_ptr<SceneItem> parent); SceneItem2D(
std::weak_ptr<SceneItem> parent,
std::weak_ptr<Scene> scene
);
/** /**
* Destroys this SceneItem2D. * Destroys this SceneItem2D.

View File

@ -7,8 +7,11 @@
using namespace Dawn; using namespace Dawn;
Rectangle::Rectangle(std::weak_ptr<SceneItem> parent) : Rectangle::Rectangle(
SceneItem2D(parent) std::weak_ptr<SceneItem> parent,
std::weak_ptr<Scene> scene
) :
SceneItem2D(parent, scene)
{ {
} }

View File

@ -11,13 +11,16 @@ namespace Dawn {
class Rectangle : public SceneItem2D { class Rectangle : public SceneItem2D {
public: public:
struct Color color = COLOR_WHITE; struct Color color = COLOR_WHITE;
float_t width = 32.0f;
float_t height = 32.0f;
/** /**
* Creates a new Rectangle. * Creates a new Rectangle.
* *
* @param parent The parent SceneItem, or nullptr if root (unlikely). * @param parent The parent SceneItem, or nullptr if root (unlikely).
* @param scene The Scene this item belongs to.
*/ */
Rectangle(std::weak_ptr<SceneItem> parent); Rectangle(std::weak_ptr<SceneItem> parent, std::weak_ptr<Scene> scene);
/** /**
* Destroys the Rectangle. * Destroys the Rectangle.