Saga
Saga Game Engine
Loading...
Searching...
No Matches
gameworld.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <unordered_map>
5#include <vector>
6#include "../Datastructures/typemap.h"
7#include "../Systems/invokableSystemManager.h"
8#include "../Systems/system.h"
9#include "../Entity/entity.h"
10#include "signature.h"
11#include "componentReference.h"
12
13namespace Saga {
14
15class IComponentContainer;
16
17template<typename Component>
18class ComponentContainer;
19
20class IComponentGroup;
21
22template<typename... Component>
23class ComponentGroup;
24
28class GameWorld : public std::enable_shared_from_this<GameWorld> {
29public:
33 GameWorld();
37 virtual ~GameWorld();
38
44 Entity createEntity();
50 void destroyEntity(Entity entity);
51
62 template<typename Component, typename ...Args>
63 ComponentReference<Component> emplace(const Entity entity, Args &&...args);
64
72 template<typename Component>
74
75 // slow af so don't use
84 template<typename Component>
85 Entity getEntity(Component* component);
86
95 template<typename Component>
96 bool hasComponent(const Entity entity);
97
104 template<typename Component>
105 void removeComponent(const Entity entity);
106
111 template<typename Component>
112 std::shared_ptr<ComponentContainer<Component>> viewAll();
113
120 template<typename... Component>
121 std::shared_ptr<ComponentGroup<Component...>> registerGroup();
122
129 template<typename... Component>
130 std::shared_ptr<ComponentGroup<Component...>> viewGroup();
131
138
145 template <class KeyType>
146 int getTypeId() { return componentMap.getTypeId<KeyType>(); }
147
156 template<typename Event, typename... DataType>
157 void broadcastEvent(Event event, DataType... args);
158
168 template<typename Event, typename... DataType>
169 void deliverEvent(Event event, Entity entity, DataType... args);
170
171protected:
173
180 template<typename... Component>
182
184private:
186 std::unordered_map<Entity, Signature> entitySignatures;
187 std::unordered_map<Signature, std::shared_ptr<IComponentGroup>> componentGroups;
188};
189
190} // namespace Saga
191
192#include "gameworld.inl"
Effectively a list of tuples each entry containing an Entity and ComponentReference to each of the co...
Definition: componentGroup.h:45
Reference to a component. Will be valid as long as that component exists on a specific Entity,...
Definition: componentReference.h:16
Maintains a game world, where Entity, Component, Systems, and MetaSystems can be added,...
Definition: gameworld.h:28
ComponentReference< Component > getComponent(const Entity entity)
Get a reference to a Component on the specified entity.
Definition: gameworld.inl:44
SystemManager & getSystems()
Get the SystemManager object.
Definition: gameworld.h:137
GameWorld()
Construct a new Game World object.
Definition: gameworld.cpp:7
std::shared_ptr< ComponentContainer< Component > > viewAll()
Definition: gameworld.inl:37
virtual ~GameWorld()
Destroy the Game World object.
Definition: gameworld.cpp:11
int getTypeId()
Get an id for a type, useful for keeping multiple typemaps consistent.
Definition: gameworld.h:146
void removeComponent(const Entity entity)
Remove a component from an Entity.
Definition: gameworld.inl:59
Entity getEntity(Component *component)
Get the Entity from a pointer to a component.
Definition: gameworld.inl:54
void destroyEntity(Entity entity)
Destoy an Entity object.
Definition: gameworld.cpp:22
ComponentReference< Component > emplace(const Entity entity, Args &&...args)
Emplace a component to an Entity. This constructs the component instead of adding them.
Definition: gameworld.inl:12
void deliverEvent(Event event, Entity entity, DataType... args)
Deliver an event to a specific object. Only System that registers to that specific object will be cal...
Definition: gameworld.inl:127
InvokableSystemManager systemManager
where all the systems are stored and can potentially be invoked.
Definition: gameworld.h:183
void broadcastEvent(Event event, DataType... args)
Broadcast an event to the world, so that Systems that listen to the event can be triggered.
Definition: gameworld.inl:122
Signature createSignature()
Create a signature from a list of component types.
Definition: gameworld.inl:114
Entity createEntity()
Create a Entity object.
Definition: gameworld.cpp:15
bool hasComponent(const Entity entity)
Determine if an entity has a specific type of Component attached.
Definition: gameworld.inl:49
std::shared_ptr< ComponentGroup< Component... > > registerGroup()
Register a ComponentGroup.
Definition: gameworld.inl:75
entity_type entity_cnt
number of entities in the world. Useful for assigning new entities.
Definition: gameworld.h:172
std::shared_ptr< ComponentGroup< Component... > > viewGroup()
Compute the view of a Group. It is mandatory that this group is registered before this operation,...
Definition: gameworld.inl:95
A SystemManager where stages and events can be invoked.
Definition: invokableSystemManager.h:12
Manages Systems in the ECS model. Allows for System to be added, but not invoked.
Definition: systemManager.h:17
Map that has Types as key.
Definition: typemap.h:15
Definition: app.cpp:8
std::uint32_t entity_type
Definition: entity.h:7
std::bitset< MAX_COMPONENTS > Signature
Used for determining if an object has a certain component type. Signature [i] is true if it does,...
Definition: signature.h:12