Compare commits
4 Commits
ea83805f4b
...
3840276f1a
| Author | SHA1 | Date | |
|---|---|---|---|
| 3840276f1a | |||
| 63b6d705a9 | |||
| 6aaf0c6101 | |||
| c458062f72 |
@@ -17,6 +17,7 @@
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
#include <queue>
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
@@ -26,10 +27,11 @@ template <Hashable AKey, Hashable BKey, typename Value,
|
||||
uint64_t baseReservation = 2048, double maxTombstoneRatio = 0.25>
|
||||
class DualkeyMap {
|
||||
public:
|
||||
using ResultPair =
|
||||
using QueryResult =
|
||||
std::pair<std::variant<std::monostate, std::reference_wrapper<const AKey>,
|
||||
std::reference_wrapper<const BKey>>,
|
||||
Value &>;
|
||||
using Entry = std::tuple<const AKey &, const BKey &, Value &>;
|
||||
|
||||
DualkeyMap() { hashList.reserve(baseReservation); }
|
||||
~DualkeyMap() {
|
||||
@@ -41,7 +43,7 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
void insert(AKey firstKey, BKey secondKey, Value value) {
|
||||
Entry insert(AKey firstKey, BKey secondKey, Value value) {
|
||||
std::size_t firstKeyHash = std::hash<AKey>{}(firstKey);
|
||||
std::size_t secondKeyHash = std::hash<BKey>{}(secondKey);
|
||||
DualkeyHash *hash =
|
||||
@@ -55,6 +57,8 @@ public:
|
||||
graveyard.pop();
|
||||
hashList[tombstone] = hash;
|
||||
}
|
||||
|
||||
return {hash->firstKey, hash->secondKey, hash->value};
|
||||
}
|
||||
|
||||
std::size_t remove(std::optional<AKey> firstKey,
|
||||
@@ -121,7 +125,7 @@ public:
|
||||
}
|
||||
|
||||
[[nodiscard("Discarding an expensive operation's result!")]]
|
||||
std::vector<ResultPair> query(std::optional<AKey> firstKey,
|
||||
std::vector<QueryResult> query(std::optional<AKey> firstKey,
|
||||
std::optional<BKey> secondKey) {
|
||||
bool isFirstKeyGiven = firstKey.has_value();
|
||||
bool isSecondKeyGiven = secondKey.has_value();
|
||||
@@ -137,7 +141,7 @@ public:
|
||||
std::size_t secondKeyHash =
|
||||
isSecondKeyGiven ? std::hash<BKey>{}(secondKey.value()) : 0;
|
||||
|
||||
std::vector<ResultPair> finishedQuery{};
|
||||
std::vector<QueryResult> finishedQuery{};
|
||||
|
||||
uint8_t stateOfIndexing = isFirstKeyGiven + (isSecondKeyGiven << 1);
|
||||
// Putting hash checks first to benefit from short circuits
|
||||
@@ -192,8 +196,8 @@ private:
|
||||
struct DualkeyHash {
|
||||
DualkeyHash(std::size_t firstKeyHash, AKey &&firstKey,
|
||||
std::size_t secondKeyHash, BKey &&secondKey, Value &&value)
|
||||
: firstKeyHash(firstKeyHash), firstKey(std::move(firstKey)),
|
||||
secondKeyHash(secondKeyHash), secondKey(std::move(secondKey)),
|
||||
: firstKeyHash(firstKeyHash), secondKeyHash(secondKeyHash),
|
||||
firstKey(std::move(firstKey)), secondKey(std::move(secondKey)),
|
||||
value(std::move(value)) {}
|
||||
|
||||
std::size_t firstKeyHash = 0;
|
||||
|
||||
@@ -14,8 +14,7 @@
|
||||
|
||||
namespace Tourmaline::Containers {
|
||||
template <typename T>
|
||||
concept Hashable = requires(T x) {
|
||||
std::equality_comparable<T>;
|
||||
concept Hashable = std::equality_comparable<T> && requires(T x) {
|
||||
{ std::hash<T>{}(x) } -> std::convertible_to<std::size_t>;
|
||||
};
|
||||
} // namespace Tourmaline::Containers
|
||||
|
||||
@@ -6,15 +6,16 @@
|
||||
* 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 "../Containers/DualkeyMap.hpp"
|
||||
#include "../Types.hpp"
|
||||
#include "Logging.hpp"
|
||||
|
||||
@@ -42,105 +43,48 @@ public:
|
||||
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")]]
|
||||
"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) {
|
||||
auto entityIter = GetEntityIterator(
|
||||
entity,
|
||||
std::format(
|
||||
"Cannot add component \"{}\"! Entity \"{}\" does not exist!",
|
||||
typeid(T).name(), entity.asString()),
|
||||
"AddComponent", Systems::Logging::LogLevel::Error);
|
||||
template <Component component, typename... Args>
|
||||
component &AddComponent(const Entity &entity,
|
||||
Args &&...constructionArguments) {
|
||||
auto newComponent = entityComponentMap.insert(
|
||||
entity, typeid(component), component(constructionArguments...));
|
||||
|
||||
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;
|
||||
return std::any_cast<component &>(std::get<2>(newComponent));
|
||||
}
|
||||
|
||||
template <Component T>
|
||||
template <Component component>
|
||||
[[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);
|
||||
component &GetComponent(const Entity &entity) {
|
||||
auto result = entityComponentMap.query(entity, typeid(component));
|
||||
if (result.empty()) {
|
||||
Logging::Log(std::format("Entity {} does not have component {}!",
|
||||
entity.asString(), typeid(component).name()),
|
||||
"ECS/GetComponent", Logging::LogLevel::Error);
|
||||
}
|
||||
return std::any_cast<component &>(result.begin()->second);
|
||||
}
|
||||
|
||||
template <Component T>
|
||||
template <Component component>
|
||||
[[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);
|
||||
// TO BE IMPLEMENTED
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<Entity, std::unordered_map<std::type_index, std::any>>
|
||||
entityComponentList{};
|
||||
template <Component component>
|
||||
[[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) {
|
||||
return entityComponentMap.remove(entity, typeid(component));
|
||||
}
|
||||
|
||||
decltype(entityComponentList)::iterator
|
||||
GetEntityIterator(const Entity &entity, const std::string &errorMessage = "",
|
||||
const std::string &position = "",
|
||||
Tourmaline::Systems::Logging::LogLevel severity =
|
||||
Systems::Logging::LogLevel::Warning);
|
||||
private:
|
||||
Tourmaline::Containers::DualkeyMap<Entity, std::type_index, std::any>
|
||||
entityComponentMap{};
|
||||
};
|
||||
} // namespace Tourmaline::Systems::ECS
|
||||
#endif
|
||||
|
||||
17
headers/Systems/ECS/BuiltinComponents.hpp
Normal file
17
headers/Systems/ECS/BuiltinComponents.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* 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"
|
||||
|
||||
namespace Tourmaline::Systems::Components {
|
||||
struct Position : public Tourmaline::Systems::ECS::BaseComponent {
|
||||
Position(double x = 0, double y = 0, double z = 0) : x(x), y(y), z(z) {}
|
||||
double x, y, z;
|
||||
};
|
||||
} // namespace Tourmaline::Systems::Components
|
||||
@@ -8,48 +8,27 @@
|
||||
*/
|
||||
|
||||
#include <Systems/ECS.hpp>
|
||||
#include <Systems/ECS/BuiltinComponents.hpp>
|
||||
#include <Systems/Random.hpp>
|
||||
#include <optional>
|
||||
|
||||
using namespace Tourmaline::Systems::ECS;
|
||||
|
||||
// It is preferable to send a copy of the UUID instead of reference since
|
||||
// the entity itself may be destroyed in the memory
|
||||
Entity World::CreateEntity() {
|
||||
auto [iterator, success] =
|
||||
entityComponentList.try_emplace(Systems::Random::GenerateUUID());
|
||||
auto newEntity = entityComponentMap.insert(
|
||||
Random::GenerateUUID(), typeid(Tourmaline::Systems::Components::Position),
|
||||
Tourmaline::Systems::Components::Position());
|
||||
|
||||
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;
|
||||
return Entity(std::get<0>(newEntity));
|
||||
}
|
||||
|
||||
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);
|
||||
// TO BE IMPLEMENTED
|
||||
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;
|
||||
bool World::DestroyEntity(Entity entity) {
|
||||
return entityComponentMap.remove(entity, std::nullopt);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user