Skip to content

Commit ce924db

Browse files
committed
merge
2 parents bf1fff0 + 61ad10f commit ce924db

File tree

317 files changed

+2217
-43214
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

317 files changed

+2217
-43214
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@
3333
# Directories
3434
cmake/
3535
cmake-build-debug/
36+
*.TMP

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[submodule "putils"]
22
path = putils
3-
url = [email protected]:phiste/putils
3+
url = [email protected]:phisko/putils

CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ project(kengine)
33
set(CMAKE_CXX_STANDARD 17)
44

55
if (KENGINE_SFML)
6+
set(TGUI TRUE)
67
set(PUTILS_BUILD_PSE TRUE)
78
endif ()
89

@@ -16,10 +17,12 @@ endif ()
1617

1718
if(KENGINE_LUA)
1819
set(PUTILS_BUILD_LUA TRUE)
20+
set(ADDITIONAL_LIBS ${ADDITIONAL_LIBS} lua)
1921
endif()
2022

2123
if(KENGINE_PYTHON)
2224
set(PUTILS_BUILD_PYTHON TRUE)
25+
set(ADDITIONAL_LIBS ${ADDITIONAL_LIBS} puthon)
2326
endif()
2427

2528
set(PUTILS_BUILD_MEDIATOR TRUE)
@@ -37,7 +40,7 @@ elseif (WIN32)
3740
endif ()
3841

3942
add_library(kengine INTERFACE)
40-
target_link_libraries(kengine INTERFACE mediator pluginManager)
43+
target_link_libraries(kengine INTERFACE mediator pluginManager ${ADDITIONAL_LIBS})
4144
target_include_directories(kengine INTERFACE . common)
4245

4346
if (KENGINE_SFML)

Component.hpp

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,72 @@
1-
//
2-
// Created by naliwe on 6/24/16.
3-
//
4-
51
#pragma once
62

7-
#include <string>
8-
#include "IComponent.hpp"
3+
#ifndef NDEBUG
4+
#include <iostream>
5+
#include "reflection/Reflectible.hpp"
6+
#endif
7+
8+
#include <cstddef>
9+
#include <unordered_map>
10+
#include <memory>
11+
#include <vector>
12+
#include "meta/type.hpp"
913

1014
namespace kengine {
11-
template<typename CRTP, typename ...DataPackets>
12-
class Component : public IComponent, public putils::Module<CRTP, DataPackets...> {
13-
public:
14-
pmeta::type_index getType() const noexcept final { return pmeta::type<CRTP>::index; }
15-
};
15+
namespace detail {
16+
static constexpr size_t INVALID = (size_t)-1;
17+
18+
struct MetadataBase {
19+
virtual ~MetadataBase() = default;
20+
};
21+
using GlobalCompMap = std::unordered_map<pmeta::type_index, std::unique_ptr<MetadataBase>>;
22+
static inline GlobalCompMap * components = nullptr;
23+
}
24+
25+
template<typename Comp>
26+
class Component {
27+
private:
28+
struct Metadata : detail::MetadataBase {
29+
std::vector<Comp> array;
30+
size_t id = detail::INVALID;
31+
};
32+
33+
public:
34+
static inline Comp & get(size_t id) { auto & meta = metadata();
35+
while (id >= meta.array.size())
36+
meta.array.resize(meta.array.size() * 2);
37+
return meta.array[id];
38+
}
39+
40+
static inline size_t id() {
41+
static const size_t ret = metadata().id;
42+
return ret;
43+
}
44+
45+
46+
private:
47+
static inline Metadata & metadata() {
48+
static Metadata * ret = nullptr;
49+
50+
if (ret == nullptr) {
51+
const auto typeIndex = pmeta::type<Comp>::index;
52+
const auto it = detail::components->find(typeIndex);
53+
if (it != detail::components->end())
54+
ret = static_cast<Metadata *>(it->second.get());
55+
else {
56+
auto ptr = std::make_unique<Metadata>();
57+
ret = static_cast<Metadata *>(ptr.get());
58+
(*detail::components)[typeIndex] = std::move(ptr);
59+
ret->id = detail::components->size() - 1;
60+
ret->array.resize(64);
61+
62+
#ifndef NDEBUG
63+
if constexpr (putils::is_reflectible<Comp>::value)
64+
std::cout << ret->id << ' ' << Comp::get_class_name() << '\n';
65+
#endif
66+
}
67+
}
68+
69+
return *ret;
70+
}
71+
};
1672
}

Component.md

Lines changed: 0 additions & 38 deletions
This file was deleted.

ComponentManager.hpp

Lines changed: 0 additions & 75 deletions
This file was deleted.

ComponentManager.md

Lines changed: 0 additions & 41 deletions
This file was deleted.

Entity.hpp

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#pragma once
2+
3+
#include <bitset>
4+
#include <cstddef>
5+
#include "Component.hpp"
6+
#include "reflection/Reflectible.hpp"
7+
8+
#ifndef KENGINE_COMPONENT_COUNT
9+
# define KENGINE_COMPONENT_COUNT 64
10+
#endif
11+
12+
namespace kengine {
13+
class EntityManager;
14+
15+
class EntityView {
16+
public:
17+
using ID = size_t;
18+
using Mask = std::bitset<KENGINE_COMPONENT_COUNT>;
19+
static constexpr auto INVALID_ID = detail::INVALID;
20+
21+
EntityView(ID id = INVALID_ID, Mask componentMask = 0) : id(id), componentMask(componentMask) {}
22+
23+
~EntityView() = default;
24+
EntityView(const EntityView &) = default;
25+
EntityView & operator=(const EntityView & rhs) = default;
26+
27+
public:
28+
template<typename T>
29+
T & get() {
30+
assert("No such component" && has<T>());
31+
return Component<T>::get(id);
32+
}
33+
template<typename T>
34+
const T & get() const {
35+
assert("No such component" && has<T>());
36+
return Component<T>::get(id);
37+
}
38+
39+
template<typename T>
40+
bool has() const {
41+
return componentMask.test(getId<T>());
42+
}
43+
44+
ID id;
45+
Mask componentMask = 0;
46+
47+
protected:
48+
template<typename T>
49+
size_t getId() const {
50+
static const auto id = Component<T>::id();
51+
assert("You are using too many component types" && id < KENGINE_COMPONENT_COUNT);
52+
return id;
53+
}
54+
55+
public:
56+
pmeta_get_class_name(EntityView);
57+
pmeta_get_attributes(
58+
pmeta_reflectible_attribute(&EntityView::id),
59+
pmeta_reflectible_attribute(&EntityView::componentMask)
60+
);
61+
pmeta_get_methods();
62+
pmeta_get_parents();
63+
};
64+
65+
class Entity : public EntityView {
66+
public:
67+
Entity(ID id = detail::INVALID, Mask componentMask = 0, EntityManager * manager = nullptr) : EntityView(id, componentMask), manager(manager) {}
68+
~Entity() = default;
69+
Entity(const Entity &) = default;
70+
Entity & operator=(const Entity & rhs) {
71+
id = rhs.id;
72+
componentMask = rhs.componentMask;
73+
return *this;
74+
}
75+
76+
template<typename T>
77+
Entity & operator+=(T && comp) {
78+
attach<T>() = FWD(comp);
79+
return *this;
80+
}
81+
82+
template<typename T>
83+
T & attach();
84+
85+
template<typename T>
86+
void detach();
87+
88+
private:
89+
EntityManager * manager;
90+
};
91+
}
92+
93+
#include "EntityManager.hpp"
94+
95+
template<typename T>
96+
T & kengine::Entity::attach() {
97+
componentMask.set(getId<T>(), true);
98+
manager->updateMask(id, componentMask);
99+
return get<T>();
100+
}
101+
102+
template<typename T>
103+
void kengine::Entity::detach() {
104+
assert("No such component" && has<T>());
105+
componentMask.set(getId<T>(), false);
106+
manager->updateMask(id, componentMask);
107+
}

0 commit comments

Comments
 (0)