aboutsummaryrefslogtreecommitdiffstats
path: root/yage/entity
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2018-06-22 00:14:10 +0100
committerGitHub <noreply@github.com>2018-06-22 00:14:10 +0100
commitb1673de1b46bd2e566b7c66197ad989d0323f061 (patch)
tree2cbfdab552025af7a8772069b9f9935f9c9f387f /yage/entity
parent39ef9c1cbe660a8369a33eb0ba13cc15be4cfa15 (diff)
parent3702e753a5f7b31c31261c968757e19e808a84ec (diff)
downloadYAGE-b1673de1b46bd2e566b7c66197ad989d0323f061.tar.gz
YAGE-b1673de1b46bd2e566b7c66197ad989d0323f061.zip
Merge pull request #19 from ymherklotz/develop
Develop
Diffstat (limited to 'yage/entity')
-rw-r--r--yage/entity/component.cpp8
-rw-r--r--yage/entity/component.h47
-rw-r--r--yage/entity/engine.cpp61
-rw-r--r--yage/entity/engine.h58
-rw-r--r--yage/entity/entity.cpp53
-rw-r--r--yage/entity/entity.h125
-rw-r--r--yage/entity/space.cpp29
-rw-r--r--yage/entity/space.h76
-rw-r--r--yage/entity/system.h25
9 files changed, 115 insertions, 367 deletions
diff --git a/yage/entity/component.cpp b/yage/entity/component.cpp
new file mode 100644
index 00000000..eba2ad0a
--- /dev/null
+++ b/yage/entity/component.cpp
@@ -0,0 +1,8 @@
+#include "component.h"
+
+namespace yage
+{
+
+GroupId BaseComponent::group_id_counter_ = 0;
+
+} // namespace yage
diff --git a/yage/entity/component.h b/yage/entity/component.h
new file mode 100644
index 00000000..a21409ff
--- /dev/null
+++ b/yage/entity/component.h
@@ -0,0 +1,47 @@
+#pragma once
+
+#include <bitset>
+#include <memory>
+#include <vector>
+
+namespace yage
+{
+
+/**
+ * The component mask represents all the components that the entity is
+ * currently attached to.
+ */
+typedef std::bitset<64> ComponentMask;
+
+typedef unsigned int GroupId;
+
+class BaseComponent
+{
+protected:
+ virtual GroupId getGroup() = 0;
+ static GroupId group_id_counter_;
+
+private:
+ friend class EntityManager;
+};
+
+template <typename T>
+class Component : public BaseComponent
+{
+ GroupId getGroup() override;
+};
+
+class ComponentGroup
+{
+public:
+ std::vector<std::unique_ptr<BaseComponent>> components_;
+};
+
+template <typename T>
+GroupId Component<T>::getGroup()
+{
+ static GroupId group_id = group_id_counter_++;
+ return group_id;
+}
+
+} // namespace yage
diff --git a/yage/entity/engine.cpp b/yage/entity/engine.cpp
deleted file mode 100644
index 1cef4504..00000000
--- a/yage/entity/engine.cpp
+++ /dev/null
@@ -1,61 +0,0 @@
-/** ---------------------------------------------------------------------------
- * -*- c++ -*-
- * @file: engine.cpp
- *
- * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
- * MIT License, see LICENSE file for more details.
- * ----------------------------------------------------------------------------
- */
-
-#include "engine.h"
-
-#include "../core/core.h"
-
-#include "space.h"
-
-namespace yage
-{
-
-Engine::~Engine()
-{
- quit();
-}
-
-void Engine::init()
-{
- yage::init();
- window_.create("Game Engine", 800, 640);
-}
-
-void Engine::mainLoop()
-{
- while (!window_.shouldClose()) {
- window_.pollEvents();
- window_.clearBuffer();
-
- update();
-
- window_.swapBuffer();
- }
-}
-
-void Engine::update()
-{
- for (auto &space : spaces_) {
- space->update();
- }
-}
-
-void Engine::addSpace(std::unique_ptr<Space> space)
-{
- spaces_.push_back(std::move(space));
-}
-
-Engine &Engine::instance()
-{
- static Engine engine_instance;
-
- return engine_instance;
-}
-
-} // namespace yage
diff --git a/yage/entity/engine.h b/yage/entity/engine.h
deleted file mode 100644
index 9a1f6d2b..00000000
--- a/yage/entity/engine.h
+++ /dev/null
@@ -1,58 +0,0 @@
-/** ---------------------------------------------------------------------------
- * -*- c++ -*-
- * @file: engine.h
- *
- * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
- * MIT License, see LICENSE file for more details.
- * ----------------------------------------------------------------------------
- */
-
-#pragma once
-
-#include "system.h"
-
-#include "../core/window.h"
-#include "../util/noncopyable.h"
-
-#include <memory>
-#include <vector>
-
-namespace yage
-{
-
-class Space;
-
-/**
- * Main engine class that contains a systems, the main loop and the update
- * function that updates all the systems.
- */
-class Engine : public NonCopyable
-{
-public:
- ~Engine();
-
- /// Initialize window and other aspects of the engine.
- void init();
-
- /// Main game loop of the engine.
- void mainLoop();
-
- /// Updates the systems.
- void update();
-
- /// Add spaces to the engine
- void addSpace(std::unique_ptr<Space> space);
-
- /// Returns the instance of the engine, as there is only one instance of the
- /// engine.
- static Engine &instance();
-
-private:
- /// Window
- Window window_;
-
- /// A vector of all the spaces
- std::vector<std::unique_ptr<Space>> spaces_;
-};
-
-} // namespace yage
diff --git a/yage/entity/entity.cpp b/yage/entity/entity.cpp
index 1a2b16ad..25e8e2d0 100644
--- a/yage/entity/entity.cpp
+++ b/yage/entity/entity.cpp
@@ -1,42 +1,51 @@
-/** ---------------------------------------------------------------------------
- * -*- c++ -*-
- * @file: entity.cpp
- *
- * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
- * MIT License, see LICENSE file for more details.
- * ----------------------------------------------------------------------------
- */
-
#include "entity.h"
+#include "component.h"
+
#include <algorithm>
namespace yage
{
-BaseComponent::Group BaseComponent::group_id_counter_ = 0;
+Entity EntityManager::create_entity()
+{
+ Entity entity = update_next_entity();
+ component_masks_.push_back(ComponentMask(0));
+ return entity;
+}
-EntityManager::EntityManager(Space *space) : space_(space) {}
+EntityManager &EntityManager::delete_entity(Entity entity)
+{
+ deleted_.push_back(entity);
+ return *this;
+}
-EntityManager::EntityManager(Space *space, std::size_t n) : space_(space)
+bool EntityManager::is_valid(Entity entity) const
{
- entities_.reserve(n);
+ auto it = std::find(deleted_.begin(), deleted_.end(), entity);
+ if (it == deleted_.end()) {
+ return true;
+ }
+ return false;
}
-Entity EntityManager::createEntity()
+EntityManager &EntityManager::add_component(Entity entity,
+ BaseComponent *component)
{
- Entity entity = next_entity_++;
- entities_.push_back(entity);
- return entity;
+ auto id = component->getGroup();
+ component_masks_[entity] =
+ component_masks_[entity] | ComponentMask(1 << id);
+ return *this;
}
-void EntityManager::deleteEntity(Entity entity)
+Entity EntityManager::update_next_entity()
{
- auto index = std::find_if(entities_.begin(), entities_.end(),
- [&](Entity &value) { return value == entity; });
- if (index != entities_.end()) {
- entities_.erase(index);
+ if (deleted_.empty()) {
+ return next_entity_++;
}
+ Entity ent = deleted_.back();
+ deleted_.pop_back();
+ return ent;
}
} // namespace yage
diff --git a/yage/entity/entity.h b/yage/entity/entity.h
index a032ad8a..2aa31a66 100644
--- a/yage/entity/entity.h
+++ b/yage/entity/entity.h
@@ -1,127 +1,40 @@
-/** ---------------------------------------------------------------------------
- * -*- c++ -*-
- * @file: entity.h
- *
- * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
- * MIT License, see LICENSE file for more details.
- * ----------------------------------------------------------------------------
- */
-
#pragma once
-#include <memory>
#include <vector>
+#include "component.h"
+
namespace yage
{
-class Space;
-class ComponentGroup;
-
+/**
+ * The entity is currently just an unsigned integer, which may change to a
+ * class in the future.
+ */
typedef unsigned int Entity;
-/** Manages entities in a space.
+/**
+ * Has to keep track of all the different entities and their current state.
+ *
+ * The key actions on an Entity are: deleting, creating.
*/
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.
- */
- Entity createEntity();
-
- /** Delete an entity.
- */
- void deleteEntity(Entity entity);
+ Entity create_entity();
+ EntityManager &delete_entity(Entity entity);
+ bool is_valid(Entity entity) const;
+ EntityManager &add_component(Entity entity, BaseComponent *component);
private:
- /** The next available handle to give to the user.
- */
- Entity next_entity_ = 0;
-
- /** The space that the entity manager belongs to.
- */
- Space *space_ = nullptr;
-
- /** The entities in the current space.
- */
- std::vector<Entity> entities_;
+ Entity update_next_entity();
- /** Component list of different component groups, that then contain the
- * different components for the entities.
- */
- std::vector<std::unique_ptr<ComponentGroup>> components;
-};
+ Entity next_entity_ = 0;
-/** Base component used to store components.
- *
- * Should not be inherited from when declaring a new component. Instead, the
- * Component<Derived> should be used.
- */
-class BaseComponent
-{
public:
- /** Group used to register a specific component internally with the entity
- * manager.
- */
- typedef unsigned Group;
-
-protected:
- static Group group_id_counter_;
-};
-
-/** The main component that is used to make a component from a defined struct.
- *
- * A component should only be declared as a struct and should not contain any
- * data itself.
- */
-template <typename Derived>
-class Component : public BaseComponent
-{
-private:
- friend class EntityManager;
-
- BaseComponent::Group group();
+ std::vector<ComponentGroup> component_group_;
+ std::vector<ComponentMask> component_masks_;
+ std::vector<Entity> deleted_;
};
-/** Contains a list of all components that belong to a sepecific group, these
- * are then stored in the main entity manager.
- */
-class ComponentGroup
-{
-private:
- friend class EntityManager;
-
- std::vector<BaseComponent *> components_;
-};
-
-// Template definitions
-
-template <typename Derived>
-BaseComponent::Group Component<Derived>::group()
-{
- static Group group_id = group_id_counter_++;
- return group_id;
-}
-
} // namespace yage
diff --git a/yage/entity/space.cpp b/yage/entity/space.cpp
deleted file mode 100644
index 530f5b49..00000000
--- a/yage/entity/space.cpp
+++ /dev/null
@@ -1,29 +0,0 @@
-/** ---------------------------------------------------------------------------
- * -*- c++ -*-
- * @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();
-}
-
-void Space::update()
-{
- for (auto &system : systems_) {
- system->update();
- }
-}
-
-} // namespace yage
diff --git a/yage/entity/space.h b/yage/entity/space.h
deleted file mode 100644
index 0763b189..00000000
--- a/yage/entity/space.h
+++ /dev/null
@@ -1,76 +0,0 @@
-/** ---------------------------------------------------------------------------
- * -*- c++ -*-
- * @file: space.h
- *
- * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
- * MIT License, see LICENSE file for more details.
- * ----------------------------------------------------------------------------
- */
-
-#pragma once
-
-#include <memory>
-#include <vector>
-
-#include "entity.h"
-#include "system.h"
-
-struct Movement {
-};
-
-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.
- */
- Entity createEntity();
-
- /**
- * Update all the systems.
- */
- void update();
-
-private:
- /**
- * The systems 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<std::unique_ptr<System>> systems_;
-
- /**
- * Manages all the entities in the system, can create them for the
- * current space.
- */
- EntityManager em_;
-
- /**
- * Manages all the components
- */
- // ComponentManager cm_;
-};
-
-} // namespace yage
diff --git a/yage/entity/system.h b/yage/entity/system.h
index 84536cc9..4f8306a8 100644
--- a/yage/entity/system.h
+++ b/yage/entity/system.h
@@ -1,26 +1,21 @@
-/** ---------------------------------------------------------------------------
- * -*- c++ -*-
- * @file: system.h
- *
- * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
- * MIT License, see LICENSE file for more details.
- * ----------------------------------------------------------------------------
- */
-
#pragma once
-#include <cstddef>
-
-#include "../util/noncopyable.h"
+#include "entity.h"
namespace yage
{
-class System : public NonCopyable
+class BaseSystem
{
public:
- virtual void init() = 0;
- virtual void update() = 0;
+ virtual void update(double dt, EntityManager &em) = 0;
+};
+
+template <typename T>
+class System : public BaseSystem
+{
+protected:
+ ComponentMask active_mask_;
};
} // namespace yage