Copperfield Engine 0.1
C++ Game Engine
Loading...
Searching...
No Matches
compact_list.hpp
1#pragma once
2#include "component_lists.hpp"
3#include <optional>
4#include <vector>
5
6#include "component_manager.hpp"
7#include "texture.hpp"
8
14template <typename T>
15class ComponentListCompact : public ComponentListBase {
16public:
17 friend class ComponentManager;
18 ComponentListCompact() : ComponentListBase(ComponentListType::kCompact) {}
19 using ComponentType = T;
21 struct Iterator {
22 using iterator_category = std::forward_iterator_tag;
23 using difference_type = std::ptrdiff_t;
24 using value_type = std::pair<unsigned, T>;
25 using pointer = std::pair<unsigned, T>*;
26 using reference = std::pair<unsigned, T>&;
27
28 Iterator(pointer ptr, bool ends = false) : m_ptr_(ptr), valid_{!ends} {}
29
30 reference operator*() const { return *m_ptr_; }
31 pointer operator->() { return m_ptr_; }
32
33 Iterator& operator++() {
34 ++m_ptr_;
35 return *this;
36 }
37 Iterator& operator--() {
38 --m_ptr_;
39 return *this;
40 }
41 Iterator operator++(int) {
42 Iterator tmp = *this;
43 ++(*this);
44 return tmp;
45 }
46 Iterator operator--(int) {
47 Iterator tmp = *this;
48 --(*this);
49 return tmp;
50 }
51 friend bool operator==(const Iterator& a, const Iterator& b) {
52 return a.m_ptr_ == b.m_ptr_;
53 }
54 friend bool operator!=(const Iterator& a, const Iterator& b) {
55 return a.m_ptr_ != b.m_ptr_;
56 }
57 int pos() const { return m_ptr_->first; }
58 bool valid() const { return valid_; }
59 T& component() const { return m_ptr_->second; }
60
61 private:
62 pointer m_ptr_;
63 bool valid_;
64 };
65
68 Iterator begin() {
69 return Iterator(components_.data());
70 }
71
75 Iterator at(unsigned e) {
76 // TODO: possible bug converting form vector iterator to
77 // ComponentListCompact
78 auto lb =
79 std::lower_bound(components_.begin(), components_.end(), e, compare);
80
81 size_t pos = lb - components_.begin();
82 if (lb != components_.end() && lb->first == e) return Iterator(&components_.at(pos));
83 return end();
84 }
87 Iterator end() {
88 if (components_.empty()) return begin();
89 Iterator it = Iterator(
90 &components_[static_cast<unsigned>(components_.size()) - 1], true);
91 ++it;
92 return it;
93 }
94
99 int size() { return static_cast<int>(components_.size()); }
100
101private:
102 static bool compare(std::pair<unsigned, T>& x, unsigned e) {
103 return x.first < e;
104 }
105
106 // does nothing since it doesn't have to insert emptys for each entity
107 bool addEntity(unsigned) override { return false; }
108
109 bool moveComponent(unsigned src, unsigned dst) override {
110 bool res = true;
111 auto src_it =
112 std::lower_bound(components_.begin(), components_.end(), dst, compare);
113 if(src_it == components_.end())
114 return false;
115 if (src_it->first != src)
116 return false;
117 res &= setComponent(dst, src_it->second);
118 res &= removeComponent(src);
119
120 return res;
121 }
122
123 // if the entity doesn't exist inserts a new pair
124 bool setComponent(unsigned e,const T& c) {
125 auto lb =
126 std::lower_bound(components_.begin(), components_.end(), e, compare);
127 if (lb == components_.end()) {
128 components_.emplace_back(std::make_pair(e, c));
129 return true;
130 } else if (lb->first != e) {
131 components_.insert(lb, std::make_pair(e, c));
132 return true;
133 }
134 return false;
135 }
136
137 // deletes a specific components if exists
138 bool removeComponent(unsigned e) override {
139 auto lb =
140 std::lower_bound(components_.begin(), components_.end(), e, compare);
141 if (lb != components_.end() && lb->first == e) {
142 components_.erase(lb);
143 return true;
144 }
145 return false;
146 }
147
148 T* getComp(unsigned e) {
149 auto lb =
150 std::lower_bound(components_.begin(), components_.end(), e, compare);
151 if (lb != components_.end() && lb->first == e) return &lb->second;
152 return nullptr;
153 }
154
155 std::vector<std::pair<unsigned, T>> components_;
156};
Handles anything related to entities and components in the engine.
Definition: component_manager.hpp:29
base class for component lists
ComponentListType
type of container to be used for storing componets
Definition: component_lists.hpp:8
@ kCompact
should be used for compoenents used by few entities