From 0e5b85633c71a25bcf9aec0d40ddd95f9188c914 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Tue, 10 Sep 2024 18:26:14 -0500 Subject: [PATCH] Worked a bit on prefabs of all things --- src/dawn/prefab/Prefab.hpp | 14 +++++++ src/dawn/prefab/SimpleSpinningCube.cpp | 26 ++++++------ src/dawn/prefab/SimpleSpinningCube.hpp | 19 ++++++++- src/dawn/scene/SceneComponent.cpp | 12 ++++-- src/dawn/scene/SceneComponent.hpp | 4 ++ src/dawn/scene/item/SceneItemComponents.hpp | 1 + src/dawnrpg/CMakeLists.txt | 2 + src/dawnrpg/component/CMakeLists.txt | 7 ++++ src/dawnrpg/component/entity/CMakeLists.txt | 9 ++++ src/dawnrpg/component/entity/Entity.cpp | 18 ++++++++ src/dawnrpg/component/entity/Entity.hpp | 21 ++++++++++ src/dawnrpg/component/world/CMakeLists.txt | 9 ++++ src/dawnrpg/component/world/World.cpp | 14 +++++++ src/dawnrpg/component/world/World.hpp | 18 ++++++++ src/dawnrpg/dawnrpg.cpp | 2 +- src/dawnrpg/prefab/CMakeLists.txt | 10 +++++ src/dawnrpg/prefab/PlayerPrefab.cpp | 20 +++++++++ src/dawnrpg/prefab/PlayerPrefab.hpp | 17 ++++++++ src/dawnrpg/prefab/WorldPrefab.cpp | 17 ++++++++ src/dawnrpg/prefab/WorldPrefab.hpp | 17 ++++++++ src/dawnrpg/scenes/CMakeLists.txt | 1 + src/dawnrpg/scenes/SceneList.hpp | 1 + src/dawnrpg/scenes/WorldScene.cpp | 46 +++++++++++++++++++++ 23 files changed, 286 insertions(+), 19 deletions(-) create mode 100644 src/dawn/prefab/Prefab.hpp create mode 100644 src/dawnrpg/component/CMakeLists.txt create mode 100644 src/dawnrpg/component/entity/CMakeLists.txt create mode 100644 src/dawnrpg/component/entity/Entity.cpp create mode 100644 src/dawnrpg/component/entity/Entity.hpp create mode 100644 src/dawnrpg/component/world/CMakeLists.txt create mode 100644 src/dawnrpg/component/world/World.cpp create mode 100644 src/dawnrpg/component/world/World.hpp create mode 100644 src/dawnrpg/prefab/CMakeLists.txt create mode 100644 src/dawnrpg/prefab/PlayerPrefab.cpp create mode 100644 src/dawnrpg/prefab/PlayerPrefab.hpp create mode 100644 src/dawnrpg/prefab/WorldPrefab.cpp create mode 100644 src/dawnrpg/prefab/WorldPrefab.hpp create mode 100644 src/dawnrpg/scenes/WorldScene.cpp diff --git a/src/dawn/prefab/Prefab.hpp b/src/dawn/prefab/Prefab.hpp new file mode 100644 index 00000000..68890b62 --- /dev/null +++ b/src/dawn/prefab/Prefab.hpp @@ -0,0 +1,14 @@ +// Copyright (c) 2024 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "scene/Scene.hpp" + +namespace Dawn { + struct Prefab { + public: + std::shared_ptr item; + }; +} \ No newline at end of file diff --git a/src/dawn/prefab/SimpleSpinningCube.cpp b/src/dawn/prefab/SimpleSpinningCube.cpp index 0e32ad5b..ae93ffd0 100644 --- a/src/dawn/prefab/SimpleSpinningCube.cpp +++ b/src/dawn/prefab/SimpleSpinningCube.cpp @@ -4,32 +4,32 @@ // https://opensource.org/licenses/MIT #include "SimpleSpinningCube.hpp" -#include "component/display/MeshRenderer.hpp" -#include "component/display/material/SimpleTexturedMaterial.hpp" #include "display/mesh/CubeMesh.hpp" #include "component/SimpleComponent.hpp" using namespace Dawn; -std::shared_ptr Dawn::createSimpleSpinningCube(Scene &s) { +struct SimpleSpinningCube Dawn::createSimpleSpinningCube(Scene &s) { + struct SimpleSpinningCube cube; + // Create the scene item. - auto cubeItem = s.createSceneItem(); + cube.item = s.createSceneItem(); // Create a simple cube mesh. - auto cubeMesh = std::make_shared(); - cubeMesh->createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT); - CubeMesh::buffer(cubeMesh, glm::vec3(-1, -1, -1), glm::vec3(2, 2, 2), 0, 0); + cube.mesh = std::make_shared(); + cube.mesh->createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT); + CubeMesh::buffer(cube.mesh, glm::vec3(-1, -1, -1), glm::vec3(2, 2, 2), 0, 0); // Add a renderer to the scene item. - auto cubeMeshRenderer = cubeItem->addComponent(); - cubeMeshRenderer->mesh = cubeMesh; + cube.meshRenderer = cube.item->addComponent(); + cube.meshRenderer->mesh = cube.mesh; // Add a material to the scene item. - auto cubeMaterial = cubeItem->addComponent(); - cubeMaterial->setColor(COLOR_MAGENTA); + cube.material = cube.item->addComponent(); + cube.material->setColor(COLOR_MAGENTA); // Add a simple event listener component to the scene item. - addSimpleComponent(cubeItem, [](auto &cmp, auto &events) { + addSimpleComponent(cube.item, [](auto &cmp, auto &events) { // Note that add component cannot receive a self reference, so we must // capture the component by reference instead. events.push_back(cmp.getScene()->onUnpausedUpdate.listen([&]( @@ -45,5 +45,5 @@ std::shared_ptr Dawn::createSimpleSpinningCube(Scene &s) { })); }); - return cubeItem; + return cube; } \ No newline at end of file diff --git a/src/dawn/prefab/SimpleSpinningCube.hpp b/src/dawn/prefab/SimpleSpinningCube.hpp index 8107683f..6e5a38c2 100644 --- a/src/dawn/prefab/SimpleSpinningCube.hpp +++ b/src/dawn/prefab/SimpleSpinningCube.hpp @@ -4,8 +4,23 @@ // https://opensource.org/licenses/MIT #pragma once -#include "scene/Scene.hpp" +#include "prefab/Prefab.hpp" +#include "component/display/MeshRenderer.hpp" +#include "component/display/material/SimpleTexturedMaterial.hpp" namespace Dawn { - std::shared_ptr createSimpleSpinningCube(Scene &s); + struct SimpleSpinningCube : public Prefab { + public: + std::shared_ptr mesh; + std::shared_ptr meshRenderer; + std::shared_ptr material; + }; + + /** + * Creates a simple spinning cube prefab on the scene. + * + * @param s Scene to add the simple spinning cube to. + * @return The simple spinning cube prefab. + */ + struct SimpleSpinningCube createSimpleSpinningCube(Scene &s); } \ No newline at end of file diff --git a/src/dawn/scene/SceneComponent.cpp b/src/dawn/scene/SceneComponent.cpp index 908ac27b..8a9d7df6 100644 --- a/src/dawn/scene/SceneComponent.cpp +++ b/src/dawn/scene/SceneComponent.cpp @@ -44,17 +44,23 @@ void SceneComponent::dispose() { } std::shared_ptr SceneComponent::getItem() { - return this->item.lock(); + auto item = this->item.lock(); + assertNotNull(item, "Item cannot be null?"); + return item; } std::shared_ptr SceneComponent::getScene() { auto item = this->getItem(); - return item->getScene(); + auto scene = item->getScene(); + assertNotNull(scene, "Scene cannot be null?"); + return scene; } std::shared_ptr SceneComponent::getGame() { auto scene = this->getScene(); - return scene->getGame(); + auto game = scene->getGame(); + assertNotNull(game, "Game cannot be null?"); + return game; } SceneComponent::~SceneComponent() { diff --git a/src/dawn/scene/SceneComponent.hpp b/src/dawn/scene/SceneComponent.hpp index 426e0005..486e8121 100644 --- a/src/dawn/scene/SceneComponent.hpp +++ b/src/dawn/scene/SceneComponent.hpp @@ -13,6 +13,7 @@ namespace Dawn { class Game; class Scene; class SceneItem; + class SceneItemComponents; class SceneComponent : std::enable_shared_from_this { private: @@ -70,5 +71,8 @@ namespace Dawn { * Disposes this scene component. */ virtual ~SceneComponent(); + + friend class SceneItem; + friend class SceneItemComponents; }; } \ No newline at end of file diff --git a/src/dawn/scene/item/SceneItemComponents.hpp b/src/dawn/scene/item/SceneItemComponents.hpp index 6a178309..1107938b 100644 --- a/src/dawn/scene/item/SceneItemComponents.hpp +++ b/src/dawn/scene/item/SceneItemComponents.hpp @@ -43,6 +43,7 @@ namespace Dawn { this->components.push_back( static_pointer_cast(component) ); + component->item = this->sceneItemComponentsSelf(); return component; } diff --git a/src/dawnrpg/CMakeLists.txt b/src/dawnrpg/CMakeLists.txt index cc5d12a9..c6c7237d 100644 --- a/src/dawnrpg/CMakeLists.txt +++ b/src/dawnrpg/CMakeLists.txt @@ -16,6 +16,8 @@ target_sources(${DAWN_TARGET_NAME} ) # Subdirs +add_subdirectory(component) +add_subdirectory(prefab) add_subdirectory(scenes) # Assets diff --git a/src/dawnrpg/component/CMakeLists.txt b/src/dawnrpg/component/CMakeLists.txt new file mode 100644 index 00000000..238c0485 --- /dev/null +++ b/src/dawnrpg/component/CMakeLists.txt @@ -0,0 +1,7 @@ +# Copyright (c) 2024 Dominic Masters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +add_subdirectory(entity) +add_subdirectory(world) \ No newline at end of file diff --git a/src/dawnrpg/component/entity/CMakeLists.txt b/src/dawnrpg/component/entity/CMakeLists.txt new file mode 100644 index 00000000..181a82f9 --- /dev/null +++ b/src/dawnrpg/component/entity/CMakeLists.txt @@ -0,0 +1,9 @@ +# Copyright (c) 2024 Dominic Masters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +target_sources(${DAWN_TARGET_NAME} + PRIVATE + Entity.cpp +) \ No newline at end of file diff --git a/src/dawnrpg/component/entity/Entity.cpp b/src/dawnrpg/component/entity/Entity.cpp new file mode 100644 index 00000000..e43e6c89 --- /dev/null +++ b/src/dawnrpg/component/entity/Entity.cpp @@ -0,0 +1,18 @@ +// Copyright (c) 2024 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "Entity.hpp" +#include "assert/assert.hpp" + +using namespace Dawn; + +void Entity::onInit() { + auto world = this->world.lock(); + assertNotNull(world, "World is no longer active."); +} + +void Entity::onDispose() { + +} \ No newline at end of file diff --git a/src/dawnrpg/component/entity/Entity.hpp b/src/dawnrpg/component/entity/Entity.hpp new file mode 100644 index 00000000..9157717e --- /dev/null +++ b/src/dawnrpg/component/entity/Entity.hpp @@ -0,0 +1,21 @@ +// Copyright (c) 2024 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "scene/SceneComponent.hpp" + +typedef uint32_t entityid_t; + +namespace Dawn { + class World; + + class Entity final : public SceneComponent { + public: + std::weak_ptr world; + + void onInit() override; + void onDispose() override; + }; +} \ No newline at end of file diff --git a/src/dawnrpg/component/world/CMakeLists.txt b/src/dawnrpg/component/world/CMakeLists.txt new file mode 100644 index 00000000..523d2ad5 --- /dev/null +++ b/src/dawnrpg/component/world/CMakeLists.txt @@ -0,0 +1,9 @@ +# Copyright (c) 2024 Dominic Masters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +target_sources(${DAWN_TARGET_NAME} + PRIVATE + World.cpp +) \ No newline at end of file diff --git a/src/dawnrpg/component/world/World.cpp b/src/dawnrpg/component/world/World.cpp new file mode 100644 index 00000000..e8c4b07f --- /dev/null +++ b/src/dawnrpg/component/world/World.cpp @@ -0,0 +1,14 @@ +// Copyright (c) 2024 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "World.hpp" + +using namespace Dawn; + +void World::onInit() { +} + +void World::onDispose() { +} \ No newline at end of file diff --git a/src/dawnrpg/component/world/World.hpp b/src/dawnrpg/component/world/World.hpp new file mode 100644 index 00000000..5c366ee1 --- /dev/null +++ b/src/dawnrpg/component/world/World.hpp @@ -0,0 +1,18 @@ +// Copyright (c) 2024 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "component/entity/Entity.hpp" + +namespace Dawn { + class World : public SceneComponent { + private: + std::map> entities; + + public: + void onInit() override; + void onDispose() override; + }; +} \ No newline at end of file diff --git a/src/dawnrpg/dawnrpg.cpp b/src/dawnrpg/dawnrpg.cpp index c21cc9d5..b62a91c2 100644 --- a/src/dawnrpg/dawnrpg.cpp +++ b/src/dawnrpg/dawnrpg.cpp @@ -8,5 +8,5 @@ using namespace Dawn; const std::function Scene::getInitialScene() { - return Dawn::testScene; + return Dawn::worldScene; } \ No newline at end of file diff --git a/src/dawnrpg/prefab/CMakeLists.txt b/src/dawnrpg/prefab/CMakeLists.txt new file mode 100644 index 00000000..109193b1 --- /dev/null +++ b/src/dawnrpg/prefab/CMakeLists.txt @@ -0,0 +1,10 @@ +# Copyright (c) 2024 Dominic Masters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +target_sources(${DAWN_TARGET_NAME} + PRIVATE + PlayerPrefab.cpp + WorldPrefab.cpp +) \ No newline at end of file diff --git a/src/dawnrpg/prefab/PlayerPrefab.cpp b/src/dawnrpg/prefab/PlayerPrefab.cpp new file mode 100644 index 00000000..3a21aebf --- /dev/null +++ b/src/dawnrpg/prefab/PlayerPrefab.cpp @@ -0,0 +1,20 @@ +// Copyright (c) 2024 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "PlayerPrefab.hpp" + +using namespace Dawn; + +struct PlayerPrefab Dawn::createPlayerPrefab( + const std::shared_ptr world +) { + struct PlayerPrefab player; + player.item = world->getScene()->createSceneItem(); + + player.entity = player.item->addComponent(); + player.entity->world = world; + + return player; +} \ No newline at end of file diff --git a/src/dawnrpg/prefab/PlayerPrefab.hpp b/src/dawnrpg/prefab/PlayerPrefab.hpp new file mode 100644 index 00000000..09c92203 --- /dev/null +++ b/src/dawnrpg/prefab/PlayerPrefab.hpp @@ -0,0 +1,17 @@ +// Copyright (c) 2024 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "prefab/Prefab.hpp" +#include "component/world/World.hpp" + +namespace Dawn { + struct PlayerPrefab : public Prefab { + public: + std::shared_ptr entity; + }; + + struct PlayerPrefab createPlayerPrefab(const std::shared_ptr world); +} \ No newline at end of file diff --git a/src/dawnrpg/prefab/WorldPrefab.cpp b/src/dawnrpg/prefab/WorldPrefab.cpp new file mode 100644 index 00000000..94bd7fcd --- /dev/null +++ b/src/dawnrpg/prefab/WorldPrefab.cpp @@ -0,0 +1,17 @@ +// Copyright (c) 2024 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "WorldPrefab.hpp" + +using namespace Dawn; + +struct WorldPrefab Dawn::createWorldPrefab(Scene &s) { + struct WorldPrefab world; + + world.item = s.createSceneItem(); + world.world = world.item->addComponent(); + + return world; +} \ No newline at end of file diff --git a/src/dawnrpg/prefab/WorldPrefab.hpp b/src/dawnrpg/prefab/WorldPrefab.hpp new file mode 100644 index 00000000..87dee0a3 --- /dev/null +++ b/src/dawnrpg/prefab/WorldPrefab.hpp @@ -0,0 +1,17 @@ +// Copyright (c) 2024 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "prefab/Prefab.hpp" +#include "component/world/World.hpp" + +namespace Dawn { + struct WorldPrefab : public Prefab { + public: + std::shared_ptr world; + }; + + struct WorldPrefab createWorldPrefab(Scene &scene); +} \ No newline at end of file diff --git a/src/dawnrpg/scenes/CMakeLists.txt b/src/dawnrpg/scenes/CMakeLists.txt index ba19fa9e..311a58fb 100644 --- a/src/dawnrpg/scenes/CMakeLists.txt +++ b/src/dawnrpg/scenes/CMakeLists.txt @@ -6,4 +6,5 @@ target_sources(${DAWN_TARGET_NAME} PRIVATE TestScene.cpp + WorldScene.cpp ) \ No newline at end of file diff --git a/src/dawnrpg/scenes/SceneList.hpp b/src/dawnrpg/scenes/SceneList.hpp index 225d44ca..8830c0b8 100644 --- a/src/dawnrpg/scenes/SceneList.hpp +++ b/src/dawnrpg/scenes/SceneList.hpp @@ -8,4 +8,5 @@ namespace Dawn { void testScene(Scene &scene); + void worldScene(Scene &scene); } diff --git a/src/dawnrpg/scenes/WorldScene.cpp b/src/dawnrpg/scenes/WorldScene.cpp new file mode 100644 index 00000000..a085fda9 --- /dev/null +++ b/src/dawnrpg/scenes/WorldScene.cpp @@ -0,0 +1,46 @@ + +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "scenes/SceneList.hpp" +#include "prefab/PlayerPrefab.hpp" +#include "prefab/WorldPrefab.hpp" +// #include "component/display/Camera.hpp" +// #include "prefab/SimpleSpinningCube.hpp" +// #include "component/display/material/SimpleTexturedMaterial.hpp" +// #include "display/mesh/CubeMesh.hpp" + +// #include "component/ui/UICanvas.hpp" +// #include "ui/elements/UIRectangle.hpp" +// #include "ui/elements/UILabel.hpp" +// #include "ui/UIMenu.hpp" +// #include "ui/container/UIRowContainer.hpp" +// #include "ui/container/UIPaddingContainer.hpp" + +// #include "poker/PokerGame.hpp" + +using namespace Dawn; + +void Dawn::worldScene(Scene &s) { + // auto cameraItem = s.createSceneItem(); + // auto camera = cameraItem->addComponent(); + // cameraItem->lookAt({ 3, 3, 3 }, { 0, 0, 0 }, { 0, 1, 0 }); + // camera->clipFar = 99999.99f; + + // auto cube = s.createSceneItem(); + // auto cubeMesh = std::make_shared(); + // auto cubeRenderer = cube->addComponent(); + // auto cubeMaterial = cube->addComponent(); + + // cubeRenderer->mesh = cubeMesh; + // cubeMesh->createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT); + // CubeMesh::buffer(cubeMesh, glm::vec3(0,0,0), glm::vec3(1, 1, 1), 0, 0); + + // auto canvasItem = s.createSceneItem(); + // auto canvas = canvasItem->addComponent(); + + auto world = createWorldPrefab(s); + auto player = createPlayerPrefab(world.world); +} \ No newline at end of file