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

@ -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;
};
}