Brenta Engine 1.0
Loading...
Searching...
No Matches
world.cpp
1/*
2 * MIT License
3 *
4 * Copyright (c) 2024 Giovanni Santini
5
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 */
26
27#include "world.hpp"
28
29#include "engine_logger.hpp"
30#include "engine_time.hpp"
31
32#include <algorithm>
33
34using namespace Brenta::ECS;
35using namespace Brenta::Utils;
36
37SetPtr<Entity> World::entities;
38UMapPtr<std::type_index, Resource> World::resources;
39UMapVecPtr<std::type_index, Component> World::components;
40
41template <> void World::QueryComponentsRec<None>(std::vector<Entity> *entities)
42{
43}
44
46{
47 using namespace Types;
48 World::entities = std::make_unique<std::set<Entity>>();
49 World::resources = std::make_unique<UMap<std::type_index, Resource>>();
50 World::components = std::make_unique<UMapVec<std::type_index, Component>>();
51
52 INFO("World initialized");
53}
54
56{
57 World::entities.reset();
58 World::components.reset();
59 World::resources.reset();
60
61 INFO("World deleted");
62}
63
65{
67}
68
70{
71 if (!World::entities)
72 {
73 ERROR("Cannot create entity: world not initialized");
74 return -1;
75 }
76
77 if (World::entities->empty())
78 {
79 World::entities->insert(1);
80 return 1;
81 }
82
83 Entity new_entity = *(World::entities->rbegin()) + 1;
84 World::entities->insert(new_entity);
85
86 INFO("New entity created: ", new_entity);
87
88 return new_entity;
89}
90
91std::set<Entity> *World::getEntities()
92{
93 if (!World::entities)
94 {
95 ERROR("Cannot get entities: world not initialized");
96 return nullptr;
97 }
98 return World::entities.get();
99}
100
101UMap<std::type_index, Resource> *World::getResources()
102{
103 if (!World::resources)
104 {
105 ERROR("Cannot get resources: world not initialized");
106 return nullptr;
107 }
108 return World::resources.get();
109}
110
111UMapVec<std::type_index, Component> *World::getComponents()
112{
113 if (!World::components)
114 {
115 ERROR("Cannot get components: world not initialized");
116 return nullptr;
117 }
118 return World::components.get();
119}
120
121void World::RemoveEntity(Entity entity)
122{
123 if (!World::entities)
124 {
125 ERROR("Cannot remove entity: world not initialized");
126 return;
127 }
128
129 World::entities->erase(entity);
130
131 for (auto iter = World::components->begin();
132 iter != World::components->end(); iter++)
133 {
134 iter->second.erase(
135 std::remove_if(iter->second.begin(), iter->second.end(),
136 [&entity](const std::shared_ptr<Component> &elem)
137 { return elem->entity == entity; }),
138 iter->second.end());
139 }
140
141 INFO("Entity removed: ", entity);
142}
static UMap< std::type_index, Resource > * getResources()
Get the resources container.
Definition world.cpp:101
static void RemoveEntity(Entity entity)
Remove an entity.
Definition world.cpp:121
static void Tick()
Tick the world.
Definition world.cpp:64
static UMapVec< std::type_index, Component > * getComponents()
Get the components container.
Definition world.cpp:111
static std::set< Entity > * getEntities()
Get the entities container.
Definition world.cpp:91
static void Init()
Initialize the world.
Definition world.cpp:45
static void RunSystems()
Run all systems.
static Entity NewEntity()
Create a new entity.
Definition world.cpp:69
static void Delete()
Delete the world.
Definition world.cpp:55