diff --git a/include/Components/Misc.hpp b/include/Components/Misc.hpp index f99a2f2..e8cccdc 100644 --- a/include/Components/Misc.hpp +++ b/include/Components/Misc.hpp @@ -20,6 +20,12 @@ struct TilePosition Vector2 Position; }; +struct Bounds +{ + Vector2 Min; + Vector2 Max; +}; + struct Level { Level() = default; @@ -59,6 +65,10 @@ inline void Flecs_Misc(flecs::world& world) world.component() .member("Position"); + world.component() + .member("Min") + .member("Max"); + world.component() .member("Val"); } diff --git a/include/Components/Sync.h b/include/Components/Sync.h deleted file mode 100644 index 3f568f6..0000000 --- a/include/Components/Sync.h +++ /dev/null @@ -1,33 +0,0 @@ -// #pragma once -// #include "core/object/ref_counted.h" - -// struct Sync -// { }; - -// class FactoryEntity; - -// struct NodePtr -// { -// NodePtr() = default; -// NodePtr(FactoryEntity* node) : Node{ node } {} - -// template -// T* AsNode() const -// { -// static_assert(std::is_base_of_v); -// DEV_ASSERT(Object::cast_to(Node)); -// return static_cast(Node); -// } - -// FactoryEntity* Node{}; -// }; - -// class Archetype; - -// struct ArchetypePtr -// { -// ArchetypePtr() = default; -// ArchetypePtr(Ref& archetpye) : Archetype{ archetpye } {} - -// Ref Archetype; -// }; \ No newline at end of file diff --git a/include/Core/Chunk.h b/include/Core/Chunk.h index 0cb2791..2e2ee54 100644 --- a/include/Core/Chunk.h +++ b/include/Core/Chunk.h @@ -1,9 +1,16 @@ #pragma once -#include "modules/factory/include/Data/Tile.h" -#include "core/math/vector2i.h" +#include #include +#include #include +#include + +#include "Util/Span.h" + +#include "config.h" +#include "Types/Tile.h" +#include "Components/Misc.hpp" struct Chunk { @@ -23,22 +30,22 @@ public: static void Assert(int x, int y) { DEV_ASSERT(x < ChunkSize && x >= 0 && y < ChunkSize && y >= 0); } public: Tile GetTile(int x, int y) const { Assert(x, y); return Tiles[y * ChunkSize + x]; } - Tile GetTile(Vector2i pos) const { return GetTile(pos.x, pos.y); } + Tile GetTile(Vector2 pos) const { return GetTile(pos.X, pos.Y); } const Tile& GetTileRef(int x, int y) const { Assert(x, y); return Tiles[y * ChunkSize + x]; } Tile& GetTile(int x, int y) { Assert(x, y); return Tiles[y * ChunkSize + x]; } - Tile& GetTile(Vector2i pos) { return GetTile(pos.x, pos.y); } + Tile& GetTile(Vector2 pos) { return GetTile(pos.X, pos.Y); } }; struct ChunkCoordinate { ChunkCoordinate() = default; ChunkCoordinate(int x, int y) : X{ static_cast(x) }, Y{ static_cast(y) } { DEV_ASSERT(x >= 0 && x < Chunk::ChunkSize && y >= 0 && y < Chunk::ChunkSize); } - ChunkCoordinate(Vector2i pos) : ChunkCoordinate{pos.x, pos.y} {} + ChunkCoordinate(Vector2 pos) : ChunkCoordinate{pos.X, pos.Y} {} uint8_t X{}; uint8_t Y{}; - operator Vector2i() const { return Vector2i(X, Y); } + operator Vector2() const { return Vector2(X, Y); } }; static_assert(sizeof(ChunkCoordinate) / 2 >= Chunk::ChunkSizePowerOfTwo / 8); @@ -47,14 +54,14 @@ struct EntityTile final { public: EntityTile() = default; - EntityTile(entt::entity entity, int worldX, int worldY) + EntityTile(flecs::entity entity, int worldX, int worldY) : Entity{ entity } , ChunkX{ Chunk::WorldToLocal(worldX) } , ChunkY{ Chunk::WorldToLocal(worldY) } {} public: - entt::entity Entity{}; + flecs::entity Entity{}; Chunk::CoordinateType ChunkX{}; Chunk::CoordinateType ChunkY{}; }; @@ -77,7 +84,7 @@ public: uint32_t hash() const { return static_cast(hash64()); } static uint32_t hash(ChunkKey key) { return key.hash(); } - Rect2i GetBounds() const { return Rect2i{ChunkToWorld(X), ChunkToWorld(Y), 1 << Chunk::ChunkSizePowerOfTwo, 1 << Chunk::ChunkSizePowerOfTwo}; } + Bounds SetBounds() const { return Bounds{Vector2{ChunkToWorld(X), ChunkToWorld(Y)}, Vector2{ChunkToWorld(X + 1), ChunkToWorld(Y + 1)}}; } public: bool operator==(const ChunkKey& rhs) const { return hash() == rhs.hash(); } @@ -88,8 +95,8 @@ static_assert(sizeof(ChunkKey) == 4); struct ChunkData { public: - void MarkAsPersistant(entt::entity entity); - void RemovePersistance(entt::entity entity); + void MarkAsPersistant(flecs::entity entity); + void RemovePersistance(flecs::entity entity); void Clear() { Chunk = {}; @@ -114,17 +121,17 @@ public: Tile GetTile(int x, int y); Tile const* TryGetTile(int x, int y) const; - entt::entity GetEntity(int x, int y) const; + flecs::entity GetEntity(int x, int y) const; void SetChunkTiles(int x, int y, std::unique_ptr&& chunk); void SetChunkTiles(ChunkKey key, std::unique_ptr&& chunk); - void AddEntity(entt::entity entity, const Vector& claimedPositions); - void AddPersistantEntity(entt::entity entity, const Vector& claimedPositions); + void AddEntity(flecs::entity entity, tcb::span claimedPositions); + void AddPersistantEntity(flecs::entity entity, tcb::span claimedPositions); - void MarkAsPersistant(entt::entity entity); - void RemovePersistance(entt::entity entity); + void MarkAsPersistant(flecs::entity entity); + void RemovePersistance(flecs::entity entity); - void RemoveEntity(entt::entity entity); + void RemoveEntity(flecs::entity entity); void RemoveChunk(int x, int y); void RemoveChunk(ChunkKey key); @@ -143,7 +150,7 @@ private: private: std::vector ChunkDatas; - HashMap ChunkMap{}; + std::unordered_map ChunkMap{}; ChunkKey CachedChunkKey{}; int CachedChunk{-1}; }; diff --git a/include/Core/FactoryCommandQueue.h b/include/Core/FactoryCommandQueue.h index 0af7d1a..ed8b159 100644 --- a/include/Core/FactoryCommandQueue.h +++ b/include/Core/FactoryCommandQueue.h @@ -1,197 +1,197 @@ -#pragma once -#include "Util/StackAllocator.h" -#include "Util/Span.h" -#include "EnTT/entity/registry.hpp" -#include -#include -#include "Components/Sync.h" -#include "Core/FactoryWorld.h" +// #pragma once +// #include "Util/StackAllocator.h" +// #include "Util/Span.h" +// #include "EnTT/entity/registry.hpp" +// #include +// #include +// #include "Components/Sync.h" +// #include "Core/FactoryWorld.h" -struct FactoryCommand -{ - void* Data; - entt::entity Entity; - void(*Command)(FactoryWorld& world, entt::entity entity, void* data); -}; +// struct FactoryCommand +// { +// void* Data; +// entt::entity Entity; +// void(*Command)(FactoryWorld& world, entt::entity entity, void* data); +// }; -class FactoryCommandQueue final -{ - static constexpr int DefaultAllocatorSize = 1024 * 1024; +// class FactoryCommandQueue final +// { +// static constexpr int DefaultAllocatorSize = 1024 * 1024; -public: - FactoryCommandQueue(size_t memorySize) - : Allocator{ memorySize } - {} - FactoryCommandQueue() - : Allocator{ DefaultAllocatorSize } - {} +// public: +// FactoryCommandQueue(size_t memorySize) +// : Allocator{ memorySize } +// {} +// FactoryCommandQueue() +// : Allocator{ DefaultAllocatorSize } +// {} -public: +// public: -public: - template - void SetComponentData(entt::entity entity, const T& component); +// public: +// template +// void SetComponentData(entt::entity entity, const T& component); - template - void SetOrAddComponentData(entt::entity entity, const T& component); +// template +// void SetOrAddComponentData(entt::entity entity, const T& component); - template - void AddIfNone(entt::entity entity, const T& component); +// template +// void AddIfNone(entt::entity entity, const T& component); - template - void AddComponent(entt::entity entity, const T& component); +// template +// void AddComponent(entt::entity entity, const T& component); - template - void AddComponent(entt::entity entity); +// template +// void AddComponent(entt::entity entity); - template - void RemoveComponent(entt::entity entity); +// template +// void RemoveComponent(entt::entity entity); - //void RemoveEntity(entt::entity entity); +// //void RemoveEntity(entt::entity entity); - void SyncEntity(entt::entity entity) { AddComponent(entity); } - void StopSyncingEntity(entt::entity entity) { RemoveComponent(entity); } +// void SyncEntity(entt::entity entity) { AddComponent(entity); } +// void StopSyncingEntity(entt::entity entity) { RemoveComponent(entity); } - void ExecuteAll(FactoryWorld& world); +// void ExecuteAll(FactoryWorld& world); - void Clear(); +// void Clear(); - template - T* AllocateData() - { - static_assert(std::is_trivially_copyable_v); - return Allocator.allocate(1); - } +// template +// T* AllocateData() +// { +// static_assert(std::is_trivially_copyable_v); +// return Allocator.allocate(1); +// } - template - T* AllocateData(Args... arguments) - { - static_assert(std::is_trivially_copyable_v); - T* data = AllocateData(); - new (data) T(arguments...); - return data; - } +// template +// T* AllocateData(Args... arguments) +// { +// static_assert(std::is_trivially_copyable_v); +// T* data = AllocateData(); +// new (data) T(arguments...); +// return data; +// } - template - tcb::span* AllocateBuffer(uint32_t size) - { - static_assert(std::is_trivially_copyable_v); - auto spanData = Allocator.allocate>(); - auto data = Allocator.allocate(size); - for (uint32_t i{}; i < size; ++i) - data[i] = {}; - *spanData = tcb::span(data, size); - return spanData; - } +// template +// tcb::span* AllocateBuffer(uint32_t size) +// { +// static_assert(std::is_trivially_copyable_v); +// auto spanData = Allocator.allocate>(); +// auto data = Allocator.allocate(size); +// for (uint32_t i{}; i < size; ++i) +// data[i] = {}; +// *spanData = tcb::span(data, size); +// return spanData; +// } -private: - void ClearUnsafe() - { - Commands.clear(); - Allocator.reset(); - } +// private: +// void ClearUnsafe() +// { +// Commands.clear(); +// Allocator.reset(); +// } -private: - StackAllocator Allocator; - std::mutex Mutex; - std::vector Commands; -}; +// private: +// StackAllocator Allocator; +// std::mutex Mutex; +// std::vector Commands; +// }; -template -inline void FactoryCommandQueue::SetComponentData(entt::entity entity, const T& component) -{ - FactoryCommand command; - command.Entity = entity; - command.Data = AllocateData(component); - command.Command = [](entt::registry& registry, entt::entity entity, void* data) - { - registry.get(entity) = *static_cast(data); - }; - std::scoped_lock lock(Mutex); - Commands.push_back(command); -} +// template +// inline void FactoryCommandQueue::SetComponentData(entt::entity entity, const T& component) +// { +// FactoryCommand command; +// command.Entity = entity; +// command.Data = AllocateData(component); +// command.Command = [](entt::registry& registry, entt::entity entity, void* data) +// { +// registry.get(entity) = *static_cast(data); +// }; +// std::scoped_lock lock(Mutex); +// Commands.push_back(command); +// } -template -inline void FactoryCommandQueue::SetOrAddComponentData(entt::entity entity, const T& component) -{ - FactoryCommand command; - command.Entity = entity; - command.Data = AllocateData(component); - command.Command = [](entt::registry& registry, entt::entity entity, void* data) - { - registry.emplace_or_replace(entity, *static_cast(data)); - }; - std::scoped_lock lock(Mutex); - Commands.push_back(command); -} +// template +// inline void FactoryCommandQueue::SetOrAddComponentData(entt::entity entity, const T& component) +// { +// FactoryCommand command; +// command.Entity = entity; +// command.Data = AllocateData(component); +// command.Command = [](entt::registry& registry, entt::entity entity, void* data) +// { +// registry.emplace_or_replace(entity, *static_cast(data)); +// }; +// std::scoped_lock lock(Mutex); +// Commands.push_back(command); +// } -template -inline void FactoryCommandQueue::AddIfNone(entt::entity entity, const T& component) -{ - FactoryCommand command; - command.Entity = entity; - command.Data = AllocateData(component); - command.Command = [](entt::registry& registry, entt::entity entity, void* data) - { - if (!registry.all_of(entity)) - registry.emplace(entity, *static_cast(data)); - }; - std::scoped_lock lock(Mutex); - Commands.push_back(command); -} +// template +// inline void FactoryCommandQueue::AddIfNone(entt::entity entity, const T& component) +// { +// FactoryCommand command; +// command.Entity = entity; +// command.Data = AllocateData(component); +// command.Command = [](entt::registry& registry, entt::entity entity, void* data) +// { +// if (!registry.all_of(entity)) +// registry.emplace(entity, *static_cast(data)); +// }; +// std::scoped_lock lock(Mutex); +// Commands.push_back(command); +// } -template -inline void FactoryCommandQueue::AddComponent(entt::entity entity, const T& component) -{ - FactoryCommand command; - command.Entity = entity; - command.Data = AllocateData(component); - command.Command = [](entt::registry& registry, entt::entity entity, void* data) - { - registry.emplace(entity, *static_cast(data)); - }; - std::scoped_lock lock(Mutex); - Commands.push_back(command); -} +// template +// inline void FactoryCommandQueue::AddComponent(entt::entity entity, const T& component) +// { +// FactoryCommand command; +// command.Entity = entity; +// command.Data = AllocateData(component); +// command.Command = [](entt::registry& registry, entt::entity entity, void* data) +// { +// registry.emplace(entity, *static_cast(data)); +// }; +// std::scoped_lock lock(Mutex); +// Commands.push_back(command); +// } -template -inline void FactoryCommandQueue::AddComponent(entt::entity entity) -{ - FactoryCommand command; - command.Entity = entity; - command.Data = nullptr; - command.Command = [](entt::registry& registry, entt::entity entity, void* data) - { - registry.emplace(entity); - }; - std::scoped_lock lock(Mutex); - Commands.push_back(command); -} - -template -inline void FactoryCommandQueue::RemoveComponent(entt::entity entity) -{ - FactoryCommand command; - command.Entity = entity; - command.Data = nullptr; - command.Command = [](entt::registry& registry, entt::entity entity, void* data) - { - registry.remove(entity); - }; - std::scoped_lock lock(Mutex); - Commands.push_back(command); -} - -// inline void FactoryCommandQueue::RemoveEntity(entt::entity entity) +// template +// inline void FactoryCommandQueue::AddComponent(entt::entity entity) // { // FactoryCommand command; // command.Entity = entity; // command.Data = nullptr; // command.Command = [](entt::registry& registry, entt::entity entity, void* data) // { -// registry.destroy(entity); +// registry.emplace(entity); // }; // std::scoped_lock lock(Mutex); // Commands.push_back(command); -// } \ No newline at end of file +// } + +// template +// inline void FactoryCommandQueue::RemoveComponent(entt::entity entity) +// { +// FactoryCommand command; +// command.Entity = entity; +// command.Data = nullptr; +// command.Command = [](entt::registry& registry, entt::entity entity, void* data) +// { +// registry.remove(entity); +// }; +// std::scoped_lock lock(Mutex); +// Commands.push_back(command); +// } + +// // inline void FactoryCommandQueue::RemoveEntity(entt::entity entity) +// // { +// // FactoryCommand command; +// // command.Entity = entity; +// // command.Data = nullptr; +// // command.Command = [](entt::registry& registry, entt::entity entity, void* data) +// // { +// // registry.destroy(entity); +// // }; +// // std::scoped_lock lock(Mutex); +// // Commands.push_back(command); +// // } \ No newline at end of file diff --git a/include/Core/FactoryWorld.h b/include/Core/FactoryWorld.h index de09f44..24f0eb9 100644 --- a/include/Core/FactoryWorld.h +++ b/include/Core/FactoryWorld.h @@ -1,159 +1,159 @@ -#pragma once +// #pragma once -#include -#include "core/templates/vector.h" -#include "core/templates/hash_map.h" -#include "core/object/ref_counted.h" -#include "modules/noise/fastnoise_lite.h" -#include "core/io/resource.h" -#include "scene/2d/tile_map_layer.h" -#include "core/os/mutex.h" -#include "core/templates/hash_set.h" -#include "core/math/vector2i.h" +// #include +// #include "core/templates/vector.h" +// #include "core/templates/hash_map.h" +// #include "core/object/ref_counted.h" +// #include "modules/noise/fastnoise_lite.h" +// #include "core/io/resource.h" +// #include "scene/2d/tile_map_layer.h" +// #include "core/os/mutex.h" +// #include "core/templates/hash_set.h" +// #include "core/math/vector2i.h" -#include "Components/Position.h" -#include "Components/Inventory.h" -#include "Components/Support.h" +// #include "Components/Position.h" +// #include "Components/Inventory.h" +// #include "Components/Support.h" -#include "Data/Tile.h" -#include "Util/ResourceAccess.h" -#include "Chunk.h" -#include "SystemBase.h" -#include "Data/WorldSettings.h" -#include "FactoryCommandQueue.h" -#include "WorldThreadData.h" +// #include "Data/Tile.h" +// #include "Util/ResourceAccess.h" +// #include "Chunk.h" +// #include "SystemBase.h" +// #include "Data/WorldSettings.h" +// #include "FactoryCommandQueue.h" +// #include "WorldThreadData.h" -class FactoryWorld; -class FactoryWorldInterface; +// class FactoryWorld; +// class FactoryWorldInterface; -// typedef ResourceAccess FactoryWorldAccess; +// // typedef ResourceAccess FactoryWorldAccess; -enum FactoryError -{ - FACTORY_ERROR_NONE = 0, - FACTORY_ERROR_NOT_ENTITY, - FACTORY_ERROR_CANT_UPGRADE, - FACTORY_ERROR_FULLY_UPGRADED, - FACTORY_ERROR_NOT_ENOUGH_ITEMS, - FACTORY_ERROR_NO_SPACE, - FACTORY_ERROR_REQUIRES_SUPPORT, - FACTORY_ERROR_PATH_TOO_LONG, - FACTORY_ERROR_INVALID_PATH, - FACTORY_ERROR_INVALID_POS, - FACTORY_ERROR_MISC_ERROR, -}; +// enum FactoryError +// { +// FACTORY_ERROR_NONE = 0, +// FACTORY_ERROR_NOT_ENTITY, +// FACTORY_ERROR_CANT_UPGRADE, +// FACTORY_ERROR_FULLY_UPGRADED, +// FACTORY_ERROR_NOT_ENOUGH_ITEMS, +// FACTORY_ERROR_NO_SPACE, +// FACTORY_ERROR_REQUIRES_SUPPORT, +// FACTORY_ERROR_PATH_TOO_LONG, +// FACTORY_ERROR_INVALID_PATH, +// FACTORY_ERROR_INVALID_POS, +// FACTORY_ERROR_MISC_ERROR, +// }; -struct ChunkUnlock final -{ - ChunkKey ChunkID{}; - Vector Items{}; -}; +// struct ChunkUnlock final +// { +// ChunkKey ChunkID{}; +// Vector Items{}; +// }; -class FactoryWorld final -{ - friend class WorldSerializer; - friend class WorldLoader; -public: - FactoryWorld() = default; - ~FactoryWorld() = default; +// class FactoryWorld final +// { +// friend class WorldSerializer; +// friend class WorldLoader; +// public: +// FactoryWorld() = default; +// ~FactoryWorld() = default; -public: - void Tick(int amount = 1); - void Initialize(FactoryWorldInterface* worldInterface); - void Save(); +// public: +// void Tick(int amount = 1); +// void Initialize(FactoryWorldInterface* worldInterface); +// void Save(); -public: - int32_t GetSeed() const { return Seed; } +// public: +// int32_t GetSeed() const { return Seed; } - FactoryError CanPlaceEntity(int x, int y, Ref archetype); - FactoryError AddEntity(int x, int y, Ref archetype); - void RemoveEntity(FactoryEntity* node); - void RemoveEntity(int x, int y); - void RemoveEntity(entt::entity entity); +// FactoryError CanPlaceEntity(int x, int y, Ref archetype); +// FactoryError AddEntity(int x, int y, Ref archetype); +// void RemoveEntity(FactoryEntity* node); +// void RemoveEntity(int x, int y); +// void RemoveEntity(entt::entity entity); - // FactoryWorldAccess GetAccess() { return FactoryWorldAccess{ *this, WorldAccessMutex }; } +// // FactoryWorldAccess GetAccess() { return FactoryWorldAccess{ *this, WorldAccessMutex }; } - entt::registry& GetRegistry() { return Registry; }; - const entt::registry& GetRegistry() const { return Registry; }; - auto& GetChunks() { return Chunks; } - const auto& GetChunks() const { return Chunks; } - auto& GetSettings() const { return WorldSettings; } +// entt::registry& GetRegistry() { return Registry; }; +// const entt::registry& GetRegistry() const { return Registry; }; +// auto& GetChunks() { return Chunks; } +// const auto& GetChunks() const { return Chunks; } +// auto& GetSettings() const { return WorldSettings; } - bool IsValidCameraPos(Rect2i viewport) const; +// bool IsValidCameraPos(Rect2i viewport) const; -public: // Chunks - FactoryError TryUnlockChunk(ChunkKey chunk); +// public: // Chunks +// FactoryError TryUnlockChunk(ChunkKey chunk); -private: - void RefreshUnlockedChunks(); +// private: +// void RefreshUnlockedChunks(); -private: - entt::entity CreateEntity(); - FactoryError AddEntity(int x, int y, Ref archetype, entt::entity entityID); - void InvalidateCachedChunk(); +// private: +// entt::entity CreateEntity(); +// FactoryError AddEntity(int x, int y, Ref archetype, entt::entity entityID); +// void InvalidateCachedChunk(); -public: // UPGRADING - FactoryError TryUpgradeEntity(FactoryEntity* entity); - FactoryError TryUpgradeEntity(entt::entity entity); - FactoryError TryUpgradeEntity(const Vector2i& position); +// public: // UPGRADING +// FactoryError TryUpgradeEntity(FactoryEntity* entity); +// FactoryError TryUpgradeEntity(entt::entity entity); +// FactoryError TryUpgradeEntity(const Vector2i& position); - FactoryError CanUpgradeEntity(entt::entity entity) const; - FactoryError CanUpgradeEntity(FactoryEntity* entity) const; - FactoryError CanUpgradeEntity(const Vector2i& position); +// FactoryError CanUpgradeEntity(entt::entity entity) const; +// FactoryError CanUpgradeEntity(FactoryEntity* entity) const; +// FactoryError CanUpgradeEntity(const Vector2i& position); -private: - void UpgradeEntity(entt::entity entity); - void UpgradeEntity(entt::entity entity, Ref archetype); - void SetEntityLevel(entt::entity entity, uint8_t level); - void SetEntityLevel(entt::entity entity, Ref archetype, uint8_t level); +// private: +// void UpgradeEntity(entt::entity entity); +// void UpgradeEntity(entt::entity entity, Ref archetype); +// void SetEntityLevel(entt::entity entity, uint8_t level); +// void SetEntityLevel(entt::entity entity, Ref archetype, uint8_t level); -public: // QUERY - void HighlightUpgradableEntities(TileMapLayer* tilemap) const; - FactoryError FindChutePath(Vector& path, Vector2i startPos, Vector2i endPos) const; - Tile const* Raycast(Vector2i startPos, Vector2i endPos) const; - bool IsSupport(int x, int y) const; - bool IsSupport(entt::entity entity) const; +// public: // QUERY +// void HighlightUpgradableEntities(TileMapLayer* tilemap) const; +// FactoryError FindChutePath(Vector& path, Vector2i startPos, Vector2i endPos) const; +// Tile const* Raycast(Vector2i startPos, Vector2i endPos) const; +// bool IsSupport(int x, int y) const; +// bool IsSupport(entt::entity entity) const; -public: // INVENTORY - void SetInventory(const Vector>& items); - Inventory& GetInventory() { return WorldInventory; } - const Inventory& GetInventory() const { return WorldInventory; } - Inventory GetWorldInventory(Vector2 position) const; - Inventory GetWorldInventory(entt::entity entity) const; +// public: // INVENTORY +// void SetInventory(const Vector>& items); +// Inventory& GetInventory() { return WorldInventory; } +// const Inventory& GetInventory() const { return WorldInventory; } +// Inventory GetWorldInventory(Vector2 position) const; +// Inventory GetWorldInventory(entt::entity entity) const; -public: // DATA - const Recipe& GetRecipe(Ref config) { return WorldInstanceData.GetRecipe(config); } +// public: // DATA +// const Recipe& GetRecipe(Ref config) { return WorldInstanceData.GetRecipe(config); } -public: // SUPPORT - // FactoryError CanPlaceSupport(int x, int y) const; - // FactoryError CanRemoveSupport(int x, int y) const; - // void RegisterSupport(int x, int y, Support& support); - // void RemoveSupport(int x, int y); +// public: // SUPPORT +// // FactoryError CanPlaceSupport(int x, int y) const; +// // FactoryError CanRemoveSupport(int x, int y) const; +// // void RegisterSupport(int x, int y, Support& support); +// // void RemoveSupport(int x, int y); -private: - bool SupportCheckerHelper(entt::entity entity) const; - uint8_t SupportValueHelper(entt::entity entity) const; - uint8_t GetSupportValue(int x, int y) const; - bool CheckIfSupportHelper(entt::entity entity, Support& support) const; +// private: +// bool SupportCheckerHelper(entt::entity entity) const; +// uint8_t SupportValueHelper(entt::entity entity) const; +// uint8_t GetSupportValue(int x, int y) const; +// bool CheckIfSupportHelper(entt::entity entity, Support& support) const; - void ConnectSupports(Vector2i pos, Support& support, Vector2i direction); +// void ConnectSupports(Vector2i pos, Support& support, Vector2i direction); -private: - Mutex WorldAccessMutex; - // std::shared_ptr CommandQueue = std::make_shared(); - // FactoryWorldInterface* Interface; +// private: +// Mutex WorldAccessMutex; +// // std::shared_ptr CommandQueue = std::make_shared(); +// // FactoryWorldInterface* Interface; - ChunkCollection Chunks{}; +// ChunkCollection Chunks{}; - entt::registry Registry{}; +// entt::registry Registry{}; - Ref WorldSettings{}; +// Ref WorldSettings{}; - int32_t Seed{}; - int32_t LastDrawnFrame{}; +// int32_t Seed{}; +// int32_t LastDrawnFrame{}; - Inventory64 WorldInventory; - WorldData WorldInstanceData; - Vector UnlockedChunks{ ChunkKey{} }; - Vector UnlockableChunks{}; -}; \ No newline at end of file +// Inventory64 WorldInventory; +// WorldData WorldInstanceData; +// Vector UnlockedChunks{ ChunkKey{} }; +// Vector UnlockableChunks{}; +// }; \ No newline at end of file diff --git a/include/Core/WorldGenerator.h b/include/Core/WorldGenerator.h index 841e3ac..15921c0 100644 --- a/include/Core/WorldGenerator.h +++ b/include/Core/WorldGenerator.h @@ -1,95 +1,95 @@ -#pragma once +// #pragma once -#include "Data/WorldSettings.h" -#include "Chunk.h" +// #include "Data/WorldSettings.h" +// #include "Chunk.h" -class FactoryWorld; +// class FactoryWorld; -// class WorldGenerator final +// // class WorldGenerator final +// // { +// // public: + + +// // public: +// // WorldGenerator() = default; +// // WorldGenerator(Ref settings, int32_t seed); + +// // public: +// // // bool GenerateChunk(ChunkKey chunkKey, Chunk& chunk) const; +// // // bool GenerateChunk(ChunkKey chunkKey, Chunk& chunk, Ref layer, Ref nextLayer = {}) const; + +// // // Vector SpawnEntities(ChunkKey chunkKey, Chunk& chunk, const std::vector& persistantEntities = {}) const; +// // // Vector SpawnEntities(ChunkKey chunkKey, Chunk& chunk, Ref layer, Ref nextLayer = {}, const std::vector& persistantEntities = {}) const; + +// // // std::unique_ptr CreateChunkVisuals(ChunkKey chunkKey, Chunk& chunk); + +// // public: +// // void ThreadedGenerateChunk(ChunkKey chunkKey, std::function callback, const std::vector& persistantEntities = {}); + +// // private: + +// // public: +// // Ref Settings{}; +// // WorldGraph Graph{}; +// // int32_t Seed{}; +// // }; + +// class ChunkGenerator final // { // public: +// struct SpawnedEntities +// { +// Ref Archetype{}; +// Vector2i SpawnPosition{}; +// Vector ClaimedPositions{}; +// }; + +// struct CreatedVisualsTile +// { +// CreatedVisualsTile() = default; +// CreatedVisualsTile(uint16_t atlasCoordinateX, uint16_t atlasCoordinateY, uint16_t atlasIndex) : AtlasCoordinateX{ atlasCoordinateX }, AtlasCoordinateY{ atlasCoordinateY }, AtlasIndex{ atlasIndex } {}; + +// uint16_t AtlasCoordinateX{}; +// uint16_t AtlasCoordinateY{}; +// uint16_t AtlasIndex{}; +// }; + +// typedef std::array CreatedVisualsChunk; +// typedef std::array ChunkShadowValues; + +// typedef std::function&&)> CreatedChunkCallback; +// typedef std::function&)> SpawnedEntitiesCallback; +// typedef std::function VisualizedChunkCallback; +// typedef std::function ShadowsCallback; + +// ChunkGenerator() = default; +// private: +// ChunkGenerator(Ref settings, ChunkKey chunk, int32_t seed) : Settings{ settings }, ChunkInfo{ chunk }, Seed{ seed } {}; // public: -// WorldGenerator() = default; -// WorldGenerator(Ref settings, int32_t seed); - -// public: -// // bool GenerateChunk(ChunkKey chunkKey, Chunk& chunk) const; -// // bool GenerateChunk(ChunkKey chunkKey, Chunk& chunk, Ref layer, Ref nextLayer = {}) const; - -// // Vector SpawnEntities(ChunkKey chunkKey, Chunk& chunk, const std::vector& persistantEntities = {}) const; -// // Vector SpawnEntities(ChunkKey chunkKey, Chunk& chunk, Ref layer, Ref nextLayer = {}, const std::vector& persistantEntities = {}) const; - -// // std::unique_ptr CreateChunkVisuals(ChunkKey chunkKey, Chunk& chunk); - -// public: -// void ThreadedGenerateChunk(ChunkKey chunkKey, std::function callback, const std::vector& persistantEntities = {}); +// static void GenerateChunk(Ref settings, ChunkKey chunkInfo, int seed, CreatedChunkCallback chunkCallback, SpawnedEntitiesCallback entitiesCallback = {}, VisualizedChunkCallback visualsCallback = {}, ShadowsCallback shadowsCallback = {}); +// std::unique_ptr GenerateChunk(); // private: +// static void GenerateChunk(void* pData); +// void GenerateChunkInternal(CreatedChunkCallback chunkCallback, SpawnedEntitiesCallback entitiesCallback, VisualizedChunkCallback visualsCallback, ShadowsCallback shadowsCallback); + +// void GenerateChunkTiles() const; +// Vector SpawnEntities(const std::vector& persistantEntities = {}) const; +// std::unique_ptr CreateVisuals(); +// std::unique_ptr CascadeShadows(); + +// Pair, Ref> GetLayers() const; +// void FillChunkCollection(int relativeX, int relativeY, ChunkCollection& collection) const; +// void CascadeShadows_Recursive(std::array& values, int posX, int posY, int value); -// public: +// Tile GetTile(int x, int y) const; +// bool InBounds(int x, int y) const; + +// private: // Ref Settings{}; -// WorldGraph Graph{}; +// ChunkKey ChunkInfo{}; // int32_t Seed{}; -// }; - -class ChunkGenerator final -{ -public: - struct SpawnedEntities - { - Ref Archetype{}; - Vector2i SpawnPosition{}; - Vector ClaimedPositions{}; - }; - - struct CreatedVisualsTile - { - CreatedVisualsTile() = default; - CreatedVisualsTile(uint16_t atlasCoordinateX, uint16_t atlasCoordinateY, uint16_t atlasIndex) : AtlasCoordinateX{ atlasCoordinateX }, AtlasCoordinateY{ atlasCoordinateY }, AtlasIndex{ atlasIndex } {}; - - uint16_t AtlasCoordinateX{}; - uint16_t AtlasCoordinateY{}; - uint16_t AtlasIndex{}; - }; - - - typedef std::array CreatedVisualsChunk; - typedef std::array ChunkShadowValues; - - typedef std::function&&)> CreatedChunkCallback; - typedef std::function&)> SpawnedEntitiesCallback; - typedef std::function VisualizedChunkCallback; - typedef std::function ShadowsCallback; - - ChunkGenerator() = default; -private: - ChunkGenerator(Ref settings, ChunkKey chunk, int32_t seed) : Settings{ settings }, ChunkInfo{ chunk }, Seed{ seed } {}; - -public: - static void GenerateChunk(Ref settings, ChunkKey chunkInfo, int seed, CreatedChunkCallback chunkCallback, SpawnedEntitiesCallback entitiesCallback = {}, VisualizedChunkCallback visualsCallback = {}, ShadowsCallback shadowsCallback = {}); - std::unique_ptr GenerateChunk(); - -private: - static void GenerateChunk(void* pData); - void GenerateChunkInternal(CreatedChunkCallback chunkCallback, SpawnedEntitiesCallback entitiesCallback, VisualizedChunkCallback visualsCallback, ShadowsCallback shadowsCallback); - - void GenerateChunkTiles() const; - Vector SpawnEntities(const std::vector& persistantEntities = {}) const; - std::unique_ptr CreateVisuals(); - std::unique_ptr CascadeShadows(); - - Pair, Ref> GetLayers() const; - void FillChunkCollection(int relativeX, int relativeY, ChunkCollection& collection) const; - void CascadeShadows_Recursive(std::array& values, int posX, int posY, int value); - - Tile GetTile(int x, int y) const; - bool InBounds(int x, int y) const; - -private: - Ref Settings{}; - ChunkKey ChunkInfo{}; - int32_t Seed{}; - std::unique_ptr TileArray{}; -}; \ No newline at end of file +// std::unique_ptr TileArray{}; +// }; \ No newline at end of file diff --git a/include/Types/Filter.h b/include/Types/Filter.h index beb8007..eea5a87 100644 --- a/include/Types/Filter.h +++ b/include/Types/Filter.h @@ -2,19 +2,19 @@ #include "Item.hpp" -struct alignas(4) ItemFilter final -{ - uint16_t FilterItem0; - uint16_t FilterItem1; - uint16_t FilterItem2; - //Item::ItemFlags FilterFlags; +// struct alignas(4) ItemFilter final +// { +// uint16_t FilterItem0; +// uint16_t FilterItem1; +// uint16_t FilterItem2; +// //Item::ItemFlags FilterFlags; -public: - inline bool ApplyFilter(Item item) - { - return //(item.Flags & FilterFlags) && - (FilterItem0 == 0 || FilterItem0 == item.ItemID) && - (FilterItem1 == 0 || FilterItem1 == item.ItemID) && - (FilterItem2 == 0 || FilterItem2 == item.ItemID); - } -}; \ No newline at end of file +// public: +// inline bool ApplyFilter(Item item) +// { +// return //(item.Flags & FilterFlags) && +// (FilterItem0 == 0 || FilterItem0 == item.ItemID) && +// (FilterItem1 == 0 || FilterItem1 == item.ItemID) && +// (FilterItem2 == 0 || FilterItem2 == item.ItemID); +// } +// }; \ No newline at end of file diff --git a/include/Types/Tile.h b/include/Types/Tile.h index 0f21028..2ec15fc 100644 --- a/include/Types/Tile.h +++ b/include/Types/Tile.h @@ -1,60 +1,21 @@ #pragma once -#include "core/string/string_name.h" -#include "core/io/resource.h" -#include "scene/resources/texture.h" -#include "modules/factory/thirdParty/EnTT/entity/entity.hpp" -#include "modules/factory/include/Util/Helpers.h" +#include -// If tile data uses all 4 bytes, it is an entity, -// if it uses only 2 bytes it is a tile -// if it is 0, it is air -// struct Tile final -// { -// private: -// union TileData -// { -// uint32_t Data; -// entt::entity Entity; -// uint16_t TileID; -// }; -// static_assert(sizeof(TileData) == sizeof(uint32_t)); -// TileData Data{}; - -// static constexpr uint32_t TileMask = 0x80000000; - -// public: -// Tile() = default; -// Tile(uint16_t id) { Data.Data = id | TileMask; } -// Tile(entt::entity entity) { DEV_ASSERT(IsValidEntity(entity)); Data.Entity = entity; } - -// public: -// constexpr bool IsAir() const { return Data.Data == 0; } -// constexpr bool IsTile() const { return (Data.Data & TileMask) != 0; } -// constexpr bool IsEntity() const { return !IsAir() && !IsTile(); } -// static constexpr bool IsValidEntity(entt::entity entity) { return (((uint32_t)entity) & TileMask) == 0; } - -// public: -// entt::entity GetEntity() const { DEV_ASSERT(IsEntity()); return Data.Entity; } -// uint16_t GetTileID() const { DEV_ASSERT(IsTile()); return Data.TileID; } -// }; - -enum TILE_TYPE : uint8_t +enum class TileType : uint8_t { - TILE_AIR, - TILE_FILLER, - TILE_LIQUID, - TILE_ORE, - TILE_NPC, - TILE_PLANT, - TILE_MAX, + Air, + Filler, + Liquid, + Ore, + NPC, + Plant, + MAX, - TILE_NONE = 0b111, + NONE = 0b111, }; -VARIANT_ENUM_CAST(TILE_TYPE); -static_assert(TILE_MAX <= TILE_NONE); -const char* TileTypeEnumString = "Air,Filler,Liquid,Ore,Npc,Plant,,None"; +static_assert(static_cast(TileType::MAX) <= static_cast(TileType::NONE)); struct Tile final { @@ -80,25 +41,25 @@ public: constexpr bool HasEntity() const { return Data & MaskEntity; } constexpr uint16_t GetID() const { return Data & MaskID; } - constexpr bool IsAir() const { return GetType() == TILE_AIR; } - constexpr bool IsFiller() const { return GetType() == TILE_FILLER; } - constexpr bool IsLiquid() const { return GetType() == TILE_LIQUID; } - constexpr bool IsOre() const { return GetType() == TILE_ORE; } - constexpr bool IsPlant() const { return GetType() == TILE_PLANT; } - constexpr bool IsNPC() const { return GetType() == TILE_NPC; } + constexpr bool IsAir() const { return GetType() == TileType::Air; } + constexpr bool IsFiller() const { return GetType() == TileType::Filler; } + constexpr bool IsLiquid() const { return GetType() == TileType::Liquid; } + constexpr bool IsOre() const { return GetType() == TileType::Ore; } + constexpr bool IsPlant() const { return GetType() == TileType::Plant; } + constexpr bool IsNPC() const { return GetType() == TileType::NPC; } - constexpr TILE_TYPE GetType() const { return static_cast((Data & MaskType) >> BytesID); } - constexpr bool IsType(TILE_TYPE type) const { return GetType() == type; } + constexpr TileType GetType() const { return static_cast((Data & MaskType) >> BytesID); } + constexpr bool IsType(TileType type) const { return GetType() == type; } void SetID(uint16_t id) { Data = (Data & ~MaskID) | (id & MaskID); } - void SetAir() { SetType(TILE_AIR); } - void SetFiller() { SetType(TILE_FILLER); } - void SetLiquid() { SetType(TILE_LIQUID); } - void SetOre() { SetType(TILE_ORE); } - void SetPlant() { SetType(TILE_PLANT); } - void SetNPC() { SetType(TILE_NPC); } + void SetAir() { SetType(TileType::Air); } + void SetFiller() { SetType(TileType::Filler); } + void SetLiquid() { SetType(TileType::Liquid); } + void SetOre() { SetType(TileType::Ore); } + void SetPlant() { SetType(TileType::Plant); } + void SetNPC() { SetType(TileType::NPC); } - void SetType(TILE_TYPE type) { Data = (Data & ~MaskType) | (type << BytesID); } + void SetType(TileType type) { Data = (Data & ~MaskType) | (static_cast(type) << BytesID); } constexpr uint16_t AsInt() const { return Data; } constexpr bool IsValid() const { return Data == MaskID; } @@ -110,67 +71,4 @@ private: inline constexpr bool operator==(Tile lhs, Tile rhs) { return lhs.AsInt() == rhs.AsInt(); } inline constexpr bool operator!=(Tile lhs, Tile rhs) { return lhs.AsInt() != rhs.AsInt(); } -static_assert(sizeof(Tile) == 2); - -class TextureWeight final : public Resource -{ - GDCLASS(TextureWeight, Resource); - static void _bind_methods(); -public: - Ref GetTexture() const { return Texture; } - uint16_t GetWeight() const { return Weight; } - - void SetTexture(Ref texture) { Texture = texture; } - void SetWeight(uint16_t weight) { Weight = weight; } - -public: - Ref Texture{}; - uint16_t Weight{1}; - uint16_t AtlasIndex{}; - uint16_t AtlasX{}; - uint16_t AtlasY{}; -}; - -class TileConfig; - -class TileTransitionConfig final : public Resource -{ - GDCLASS(TileTransitionConfig, Resource); - static void _bind_methods(); -public: - auto GetNeighbor() const { return Neighbor; } - auto GetPossibleTextures() const { return VectorToTypedArray(PossibleTextures); } - - void SetNeighbor(Ref neighbor) { Neighbor = neighbor; } - void SetPossibleTextures(TypedArray textures) { PossibleTextures = TypedArrayToVector(textures); } - -public: - Ref Neighbor; - Vector> PossibleTextures; -}; - -class TileConfig final : public Resource -{ - GDCLASS(TileConfig, Resource); -public: - static void _bind_methods(); - -public: - uint16_t GetID() const { return TileData.GetID(); } - TILE_TYPE GetType() const { return TileData.GetType(); } - auto GetTextures() const { return VectorToTypedArray(PossibleTextures); } - auto GetTransitions() const { return VectorToTypedArray(NeighborTransitions); } - auto GetLightResistance() const { return VectorToTypedArray(NeighborTransitions); } - - void SetID(uint16_t id) { TileData.SetID(id); } - void SetTextures(TypedArray val) { PossibleTextures = TypedArrayToVector(val); } - void SetTransitions(TypedArray val) { NeighborTransitions = TypedArrayToVector(val); } - void SetType(TILE_TYPE type) { TileData.SetType(type); } - void SetLightResistance(int resistance) { LightResistance = resistance; } - -public: - Tile TileData{}; - // Vector> PossibleTextures; - // Vector> NeighborTransitions; - int LightResistance{}; -}; \ No newline at end of file +static_assert(sizeof(Tile) == 2); \ No newline at end of file