idk if I like this structure.

This commit is contained in:
2024-09-13 09:26:21 -05:00
parent b309478922
commit b4c2ce16a0
20 changed files with 195 additions and 111 deletions

View File

@ -45,11 +45,10 @@ void SceneComponent::dispose() {
this->listeners.clear();
this->onDispose();
this->item.reset();
this->item = nullptr;
}
std::shared_ptr<SceneItem> SceneComponent::getItem() {
auto item = this->item.lock();
assertNotNull(item, "Item cannot be null?");
return item;
}

View File

@ -17,7 +17,7 @@ namespace Dawn {
class SceneComponent : std::enable_shared_from_this<SceneComponent> {
private:
std::weak_ptr<SceneItem> item;
std::shared_ptr<SceneItem> item;
uint_fast8_t sceneComponentState = 0;
protected:

View File

@ -44,4 +44,5 @@ SceneItem::~SceneItem() {
component->dispose();
}
);
this->components.clear();
}

View File

@ -8,6 +8,7 @@
#include "assert/assert.hpp"
#include "util/Easing.hpp"
#include "component/world/World.hpp"
#include "component/world/Map.hpp"
using namespace Dawn;
@ -20,12 +21,16 @@ const glm::vec3 EntityTilePosition::toWorldSpace() {
}
void Entity::onInit() {
assertNotNull(this->map, "Entity map cannot be null.");
// Listen for turn time
listeners.push_back(getScene()->onUnpausedUpdate.listen([this](float_t delta){
if(this->turnTime <= 0.0f) return;
this->turnTime -= delta;
if(this->turnTime <= 0) this->turnTime = 0.0f;
}));
// Listen for movement
listeners.push_back(getScene()->onUnpausedUpdate.listen([this](float_t delta){
if(this->stepTime <= 0.0f) return;
this->stepTime -= delta * this->stepSpeed;
@ -47,17 +52,13 @@ void Entity::onInit() {
this->eventMove.emit();
}));
this->getWorld()->entityNotifyInit(this->getItem());
// Notify map.
this->map->entityNotifyInit(this->getItem());
}
void Entity::onDispose() {
this->getWorld()->entityNotifyDispose(this->getItem());
}
std::shared_ptr<World> Entity::getWorld() {
auto world = this->world.lock();
assertNotNull(world, "World is no longer active?");
return world;
this->map->entityNotifyDispose(this->getItem());
this->map = nullptr;
}
struct EntityStepResult Entity::move(
@ -92,7 +93,7 @@ struct EntityStepResult Entity::move(
}
// Check for entity in way.
auto entityInWay = this->getWorld()->getEntityAt(newPosition);
auto entityInWay = this->map->getEntityAt(newPosition);
if(entityInWay) {
result.type = EntityStepResultType::EntityInWay;
result.entityInWay = entityInWay;

View File

@ -4,17 +4,14 @@
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/SceneComponent.hpp"
#include "component/world/Map.hpp"
#include "event/Event.hpp"
#include "EntityID.hpp"
#include "EntityTilePosition.hpp"
#define ENTITY_STEP_SPEED_DEFAULT 3.0f
#define ENTITY_STEP_SPEED_RUNNING 6.0f
#define ENTITY_TURN_TIME 0.06f
namespace Dawn {
class World;
class Entity;
enum class EntityDirection {
@ -51,7 +48,7 @@ namespace Dawn {
public:
enum EntityID id = EntityID::Null;
std::weak_ptr<World> world;
std::shared_ptr<Map> map;
Event<> eventStepStart;
Event<> eventStepEnd;
@ -61,13 +58,6 @@ namespace Dawn {
void onInit() override;
void onDispose() override;
/**
* Gets the world this entity is in.
*
* @return The world this entity is in.
*/
std::shared_ptr<World> getWorld();
/**
* Moves the entity in the given direction.
*
@ -103,5 +93,6 @@ namespace Dawn {
bool_t isMoving();
friend class World;
friend class Map;
};
}

View File

@ -4,7 +4,7 @@
// https://opensource.org/licenses/MIT
#pragma once
#include "Entity.hpp"
#include "dawnlibs.hpp"
namespace Dawn {
enum class EntityID : uint32_t {

View File

@ -6,4 +6,6 @@
target_sources(${DAWN_TARGET_NAME}
PRIVATE
World.cpp
Map.cpp
Chunk.cpp
)

View File

@ -0,0 +1,9 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "Chunk.hpp"
using namespace Dawn;

View File

@ -0,0 +1,14 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnlibs.hpp"
namespace Dawn {
class Chunk {
public:
Chunk();
};
}

View File

@ -0,0 +1,59 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "assert/assert.hpp"
#include "scene/SceneItem.hpp"
#include "Map.hpp"
#include "component/entity/Entity.hpp"
using namespace Dawn;
void Map::entityNotifyInit(std::shared_ptr<SceneItem> item) {
auto entity = item->getComponent<Entity>();
assertNotNull(entity, "Entity component not found on item.");
auto id = entity->id;
assertMapNotHasKey(this->entities, id, "Entity already exists in Map.");
assertTrue(id != EntityID::Null, "Entity ID is invalid.");
this->entities[id] = entity;
}
void Map::entityNotifyDispose(std::shared_ptr<SceneItem> item) {
auto entity = item->getComponent<Entity>();
assertNotNull(entity, "Entity component not found on item.");
auto id = entity->id;
assertMapHasKey(this->entities, id, "Entity does not exist in Map.");
assertTrue(id != EntityID::Null, "Entity ID is invalid.");
this->entities.erase(id);
}
void Map::onInit() {
assertNotNull(this->world, "Map must have a world.");
}
void Map::onDispose() {
this->world = nullptr;
}
std::shared_ptr<Entity> Map::getEntity(const EntityID id) {
assertMapHasKey(this->entities, id, "Entity does not exist in Map.");
auto ent = this->entities[id];
auto lock = ent.lock();
if(!lock) {
this->entities.erase(id);
return nullptr;
}
assertTrue(lock->id == id, "Entity ID mismatch.");
return lock;
}
std::shared_ptr<Entity> Map::getEntityAt(const EntityTilePosition &pos) {
for(auto &pair : this->entities) {
auto ent = pair.second.lock();
if(!ent) continue;
if(ent->tilePosition != pos) continue;
return ent;
}
return nullptr;
}

View File

@ -0,0 +1,54 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "component/world/World.hpp"
#include "component/entity/EntityID.hpp"
#include "component/entity/EntityTilePosition.hpp"
namespace Dawn {
class Map : public SceneComponent {
private:
std::unordered_map<enum EntityID, std::weak_ptr<Entity>> entities;
/**
* Part of the ECS, this function is called when an entity is initialized.
*
* @param item Scene Item that has been initialized.
*/
void entityNotifyInit(std::shared_ptr<SceneItem> item);
/**
* Part of the ECS, this function is called when an entity is disposed.
*
* @param item Scene Item that has been disposed.
*/
void entityNotifyDispose(std::shared_ptr<SceneItem> entity);
public:
std::shared_ptr<World> world;
void onInit() override;
void onDispose() override;
/**
* Gets the entity with the given ID.
*
* @param id The ID of the entity to get.
* @return The entity with the given ID.
*/
std::shared_ptr<Entity> getEntity(const EntityID id);
/**
* Gets the entity at the given position.
*
* @param pos The position to check for an entity.
* @return The entity at the given position, or nullptr.
*/
std::shared_ptr<Entity> getEntityAt(const EntityTilePosition &pos);
friend class Entity;
};
}

View File

@ -8,23 +8,6 @@
using namespace Dawn;
void World::entityNotifyInit(std::shared_ptr<SceneItem> item) {
auto entity = item->getComponent<Entity>();
assertNotNull(entity, "Entity component not found on item.");
auto id = entity->id;
assertMapNotHasKey(this->entities, id, "Entity already exists in world.");
assertTrue(id != EntityID::Null, "Entity ID is invalid.");
this->entities[id] = entity;
}
void World::entityNotifyDispose(std::shared_ptr<SceneItem> item) {
auto entity = item->getComponent<Entity>();
assertNotNull(entity, "Entity component not found on item.");
auto id = entity->id;
assertMapHasKey(this->entities, id, "Entity does not exist in world.");
assertTrue(id != EntityID::Null, "Entity ID is invalid.");
this->entities.erase(id);
}
void World::onInit() {
@ -32,26 +15,4 @@ void World::onInit() {
void World::onDispose() {
}
std::shared_ptr<Entity> World::getEntity(const EntityID id) {
assertMapHasKey(this->entities, id, "Entity does not exist in world.");
auto ent = this->entities[id];
auto lock = ent.lock();
if(!lock) {
this->entities.erase(id);
return nullptr;
}
assertTrue(lock->id == id, "Entity ID mismatch.");
return lock;
}
std::shared_ptr<Entity> World::getEntityAt(const EntityTilePosition &pos) {
for(auto &pair : this->entities) {
auto ent = pair.second.lock();
if(!ent) continue;
if(ent->tilePosition != pos) continue;
return ent;
}
return nullptr;
}

View File

@ -4,48 +4,20 @@
// https://opensource.org/licenses/MIT
#pragma once
#include "component/entity/Entity.hpp"
#include "component/entity/Player.hpp"
#include "scene/SceneComponent.hpp"
namespace Dawn {
class Map;
class Entity;
class World : public SceneComponent {
private:
std::unordered_map<enum EntityID, std::weak_ptr<Entity>> entities;
/**
* Part of the ECS, this function is called when an entity is initialized.
*
* @param item Scene Item that has been initialized.
*/
void entityNotifyInit(std::shared_ptr<SceneItem> item);
/**
* Part of the ECS, this function is called when an entity is disposed.
*
* @param item Scene Item that has been disposed.
*/
void entityNotifyDispose(std::shared_ptr<SceneItem> entity);
public:
void onInit() override;
void onDispose() override;
/**
* Gets the entity with the given ID.
*
* @param id The ID of the entity to get.
* @return The entity with the given ID.
*/
std::shared_ptr<Entity> getEntity(const EntityID id);
/**
* Gets the entity at the given position.
*
* @param pos The position to check for an entity.
* @return The entity at the given position, or nullptr.
*/
std::shared_ptr<Entity> getEntityAt(const EntityTilePosition &pos);
friend class Map;
friend class Entity;
};
}

View File

@ -9,14 +9,14 @@
using namespace Dawn;
struct PlayerPrefab Dawn::createPlayerPrefab(
const std::shared_ptr<World> world
const std::shared_ptr<Map> map
) {
struct PlayerPrefab player;
player.item = world->getScene()->createSceneItem();
player.item->setParent(world->getItem());
player.item = map->getScene()->createSceneItem();
player.item->setParent(map->getItem());
player.player = player.item->addComponent<Player>();
player.player->world = world;
player.player->map = map;
player.player->id = EntityID::Player;
player.mesh = std::make_shared<Mesh>();

View File

@ -5,7 +5,6 @@
#pragma once
#include "prefab/Prefab.hpp"
#include "component/world/World.hpp"
#include "component/display/MeshRenderer.hpp"
#include "component/display/material/SimpleTexturedMaterial.hpp"
#include "component/entity/Player.hpp"
@ -19,5 +18,5 @@ namespace Dawn {
std::shared_ptr<SimpleTexturedMaterial> material;
};
struct PlayerPrefab createPlayerPrefab(const std::shared_ptr<World> world);
struct PlayerPrefab createPlayerPrefab(const std::shared_ptr<Map> world);
}

View File

@ -9,14 +9,14 @@
using namespace Dawn;
struct TestEntityPrefab Dawn::createTestEntityPrefab(
const std::shared_ptr<World> world
const std::shared_ptr<Map> map
) {
struct TestEntityPrefab entity;
entity.item = world->getScene()->createSceneItem();
entity.item->setParent(world->getItem());
entity.item = map->getScene()->createSceneItem();
entity.item->setParent(map->getItem());
entity.entity = entity.item->addComponent<Entity>();
entity.entity->world = world;
entity.entity->map = map;
entity.entity->id = EntityID::TestEntity;
entity.mesh = std::make_shared<Mesh>();

View File

@ -5,7 +5,7 @@
#pragma once
#include "prefab/Prefab.hpp"
#include "component/world/World.hpp"
#include "component/entity/Entity.hpp"
#include "component/display/MeshRenderer.hpp"
#include "component/display/material/SimpleTexturedMaterial.hpp"
@ -19,6 +19,6 @@ namespace Dawn {
};
struct TestEntityPrefab createTestEntityPrefab(
const std::shared_ptr<World> world
const std::shared_ptr<Map> map
);
}

View File

@ -15,4 +15,16 @@ struct WorldPrefab Dawn::createWorldPrefab(Scene &s) {
world.world = world.item->addComponent<World>();
return world;
}
struct MapPrefab Dawn::createMapPrefab(
const std::shared_ptr<World> world
) {
struct MapPrefab map;
map.item = world->getScene()->createSceneItem();
map.map = map.item->addComponent<Map>();
map.map->world = world;
return map;
}

View File

@ -6,6 +6,7 @@
#pragma once
#include "prefab/Prefab.hpp"
#include "component/world/World.hpp"
#include "component/world/Map.hpp"
namespace Dawn {
struct WorldPrefab : public Prefab {
@ -13,5 +14,12 @@ namespace Dawn {
std::shared_ptr<World> world;
};
struct MapPrefab : public Prefab {
public:
std::shared_ptr<Map> map;
};
struct WorldPrefab createWorldPrefab(Scene &scene);
struct MapPrefab createMapPrefab(const std::shared_ptr<World> world);
}

View File

@ -43,9 +43,11 @@ void Dawn::worldScene(Scene &s) {
// auto canvas = canvasItem->addComponent<UICanvas>();
auto world = createWorldPrefab(s);
auto player = createPlayerPrefab(world.world);
auto map = createMapPrefab(world.world);
auto player = createPlayerPrefab(map.map);
player.player->camera = camera;
auto test = createTestEntityPrefab(world.world);
auto test = createTestEntityPrefab(map.map);
test.entity->setPosition({ 5, 0, 0 });
}