Compare commits
3 Commits
8174c19f00
...
959722686f
| Author | SHA1 | Date | |
|---|---|---|---|
| 959722686f | |||
| c36c3468bb | |||
| 6d9edc18b4 |
147
sourceCode/ECS.hpp
Normal file
147
sourceCode/ECS.hpp
Normal file
@@ -0,0 +1,147 @@
|
|||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: Dora "cat" <cat@thenight.club>
|
||||||
|
* SPDX-License-Identifier: MPL-2.0
|
||||||
|
*
|
||||||
|
* This Source Code Form is subject to the terms of the Mozilla Public License,
|
||||||
|
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
|
||||||
|
* obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
*/
|
||||||
|
#ifndef GUARD_TOURMALINE_ECS_H
|
||||||
|
#define GUARD_TOURMALINE_ECS_H
|
||||||
|
#include <any>
|
||||||
|
#include <concepts>
|
||||||
|
#include <format>
|
||||||
|
#include <typeindex>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
#include "Systems/Logging.hpp"
|
||||||
|
#include "Types.hpp"
|
||||||
|
|
||||||
|
namespace Tourmaline::ECS {
|
||||||
|
using Entity = Tourmaline::Type::UUID;
|
||||||
|
class World;
|
||||||
|
|
||||||
|
struct BaseComponent {
|
||||||
|
public:
|
||||||
|
virtual ~BaseComponent() = default;
|
||||||
|
const Entity &GetOwner();
|
||||||
|
|
||||||
|
private:
|
||||||
|
const Entity *owner;
|
||||||
|
friend World;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Concepts
|
||||||
|
template <typename T>
|
||||||
|
concept Component = std::derived_from<T, BaseComponent>;
|
||||||
|
|
||||||
|
class World {
|
||||||
|
public:
|
||||||
|
// Entity
|
||||||
|
Entity CreateEntity();
|
||||||
|
bool EntityExists(const Entity &entity) noexcept;
|
||||||
|
[[nodiscard(
|
||||||
|
"It is not guranteed that an entity can always be destroyed, please make "
|
||||||
|
"sure by checking the returned bool")]]
|
||||||
|
bool DestroyEntity(Entity entity);
|
||||||
|
|
||||||
|
// Components
|
||||||
|
template <Component T, typename... Args>
|
||||||
|
T &AddComponent(const Entity &entity, Args &&...constructionArguments) {
|
||||||
|
// Insert to entity list
|
||||||
|
auto entityIter = GetEntityIterator(
|
||||||
|
entity,
|
||||||
|
std::format(
|
||||||
|
"Cannot add component \"{}\"! Entity \"{}\" does not exist!",
|
||||||
|
typeid(T).name(), entity.asString()),
|
||||||
|
"AddComponent", Systems::Logging::LogLevel::Error);
|
||||||
|
|
||||||
|
auto [componentIter, success] = entityIter->second.try_emplace(
|
||||||
|
typeid(T), T(std::forward<Args>(constructionArguments)...));
|
||||||
|
Systems::Logging::Log(
|
||||||
|
std::format("Cannot add component! Component \"{}\" already exists "
|
||||||
|
"in entity \"{}\" ",
|
||||||
|
typeid(T).name(), entity.asString()),
|
||||||
|
"AddComponent", Systems::Logging::LogLevel::Error, !success);
|
||||||
|
|
||||||
|
T &component = std::any_cast<T &>(componentIter->second);
|
||||||
|
component.owner = &entity;
|
||||||
|
return component;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <Component T>
|
||||||
|
[[nodiscard("Discarding an expensive operation's result!")]]
|
||||||
|
T &GetComponent(const Entity &entity) {
|
||||||
|
auto iter = GetEntityIterator(
|
||||||
|
entity,
|
||||||
|
std::format("Can't get entity \"{}\"'s component \"{}\", since "
|
||||||
|
"entity does not exist!",
|
||||||
|
entity.asString(), typeid(T).name()),
|
||||||
|
"GetComponent", Systems::Logging::LogLevel::Error);
|
||||||
|
|
||||||
|
auto component = iter->second.find(typeid(T));
|
||||||
|
Systems::Logging::Log(
|
||||||
|
std::format(
|
||||||
|
"Entity \"{}\" does not have component \"{}\", cannot get it!",
|
||||||
|
entity.asString(), typeid(T).name()),
|
||||||
|
"GetComponent", Systems::Logging::LogLevel::Error,
|
||||||
|
component == iter->second.end());
|
||||||
|
|
||||||
|
return std::any_cast<T &>(component->second);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <Component T>
|
||||||
|
[[nodiscard("Discarding an expensive operation's result!")]]
|
||||||
|
bool HasComponent(const Entity &entity) {
|
||||||
|
auto iter = GetEntityIterator(
|
||||||
|
entity,
|
||||||
|
std::format("Can't find if entity \"{}\" has component \"{}\", since "
|
||||||
|
"entity does not exist!",
|
||||||
|
entity.asString(), typeid(T).name()));
|
||||||
|
|
||||||
|
return iter != entityComponentList.end() &&
|
||||||
|
(iter->second.find(typeid(T)) != iter->second.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
template <Component T>
|
||||||
|
[[nodiscard(
|
||||||
|
"It is not guranteed that a component can always be removed, please make "
|
||||||
|
"sure by checking the returned bool")]]
|
||||||
|
bool RemoveComponent(const Entity &entity) {
|
||||||
|
auto entityIter = GetEntityIterator(
|
||||||
|
entity,
|
||||||
|
std::format("Cannot remove component {} from entity {}, since entity "
|
||||||
|
"does not exist!",
|
||||||
|
typeid(T).name(), entity.asString()),
|
||||||
|
"RemoveComponent", Systems::Logging::LogLevel::Warning);
|
||||||
|
if (entityIter == entityComponentList.end()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto componentIter = entityIter->second.find(typeid(T));
|
||||||
|
if (componentIter == entityIter->second.end()) {
|
||||||
|
Systems::Logging::Log(
|
||||||
|
std::format("Cannot remove component {} from entity {}, since entity "
|
||||||
|
"does not have that component",
|
||||||
|
typeid(T).name(), entity.asString()),
|
||||||
|
"RemoveComponent", Systems::Logging::LogLevel::Warning);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
entityIter->second.erase(componentIter);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::unordered_map<Entity, std::unordered_map<std::type_index, std::any>>
|
||||||
|
entityComponentList{};
|
||||||
|
|
||||||
|
decltype(entityComponentList)::iterator
|
||||||
|
GetEntityIterator(const Entity &entity, const std::string &errorMessage = "",
|
||||||
|
const std::string &position = "",
|
||||||
|
Tourmaline::Systems::Logging::LogLevel severity =
|
||||||
|
Systems::Logging::LogLevel::Warning);
|
||||||
|
};
|
||||||
|
} // namespace Tourmaline::ECS
|
||||||
|
#endif
|
||||||
14
sourceCode/ECS/Component.cpp
Normal file
14
sourceCode/ECS/Component.cpp
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: Dora "cat" <cat@thenight.club>
|
||||||
|
* SPDX-License-Identifier: MPL-2.0
|
||||||
|
*
|
||||||
|
* This Source Code Form is subject to the terms of the Mozilla Public License,
|
||||||
|
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
|
||||||
|
* obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "../ECS.hpp"
|
||||||
|
|
||||||
|
using namespace Tourmaline::ECS;
|
||||||
|
|
||||||
|
const Entity &BaseComponent::GetOwner() { return *this->owner; }
|
||||||
55
sourceCode/ECS/World.cpp
Normal file
55
sourceCode/ECS/World.cpp
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: Dora "cat" <cat@thenight.club>
|
||||||
|
* SPDX-License-Identifier: MPL-2.0
|
||||||
|
*
|
||||||
|
* This Source Code Form is subject to the terms of the Mozilla Public License,
|
||||||
|
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
|
||||||
|
* obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "../ECS.hpp"
|
||||||
|
#include "../Systems/Random.hpp"
|
||||||
|
|
||||||
|
using namespace Tourmaline::ECS;
|
||||||
|
|
||||||
|
Entity World::CreateEntity() {
|
||||||
|
auto [iterator, success] =
|
||||||
|
entityComponentList.try_emplace(Systems::Random::GenerateUUID());
|
||||||
|
|
||||||
|
Systems::Logging::Log("Failed to create an entity! Possibly by incredible "
|
||||||
|
"luck generated already existing UUID?",
|
||||||
|
"CreateEntity", Systems::Logging::LogLevel::Critical,
|
||||||
|
!success);
|
||||||
|
return iterator->first;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool World::EntityExists(const Entity &entity) noexcept {
|
||||||
|
return entityComponentList.find(entity) != entityComponentList.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool World::DestroyEntity(Entity entity) {
|
||||||
|
auto entityIter = GetEntityIterator(
|
||||||
|
entity,
|
||||||
|
std::format("Cannot delete entity \"{}\", it does not exist!",
|
||||||
|
entity.asString()),
|
||||||
|
"DestroyEntity");
|
||||||
|
|
||||||
|
if (entityIter == entityComponentList.end()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
entityComponentList.erase(entityIter);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Very repetitive code
|
||||||
|
decltype(World::entityComponentList)::iterator
|
||||||
|
World::GetEntityIterator(const Entity &entity, const std::string &errorMessage,
|
||||||
|
const std::string &position,
|
||||||
|
Tourmaline::Systems::Logging::LogLevel severity) {
|
||||||
|
auto iter = entityComponentList.find(entity);
|
||||||
|
|
||||||
|
Systems::Logging::Log(errorMessage, "GetEntityIterator/" + position, severity,
|
||||||
|
iter == entityComponentList.end());
|
||||||
|
return iter;
|
||||||
|
}
|
||||||
@@ -22,8 +22,8 @@ using namespace Tourmaline::Systems;
|
|||||||
|
|
||||||
// This is what happens when it takes you 50 years to implement
|
// This is what happens when it takes you 50 years to implement
|
||||||
// reflections to a language
|
// reflections to a language
|
||||||
std::array<const std::string, 5> Logging::LogLevelToString{
|
std::array<const std::string, 6> Logging::LogLevelToString{
|
||||||
"Critical", "Error", "Info", "Debug", "Trace"};
|
"Critical", "Error", "Warning", "Info", "Debug", "Trace"};
|
||||||
std::fstream Logging::File;
|
std::fstream Logging::File;
|
||||||
|
|
||||||
void Logging::LogToFile(std::string File) {
|
void Logging::LogToFile(std::string File) {
|
||||||
@@ -53,7 +53,8 @@ void Logging::Log(const std::string &message, const std::string &position,
|
|||||||
Logging::File.flush(); // Terrible but necessary sadly
|
Logging::File.flush(); // Terrible but necessary sadly
|
||||||
}
|
}
|
||||||
|
|
||||||
if (severity == Logging::LogLevel::Critical) {
|
// Error and Critical
|
||||||
|
if (severity < Logging::LogLevel::Warning) {
|
||||||
throw std::runtime_error(output);
|
throw std::runtime_error(output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,9 +17,10 @@ public:
|
|||||||
enum class LogLevel {
|
enum class LogLevel {
|
||||||
Critical = 0,
|
Critical = 0,
|
||||||
Error = 1,
|
Error = 1,
|
||||||
Info = 2,
|
Warning = 2,
|
||||||
Debug = 3,
|
Info = 3,
|
||||||
Trace = 4
|
Debug = 4,
|
||||||
|
Trace = 5
|
||||||
};
|
};
|
||||||
|
|
||||||
static void LogToFile(std::string File = "");
|
static void LogToFile(std::string File = "");
|
||||||
@@ -29,6 +30,6 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
static std::fstream File;
|
static std::fstream File;
|
||||||
static std::array<const std::string, 5> LogLevelToString;
|
static std::array<const std::string, 6> LogLevelToString;
|
||||||
};
|
};
|
||||||
} // namespace Tourmaline::Systems
|
} // namespace Tourmaline::Systems
|
||||||
|
|||||||
@@ -9,15 +9,18 @@
|
|||||||
#ifndef GUARD_TOURMALINE_TYPES_H
|
#ifndef GUARD_TOURMALINE_TYPES_H
|
||||||
#define GUARD_TOURMALINE_TYPES_H
|
#define GUARD_TOURMALINE_TYPES_H
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <functional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
namespace Tourmaline::Type {
|
namespace Tourmaline::Type {
|
||||||
struct UUID {
|
class UUID {
|
||||||
|
public:
|
||||||
constexpr static uint8_t BitLength = 128;
|
constexpr static uint8_t BitLength = 128;
|
||||||
constexpr static uint8_t QWORDLength = BitLength / 64;
|
constexpr static uint8_t QWORDLength = BitLength / 64;
|
||||||
constexpr static uint8_t ByteLength = BitLength / 8;
|
constexpr static uint8_t ByteLength = BitLength / 8;
|
||||||
|
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
std::string asString() const;
|
std::string asString() const;
|
||||||
|
bool operator==(const UUID &rhs) const;
|
||||||
|
|
||||||
UUID(uint64_t firstHalf, uint64_t secondHalf);
|
UUID(uint64_t firstHalf, uint64_t secondHalf);
|
||||||
UUID(const std::string &uuid);
|
UUID(const std::string &uuid);
|
||||||
@@ -30,6 +33,22 @@ struct UUID {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<uint64_t[]> data = std::make_unique<uint64_t[]>(QWORDLength);
|
std::unique_ptr<uint64_t[]> data = std::make_unique<uint64_t[]>(QWORDLength);
|
||||||
|
friend struct std::hash<Tourmaline::Type::UUID>;
|
||||||
};
|
};
|
||||||
} // namespace Tourmaline::Type
|
} // namespace Tourmaline::Type
|
||||||
|
|
||||||
|
namespace std {
|
||||||
|
template <> struct hash<Tourmaline::Type::UUID> {
|
||||||
|
size_t operator()(const Tourmaline::Type::UUID &uuid) const noexcept {
|
||||||
|
const auto data = uuid.data.get();
|
||||||
|
size_t h1 = std::hash<uint64_t>{}(data[0]);
|
||||||
|
size_t h2 = std::hash<uint64_t>{}(data[1]);
|
||||||
|
|
||||||
|
// Combine the two hashes
|
||||||
|
return h1 ^ (h2 << 1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace std
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -20,6 +20,16 @@ std::string UUID::asString() const {
|
|||||||
return std::format("{:016X}{:016X}", data[0], data[1]);
|
return std::format("{:016X}{:016X}", data[0], data[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool UUID::operator==(const UUID &rhs) const {
|
||||||
|
// Since size may be increased
|
||||||
|
for (uint8_t index = 0; index < QWORDLength; index++) {
|
||||||
|
if (this->data[index] != rhs.data[index]) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
UUID::UUID(const UUID &uuid) {
|
UUID::UUID(const UUID &uuid) {
|
||||||
std::memcpy(data.get(), uuid.data.get(), UUID::ByteLength);
|
std::memcpy(data.get(), uuid.data.get(), UUID::ByteLength);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user