code cleanup
This commit is contained in:
parent
e35ddf2ce4
commit
2dcf656af4
|
|
@ -11,6 +11,6 @@ Pos=60,60
|
|||
Size=536,286
|
||||
|
||||
[Window][sdfkjasbdf]
|
||||
Pos=679,86
|
||||
Pos=679,85
|
||||
Size=610,481
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
namespace container
|
||||
{
|
||||
|
||||
using CapacityType = EntityIndex;
|
||||
using BoolArrayType = u8;
|
||||
|
||||
|
|
@ -22,69 +23,69 @@ public:
|
|||
using iterator_category = std::forward_iterator_tag;
|
||||
|
||||
public:
|
||||
class Iterator
|
||||
class iterator
|
||||
{
|
||||
public:
|
||||
Iterator(data_type* const input)
|
||||
iterator(data_type* input)
|
||||
: m_ptr(input)
|
||||
{ }
|
||||
|
||||
Iterator& operator++()
|
||||
iterator& operator++()
|
||||
{ m_ptr++; return *this; }
|
||||
|
||||
Iterator operator++(int)
|
||||
iterator operator++(int)
|
||||
{ auto it = *this; m_ptr++; return it; }
|
||||
|
||||
Iterator& operator--()
|
||||
iterator& operator--()
|
||||
{ m_ptr--; return *this; }
|
||||
|
||||
Iterator operator--(int)
|
||||
iterator operator--(int)
|
||||
{ auto it = *this; m_ptr++; return it; }
|
||||
|
||||
|
||||
reference operator[](const CapacityType index) const { return m_ptr[index]; }
|
||||
reference operator[](CapacityType index) const { return m_ptr[index]; }
|
||||
pointer operator->() const { return m_ptr; }
|
||||
reference operator*() const { return *m_ptr; }
|
||||
|
||||
bool operator==(const Iterator& other) const { return m_ptr == other.m_ptr; }
|
||||
bool operator!=(const Iterator& other) const { return m_ptr != other.m_ptr; }
|
||||
bool operator==(iterator other) const { return m_ptr == other.m_ptr; }
|
||||
bool operator!=(iterator other) const { return m_ptr != other.m_ptr; }
|
||||
private:
|
||||
pointer m_ptr;
|
||||
};
|
||||
|
||||
class ConstIterator
|
||||
class const_iterator
|
||||
{
|
||||
public:
|
||||
ConstIterator(data_type* const input)
|
||||
const_iterator(data_type* input)
|
||||
: m_ptr(input)
|
||||
{ }
|
||||
|
||||
ConstIterator& operator++()
|
||||
const_iterator& operator++()
|
||||
{ m_ptr++; return *this; }
|
||||
|
||||
ConstIterator operator++(int)
|
||||
const_iterator operator++(int)
|
||||
{ auto it = *this; m_ptr++; return it; }
|
||||
|
||||
ConstIterator& operator--()
|
||||
const_iterator& operator--()
|
||||
{ m_ptr--; return *this; }
|
||||
|
||||
ConstIterator operator--(int)
|
||||
const_iterator operator--(int)
|
||||
{ auto it = *this; m_ptr++; return it; }
|
||||
|
||||
|
||||
const_reference operator[](const CapacityType index) const { return m_ptr[index]; }
|
||||
const_reference operator[](CapacityType index) const { return m_ptr[index]; }
|
||||
const_pointer operator->() const { return m_ptr; }
|
||||
const_reference operator*() const { return *m_ptr; }
|
||||
|
||||
bool operator==(const ConstIterator& other) const { return m_ptr == other.m_ptr; }
|
||||
bool operator!=(const ConstIterator& other) const { return m_ptr != other.m_ptr; }
|
||||
bool operator==(const_iterator other) const { return m_ptr == other.m_ptr; }
|
||||
bool operator!=(const_iterator other) const { return m_ptr != other.m_ptr; }
|
||||
|
||||
private:
|
||||
pointer m_ptr;
|
||||
};
|
||||
|
||||
public:
|
||||
Array() { m_data = new T[capacity]; }
|
||||
Array() { m_data = new T[capacity]{}; }
|
||||
|
||||
~Array() { delete[] m_data; }
|
||||
|
||||
|
|
@ -114,9 +115,9 @@ public:
|
|||
|
||||
const_pointer cosntData() const { return m_data; }
|
||||
|
||||
reference operator[](const CapacityType index) { return m_data[index]; }
|
||||
reference operator[](CapacityType index) { return m_data[index]; }
|
||||
|
||||
const_reference operator[](const CapacityType index) const { return m_data[index]; }
|
||||
const_reference operator[](CapacityType index) const { return m_data[index]; }
|
||||
|
||||
pointer operator->() { return m_data; }
|
||||
|
||||
|
|
@ -128,11 +129,11 @@ public:
|
|||
|
||||
inline constexpr CapacityType size() const { return capacity; }
|
||||
|
||||
Iterator begin() { return Iterator(m_data); }
|
||||
Iterator end() { return Iterator(m_data + capacity); }
|
||||
iterator begin() { return iterator(m_data); }
|
||||
iterator end() { return iterator(m_data + capacity); }
|
||||
|
||||
ConstIterator begin() const { return ConstIterator(m_data); }
|
||||
ConstIterator end() const { return ConstIterator(m_data + capacity); }
|
||||
const_iterator begin() const { return const_iterator(m_data); }
|
||||
const_iterator end() const { return const_iterator(m_data + capacity); }
|
||||
|
||||
private:
|
||||
pointer m_data;
|
||||
|
|
@ -145,10 +146,9 @@ class Array<bool, capacity>
|
|||
{
|
||||
public:
|
||||
class reference;
|
||||
class const_reference;
|
||||
using data_type = BoolArrayType;
|
||||
using pointer = reference;
|
||||
using const_pointer = const_reference;
|
||||
using const_reference = bool;
|
||||
using data_type = BoolArrayType;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using iterator_category = std::forward_iterator_tag;
|
||||
|
||||
|
|
@ -156,7 +156,7 @@ public:
|
|||
class reference
|
||||
{
|
||||
friend class Array;
|
||||
reference(data_type* const ptr_in, const data_type mask_in)
|
||||
reference(data_type* ptr_in, data_type mask_in)
|
||||
: m_ptr(ptr_in)
|
||||
, m_mask(mask_in)
|
||||
{
|
||||
|
|
@ -167,14 +167,14 @@ public:
|
|||
~reference()
|
||||
{ }
|
||||
|
||||
reference(const reference&) = default;
|
||||
reference(reference&) = default;
|
||||
|
||||
operator bool () const
|
||||
{
|
||||
return !!(*m_ptr & m_mask);
|
||||
}
|
||||
|
||||
reference& operator=(const bool input)
|
||||
reference& operator=(bool input)
|
||||
{
|
||||
switch ((int)input)
|
||||
{
|
||||
|
|
@ -182,7 +182,7 @@ public:
|
|||
*m_ptr |= m_mask;
|
||||
break;
|
||||
case false:
|
||||
*m_ptr &= m_mask;
|
||||
*m_ptr &= ~m_mask;
|
||||
break;
|
||||
}
|
||||
return *this;
|
||||
|
|
@ -203,42 +203,44 @@ public:
|
|||
const data_type m_mask;
|
||||
};
|
||||
|
||||
class const_reference
|
||||
{
|
||||
friend class Array;
|
||||
const_reference(const data_type* ptr_in, const data_type mask_in)
|
||||
: m_ptr(ptr_in)
|
||||
, m_mask(mask_in)
|
||||
{ }
|
||||
|
||||
public:
|
||||
~const_reference()
|
||||
{ }
|
||||
//class const_reference
|
||||
//{
|
||||
// friend class Array;
|
||||
// const_reference(const data_type* ptr_in, const data_type mask_in)
|
||||
// : m_ptr(ptr_in)
|
||||
// , m_mask(mask_in)
|
||||
// { }
|
||||
|
||||
const_reference(const const_reference&) = delete;
|
||||
//public:
|
||||
// ~const_reference()
|
||||
// { }
|
||||
|
||||
operator bool () const { return !!(*m_ptr & m_mask); }
|
||||
// const_reference(const const_reference&) = delete;
|
||||
|
||||
const_reference& operator=(bool) = delete;
|
||||
// operator bool () const { return !!(*m_ptr & m_mask); }
|
||||
|
||||
const_reference& operator=(const const_reference& other) = delete;
|
||||
const_reference& operator=(const reference& other) = delete;
|
||||
// const_reference& operator=(bool) = delete;
|
||||
|
||||
private:
|
||||
const data_type* const m_ptr;
|
||||
const data_type m_mask;
|
||||
};
|
||||
// const_reference& operator=(const const_reference& other) = delete;
|
||||
// const_reference& operator=(const reference& other) = delete;
|
||||
|
||||
//private:
|
||||
// const data_type* const m_ptr;
|
||||
// const data_type m_mask;
|
||||
//};
|
||||
|
||||
|
||||
class Iterator
|
||||
|
||||
class iterator
|
||||
{
|
||||
public:
|
||||
Iterator(data_type* const input, const data_type index)
|
||||
iterator(const data_type* input, data_type index)
|
||||
: m_ptr(input)
|
||||
, m_bitIndex(index)
|
||||
{ }
|
||||
|
||||
Iterator& operator++()
|
||||
iterator& operator++()
|
||||
{
|
||||
switch (m_bitIndex++)
|
||||
{
|
||||
|
|
@ -253,7 +255,7 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
Iterator operator++(int)
|
||||
iterator operator++(int)
|
||||
{
|
||||
auto it = *this;
|
||||
switch (m_bitIndex++)
|
||||
|
|
@ -269,7 +271,7 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
Iterator& operator--()
|
||||
iterator& operator--()
|
||||
{
|
||||
switch (m_bitIndex--)
|
||||
{
|
||||
|
|
@ -284,7 +286,7 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
Iterator operator--(int)
|
||||
iterator operator--(int)
|
||||
{
|
||||
auto it = *this;
|
||||
switch (m_bitIndex--)
|
||||
|
|
@ -300,18 +302,22 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
bool operator[](const int) = delete;
|
||||
reference operator[](CapacityType index)
|
||||
{
|
||||
return reference(m_ptr + (index / 7), m_bitIndex + (index % 8));
|
||||
}
|
||||
|
||||
reference operator*() const
|
||||
{
|
||||
return reference(m_ptr, 1 << m_bitIndex);
|
||||
}
|
||||
|
||||
bool operator==(const Iterator& other) const
|
||||
bool operator==(const iterator& other) const
|
||||
{
|
||||
return (m_ptr == other.m_ptr) && (m_bitIndex == other.m_bitIndex);
|
||||
}
|
||||
|
||||
bool operator!=(const Iterator& other) const
|
||||
bool operator!=(const iterator& other) const
|
||||
{
|
||||
return (m_ptr != other.m_ptr) || (m_bitIndex != other.m_bitIndex);
|
||||
}
|
||||
|
|
@ -321,15 +327,15 @@ public:
|
|||
data_type m_bitIndex{};
|
||||
};
|
||||
|
||||
class ConstIterator
|
||||
class const_iterator
|
||||
{
|
||||
public:
|
||||
ConstIterator(const data_type* input, const data_type index)
|
||||
const_iterator(const data_type* input, data_type index)
|
||||
: m_ptr(input)
|
||||
, m_bitIndex(index)
|
||||
{ }
|
||||
|
||||
ConstIterator& operator++()
|
||||
const_iterator& operator++()
|
||||
{
|
||||
switch (m_bitIndex++)
|
||||
{
|
||||
|
|
@ -344,7 +350,7 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
ConstIterator operator++(int)
|
||||
const_iterator operator++(int)
|
||||
{
|
||||
auto it = *this;
|
||||
switch (m_bitIndex++)
|
||||
|
|
@ -360,7 +366,7 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
ConstIterator& operator--()
|
||||
const_iterator& operator--()
|
||||
{
|
||||
switch (m_bitIndex--)
|
||||
{
|
||||
|
|
@ -375,7 +381,7 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
ConstIterator operator--(int)
|
||||
const_iterator operator--(int)
|
||||
{
|
||||
auto it = *this;
|
||||
switch (m_bitIndex--)
|
||||
|
|
@ -391,15 +397,17 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
bool operator[](const int) = delete;
|
||||
const_reference operator[](CapacityType index) const
|
||||
{ return (m_ptr + (index / 8)) & (1 << (index % 8)); }
|
||||
|
||||
const_reference operator*() const
|
||||
{ return const_reference(m_ptr, 1 << m_bitIndex); }
|
||||
{ return *m_ptr & (1 << m_bitIndex); }
|
||||
|
||||
bool operator==(const ConstIterator& other) const
|
||||
bool operator==(const const_iterator& other) const
|
||||
{ return (m_ptr == other.m_ptr) && (m_bitIndex == other.m_bitIndex); }
|
||||
|
||||
|
||||
bool operator!=(const ConstIterator& other) const
|
||||
bool operator!=(const const_iterator& other) const
|
||||
{ return (m_ptr != other.m_ptr) || (m_bitIndex != other.m_bitIndex); }
|
||||
|
||||
private:
|
||||
|
|
@ -408,7 +416,7 @@ public:
|
|||
};
|
||||
|
||||
public:
|
||||
Array() { m_data = new data_type[(capacity / 8) + 1]; }
|
||||
Array() { m_data = new data_type[(capacity / 8) + 1]{}; }
|
||||
|
||||
~Array() { delete[] m_data; }
|
||||
|
||||
|
|
@ -433,20 +441,19 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
|
||||
//use readAt() instead
|
||||
reference operator[](const CapacityType index)
|
||||
reference operator[](CapacityType index)
|
||||
{ return reference(m_data + (index / 8), 1 << (index % 8)); }
|
||||
|
||||
|
||||
const_reference operator[](const CapacityType index) const
|
||||
{ return reference(m_data + (index / 8), 1 << (index % 8)); }
|
||||
const_reference operator[](CapacityType index) const
|
||||
{ return m_data[index / 8] & (1 << (index % 8)); }
|
||||
|
||||
inline constexpr CapacityType size() { return capacity; }
|
||||
|
||||
bool readAt(const CapacityType index) const { return m_data[index / 8] & (1 << (index % 8)); }
|
||||
const_reference readAt(CapacityType index) const
|
||||
{ return m_data[index / 8] & (1 << (index % 8)); }
|
||||
|
||||
void changeAt(const CapacityType index, const bool input)
|
||||
void changeAt(CapacityType index, bool input)
|
||||
{
|
||||
switch ((int)input)
|
||||
{
|
||||
|
|
@ -459,11 +466,11 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
Iterator begin() { return Iterator(m_data, 0); }
|
||||
Iterator end() { return Iterator(m_data + (capacity / 8), capacity % 8); }
|
||||
iterator begin() { return iterator(m_data, 0); }
|
||||
iterator end() { return iterator(m_data + (capacity / 8), capacity % 8); }
|
||||
|
||||
ConstIterator begin() const { return ConstIterator(m_data, 0); }
|
||||
ConstIterator end() const { return ConstIterator(m_data + (capacity / 8), capacity % 8); }
|
||||
const_iterator begin() const { return const_iterator(m_data, 0); }
|
||||
const_iterator end() const { return const_iterator(m_data + (capacity / 8), capacity % 8); }
|
||||
|
||||
private:
|
||||
data_type* m_data;
|
||||
|
|
|
|||
|
|
@ -28,11 +28,13 @@ public:
|
|||
class Texture : public Component
|
||||
{
|
||||
public:
|
||||
sf::Texture& texture;
|
||||
Texture() = default;
|
||||
int texture{};
|
||||
};
|
||||
|
||||
class BoundingBox : public Component
|
||||
{
|
||||
public:
|
||||
BoundingBox() = default;
|
||||
sf::FloatRect bBox{};
|
||||
};
|
||||
|
|
@ -3,16 +3,15 @@
|
|||
#include <utility.h>
|
||||
|
||||
class EntityManager;
|
||||
class EntityViewIterator;
|
||||
class EntityViewConstIterator;
|
||||
class EntityView;
|
||||
class EntityMemoryPool;
|
||||
|
||||
class Entity
|
||||
{
|
||||
//private:
|
||||
public:
|
||||
friend class EntityManager;
|
||||
friend class EntityViewIterator;
|
||||
friend class EntityViewConstIterator;
|
||||
friend class EntityView;
|
||||
friend class EntityMemoryPool;
|
||||
|
||||
Entity() = delete;
|
||||
Entity(EntityIndex);
|
||||
|
|
@ -25,14 +24,11 @@ public:
|
|||
T& getComponent() const;
|
||||
|
||||
template<typename T>
|
||||
T& getComponent();
|
||||
void addComponent(const T&);
|
||||
|
||||
template<typename T>
|
||||
void addComponent(const T&&);
|
||||
size_t id() const;
|
||||
|
||||
EntityIndex id() const;
|
||||
|
||||
EntityTag tag() const;
|
||||
Tag tag() const;
|
||||
|
||||
bool isAlive() const;
|
||||
|
||||
|
|
|
|||
|
|
@ -4,18 +4,18 @@
|
|||
#include "Entities/EntityView.h"
|
||||
#include "utility.h"
|
||||
#include <Entities/EntityMemoryPool.h>
|
||||
#include <array>
|
||||
#include <Containers.h>
|
||||
|
||||
class EntityManager
|
||||
{
|
||||
public:
|
||||
void update();
|
||||
private:
|
||||
EntityView getEntiites(EntityTag);
|
||||
|
||||
EntityView getEntiites(Tag);
|
||||
|
||||
inline Entity player();
|
||||
|
||||
private:
|
||||
Entity m_player{0};
|
||||
std::array<u16, tagCount> m_numEntitiesByTag{};
|
||||
EntityIndex m_numEntitiesByTag[util::TAG_COUNT];
|
||||
u16 m_numEntities{};
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,50 +1,73 @@
|
|||
#pragma once
|
||||
|
||||
#include "utility.h"
|
||||
#include <cstddef>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
#include <Entities/Components.h>
|
||||
#include <Entities/Entity.h>
|
||||
#include <Containers.h>
|
||||
#include <utility.h>
|
||||
|
||||
using ComponentVectorTuple = std::tuple
|
||||
class EntityManager;
|
||||
class Entity;
|
||||
|
||||
using ComponentsContainer = std::tuple
|
||||
<
|
||||
std::vector<Transform>,
|
||||
std::vector<Texture>,
|
||||
std::vector<BoundingBox>
|
||||
container::Array<Transform, util::MAX_ENTITIES>,
|
||||
container::Array<Texture, util::MAX_ENTITIES>,
|
||||
container::Array<BoundingBox, util::MAX_ENTITIES>
|
||||
>;
|
||||
|
||||
class EntityMemoryPool
|
||||
{
|
||||
private:
|
||||
ComponentVectorTuple m_components;
|
||||
std::vector<EntityTag> m_tags;
|
||||
std::vector<bool> m_aliveStates;
|
||||
std::vector<size_t> m_ids;
|
||||
friend class EntityManager;
|
||||
friend class Entity;
|
||||
|
||||
ComponentsContainer m_components;
|
||||
container::Array<Tag, util::MAX_ENTITIES> m_tags;
|
||||
container::Array<bool, util::MAX_ENTITIES> m_aliveStates;
|
||||
container::Array<size_t, util::MAX_ENTITIES> m_ids;
|
||||
|
||||
private:
|
||||
EntityMemoryPool();
|
||||
|
||||
EntityMemoryPool(const EntityMemoryPool&) = delete;
|
||||
|
||||
public:
|
||||
static EntityMemoryPool& instance();
|
||||
static EntityMemoryPool& instance()
|
||||
{
|
||||
static EntityMemoryPool pool{};
|
||||
return pool;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool hasComponent(EntityIndex) const;
|
||||
bool hasComponent(EntityIndex index) const
|
||||
{ return std::get<container::Array<T, util::MAX_ENTITIES>>(m_components)[index].active; }
|
||||
|
||||
template<typename T>
|
||||
T& getComponent(EntityIndex) const;
|
||||
T& getComponent(EntityIndex index)
|
||||
{
|
||||
return std::get<container::Array<T, util::MAX_ENTITIES>>(m_components)[index];
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T& getComponent(EntityIndex);
|
||||
Tag getTag(EntityIndex index) const
|
||||
{
|
||||
return m_tags[index];
|
||||
}
|
||||
|
||||
EntityTag getTag(EntityIndex) const;
|
||||
bool getAlive(EntityIndex index) const
|
||||
{
|
||||
return m_aliveStates[index];
|
||||
}
|
||||
|
||||
bool getAlive(EntityIndex) const;
|
||||
size_t& getId(EntityIndex index)
|
||||
{
|
||||
return m_ids[index];
|
||||
}
|
||||
|
||||
size_t getId(EntityIndex) const;
|
||||
|
||||
void removeEntity(EntityIndex);
|
||||
void removeEntity(EntityIndex index)
|
||||
{
|
||||
m_aliveStates[index] = false;
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
|
@ -3,59 +3,6 @@
|
|||
#include "utility.h"
|
||||
#include <Entities/Entity.h>
|
||||
|
||||
class EntityView;
|
||||
|
||||
class EntityViewIterator
|
||||
{
|
||||
friend class EntityView;
|
||||
|
||||
EntityViewIterator(EntityIndex);
|
||||
|
||||
EntityViewIterator& operator++();
|
||||
|
||||
EntityViewIterator operator++(int);
|
||||
|
||||
EntityViewIterator& operator--();
|
||||
|
||||
EntityViewIterator operator--(int);
|
||||
|
||||
Entity operator*();
|
||||
|
||||
Entity operator[](int);
|
||||
|
||||
bool operator==(EntityViewIterator);
|
||||
bool operator!=(EntityViewIterator);
|
||||
|
||||
private:
|
||||
EntityIndex m_currentEntity;
|
||||
};
|
||||
|
||||
class EntityViewConstIterator
|
||||
{
|
||||
friend class EntityView;
|
||||
|
||||
EntityViewConstIterator(EntityIndex index);
|
||||
|
||||
EntityViewConstIterator& operator++();
|
||||
|
||||
EntityViewConstIterator operator++(int);
|
||||
|
||||
EntityViewConstIterator& operator--();
|
||||
|
||||
EntityViewConstIterator operator--(int);
|
||||
|
||||
const Entity operator*();
|
||||
|
||||
const Entity operator[](int index);
|
||||
|
||||
|
||||
bool operator==(EntityViewConstIterator);
|
||||
bool operator!=(EntityViewConstIterator);
|
||||
|
||||
private:
|
||||
EntityIndex m_currentEntity;
|
||||
};
|
||||
|
||||
class EntityView
|
||||
{
|
||||
public:
|
||||
|
|
@ -63,8 +10,40 @@ public:
|
|||
EntityView() = delete;
|
||||
EntityView(EntityIndex, EntityIndex);
|
||||
|
||||
using iterator = EntityViewIterator;
|
||||
using const_iterator = EntityViewConstIterator;
|
||||
|
||||
class iterator
|
||||
{
|
||||
public:
|
||||
iterator(EntityIndex);
|
||||
iterator& operator++();
|
||||
iterator operator++(int);
|
||||
iterator& operator--();
|
||||
iterator operator--(int);
|
||||
Entity operator*();
|
||||
Entity operator[](int);
|
||||
bool operator==(iterator);
|
||||
bool operator!=(iterator);
|
||||
private:
|
||||
EntityIndex m_currentEntity;
|
||||
};
|
||||
|
||||
class const_iterator
|
||||
{
|
||||
public:
|
||||
const_iterator(EntityIndex index);
|
||||
const_iterator& operator++();
|
||||
const_iterator operator++(int);
|
||||
const_iterator& operator--();
|
||||
const_iterator operator--(int);
|
||||
const Entity operator*();
|
||||
const Entity operator[](int index);
|
||||
bool operator==(const_iterator);
|
||||
bool operator!=(const_iterator);
|
||||
private:
|
||||
EntityIndex m_currentEntity;
|
||||
};
|
||||
|
||||
Entity operator[](EntityIndex) const;
|
||||
|
||||
iterator begin() { return iterator(m_start); }
|
||||
iterator end() { return iterator(m_start + m_size); }
|
||||
|
|
|
|||
|
|
@ -12,10 +12,11 @@ using s8 = int8_t;
|
|||
using s16 = int16_t;
|
||||
using s32 = int32_t;
|
||||
using s64 = int64_t;
|
||||
using tag_type = u8;
|
||||
|
||||
using EntityIndex = u16;
|
||||
|
||||
enum EntityTag : u8
|
||||
enum class Tag : tag_type
|
||||
{
|
||||
player,
|
||||
tile,
|
||||
|
|
@ -23,8 +24,6 @@ enum EntityTag : u8
|
|||
tagCount
|
||||
};
|
||||
|
||||
|
||||
|
||||
namespace util
|
||||
{
|
||||
using namespace std::string_view_literals;
|
||||
|
|
@ -33,28 +32,36 @@ namespace util
|
|||
inline constexpr EntityIndex MAX_TILES {1'000u};
|
||||
inline constexpr EntityIndex MAX_ENEMIES {1'000u};
|
||||
inline constexpr EntityIndex MAX_ENTITIES {MAX_PLAYERS + MAX_TILES + MAX_ENEMIES};
|
||||
inline constexpr tag_type TAG_COUNT {(tag_type)Tag::tagCount};
|
||||
|
||||
// used for imgui
|
||||
inline constexpr std::array<const char*, tagCount> tagStringsC =
|
||||
inline constexpr char* tagStringsC[TAG_COUNT] =
|
||||
{
|
||||
"player",
|
||||
"tile",
|
||||
"enemy"
|
||||
"enemy",
|
||||
};
|
||||
|
||||
inline constexpr std::array<std::string_view, tagCount> tagStrings =
|
||||
inline constexpr std::string_view tagStrings[TAG_COUNT] =
|
||||
{
|
||||
"player"sv,
|
||||
"tile"sv,
|
||||
"enemy"sv
|
||||
"enemy"sv,
|
||||
};
|
||||
|
||||
inline constexpr std::array<EntityIndex, tagCount> tagStart =
|
||||
inline constexpr EntityIndex tagStart[TAG_COUNT] =
|
||||
{
|
||||
0,//player
|
||||
1,//tile start
|
||||
MAX_TILES,//enemy start
|
||||
};
|
||||
|
||||
inline constexpr EntityIndex tagSize[TAG_COUNT] =
|
||||
{
|
||||
MAX_PLAYERS,
|
||||
MAX_TILES,
|
||||
MAX_ENEMIES,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
21
premake5.lua
21
premake5.lua
|
|
@ -153,15 +153,16 @@ project "2d-platformer"
|
|||
|
||||
--config settings
|
||||
filter "configurations:debug"
|
||||
defines"LOG_ENABLE"
|
||||
symbols "on"
|
||||
runtime "Debug"
|
||||
kind "ConsoleApp"
|
||||
defines {"LOG_ENABLE", "GAME_DEBUG"}
|
||||
symbols "on"
|
||||
runtime "Debug"
|
||||
kind "ConsoleApp"
|
||||
|
||||
filter "configurations:release"
|
||||
optimize "Speed"
|
||||
inlining "Auto"
|
||||
symbols "off"
|
||||
runtime "Release"
|
||||
kind "WindowedApp"
|
||||
entrypoint "mainCRTStartup"
|
||||
defines {"GAME_RELEASE"}
|
||||
optimize "Speed"
|
||||
inlining "Auto"
|
||||
symbols "off"
|
||||
runtime "Release"
|
||||
kind "WindowedApp"
|
||||
entrypoint "mainCRTStartup"
|
||||
|
|
|
|||
|
|
@ -23,25 +23,19 @@ T& Entity::getComponent() const
|
|||
}
|
||||
|
||||
template<typename T>
|
||||
T& Entity::getComponent()
|
||||
{
|
||||
return EntityMemoryPool::instance().getComponent<T>(m_index);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Entity::addComponent(const T&& data)
|
||||
void Entity::addComponent(const T& data)
|
||||
{
|
||||
T& component = EntityMemoryPool::instance().getComponent<T>(m_index);
|
||||
component = data;
|
||||
component.active = true;
|
||||
}
|
||||
|
||||
EntityIndex Entity::id() const
|
||||
size_t Entity::id() const
|
||||
{
|
||||
return EntityMemoryPool::instance().getId(m_index);
|
||||
}
|
||||
|
||||
EntityTag Entity::tag() const
|
||||
Tag Entity::tag() const
|
||||
{
|
||||
return EntityMemoryPool::instance().getTag(m_index);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
#include "Entities/EntityView.h"
|
||||
#include "utility.h"
|
||||
#include <Entities/EntityManager.h>
|
||||
#include <Entities/Entity.h>
|
||||
|
||||
inline constexpr EntityIndex PLAYER_INDEX = 0;
|
||||
#include "utility.h"
|
||||
|
||||
inline constexpr EntityIndex PLAYER_INDEX = util::tagStart[(tag_type)Tag::player];
|
||||
|
||||
void EntityManager::update()
|
||||
{
|
||||
|
|
@ -12,10 +13,10 @@ void EntityManager::update()
|
|||
|
||||
inline Entity EntityManager::player()
|
||||
{
|
||||
return m_player;
|
||||
return Entity(PLAYER_INDEX);
|
||||
}
|
||||
|
||||
EntityView EntityManager::getEntiites(EntityTag tag)
|
||||
EntityView EntityManager::getEntiites(Tag tag)
|
||||
{
|
||||
return EntityView(util::tagStart[tag], m_numEntitiesByTag[tag]);
|
||||
return EntityView(util::tagStart[(tag_type)tag], m_numEntitiesByTag[(tag_type)tag]);
|
||||
}
|
||||
|
|
@ -4,55 +4,22 @@
|
|||
#include <cstddef>
|
||||
#include <vector>
|
||||
#include <utility.h>
|
||||
#include <Entities/EntityView.h>
|
||||
|
||||
EntityMemoryPool::EntityMemoryPool()
|
||||
{
|
||||
/*
|
||||
std::apply([=](auto&&... args) {((args.reserve(util::MAX_ENTITIES)), ...); }, m_components);
|
||||
m_tags.reserve(util::MAX_ENTITIES);
|
||||
m_aliveStates.reserve(util::MAX_ENTITIES);
|
||||
}
|
||||
*/
|
||||
|
||||
EntityMemoryPool& EntityMemoryPool::instance()
|
||||
{
|
||||
static EntityMemoryPool pool{};
|
||||
return pool;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool EntityMemoryPool::hasComponent(EntityIndex index) const
|
||||
{
|
||||
return std::get<std::vector<T>>(m_components)[index].active;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T& EntityMemoryPool::getComponent(EntityIndex index) const
|
||||
{
|
||||
return std::get<std::vector<T>>(m_components)[index];
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T& EntityMemoryPool::getComponent(EntityIndex index)
|
||||
{
|
||||
return std::get<std::vector<T>>(m_components)[index];
|
||||
}
|
||||
|
||||
EntityTag EntityMemoryPool::getTag(EntityIndex index) const
|
||||
{
|
||||
return m_tags[index];
|
||||
}
|
||||
|
||||
bool EntityMemoryPool::getAlive(EntityIndex index) const
|
||||
{
|
||||
return m_aliveStates[index];
|
||||
}
|
||||
|
||||
size_t EntityMemoryPool::getId(EntityIndex index) const
|
||||
{
|
||||
return m_ids[index];
|
||||
}
|
||||
|
||||
void EntityMemoryPool::removeEntity(EntityIndex index)
|
||||
{
|
||||
m_aliveStates[index] = false;
|
||||
return;
|
||||
for (int i = 0; i < util::TAG_COUNT; i++)
|
||||
{
|
||||
EntityView x(util::tagStart[i], util::tagSize[i]);
|
||||
for (auto entity : x)
|
||||
{
|
||||
m_tags[entity.m_index] = (Tag)i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -5,109 +5,111 @@
|
|||
EntityView::EntityView(EntityIndex start, EntityIndex size)
|
||||
: m_start(start)
|
||||
, m_size(size)
|
||||
{
|
||||
{ }
|
||||
|
||||
Entity EntityView::operator[](EntityIndex index) const
|
||||
{
|
||||
return Entity(m_start + index);
|
||||
}
|
||||
|
||||
|
||||
//non-const iterator
|
||||
EntityViewIterator::EntityViewIterator(EntityIndex index)
|
||||
EntityView::iterator::iterator(EntityIndex index)
|
||||
: m_currentEntity(index)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
EntityViewIterator& EntityViewIterator::operator++()
|
||||
EntityView::iterator& EntityView::iterator::operator++()
|
||||
{
|
||||
m_currentEntity++;
|
||||
return *this;
|
||||
}
|
||||
|
||||
EntityViewIterator EntityViewIterator::operator++(int)
|
||||
EntityView::iterator EntityView::iterator::operator++(int)
|
||||
{
|
||||
m_currentEntity++;
|
||||
return m_currentEntity - 1;
|
||||
}
|
||||
|
||||
EntityViewIterator& EntityViewIterator::operator--()
|
||||
EntityView::iterator& EntityView::iterator::operator--()
|
||||
{
|
||||
m_currentEntity--;
|
||||
return *this;
|
||||
}
|
||||
|
||||
EntityViewIterator EntityViewIterator::operator--(int)
|
||||
EntityView::iterator EntityView::iterator::operator--(int)
|
||||
{
|
||||
m_currentEntity--;
|
||||
return m_currentEntity + 1;
|
||||
}
|
||||
|
||||
Entity EntityViewIterator::operator*()
|
||||
Entity EntityView::iterator::operator*()
|
||||
{
|
||||
return m_currentEntity;
|
||||
}
|
||||
|
||||
Entity EntityViewIterator::operator[](int index)
|
||||
Entity EntityView::iterator::operator[](int index)
|
||||
{
|
||||
return m_currentEntity + index;
|
||||
}
|
||||
|
||||
bool EntityViewIterator::operator==(EntityViewIterator other)
|
||||
bool EntityView::iterator::operator==(iterator other)
|
||||
{
|
||||
return m_currentEntity == other.m_currentEntity;
|
||||
}
|
||||
|
||||
bool EntityViewIterator::operator!=(EntityViewIterator other)
|
||||
bool EntityView::iterator::operator!=(iterator other)
|
||||
{
|
||||
return m_currentEntity != other.m_currentEntity;
|
||||
}
|
||||
|
||||
//const iterator
|
||||
EntityViewConstIterator::EntityViewConstIterator(EntityIndex index)
|
||||
EntityView::const_iterator::const_iterator(EntityIndex index)
|
||||
: m_currentEntity(index)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
EntityViewConstIterator& EntityViewConstIterator::operator++()
|
||||
EntityView::const_iterator& EntityView::const_iterator::operator++()
|
||||
{
|
||||
m_currentEntity++;
|
||||
return *this;
|
||||
}
|
||||
|
||||
EntityViewConstIterator EntityViewConstIterator::operator++(int)
|
||||
EntityView::const_iterator EntityView::const_iterator::operator++(int)
|
||||
{
|
||||
m_currentEntity++;
|
||||
return m_currentEntity - 1;
|
||||
}
|
||||
|
||||
EntityViewConstIterator& EntityViewConstIterator::operator--()
|
||||
EntityView::const_iterator& EntityView::const_iterator::operator--()
|
||||
{
|
||||
m_currentEntity--;
|
||||
return *this;
|
||||
}
|
||||
|
||||
EntityViewConstIterator EntityViewConstIterator::operator--(int)
|
||||
EntityView::const_iterator EntityView::const_iterator::operator--(int)
|
||||
{
|
||||
m_currentEntity--;
|
||||
return m_currentEntity + 1;
|
||||
}
|
||||
|
||||
const Entity EntityViewConstIterator::operator*()
|
||||
const Entity EntityView::const_iterator::operator*()
|
||||
{
|
||||
return m_currentEntity;
|
||||
}
|
||||
|
||||
const Entity EntityViewConstIterator::operator[](int index)
|
||||
const Entity EntityView::const_iterator::operator[](int index)
|
||||
{
|
||||
return m_currentEntity + index;
|
||||
}
|
||||
|
||||
bool EntityViewConstIterator::operator==(EntityViewConstIterator other)
|
||||
bool EntityView::const_iterator::operator==(EntityView::const_iterator other)
|
||||
{
|
||||
return m_currentEntity == other.m_currentEntity;
|
||||
}
|
||||
|
||||
bool EntityViewConstIterator::operator!=(EntityViewConstIterator other)
|
||||
bool EntityView::const_iterator::operator!=(EntityView::const_iterator other)
|
||||
{
|
||||
return m_currentEntity != other.m_currentEntity;
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
#include "Containers/Array.h"
|
||||
#include "Containers.h"
|
||||
#include "log.h"
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <imgui-SFML.h>
|
||||
|
|
@ -12,8 +12,6 @@
|
|||
|
||||
int main()
|
||||
{
|
||||
container::Array<bool, 3> arr;
|
||||
|
||||
auto window = sf::RenderWindow(sf::VideoMode({ 1920u, 1080u }), "Fake Mario");
|
||||
window.setFramerateLimit(144);
|
||||
if (!ImGui::SFML::Init(window))
|
||||
|
|
|
|||
Loading…
Reference in New Issue