瀏覽代碼

Lesson 10.3

Godzil 3 年之前
父節點
當前提交
cfbc385542
共有 2 個文件被更改,包括 52 次插入0 次删除
  1. 10 0
      source/include/ECS.h
  2. 42 0
      source/include/Pool.h

+ 10 - 0
source/include/ECS.h

@@ -15,6 +15,8 @@
 #include <bitset>
 #include <vector>
 
+#include <Pool.h>
+
 const uint32_t MAX_COMPONENTS = 32;
 
 typedef std::bitset<MAX_COMPONENTS> Signature;
@@ -79,6 +81,14 @@ template<typename T> void System::requireComponent()
 
 class Registry
 {
+private:
+    uint32_t numEntities = 0;
+
+    std::vector<IPool*> componentPools;
+
+public:
+    Registry() = default;
+    ~Registry() = default;
 
 };
 

+ 42 - 0
source/include/Pool.h

@@ -0,0 +1,42 @@
+/*
+ * 2D Game Engine 
+ * Pool.h: 
+ * Based on pikuma.com 2D game engine in C++ and Lua course
+ * Copyright (c) 2021 986-Studio. All rights reserved.
+ *
+ * Created by Manoël Trapier on 11/02/2021.
+ */
+
+#ifndef GAMEENGINE_POOL_H
+#define GAMEENGINE_POOL_H
+
+#include <vector>
+
+class IPool
+{
+public:
+    virtual ~IPool() = default;
+};
+
+template <typename T> class Pool:public IPool
+{
+private:
+    std::vector<T> data;
+
+public:
+    Pool(int size = 100) { this->resize(size); }
+    virtual ~Pool() = default;
+
+    bool isEmpty() const { return this->data.empty(); }
+    void resize(int size) { this->data.reserve(size); }
+    void clear() { this->data.clear(); }
+
+    void add(T object) { this->data.push_back(object); }
+
+    void set(uint32_t index, T object) { this->data[index] = object; }
+    T& get(uint32_t index) { return this->data[index]; }
+
+    T& operator[](uint32_t index) { return this->data[index]; }
+};
+
+#endif /* GAMEENGINE_POOL_H */