aboutsummaryrefslogtreecommitdiffstats
path: root/yage/entity
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2018-02-13 19:16:22 +0000
committerYann Herklotz <ymherklotz@gmail.com>2018-02-13 19:16:22 +0000
commit2fe3434682aae38bb0ab414135c2311110a6db90 (patch)
tree1dff4aea6a59c466ea6a6f15ad7f5e2ac69f5b37 /yage/entity
parent400e8188a312abc7e5a0c7378dbd64fb8e0705e0 (diff)
parent49af8b16ae3f9e6579656ed10f815e9c465557d0 (diff)
downloadYAGE-2fe3434682aae38bb0ab414135c2311110a6db90.tar.gz
YAGE-2fe3434682aae38bb0ab414135c2311110a6db90.zip
Merge branch 'entity'
Diffstat (limited to 'yage/entity')
-rw-r--r--yage/entity/engine.cpp55
-rw-r--r--yage/entity/engine.h51
-rw-r--r--yage/entity/entity.cpp23
-rw-r--r--yage/entity/entity.h55
-rw-r--r--yage/entity/entitymanager.cpp34
-rw-r--r--yage/entity/entitymanager.h83
-rw-r--r--yage/entity/space.cpp21
-rw-r--r--yage/entity/space.h61
-rw-r--r--yage/entity/system.h57
9 files changed, 440 insertions, 0 deletions
diff --git a/yage/entity/engine.cpp b/yage/entity/engine.cpp
new file mode 100644
index 00000000..cf6f73b7
--- /dev/null
+++ b/yage/entity/engine.cpp
@@ -0,0 +1,55 @@
+/** ---------------------------------------------------------------------------
+ * @file: engine.cpp
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
+ * MIT License, see LICENSE file for more details.
+ * ----------------------------------------------------------------------------
+ */
+
+#include "engine.h"
+
+namespace yage
+{
+
+void Engine::init()
+{
+ window_.create("Game Engine", 800, 640);
+
+ for (auto &system : systems_) {
+ system->init();
+ }
+}
+
+void Engine::mainLoop()
+{
+ while (!window_.shouldClose()) {
+ window_.clearBuffer();
+
+ update();
+
+ window_.swapBuffer();
+ }
+}
+
+void Engine::update()
+{
+ const double dt = 1.0 / 60.0;
+
+ for (auto &system : systems_) {
+ system->update(dt);
+ }
+}
+
+void Engine::addSystem(System *system)
+{
+ systems_.push_back(system);
+}
+
+Engine &Engine::instance()
+{
+ static Engine engine_instance;
+
+ return engine_instance;
+}
+
+} // namespace yage
diff --git a/yage/entity/engine.h b/yage/entity/engine.h
new file mode 100644
index 00000000..147769e5
--- /dev/null
+++ b/yage/entity/engine.h
@@ -0,0 +1,51 @@
+/** ---------------------------------------------------------------------------
+ * @file: engine.h
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
+ * MIT License, see LICENSE file for more details.
+ * ----------------------------------------------------------------------------
+ */
+
+#ifndef YAGE_CORE_ENGINE_H
+#define YAGE_CORE_ENGINE_H
+
+#include "../core/window.h"
+#include "system.h"
+
+#include <vector>
+
+namespace yage
+{
+
+/// Main engine class that contains a systems, the main loop and the update
+/// function that updates all the systems.
+class Engine
+{
+public:
+ /// Initialize window and other aspects of the engine.
+ void init();
+
+ /// Main game loop of the engine.
+ void mainLoop();
+
+ /// Updates the systems.
+ void update();
+
+ /// Adds a system to the engine.
+ void addSystem(System *system);
+
+ /// Returns the instance of the engine, as there is only one instance of the
+ /// engine.
+ static Engine &instance();
+
+private:
+ /// Vector of all the systems in the engine.
+ std::vector<System *> systems_;
+
+ /// Window
+ Window window_;
+};
+
+} // namespace yage
+
+#endif
diff --git a/yage/entity/entity.cpp b/yage/entity/entity.cpp
new file mode 100644
index 00000000..4d9a4b0a
--- /dev/null
+++ b/yage/entity/entity.cpp
@@ -0,0 +1,23 @@
+/** ---------------------------------------------------------------------------
+ * @file: entity.cpp
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
+ * MIT License, see LICENSE file for more details.
+ * ----------------------------------------------------------------------------
+ */
+
+#include "entity.h"
+
+#include "space.h"
+
+namespace yage
+{
+
+Entity::Entity(unsigned handle) : handle_(handle) {}
+
+unsigned Entity::getHandle() const
+{
+ return handle_;
+}
+
+} // naemspace yage
diff --git a/yage/entity/entity.h b/yage/entity/entity.h
new file mode 100644
index 00000000..e6742893
--- /dev/null
+++ b/yage/entity/entity.h
@@ -0,0 +1,55 @@
+/** ---------------------------------------------------------------------------
+ * @file: entity.h
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
+ * MIT License, see LICENSE file for more details.
+ * ----------------------------------------------------------------------------
+ */
+
+#ifndef YAGE_ENGINE_ENTITY_H
+#define YAGE_ENGINE_ENTITY_H
+
+#include <vector>
+
+namespace yage
+{
+
+typedef unsigned EntityHandle;
+
+class Space;
+
+/**
+ * Entity convenience class. It contains handles to where the entity is in the
+ * entity manager of the space, and a pointer back to the space itself.
+ *
+ * This class cannot be instantiated outside of a Space and should only be
+ * instantiated through an entity manager, as otherwise the handle will not have
+ * a meaning.
+ */
+class Entity
+{
+public:
+ /**
+ * Creates an instance of an Entity with a handle that is associated to it.
+ * This handle refers to the position of he Entity in the list that is held
+ * by the EntityManager, and therefore the id is enough to refer to it.
+ */
+ Entity(EntityHandle handle);
+
+ /**
+ * Handle getter, as the user will only interact with the id itself. The
+ * handle is the unique identifier that the user can use to refer to the
+ * entity.
+ */
+ EntityHandle getHandle() const;
+
+private:
+ /**
+ * Entity handle for the entity manager.
+ */
+ EntityHandle handle_;
+};
+
+} // namespace yage
+
+#endif
diff --git a/yage/entity/entitymanager.cpp b/yage/entity/entitymanager.cpp
new file mode 100644
index 00000000..332ed9b8
--- /dev/null
+++ b/yage/entity/entitymanager.cpp
@@ -0,0 +1,34 @@
+/** ---------------------------------------------------------------------------
+ * @file: entitymanager.cpp
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
+ * MIT License, see LICENSE file for more details.
+ * ----------------------------------------------------------------------------
+ */
+
+#include "entitymanager.h"
+
+namespace yage
+{
+
+EntityManager::EntityManager(Space *space) : next_handle_(0), space_(space) {}
+
+EntityManager::EntityManager(Space *space, std::size_t n)
+ : next_handle_(0), space_(space)
+{
+ entities_.reserve(n);
+}
+
+unsigned EntityManager::createEntity()
+{
+ return createEntityInstance().getHandle();
+}
+
+Entity EntityManager::createEntityInstance()
+{
+ Entity entity(next_handle_++);
+ entities_.push_back(entity);
+ return entity;
+}
+
+} // namespace yage
diff --git a/yage/entity/entitymanager.h b/yage/entity/entitymanager.h
new file mode 100644
index 00000000..da125d94
--- /dev/null
+++ b/yage/entity/entitymanager.h
@@ -0,0 +1,83 @@
+/** ---------------------------------------------------------------------------
+ * @file: entitymanager.h
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
+ * MIT License, see LICENSE file for more details.
+ * ----------------------------------------------------------------------------
+ */
+
+#ifndef YAGE_ENGINE_ENTITYMANAGER_H
+#define YAGE_ENGINE_ENTITYMANAGER_H
+
+#include "entity.h"
+
+#include <vector>
+
+namespace yage
+{
+
+class Space;
+
+/**
+ * Manages entities in a space.
+ */
+class EntityManager
+{
+public:
+ /**
+ * Default instance of an EntityManager.
+ */
+ EntityManager() = default;
+
+ /**
+ * Creates an instance of the entity manager, which refers back to the space
+ * it was created in and belongs to.
+ *
+ * @param space Current space that the EntityManager belongs to.
+ */
+ EntityManager(Space *space);
+
+ /**
+ * Creates an instance of the entitiy manager with an initial size.
+ *
+ * @param space Current space that the EntityManager belongs to.
+ * @param n Initial size of the EntityManager.
+ */
+ EntityManager(Space *space, std::size_t n);
+
+ /**
+ * Creates an Entity and returns the handle to the entity, which can then be
+ * used by the user to do operations on it.
+ *
+ * @return The handle to the entity that was created in the space.
+ */
+ unsigned createEntity();
+
+ /**
+ * Creates an Entity and returns it.
+ *
+ * @return The entity that was created by the entity manager in the current
+ * space.
+ */
+ Entity createEntityInstance();
+
+private:
+ /**
+ * The next available handle to give to the user.
+ */
+ unsigned next_handle_;
+
+ /**
+ * The space that the entity manager belongs to.
+ */
+ Space *space_;
+
+ /**
+ * The entities in the current space.
+ */
+ std::vector<Entity> entities_;
+};
+
+} // namespace yage
+
+#endif
diff --git a/yage/entity/space.cpp b/yage/entity/space.cpp
new file mode 100644
index 00000000..f3e343b5
--- /dev/null
+++ b/yage/entity/space.cpp
@@ -0,0 +1,21 @@
+/** ---------------------------------------------------------------------------
+ * @file: space.cpp
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
+ * MIT License, see LICENSE file for more details.
+ * ----------------------------------------------------------------------------
+ */
+
+#include "space.h"
+
+namespace yage
+{
+
+Space::Space() : em_(this) {}
+
+unsigned Space::createEntity()
+{
+ return em_.createEntity();
+}
+
+} // namespace yage
diff --git a/yage/entity/space.h b/yage/entity/space.h
new file mode 100644
index 00000000..e69df37a
--- /dev/null
+++ b/yage/entity/space.h
@@ -0,0 +1,61 @@
+/** ---------------------------------------------------------------------------
+ * @file: space.h
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
+ * MIT License, see LICENSE file for more details.
+ * ----------------------------------------------------------------------------
+ */
+
+#ifndef YAGE_ENGINE_SPACE_H
+#define YAGE_ENGINE_SPACE_H
+
+#include <vector>
+
+#include "entitymanager.h"
+
+namespace yage
+{
+
+class System;
+
+/**
+ * Space that keeps track of all the entities, componenets and runs the systems
+ * on the data to update them. There can be multiple instances of a space, which
+ * can be used, for example, for different levels in the game that can be loaded
+ * separately, or a game menu that can be loaded above the other spaces when the
+ * user presses on pause.
+ */
+class Space
+{
+public:
+ /**
+ * Default instance for a space.
+ */
+ Space();
+
+ /**
+ * Create an entity that will belong to this space, and return the handle to
+ * the user. The Entity class itself should not be visible to the user, as
+ * the user only needs to worry about the handle when referring to the
+ * Entity and changing it.
+ */
+ unsigned createEntity();
+
+private:
+ /**
+ * The subspaces of the Space that act on the data and on their respective
+ * component. These are specific to the Space, as other spaces might have
+ * different Systems and not act on the same entities.
+ */
+ std::vector<System *> systems_;
+
+ /**
+ * Manages all the entities in the system, can create them for the current
+ * space.
+ */
+ EntityManager em_;
+};
+
+} // namespace yage
+
+#endif
diff --git a/yage/entity/system.h b/yage/entity/system.h
new file mode 100644
index 00000000..9100fa17
--- /dev/null
+++ b/yage/entity/system.h
@@ -0,0 +1,57 @@
+/** ---------------------------------------------------------------------------
+ * @file: system.h
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
+ * MIT License, see LICENSE file for more details.
+ * ----------------------------------------------------------------------------
+ */
+
+#ifndef YAGE_ENGINE_SYSTEM_H
+#define YAGE_ENGINE_SYSTEM_H
+
+namespace yage
+{
+
+/**
+ * System interface for the different systems in the engine.
+ */
+class System
+{
+public:
+ /**
+ * Virtual destructor to destroy all the objects that implement this
+ * properly.
+ */
+ virtual ~System() = 0;
+
+ /**
+ * Initializes the system. Good practice to have this function instead
+ * using the constructor.
+ */
+ virtual void init() = 0;
+
+ /**
+ * Updates the system at each interval using the time step.
+ *
+ * @param dt The time difference between the previous frame and the current one.
+ */
+ virtual void update(double dt) = 0;
+
+ /**
+ * Destroy the system and the components that are contained in it.
+ */
+ virtual void destroy() = 0;
+};
+
+/**
+ * Implement the default destructor, but leaving it as purely virtual in the
+ * definition of the abstract class. This is so that the classes that implement
+ * the abstract class have to implement a desctructor, but at the same time,
+ * that there is no undefined behavious when the stack unwinds to the system and
+ * calls the system destructor.
+ */
+inline System::~System() {}
+
+} // namespace yage
+
+#endif