112 lines
2.7 KiB
C++
112 lines
2.7 KiB
C++
// Copyright (c) 2024 Dominic Masters
|
|
//
|
|
// This software is released under the MIT License.
|
|
// https://opensource.org/licenses/MIT
|
|
|
|
#pragma once
|
|
#include "component/world/Map.hpp"
|
|
#include "event/Event.hpp"
|
|
|
|
#define ENTITY_STEP_SPEED_DEFAULT 3.0f
|
|
#define ENTITY_STEP_SPEED_RUNNING 6.0f
|
|
#define ENTITY_TURN_TIME 0.06f
|
|
|
|
namespace Dawn {
|
|
class Entity;
|
|
|
|
enum class EntityDirection {
|
|
Up,
|
|
Down,
|
|
Left,
|
|
Right
|
|
};
|
|
|
|
enum class EntityStepResultType {
|
|
Turn,
|
|
Step,
|
|
EntityInWay,
|
|
};
|
|
|
|
struct EntityStepResult {
|
|
enum EntityStepResultType type;
|
|
|
|
// I'd love to unionize this but it seems that it's not ideal rn.
|
|
std::shared_ptr<Entity> entityInWay;
|
|
};
|
|
|
|
class Entity : public SceneComponent {
|
|
private:
|
|
enum EntityDirection direction = EntityDirection::Down;
|
|
float_t turnTime = 0.0f;
|
|
|
|
struct EntityTilePosition lastTilePosition;
|
|
float_t stepTime = 0.0f;
|
|
float_t stepSpeed = ENTITY_STEP_SPEED_DEFAULT;
|
|
|
|
protected:
|
|
struct EntityTilePosition tilePosition;
|
|
|
|
public:
|
|
std::weak_ptr<Map> map;
|
|
enum EntityID id = EntityID::Null;
|
|
|
|
Event<> eventStepStart;
|
|
Event<> eventStepEnd;
|
|
Event<> eventMove;
|
|
Event<> eventTurn;
|
|
|
|
void onInit() override;
|
|
void onDispose() override;
|
|
|
|
/**
|
|
* Gets the map that the entity is on.
|
|
*
|
|
* @return The map that the entity is on.
|
|
*/
|
|
std::shared_ptr<Map> getMap();
|
|
|
|
/**
|
|
* Gets the world that the entity is on.
|
|
*
|
|
* @return The world that the entity is on.
|
|
*/
|
|
std::shared_ptr<World> getWorld();
|
|
|
|
/**
|
|
* Moves the entity in the given direction.
|
|
*
|
|
* @param direction The direction to move in.
|
|
* @param stepSpeed The speed to move at.
|
|
*/
|
|
struct EntityStepResult move(
|
|
const enum EntityDirection direction,
|
|
const float_t stepSpeed = ENTITY_STEP_SPEED_DEFAULT
|
|
);
|
|
|
|
/**
|
|
* Turns the entity in the given direction. This will have a slight delay
|
|
* and count as moving.
|
|
*
|
|
* @param direction The direction to turn in.
|
|
*/
|
|
void turn(const enum EntityDirection direction);
|
|
|
|
/**
|
|
* Safely sets the position of the entity. This will invoke movement
|
|
* events but not step events, so steps may be cancelled in this context.
|
|
*
|
|
* @param position The new position of the entity.
|
|
*/
|
|
void setPosition(struct EntityTilePosition position);
|
|
|
|
/**
|
|
* Checks if the entity is currently moving (in a step).
|
|
*
|
|
* @return True if the entity is moving, false otherwise.
|
|
*/
|
|
bool_t isMoving();
|
|
|
|
friend class World;
|
|
friend class Map;
|
|
};
|
|
} |