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

@ -14,6 +14,7 @@ Game::Game() {
}
void Game::init() {
currentScene = std::make_shared<Scene>(weak_from_this());
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() {
}

@ -31,6 +31,20 @@ namespace Dawn {
*/
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.
*/

@ -7,10 +7,27 @@
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;
}
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() {
}

@ -8,10 +8,22 @@
#include "game/Game.hpp"
namespace Dawn {
class Scene : public SceneItem {
class Scene : public SceneItem, public std::enable_shared_from_this<Scene> {
private:
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:
/**
* Creates a new Scene.
@ -20,9 +32,18 @@ namespace Dawn {
*/
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.
*/
virtual ~Scene();
friend class Game;
};
}

@ -8,17 +8,22 @@
using namespace Dawn;
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() {
return parent.lock();
}
std::shared_ptr<Scene> SceneItem::getScene() {
return scene.lock();
}
void SceneItem::removeSceneItem(std::shared_ptr<SceneItem> item) {
// 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.

@ -5,23 +5,35 @@
#pragma once
#include "dawn.hpp"
#include "event/Event.hpp"
namespace Dawn {
class Scene;
class SceneItem : public std::enable_shared_from_this<SceneItem> {
private:
std::weak_ptr<SceneItem> parent;
std::vector<std::shared_ptr<SceneItem>> items;
std::weak_ptr<Scene> scene;
bool_t isDisposing = false;
protected:
std::vector<std::shared_ptr<SceneItem>> items;
public:
std::string name;
Event<> onEnter;
Event<> onLeave;
/**
* Creates a new SceneItem.
*
* @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.
@ -31,13 +43,14 @@ namespace Dawn {
*/
template<typename C>
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);
if(asItem == nullptr) {
throw std::runtime_error("SceneItem must be a SceneItem.");
}
asItem->name = name;
items.push_back(asItem);
return item;
}
@ -80,9 +93,18 @@ namespace Dawn {
*/
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.
*/
virtual ~SceneItem();
friend class Scene;
};
}

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

@ -20,7 +20,10 @@ namespace Dawn {
*
* @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.

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

@ -11,13 +11,16 @@ namespace Dawn {
class Rectangle : public SceneItem2D {
public:
struct Color color = COLOR_WHITE;
float_t width = 32.0f;
float_t height = 32.0f;
/**
* Creates a new Rectangle.
*
* @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.