Restructing on the project
This commit is contained in:
147
source/ECS.hpp
147
source/ECS.hpp
@@ -1,147 +0,0 @@
|
||||
/*
|
||||
* 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 guaranteed 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 guaranteed 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
|
||||
@@ -7,7 +7,7 @@
|
||||
* obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include "../ECS.hpp"
|
||||
#include "../../headers/ECS.hpp"
|
||||
|
||||
using namespace Tourmaline::ECS;
|
||||
|
||||
|
||||
@@ -7,8 +7,8 @@
|
||||
* obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include "../ECS.hpp"
|
||||
#include "../Systems/Random.hpp"
|
||||
#include "../../headers/ECS.hpp"
|
||||
#include "../../headers/Systems/Random.hpp"
|
||||
|
||||
using namespace Tourmaline::ECS;
|
||||
|
||||
|
||||
@@ -7,7 +7,8 @@
|
||||
* obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include "../Logging.hpp"
|
||||
#include "../../headers/Systems/Logging.hpp"
|
||||
|
||||
#include <cerrno>
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
@@ -1,35 +0,0 @@
|
||||
/*
|
||||
* 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 <array>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
namespace Tourmaline::Systems {
|
||||
class Logging {
|
||||
public:
|
||||
enum class LogLevel {
|
||||
Critical = 0,
|
||||
Error = 1,
|
||||
Warning = 2,
|
||||
Info = 3,
|
||||
Debug = 4,
|
||||
Trace = 5
|
||||
};
|
||||
|
||||
static void LogToFile(std::string File = "");
|
||||
static void Log(const std::string &message,
|
||||
const std::string &position = "Unknown",
|
||||
LogLevel severity = LogLevel::Info, bool assertion = true);
|
||||
|
||||
private:
|
||||
static std::fstream File;
|
||||
static std::array<const std::string, 6> LogLevelToString;
|
||||
};
|
||||
} // namespace Tourmaline::Systems
|
||||
@@ -6,7 +6,8 @@
|
||||
* 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 "../Random.hpp"
|
||||
#include "../../headers/Systems/Random.hpp"
|
||||
|
||||
#include <bit>
|
||||
#include <cstdint>
|
||||
#include <ctime>
|
||||
@@ -1,30 +0,0 @@
|
||||
/*
|
||||
* 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_RANDOM_H
|
||||
#define GUARD_TOURMALINE_RANDOM_H
|
||||
#include "../../libraries/random/xoshiro.h"
|
||||
#include "../Types.hpp"
|
||||
#include <type_traits>
|
||||
|
||||
namespace Tourmaline::Systems {
|
||||
class Random {
|
||||
public:
|
||||
template <typename T>
|
||||
requires std::is_integral_v<T>
|
||||
static T Generate(T max, T min = 0) {
|
||||
return (generator() % (max - min + 1)) + min;
|
||||
}
|
||||
static Tourmaline::Type::UUID GenerateUUID();
|
||||
|
||||
private:
|
||||
static Xoshiro::Xoshiro256PP generator;
|
||||
};
|
||||
} // namespace Tourmaline::Systems
|
||||
#endif
|
||||
@@ -1,54 +0,0 @@
|
||||
/*
|
||||
* 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_TYPES_H
|
||||
#define GUARD_TOURMALINE_TYPES_H
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
namespace Tourmaline::Type {
|
||||
class UUID {
|
||||
public:
|
||||
constexpr static uint8_t BitLength = 128;
|
||||
constexpr static uint8_t QWORDLength = BitLength / 64;
|
||||
constexpr static uint8_t ByteLength = BitLength / 8;
|
||||
|
||||
[[nodiscard]]
|
||||
std::string asString() const;
|
||||
bool operator==(const UUID &rhs) const;
|
||||
|
||||
UUID(uint64_t firstHalf, uint64_t secondHalf);
|
||||
UUID(const std::string &uuid);
|
||||
|
||||
UUID(const UUID &uuid);
|
||||
UUID(UUID &&uuid) noexcept;
|
||||
UUID &operator=(const UUID &uuid);
|
||||
UUID &operator=(UUID &&uuid);
|
||||
~UUID() = default;
|
||||
|
||||
private:
|
||||
std::unique_ptr<uint64_t[]> data = std::make_unique<uint64_t[]>(QWORDLength);
|
||||
friend struct std::hash<Tourmaline::Type::UUID>;
|
||||
};
|
||||
} // 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
|
||||
@@ -7,7 +7,8 @@
|
||||
* obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include "../Types.hpp"
|
||||
#include "../../headers/Types.hpp"
|
||||
|
||||
#include <charconv>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
|
||||
Reference in New Issue
Block a user