Skip to content

Commit

Permalink
get_scene to main (#2)
Browse files Browse the repository at this point in the history
* Implemented logic to get current scene node instance.
* Added functionality to get instances from collisions.
* Added queue_deletion method for Node for the Python API.
  • Loading branch information
Chukobyte authored May 8, 2021
1 parent aed1cb7 commit 30f7ee3
Show file tree
Hide file tree
Showing 19 changed files with 290 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ PROJECT_NAME := roll_back_engine
BUILD_OBJECT := $(PROJECT_NAME).exe
TEST_BUILD_OBJECT := test_$(PROJECT_NAME).exe

SRC = $(wildcard src/main.cpp src/core/*.cpp src/math/*.cpp src/core/utils/*.cpp src/core/rendering/*.cpp src/core/rendering/shader/*.cpp src/core/input/*.cpp src/core/timer/*.cpp src/core/scripting/python/*.cpp include/stb_image/*.cpp)
SRC = $(wildcard src/main.cpp src/core/*.cpp src/math/*.cpp src/core/utils/*.cpp src/core/rendering/*.cpp src/core/rendering/shader/*.cpp src/core/input/*.cpp src/core/timer/*.cpp src/core/scripting/python/*.cpp src/core/ecs/*.cpp include/stb_image/*.cpp)
SRC_C = $(wildcard lib/glad.c)

OBJ = $(SRC:.cpp=.o)
OBJ_C = $(SRC_C:.c=.o)

TEST_SRC = $(wildcard src/test/*.cpp src/test/unit/*.cpp src/core/*.cpp src/math/*.cpp src/core/utils/*.cpp src/core/rendering/*.cpp src/core/rendering/shader/*.cpp src/core/input/*.cpp src/core/timer/*.cpp src/core/scripting/python/*.cpp include/stb_image/*.cpp)
TEST_SRC = $(wildcard src/test/*.cpp src/test/unit/*.cpp src/core/*.cpp src/math/*.cpp src/core/utils/*.cpp src/core/rendering/*.cpp src/core/rendering/shader/*.cpp src/core/input/*.cpp src/core/timer/*.cpp src/core/scripting/python/*.cpp src/core/ecs/*.cpp include/stb_image/*.cpp)
TEST_OBJ = $(TEST_SRC:.cpp=.o)

EXPORT_PACKAGE_DIR := export_package
Expand Down
8 changes: 7 additions & 1 deletion assets/game_projects/test/scenes/test_battle.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
"z_index_relative_to_parent": false,
"ignore_camera": false
}
},
{
"scriptable_class": {
"class_path": "assets.game_projects.test.src.test",
"class_name": "Test"
}
}
],
"children": [
Expand Down Expand Up @@ -178,7 +184,7 @@
},
{
"name": "Puncher2",
"type": "AnimatedSprite",
"type": "Node2D",
"tags": [],
"external_scene_source": "",
"components": [
Expand Down
8 changes: 4 additions & 4 deletions assets/game_projects/test/src/fighter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ def _start(self) -> None:

def _physics_process(self, delta_time: float) -> None:
if Input.is_action_just_pressed(action_name="quit"):
# Engine.exit()
SceneTree.change_scene(scene_path="assets/game_projects/test/scenes/test_battle.json")
Engine.exit()

self.camera_controls()
self.movement_controls()

if Collision.check(node=self):
print("Has collided!")
for collided_node in Collision.get_collided_nodes(self):
print(f"collided_node = {collided_node}")
collided_node.queue_deletion()

def movement_controls(self) -> None:
if Input.is_action_pressed(action_name="left"):
Expand Down
5 changes: 3 additions & 2 deletions assets/game_projects/test/src/main_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

class MainMenu(Node):
def _start(self) -> None:
Audio.play_music(music_id="assets/audio/music/test_music.wav")
# Audio.play_music(music_id="assets/audio/music/test_music.wav")
pass

def _physics_process(self, delta_time: float) -> None:
if Input.is_action_just_pressed(action_name="confirm"):
SceneTree.change_scene(scene_path="assets/game_projects/test/scenes/test_battle.json")
Audio.stop_music()
# Audio.stop_music()
5 changes: 5 additions & 0 deletions assets/game_projects/test/src/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from roll.node import Node2D

class Test(Node2D):
def _start(self) -> None:
print("Test created!")
23 changes: 23 additions & 0 deletions src/core/ecs/component/components/node_component.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef NODE_COMPONENT_H
#define NODE_COMPONENT_H

#include <string>
#include <vector>

#include "../component.h"

using NodeType = std::uint32_t;

enum _NodeType {
NodeType_INVALID = 0,
NodeType_NODE = 1,
NodeType_NODE2D = 2,
};

struct NodeComponent {
NodeType type;
std::string name;
std::vector<std::string> tags;
};

#endif //NODE_COMPONENT_H
15 changes: 15 additions & 0 deletions src/core/ecs/entity_component_orchestrator.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class EntityComponentOrchestrator {

std::string scenePathToSwitchTo;
bool removeCurrentSceneAtEndOfUpdate = false;
std::vector<Entity> entitiesQueuedForDeletion;

// Will be the function to initialize stuff for a scene
void RegisterAllNodeSystemSignatures(SceneNode sceneNode) {
Expand All @@ -41,6 +42,20 @@ class EntityComponentOrchestrator {
return entityManager->CreateEntity();
}

void QueueEntityForDeletion(Entity entity) {
entitiesQueuedForDeletion.emplace_back(entity);
}

void DestroyQueuedEntities() {
for (Entity entity : entitiesQueuedForDeletion) {
if (sceneManager->HasEntitySceneNode(entity)) {
SceneNode sceneNode = sceneManager->GetEntitySceneNode(entity);
DestroyEntity(sceneNode);
}
}
entitiesQueuedForDeletion.clear();
}

void DestroyEntity(SceneNode sceneNode) {
sceneManager->RemoveNode(sceneNode);
std::vector<Entity> entitiesRemovedFromScene = sceneManager->FlushRemovedEntities();
Expand Down
5 changes: 5 additions & 0 deletions src/core/ecs/node_type_helper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "node_type_helper.h"

const std::string NodeTypeHelper::NODE_TYPE_INVALID = "Invalid";
const std::string NodeTypeHelper::NODE_TYPE_NODE = "Node";
const std::string NodeTypeHelper::NODE_TYPE_NODE2D = "Node2D";
32 changes: 32 additions & 0 deletions src/core/ecs/node_type_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef NODE_TYPE_HELPER_H
#define NODE_TYPE_HELPER_H

#include "component/components/node_component.h"

class NodeTypeHelper {
private:
static const std::string NODE_TYPE_INVALID;
static const std::string NODE_TYPE_NODE;
static const std::string NODE_TYPE_NODE2D;
public:
static std::string GetNodeTypeString(NodeType nodeType) {
switch (nodeType) {
case NodeType_NODE:
return NODE_TYPE_NODE;
case NodeType_NODE2D:
return NODE_TYPE_NODE2D;
default:
return NODE_TYPE_INVALID;
}
}
static NodeType GetNodeTypeInt(const std::string &nodeTypeString) {
if (nodeTypeString == NODE_TYPE_NODE) {
return NodeType_NODE;
} else if(nodeTypeString == NODE_TYPE_NODE2D) {
return NodeType_NODE2D;
}
return NodeType_INVALID;
}
};

#endif //NODE_TYPE_HELPER_H
3 changes: 3 additions & 0 deletions src/core/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ void Game::InitializeSDL() {
void Game::InitializeECS() {
EntityComponentOrchestrator *entityComponentOrchestrator = GD::GetContainer()->entityComponentOrchestrator;
// Components
entityComponentOrchestrator->RegisterComponent<NodeComponent>();
entityComponentOrchestrator->RegisterComponent<Transform2DComponent>();
entityComponentOrchestrator->RegisterComponent<SpriteComponent>();
entityComponentOrchestrator->RegisterComponent<AnimatedSpriteComponent>();
Expand Down Expand Up @@ -165,6 +166,8 @@ void Game::Update() {
entityComponentOrchestrator->DestroyCurrentScene();
}

entityComponentOrchestrator->DestroyQueuedEntities();

// Change Scene
if (entityComponentOrchestrator->HasSceneToSwitchTo()) {
CameraManager *cameraManager = GD::GetContainer()->cameraManager;
Expand Down
4 changes: 3 additions & 1 deletion src/core/global_dependencies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ void GD::ResetDependencies() {
delete entityManager;
delete entitySystemManager;
delete componentManager;
delete sceneContext;
delete sceneManager;
delete entityComponentOrchestrator;
delete cameraManager;
Expand All @@ -33,7 +34,8 @@ void GD::ResetDependencies() {
entityManager = new EntityManager();
entitySystemManager = new EntitySystemManager();
componentManager = new ComponentManager();
sceneManager = new SceneManager(entityManager, componentManager, assetManager);
sceneContext = new SceneContext();
sceneManager = new SceneManager(sceneContext, entityManager, componentManager, assetManager);
entityComponentOrchestrator = new EntityComponentOrchestrator(entityManager, entitySystemManager, componentManager, sceneManager);
cameraManager = new CameraManager();
}
2 changes: 2 additions & 0 deletions src/core/global_dependencies.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "ecs/entity_component_orchestrator.h"
#include "physics/collision/collision_context.h"
#include "scene/scene.h"
#include "scene/scene_context.h"
#include "camera/camera_manager.h"

class GD {
Expand All @@ -29,6 +30,7 @@ class GD {
EntitySystemManager *entitySystemManager = nullptr;
ComponentManager *componentManager = nullptr;
SceneManager *sceneManager = nullptr;
SceneContext *sceneContext = nullptr;
EntityComponentOrchestrator *entityComponentOrchestrator = nullptr;
CameraManager *cameraManager = nullptr;

Expand Down
8 changes: 8 additions & 0 deletions src/core/physics/collision/collision_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ class CollisionContext {
bool HasEntityCollided(Entity entity) {
return collisionResultsMap.count(entity) > 0;
}

std::vector<Entity> GetEntitiesCollidedWith(Entity entity) {
if (HasEntityCollided(entity)) {
return collisionResultsMap[entity].collidedEntities;
}
std::vector<Entity> entitiesColliderWith;
return entitiesColliderWith;
}
};

#endif //COLLISION_CONTEXT_H
40 changes: 38 additions & 2 deletions src/core/scene/scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#include "../ecs/component/components/text_label_component.h"
#include "../ecs/component/components/collider_component.h"
#include "../ecs/component/components/scriptable_class_component.h"
#include "scene_context.h"
#include "../ecs/component/components/node_component.h"
#include "../ecs/node_type_helper.h"

struct SceneNode {
Entity entity = NO_ENTITY;
Expand All @@ -33,6 +36,7 @@ class SceneManager {
std::vector<Entity> entitiesRecentlyRemoved;

Scene currentScene;
SceneContext *sceneContext = nullptr;
EntityManager *entityManager = nullptr;
ComponentManager *componentManager = nullptr;
AssetManager *assetManager = nullptr;
Expand All @@ -46,6 +50,20 @@ class SceneManager {
nlohmann::json nodeComponentJsonArray = nodeJson["components"].get<nlohmann::json>();
nlohmann::json nodeChildrenJsonArray = nodeJson["children"].get<nlohmann::json>();

// Configure node type component
// TODO: Figure out if node type info should go into a component within scene json
std::vector<std::string> nodeTags;
for (const std::string &nodeTag : nodeTagsJsonArray) {
nodeTags.emplace_back(nodeTag);
}
componentManager->AddComponent(sceneNode.entity, NodeComponent{
.type = NodeTypeHelper::GetNodeTypeInt(nodeType),
.name = nodeName,
.tags = nodeTags
});


// Rest of components
for (nlohmann::json nodeComponentJson : nodeComponentJsonArray) {
nlohmann::json::iterator it = nodeComponentJson.begin();
std::string nodeComponentType = it.key();
Expand Down Expand Up @@ -193,14 +211,23 @@ class SceneManager {
return sceneNode;
}
public:
SceneManager(EntityManager *vEntityManager, ComponentManager *vComponentManager, AssetManager *vAssetManager) :
entityManager(vEntityManager), componentManager(vComponentManager), assetManager(vAssetManager) {
SceneManager(SceneContext *vSceneContext, EntityManager *vEntityManager, ComponentManager *vComponentManager, AssetManager *vAssetManager) :
sceneContext(vSceneContext), entityManager(vEntityManager), componentManager(vComponentManager), assetManager(vAssetManager) {
}

Scene GetCurrentScene() {
return currentScene;
}

SceneNode GetEntitySceneNode(Entity entity) {
assert(HasEntitySceneNode(entity) && "Tried to get scene node that doesn't exist!");
return entityToSceneNodeMap[entity];
}

bool HasEntitySceneNode(Entity entity) {
return entityToSceneNodeMap.count(entity) > 0;
}

void AddSingletonScene(Entity singletonEntity) {
SceneNode sceneNode = SceneNode{.entity = singletonEntity};
Scene scene = Scene{.rootNode = sceneNode};
Expand All @@ -215,6 +242,7 @@ class SceneManager {

void ChangeToScene(Scene scene) {
currentScene = scene;
sceneContext->currentSceneEntity = currentScene.rootNode.entity;
entityToMainScenesMap.emplace(currentScene.rootNode.entity, currentScene);
AddChild(NO_ENTITY, currentScene.rootNode.entity);
}
Expand Down Expand Up @@ -248,6 +276,14 @@ class SceneManager {
}
}

void RemoveNode(Entity entity) {
if (entityToSceneNodeMap.count(entity) > 0) {
RemoveNode(entityToSceneNodeMap[entity]);
} else {
Logger::GetInstance()->Warn("Tried to remove non existent entity");
}
}

std::vector<Entity> FlushRemovedEntities() {
std::vector<Entity> removedEntitiesCopy = entitiesRecentlyRemoved;
entitiesRecentlyRemoved.clear();
Expand Down
13 changes: 13 additions & 0 deletions src/core/scene/scene_context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef SCENE_CONTEXT_H
#define SCENE_CONTEXT_H

#include "../ecs/entity/entity.h"

class SceneContext {
public:
Entity currentSceneEntity;

SceneContext() = default;
};

#endif //SCENE_CONTEXT_H
1 change: 1 addition & 0 deletions src/core/scripting/python/python_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "pyhelper.hpp"

#include "../../ecs/entity/entity.h"
#include "../../utils/logger.h"

struct CachedPythonModule {
CPyObject module;
Expand Down
Loading

0 comments on commit 30f7ee3

Please sign in to comment.