Add Scene deserialization

This commit is contained in:
Thraix
2023-07-13 10:58:41 +02:00
parent 042d1b6c70
commit 4a1a149a68
19 changed files with 456 additions and 157 deletions
+1
View File
@@ -259,6 +259,7 @@
<ClInclude Include="src\copium\ecs\EntitySet.h" />
<ClInclude Include="src\copium\example\AnimationSystem.h" />
<ClInclude Include="src\copium\example\DebugSystem.h" />
<ClInclude Include="src\copium\example\LevelGeneratorComponentListener.h" />
<ClInclude Include="src\copium\example\PickupSystem.h" />
<ClInclude Include="src\copium\ecs\Signal.h" />
<ClInclude Include="src\copium\ecs\System.h" />
@@ -521,5 +521,8 @@
<ClInclude Include="src\copium\example\AnimationSystem.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\copium\example\LevelGeneratorComponentListener.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
+121
View File
@@ -0,0 +1,121 @@
[LevelGenerator]
---
[Transform]
position=0.1 -0.4
size=0.8 0.8
[Texture]
texture-uuid=3e9ef1a2-59ba-2e4d-b2cd-f5efaa531af7
[StaticCollider]
resolve-collision=false
[Pickup]
[Renderable]
---
[Transform]
position=0.1 0.1
size=0.2 0.2
[Texture]
texture-uuid=f49a5284-d666-0982-95ca-cf68cc3d4f45
[MouseFollow]
[Renderable]
---
[Transform]
position=10 10
size=0
[Text]
font=3e9ef1a2-59ba-2e4d-b2cd-f5efaa531af7
text=0 fps
font-size=20
[FrameCount]
[UiRenderable]
---
[Camera]
static-bounding-box=false
ui-camera=false
[Transform]
position=0 0
size=2 2
---
[Camera]
static-bounding-box=false
ui-camera=false
[Transform]
position=0 0
size=2 2
[Uuid]
uuid=a14e2328-ebce-5026-e5ab-495daf8c3660
---
[Camera]
static-bounding-box=false
ui-camera=true
[Transform]
position=0 0
size=1 1
---
[Player]
camera-uuid=a14e2328-ebce-5026-e5ab-495daf8c3660
[Transform]
position=0 2
size=1 1
[Health]
health=10
[Physics]
mass=0.1
[Texture]
texture-uuid=0964e525-22c3-4d25-d5c6-a162965f6e8d
tex-coord=0 0
tex-size=0.25 0.25
[Animation]
sheet-size=4 4
sheet-coord=0 3
images=4
horizontal=true
time=0.5
[DynamicCollider]
resolve-collision=false
collider-offset=0.4375 0
collider-size=0.125 0.65625
[Uuid]
uuid=09617c73-2b1e-0a83-db81-3ae3b5971c11
[Renderable]
---
[Transform]
position=10 800
size=1 1
[Text]
font=3e9ef1a2-59ba-2e4d-b2cd-f5efaa531af7
text=
font-size=20
[Debug]
player-uuid=09617c73-2b1e-0a83-db81-3ae3b5971c11
[UiRenderable]
@@ -10,6 +10,8 @@ namespace Copium
}
};
AssetRef::AssetRef() = default;
AssetRef::AssetRef(AssetHandle handle)
: handle{std::shared_ptr<AssetHandle>(new AssetHandle{handle}, AssetHandleUnloader{})}
{}
+1
View File
@@ -10,6 +10,7 @@ namespace Copium
std::shared_ptr<AssetHandle> handle;
public:
AssetRef();
AssetRef(AssetHandle handle);
operator AssetHandle() const;
+171 -104
View File
@@ -7,16 +7,17 @@
#include "copium/ecs/Entity.h"
#include "copium/ecs/System.h"
#include "copium/event/MouseMoveEvent.h"
#include "copium/example/AnimationSystem.h"
#include "copium/example/CameraFollowPlayerSystem.h"
#include "copium/example/CameraUpdateSystem.h"
#include "copium/example/ColliderSystem.h"
#include "copium/example/Components.h"
#include "copium/example/DebugSystem.h"
#include "copium/example/AnimationSystem.h"
#include "copium/example/FrameCountSystem.h"
#include "copium/example/HealthChangeSystem.h"
#include "copium/example/HealthComponentListener.h"
#include "copium/example/HealthDisplaySystem.h"
#include "copium/example/LevelGeneratorComponentListener.h"
#include "copium/example/MouseFollowSystem.h"
#include "copium/example/PhysicsSystem.h"
#include "copium/example/PickupSystem.h"
@@ -53,110 +54,9 @@ namespace Copium
ecs->AddSystem<RenderSystem>(renderer.get(), descriptorSetRenderer.get(), &commandBuffer, &viewMatrix, &projectionMatrix); // better way to store the RenderSystem data?
ecs->AddSystem<UiRenderSystem>(uiRenderer.get(), uiDescriptorSetRenderer.get(), &commandBuffer, &uiProjectionMatrix);
ecs->SetComponentListener<HealthComponentListener>();
ecs->SetComponentListener<LevelGeneratorComponentListener>();
// TODO: Load from scene file
for (int y = 0; y < 20; y++)
{
{
Entity entity = Entity::Create(ecs.get());
entity.AddComponent<TransformC>(glm::vec2{-10.0f, -10.0f + y * 1.0}, glm::vec2{1.0f, 1.0f});
if(y == 0 || y == 19)
entity.AddComponent<TextureC>(AssetRef{AssetManager::LoadAsset("stone.meta")}, glm::vec2{0.0f, 0.0f}, glm::vec2{0.25f, 1.0f});
else
entity.AddComponent<TextureC>(AssetRef{AssetManager::LoadAsset("stone.meta")}, glm::vec2{0.75f, 0.0f}, glm::vec2{1.0f, 1.0f});
entity.AddComponent<StaticColliderC>(true);
entity.AddComponent<Renderable>();
}
{
Entity entity = Entity::Create(ecs.get());
entity.AddComponent<TransformC>(glm::vec2{10.0f, -10.0f + y * 1.0}, glm::vec2{1.0f, 1.0f});
if(y == 0 || y == 19)
entity.AddComponent<TextureC>(AssetRef{AssetManager::LoadAsset("stone.meta")}, glm::vec2{0.5f, 0.0f}, glm::vec2{0.75f, 1.0f});
else
entity.AddComponent<TextureC>(AssetRef{AssetManager::LoadAsset("stone.meta")}, glm::vec2{0.75f, 0.0f}, glm::vec2{1.0f, 1.0f});
entity.AddComponent<StaticColliderC>(true);
entity.AddComponent<Renderable>();
}
}
for (int x = 1; x < 20; x++)
{
{
Entity entity = Entity::Create(ecs.get());
entity.AddComponent<TransformC>(glm::vec2{-10.0f + x * 1.0, -10.0f}, glm::vec2{1.0f, 1.0f});
entity.AddComponent<TextureC>(AssetRef{AssetManager::LoadAsset("stone.meta")}, glm::vec2{0.25f, 0.0f}, glm::vec2{0.5f, 1.0f});
entity.AddComponent<StaticColliderC>(true);
entity.AddComponent<Renderable>();
}
{
Entity entity = Entity::Create(ecs.get());
entity.AddComponent<TransformC>(glm::vec2{-10.0f + x * 1.0, 10.0f}, glm::vec2{1.0f, 1.0f});
entity.AddComponent<TextureC>(AssetRef{AssetManager::LoadAsset("stone.meta")}, glm::vec2{0.25f, 0.0f}, glm::vec2{0.5f, 1.0f});
entity.AddComponent<StaticColliderC>(true);
entity.AddComponent<Renderable>();
}
}
for (int y = 0; y < 10; y++)
{
for (int x = 0; x < 10; x++)
{
Entity entity = Entity::Create(ecs.get());
entity.AddComponent<TransformC>(glm::vec2{-10.0f + x * 1.6f + 0.4f, -10.0f + y * 1.6 + 0.4f}, glm::vec2{0.8f, 0.8f});
entity.AddComponent<ColorC>(glm::vec3{x * 0.1f, y * 0.1f, 1.0f});
entity.AddComponent<StaticColliderC>(false);
entity.AddComponent<PickupC>();
entity.AddComponent<Renderable>();
}
}
float aspect = Vulkan::GetSwapChain().GetExtent().width / (float)Vulkan::GetSwapChain().GetExtent().height;
Entity entityFox = Entity::Create(ecs.get());
entityFox.AddComponent<TransformC>(glm::vec2{-0.9f, -0.4f}, glm::vec2{0.8f, 0.8f});
entityFox.AddComponent<TextureC>(AssetRef{AssetManager::LoadAsset("fox.meta")}, glm::vec2{0.0f, 0.0f}, glm::vec2{1.0f, 1.0f});
entityFox.AddComponent<StaticColliderC>(true);
entityFox.AddComponent<Renderable>();
Entity entityFontAtlas = Entity::Create(ecs.get());
entityFontAtlas.AddComponent<TransformC>(glm::vec2{0.1f, -0.4f}, glm::vec2{0.8, 0.8});
entityFontAtlas.AddComponent<TextureC>(AssetRef{AssetManager::LoadAsset("font.meta")}, glm::vec2{0.0f, 0.0f}, glm::vec2{1.0f, 1.0f});
entityFontAtlas.AddComponent<StaticColliderC>(false);
entityFontAtlas.AddComponent<PickupC>();
entityFontAtlas.AddComponent<Renderable>();
Entity entityMouse = Entity::Create(ecs.get());
entityMouse.AddComponent<TransformC>(glm::vec2(0.1), glm::vec2{0.2});
entityMouse.AddComponent<TextureC>(AssetRef{AssetManager::LoadAsset("fox.meta")}, glm::vec2{0.0f, 0.0f}, glm::vec2{1.0f, 1.0f});
entityMouse.AddComponent<MouseFollowC>();
entityMouse.AddComponent<Renderable>();
Entity entityText = Entity::Create(ecs.get());
entityText.AddComponent<TransformC>(glm::vec2{-aspect * 10.0f + 0.1f, 9.4f}, glm::vec2{1.0});
entityText.AddComponent<TextC>(AssetRef{AssetManager::LoadAsset("font.meta")}, std::to_string(0) + " fps", 0.6f);
entityText.AddComponent<FrameCountC>();
entityText.AddComponent<Renderable>();
Entity entityCamera = Entity::Create(ecs.get());
entityCamera.AddComponent<CameraC>(BoundingBox(-aspect, -1.0f, aspect, 1.0f), false, false);
entityCamera.AddComponent<TransformC>(glm::vec2{0.0f}, glm::vec2{2.0f});
Entity entityUiCamera = Entity::Create(ecs.get());
entityUiCamera.AddComponent<CameraC>(BoundingBox(0.0f, 0.0f, Vulkan::GetSwapChain().GetExtent().width, Vulkan::GetSwapChain().GetExtent().height), false, true);
entityUiCamera.AddComponent<TransformC>(glm::vec2{0.0f}, glm::vec2{1.0f});
Entity entityPlayer = Entity::Create(ecs.get());
entityPlayer.AddComponent<PlayerC>(entityCamera, false);
entityPlayer.AddComponent<HealthC>(10, 10);
entityPlayer.AddComponent<PhysicsC>(0.1f, glm::vec2{0.0f, 0.0f}, glm::vec2{0.0f, 0.0f});
entityPlayer.AddComponent<TransformC>(glm::vec2{0.0f, 2.0f}, glm::vec2{1.0f});
entityPlayer.AddComponent<TextureC>(AssetRef{AssetManager::LoadAsset("character.meta")}, glm::vec2{0.0f, 0.0f}, glm::vec2{0.25f, 1.0f});
entityPlayer.AddComponent<AnimationC>(4, 4, 0, 3, 4, true, 0.5f);
entityPlayer.AddComponent<DynamicColliderC>(false, glm::vec2{14.0f / 32.0f, 0.0f / 32.0f}, glm::vec2{4.0f / 32.0f, 21.0f / 32.0f}, glm::vec2{0.0f});
entityPlayer.AddComponent<Renderable>();
Entity entityDebug = Entity::Create(ecs.get());
entityDebug.AddComponent<DebugC>(entityPlayer);
entityDebug.AddComponent<TextC>(AssetRef(AssetManager::LoadAsset("font.meta")), "", 20.0f);
entityDebug.AddComponent<TransformC>(glm::vec2{10.0f, Vulkan::GetSwapChain().GetExtent().height - 10.0f}, glm::vec2{1.0f});
entityDebug.AddComponent<UiRenderable>();
Deserialize("res/scenes/scene.meta");
}
void Scene::Update()
@@ -169,4 +69,171 @@ namespace Copium
ecs->UpdateSystems(EventSignal{event});
return EventResult::Continue;
}
void Scene::Deserialize(const std::string& file)
{
float aspect = Vulkan::GetSwapChain().GetExtent().width / (float)Vulkan::GetSwapChain().GetExtent().height;
std::vector<MetaFile> metaFiles = MetaFile::ReadList(file);
for (auto& metaFile : metaFiles)
{
Entity entity = Entity::Create(ecs.get());
for (auto& [name, metaClass] : metaFile.GetMetaFileClasses())
{
try
{
char* endPtr;
if (name == "Transform")
{
TransformC transform;
transform.position = ReadVec2Opt(metaClass, "position", glm::vec2{0.0f, 0.0f});
transform.size = ReadVec2Opt(metaClass, "size", glm::vec2{1.0f, 1.0f});
entity.AddComponent<TransformC>(transform);
}
else if (name == "Texture")
{
TextureC texture;
texture.asset = AssetRef{AssetManager::LoadAsset(Uuid{metaClass.GetValue("texture-uuid")})};
texture.texCoord1 = ReadVec2Opt(metaClass, "tex-coord", glm::vec2{0.0f, 0.0f});
texture.texCoord2 = texture.texCoord1 + ReadVec2Opt(metaClass, "tex-size", glm::vec2{1.0f, 1.0f} - texture.texCoord1);
entity.AddComponent<TextureC>(texture);
}
else if (name == "StaticCollider")
{
StaticColliderC staticCollider;
staticCollider.resolveCollision = ReadBoolOpt(metaClass, "resolve-collision", true);
entity.AddComponent<StaticColliderC>(staticCollider);
}
else if (name == "DynamicCollider")
{
DynamicColliderC dynamicCollider;
dynamicCollider.resolveCollision = ReadBoolOpt(metaClass, "resolve-collision", true);
dynamicCollider.colliderOffset = ReadVec2Opt(metaClass, "collider-offset", glm::vec2{0.0f, 0.0f});
dynamicCollider.colliderSize = ReadVec2Opt(metaClass, "collider-size", glm::vec2{1.0f, 1.0f});
entity.AddComponent<DynamicColliderC>(dynamicCollider);
}
else if (name == "Text")
{
TextC text;
text.font = AssetRef{AssetManager::LoadAsset(Uuid{metaClass.GetValue("font")})};
text.text = metaClass.GetValue("text");
text.fontSize = std::strtof(metaClass.GetValue("font-size").c_str(), &endPtr);
entity.AddComponent<TextC>(text);
}
else if (name == "Camera")
{
CameraC camera;
camera.staticBoundingBox = ReadBoolOpt(metaClass, "static-bounding-box", false);
camera.uiCamera = ReadBoolOpt(metaClass, "ui-camera", false);
if (camera.uiCamera)
camera.projection = BoundingBox(0, 0, Vulkan::GetSwapChain().GetExtent().width, Vulkan::GetSwapChain().GetExtent().height);
else
camera.projection = BoundingBox(-aspect, -1.0f, aspect, 1.0f);
entity.AddComponent<CameraC>(camera);
}
else if (name == "Uuid")
{
UuidC uuid;
uuid.uuid = Uuid{metaClass.GetValue("uuid")};
entity.AddComponent<UuidC>(uuid);
}
else if (name == "Player")
{
PlayerC player;
player.camera = GetEntity(Uuid{metaClass.GetValue("camera-uuid")});
entity.AddComponent<PlayerC>(player);
}
else if (name == "Health")
{
HealthC health;
health.max = std::strtol(metaClass.GetValue("health").c_str(), &endPtr, 10);
health.current = health.max;
entity.AddComponent<HealthC>(health);
}
else if (name == "Physics")
{
PhysicsC physics;
physics.mass = std::strtof(metaClass.GetValue("mass").c_str(), &endPtr);
entity.AddComponent<PhysicsC>(physics);
}
else if (name == "Animation")
{
AnimationC animation;
animation.sheetCoord = ReadVec2Opt(metaClass, "sheet-coord", glm::ivec2{0, 0});
animation.sheetSize = ReadVec2Opt(metaClass, "sheet-size", glm::ivec2{1, 1});
animation.images = std::strtol(metaClass.GetValue("images").c_str(), &endPtr, 10);
animation.horizontal = ReadBoolOpt(metaClass, "horizontal", true);
animation.time = std::strtof(metaClass.GetValue("time").c_str(), &endPtr);
entity.AddComponent<AnimationC>(animation);
}
else if (name == "Debug")
{
DebugC debug;
debug.playerEntity = GetEntity(Uuid{metaClass.GetValue("player-uuid")});
entity.AddComponent<DebugC>(debug);
}
else if (name == "Renderable")
entity.AddComponent<RenderableC>();
else if (name == "Pickup")
entity.AddComponent<PickupC>();
else if (name == "FrameCount")
entity.AddComponent<FrameCountC>();
else if (name == "MouseFollow")
entity.AddComponent<MouseFollowC>();
else if (name == "UiRenderable")
entity.AddComponent<UiRenderableC>();
else if(name == "LevelGenerator")
entity.AddComponent<LevelGeneratorC>();
else
CP_WARN("Unknown component: %s", name.c_str());
}
catch (RuntimeException& exception) { CP_ERR("Invalid %s component: %s", name.c_str(), exception.GetErrorMessage().c_str()); }
}
}
}
Entity Scene::GetEntity(const Uuid& uuid) const
{
Entity entity{ecs.get(),ecs->Find<UuidC>([&](EntityId entity, const UuidC& uuidArg) { return uuid == uuidArg.uuid; })};
CP_ASSERT(entity, "Failed to find entity with Uuid=%s", uuid.ToString().c_str());
return entity;
}
glm::vec2 Scene::ReadVec2Opt(const MetaFileClass& metaClass, const std::string& key, glm::vec2 vec)
{
if (!metaClass.HasValue(key))
return vec;
const std::string& value = metaClass.GetValue(key);
char* endPos;
vec.x = std::strtof(value.c_str(), &endPos);
vec.y = std::strtof(endPos, &endPos);
return vec;
}
glm::ivec2 Scene::ReadVec2Opt(const MetaFileClass& metaClass, const std::string& key, glm::ivec2 vec)
{
if (!metaClass.HasValue(key))
return vec;
const std::string& value = metaClass.GetValue(key);
char* endPos;
vec.x = std::strtof(value.c_str(), &endPos);
vec.y = std::strtof(endPos, &endPos);
return vec;
}
bool Scene::ReadBoolOpt(const MetaFileClass& metaClass, const std::string& key, bool vec)
{
if (!metaClass.HasValue(key))
return vec;
const std::string& value = metaClass.GetValue(key);
if (value == "true")
return true;
return false;
}
}
+10 -1
View File
@@ -1,9 +1,11 @@
#pragma once
#include "copium/renderer/Renderer.h"
#include "copium/ecs/ECSManager.h"
#include "copium/ecs/Entity.h"
#include "copium/event/Event.h"
#include "copium/event/EventResult.h"
#include "copium/renderer/Renderer.h"
#include "copium/util/Uuid.h"
#include <memory>
@@ -25,5 +27,12 @@ namespace Copium
Scene(CommandBuffer& commandBuffer, DescriptorPool& descriptorPool);
void Update();
EventResult OnEvent(const Event& event);
private:
void Deserialize(const std::string& file);
Entity GetEntity(const Uuid& uuid) const;
glm::vec2 ReadVec2Opt(const MetaFileClass& metaClass, const std::string& key, glm::vec2 vec);
glm::ivec2 ReadVec2Opt(const MetaFileClass& metaClass, const std::string& key, glm::ivec2 vec);
bool ReadBoolOpt(const MetaFileClass& metaClass, const std::string& key, bool vec);
};
}
@@ -4,6 +4,7 @@
#include "copium/ecs/EntitySet.h"
#include "copium/ecs/ComponentPoolBase.h"
#include "copium/ecs/ComponentListener.h"
#include "copium/util/Common.h"
#include <vector>
@@ -16,16 +16,16 @@ namespace Copium
animation.frame = (animation.frame + 1) % animation.images;
}
if (animation.horizontal) {
texture.texCoord1.x = (animation.sheetStartCoordX + animation.frame) / (float)animation.sheetSizeX;
texture.texCoord2.x = (animation.sheetStartCoordX + animation.frame + 1) / (float)animation.sheetSizeX;
texture.texCoord1.y = (animation.sheetStartCoordY) / (float)animation.sheetSizeY;
texture.texCoord2.y = (animation.sheetStartCoordY + 1) / (float)animation.sheetSizeY;
texture.texCoord1.x = (animation.sheetCoord.x + animation.frame) / (float)animation.sheetSize.x;
texture.texCoord2.x = (animation.sheetCoord.x + animation.frame + 1) / (float)animation.sheetSize.x;
texture.texCoord1.y = (animation.sheetCoord.y) / (float)animation.sheetSize.y;
texture.texCoord2.y = (animation.sheetCoord.y + 1) / (float)animation.sheetSize.y;
}
else {
texture.texCoord1.x = (animation.sheetStartCoordX) / (float)animation.sheetSizeX;
texture.texCoord2.x = (animation.sheetStartCoordX + 1) / (float)animation.sheetSizeX;
texture.texCoord1.y = (animation.sheetStartCoordY + animation.frame) / (float)animation.sheetSizeY;
texture.texCoord2.y = (animation.sheetStartCoordY + animation.frame + 1) / (float)animation.sheetSizeY;
texture.texCoord1.x = (animation.sheetCoord.x) / (float)animation.sheetSize.x;
texture.texCoord2.x = (animation.sheetCoord.x + 1) / (float)animation.sheetSize.x;
texture.texCoord1.y = (animation.sheetCoord.y + animation.frame) / (float)animation.sheetSize.y;
texture.texCoord2.y = (animation.sheetCoord.y + animation.frame + 1) / (float)animation.sheetSize.y;
}
}
};
+25 -14
View File
@@ -2,6 +2,7 @@
#include "copium/asset/AssetRef.h"
#include "copium/ecs/Entity.h"
#include "copium/util/Uuid.h"
#include <string>
@@ -15,8 +16,8 @@ namespace Copium
glm::vec2 size;
};
struct Renderable {};
struct UiRenderable {};
struct RenderableC {};
struct UiRenderableC {};
struct ColorC
{
@@ -53,14 +54,16 @@ namespace Copium
struct PhysicsC
{
float mass;
glm::vec2 force;
glm::vec2 velocity;
glm::vec2 force{};
glm::vec2 velocity{};
};
struct PlayerC
{
Entity camera;
bool grounded;
bool grounded = false;
};
struct HealthC
@@ -68,8 +71,8 @@ namespace Copium
int current;
int max;
Entity background;
Entity foreground;
Entity background{};
Entity foreground{};
};
struct StaticColliderC
@@ -83,7 +86,7 @@ namespace Copium
glm::vec2 colliderOffset;
glm::vec2 colliderSize;
glm::vec2 oldPosition;
glm::vec2 oldPosition{};
};
struct PickupC
@@ -98,15 +101,23 @@ namespace Copium
struct AnimationC
{
int sheetSizeX;
int sheetSizeY;
int sheetStartCoordX;
int sheetStartCoordY;
glm::ivec2 sheetCoord;
glm::ivec2 sheetSize;
int images;
bool horizontal;
float time;
float timeElapsed;
int frame;
float timeElapsed = 0.0f;
int frame = 0;
};
struct UuidC
{
Uuid uuid;
};
struct LevelGeneratorC
{
std::vector<Entity> entities;
};
}
@@ -16,12 +16,12 @@ namespace Copium
health.background = Entity::Create(manager);
health.background.AddComponent<TransformC>(glm::vec2{0.0f, 0.0f}, glm::vec2{0.5f, 0.05f});
health.background.AddComponent<ColorC>(glm::vec3{0.152f, 0.14f, 0.207f});
health.background.AddComponent<Renderable>();
health.background.AddComponent<RenderableC>();
health.foreground = Entity::Create(manager);
health.foreground.AddComponent<TransformC>(glm::vec2{0.0f, 0.0f}, glm::vec2{0.5f * std::clamp(health.current, 0, health.max) / (float)health.max, 0.05f});
health.foreground.AddComponent<ColorC>(glm::vec3{0.581f, 0.393f, 0.462f});
health.foreground.AddComponent<Renderable>();
health.foreground.AddComponent<RenderableC>();
}
void Removed(EntityId entityId, HealthC& health) override
@@ -0,0 +1,77 @@
#pragma once
#include "copium/asset/AssetManager.h"
#include "copium/ecs/ComponentListener.h"
#include "copium/example/Components.h"
namespace Copium
{
class LevelGeneratorComponentListener : public ComponentListener<LevelGeneratorC>
{
void Added(EntityId entityId, LevelGeneratorC& levelGenerator) override
{
CP_ASSERT(levelGenerator.entities.empty(), "LevelGenerator has already generated the level");
for (int y = 0; y < 20; y++)
{
{
Entity entity = Entity::Create(manager);
entity.AddComponent<TransformC>(glm::vec2{-10.0f, -10.0f + y * 1.0}, glm::vec2{1.0f, 1.0f});
if(y == 0 || y == 19)
entity.AddComponent<TextureC>(AssetRef{AssetManager::LoadAsset("stone.meta")}, glm::vec2{0.0f, 0.0f}, glm::vec2{0.25f, 1.0f});
else
entity.AddComponent<TextureC>(AssetRef{AssetManager::LoadAsset("stone.meta")}, glm::vec2{0.75f, 0.0f}, glm::vec2{1.0f, 1.0f});
entity.AddComponent<StaticColliderC>(true);
entity.AddComponent<RenderableC>();
}
{
Entity entity = Entity::Create(manager);
entity.AddComponent<TransformC>(glm::vec2{10.0f, -10.0f + y * 1.0}, glm::vec2{1.0f, 1.0f});
if(y == 0 || y == 19)
entity.AddComponent<TextureC>(AssetRef{AssetManager::LoadAsset("stone.meta")}, glm::vec2{0.5f, 0.0f}, glm::vec2{0.75f, 1.0f});
else
entity.AddComponent<TextureC>(AssetRef{AssetManager::LoadAsset("stone.meta")}, glm::vec2{0.75f, 0.0f}, glm::vec2{1.0f, 1.0f});
entity.AddComponent<StaticColliderC>(true);
entity.AddComponent<RenderableC>();
}
}
for (int x = 1; x < 20; x++)
{
{
Entity entity = Entity::Create(manager);
entity.AddComponent<TransformC>(glm::vec2{-10.0f + x * 1.0, -10.0f}, glm::vec2{1.0f, 1.0f});
entity.AddComponent<TextureC>(AssetRef{AssetManager::LoadAsset("stone.meta")}, glm::vec2{0.25f, 0.0f}, glm::vec2{0.5f, 1.0f});
entity.AddComponent<StaticColliderC>(true);
entity.AddComponent<RenderableC>();
}
{
Entity entity = Entity::Create(manager);
entity.AddComponent<TransformC>(glm::vec2{-10.0f + x * 1.0, 10.0f}, glm::vec2{1.0f, 1.0f});
entity.AddComponent<TextureC>(AssetRef{AssetManager::LoadAsset("stone.meta")}, glm::vec2{0.25f, 0.0f}, glm::vec2{0.5f, 1.0f});
entity.AddComponent<StaticColliderC>(true);
entity.AddComponent<RenderableC>();
}
}
for (int y = 0; y < 10; y++)
{
for (int x = 0; x < 10; x++)
{
Entity entity = Entity::Create(manager);
entity.AddComponent<TransformC>(glm::vec2{-10.0f + x * 1.6f + 0.4f, -10.0f + y * 1.6 + 0.4f}, glm::vec2{0.8f, 0.8f});
entity.AddComponent<ColorC>(glm::vec3{x * 0.1f, y * 0.1f, 1.0f});
entity.AddComponent<StaticColliderC>(false);
entity.AddComponent<PickupC>();
entity.AddComponent<RenderableC>();
}
}
}
void Removed(EntityId entityId, LevelGeneratorC& levelGenerator) override
{
for (auto& entity : levelGenerator.entities)
{
entity.Destroy();
}
}
};
}
@@ -30,20 +30,20 @@ namespace Copium
animation.time = 0.5;
if (Input::IsKeyDown(CP_KEY_A)) {
force -= 1.0f;
animation.sheetStartCoordY = 0;
animation.sheetCoord.y = 0;
animation.time = 0.2;
}
if (Input::IsKeyDown(CP_KEY_D)) {
force += 1.0f;
animation.sheetStartCoordY = 1;
animation.sheetCoord.y = 1;
animation.time = 0.2;
}
if (force == 0.0f && animation.sheetStartCoordY == 1)
animation.sheetStartCoordY = 3;
if (force == 0.0f && animation.sheetStartCoordY == 0)
animation.sheetStartCoordY = 2;
if (force == 0.0f && animation.sheetCoord.y == 1)
animation.sheetCoord.y = 3;
if (force == 0.0f && animation.sheetCoord.y == 0)
animation.sheetCoord.y = 2;
float magnitude = 75.0f;
physics.force.x += force * magnitude;
@@ -10,7 +10,7 @@
namespace Copium
{
class RenderSystem : public System<Renderable, TransformC>
class RenderSystem : public System<RenderableC, TransformC>
{
private:
// Find better way to store these?
@@ -30,7 +30,7 @@ namespace Copium
projectionMatrix{projectionMatrix}
{}
void RunEntity(Entity entity, Renderable& renderable, TransformC& transform) override
void RunEntity(Entity entity, RenderableC& renderable, TransformC& transform) override
{
if (entity.HasComponent<TextC>())
{
@@ -10,7 +10,7 @@
namespace Copium
{
class UiRenderSystem : public System<UiRenderable, TransformC>
class UiRenderSystem : public System<UiRenderableC, TransformC>
{
private:
// Find better way to store these?
@@ -26,7 +26,7 @@ namespace Copium
projectionMatrix{projectionMatrix}
{}
void RunEntity(Entity entity, UiRenderable& uiRenderable, TransformC& transform)
void RunEntity(Entity entity, UiRenderableC& uiRenderable, TransformC& transform)
{
if (entity.HasComponent<TextC>())
{
+16 -12
View File
@@ -61,6 +61,11 @@ namespace Copium
LoadMetaFile(stream);
}
const std::map<std::string, MetaFileClass>& MetaFile::GetMetaFileClasses()
{
return classes;
}
bool MetaFile::HasMetaClass(const std::string& className) const
{
return classes.find(className) != classes.end();
@@ -161,19 +166,18 @@ namespace Copium
{
std::vector<MetaFile> metaFiles;
std::ifstream stream{file};
if(stream)
CP_ASSERT(stream, "Failed to read file: %s", file.c_str());
MetaFile meta;
while(!stream.eof())
{
MetaFile meta;
while(!stream.eof())
{
MetaFile meta{};
stream >> meta;
if(meta.classes.empty())
continue;
metaFiles.emplace_back(meta);
}
return metaFiles;
MetaFile meta{};
stream >> meta;
if(meta.classes.empty())
continue;
metaFiles.emplace_back(meta);
}
return {};
return metaFiles;
}
}
+2
View File
@@ -40,6 +40,8 @@ namespace Copium
MetaFile(std::istream& stream);
bool HasMetaClass(const std::string& className) const;
const std::map<std::string, MetaFileClass>& GetMetaFileClasses();
MetaFileClass& GetMetaClass(const std::string& className);
const MetaFileClass& GetMetaClass(const std::string& className) const;
const std::string& GetFilePath() const;
+3 -3
View File
@@ -55,17 +55,17 @@ namespace Copium
return string;
}
bool Uuid::operator==(const Uuid& rhs)
bool Uuid::operator==(const Uuid& rhs) const
{
return msb == rhs.msb && lsb == rhs.lsb;
}
bool Uuid::operator!=(const Uuid& rhs)
bool Uuid::operator!=(const Uuid& rhs) const
{
return !(*this == rhs);
}
bool Uuid::operator<(const Uuid& rhs)
bool Uuid::operator<(const Uuid& rhs) const
{
if (msb != rhs.msb)
return msb < rhs.msb;
+3 -3
View File
@@ -22,9 +22,9 @@ namespace Copium
std::string ToString() const;
bool operator==(const Uuid& rhs);
bool operator!=(const Uuid& rhs);
bool operator<(const Uuid& rhs);
bool operator==(const Uuid& rhs) const;
bool operator!=(const Uuid& rhs) const;
bool operator<(const Uuid& rhs) const;
friend std::ostream& operator<<(std::ostream& os, const Uuid& uuid);