Basic movement example
This commit is contained in:
@ -39,6 +39,11 @@ void SceneComponent::dispose() {
|
||||
sceneComponentState,
|
||||
SCENE_COMPONENT_STATE_DISPOSED
|
||||
);
|
||||
|
||||
// Clear Listeners
|
||||
for(auto &listener : this->listeners) listener();
|
||||
this->listeners.clear();
|
||||
|
||||
this->onDispose();
|
||||
this->item.reset();
|
||||
}
|
||||
|
@ -21,6 +21,8 @@ namespace Dawn {
|
||||
uint_fast8_t sceneComponentState = 0;
|
||||
|
||||
protected:
|
||||
std::vector<std::function<void()>> listeners;
|
||||
|
||||
/**
|
||||
* Custom component listener that is invoked when the component is meant
|
||||
* to initialize.
|
||||
|
@ -4,15 +4,56 @@
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "Entity.hpp"
|
||||
#include "scene/Scene.hpp"
|
||||
#include "assert/assert.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
void Entity::onInit() {
|
||||
auto world = this->world.lock();
|
||||
assertNotNull(world, "World is no longer active? ");
|
||||
assertNotNull(world, "World is no longer active?");
|
||||
|
||||
listeners.push_back(getScene()->onUnpausedUpdate.listen([this](float delta) {
|
||||
if(this->stepTime <= 0.0f) return;
|
||||
this->stepTime = 0;
|
||||
this->getItem()->setLocalPosition(glm::vec3(this->tileX, this->tileY, this->tileZ));
|
||||
}));
|
||||
}
|
||||
|
||||
void Entity::onDispose() {
|
||||
|
||||
}
|
||||
|
||||
void Entity::move(const enum EntityDirection direction) {
|
||||
entitytileposition_t newTileX, newTileY, newTileZ;
|
||||
newTileX = tileX;
|
||||
newTileY = tileY;
|
||||
newTileZ = tileZ;
|
||||
|
||||
switch(direction) {
|
||||
case EntityDirection::Up:
|
||||
newTileZ--;
|
||||
break;
|
||||
case EntityDirection::Down:
|
||||
newTileZ++;
|
||||
break;
|
||||
case EntityDirection::Left:
|
||||
newTileX--;
|
||||
break;
|
||||
case EntityDirection::Right:
|
||||
newTileX++;
|
||||
break;
|
||||
default:
|
||||
assertUnreachable("Invalid direction: %d", direction);
|
||||
}
|
||||
|
||||
// Get the tile at the destination, check for height if we are on stairs, etc.
|
||||
// If the tile is not walkable, return early.
|
||||
|
||||
// Move the entity to the new tile.
|
||||
this->tileX = newTileX;
|
||||
this->tileY = newTileY;
|
||||
this->tileZ = newTileZ;
|
||||
this->direction = direction;
|
||||
this->stepTime = 1.0f;
|
||||
}
|
@ -7,15 +7,31 @@
|
||||
#include "scene/SceneComponent.hpp"
|
||||
|
||||
typedef uint32_t entityid_t;
|
||||
typedef int64_t entitytileposition_t;
|
||||
|
||||
namespace Dawn {
|
||||
class World;
|
||||
|
||||
class Entity final : public SceneComponent {
|
||||
enum class EntityDirection {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right
|
||||
};
|
||||
|
||||
class Entity : public SceneComponent {
|
||||
private:
|
||||
entitytileposition_t tileX = 0, tileY = 0, tileZ = 0;
|
||||
enum EntityDirection direction = EntityDirection::Down;
|
||||
float_t stepTime = 0.0f;
|
||||
|
||||
protected:
|
||||
|
||||
public:
|
||||
std::weak_ptr<World> world;
|
||||
|
||||
void onInit() override;
|
||||
void onDispose() override;
|
||||
void move(const enum EntityDirection direction);
|
||||
};
|
||||
}
|
@ -10,14 +10,22 @@
|
||||
using namespace Dawn;
|
||||
|
||||
void Player::onInit() {
|
||||
listenerUpdate = getScene()->onUnpausedUpdate.listen([this](float delta) {
|
||||
InputManager &im = getGame()->inputManager;
|
||||
if(im.isDown(InputBind::Up)) {
|
||||
Entity::onInit();
|
||||
|
||||
listeners.push_back(getScene()->onUnpausedUpdate.listen([this](float delta) {
|
||||
InputManager &im = getGame()->inputManager;
|
||||
if(im.isPressed(InputBind::Up)) {
|
||||
this->move(EntityDirection::Up);
|
||||
} else if(im.isPressed(InputBind::Down)) {
|
||||
this->move(EntityDirection::Down);
|
||||
} else if(im.isPressed(InputBind::Left)) {
|
||||
this->move(EntityDirection::Left);
|
||||
} else if(im.isPressed(InputBind::Right)) {
|
||||
this->move(EntityDirection::Right);
|
||||
}
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
void Player::onDispose() {
|
||||
listenerUpdate();
|
||||
Entity::onDispose();
|
||||
}
|
@ -4,12 +4,11 @@
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "scene/SceneComponent.hpp"
|
||||
#include "component/entity/Entity.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class Player : public SceneComponent {
|
||||
class Player : public Entity {
|
||||
protected:
|
||||
std::function<void()> listenerUpdate;
|
||||
|
||||
public:
|
||||
void onInit() override;
|
||||
|
@ -8,6 +8,8 @@
|
||||
namespace Dawn {
|
||||
enum class InputBind : uint8_t {
|
||||
Up,
|
||||
Down
|
||||
Down,
|
||||
Left,
|
||||
Right
|
||||
};
|
||||
}
|
@ -15,10 +15,8 @@ struct PlayerPrefab Dawn::createPlayerPrefab(
|
||||
player.item = world->getScene()->createSceneItem();
|
||||
player.item->setParent(world->getItem());
|
||||
|
||||
player.entity = player.item->addComponent<Entity>();
|
||||
player.entity->world = world;
|
||||
|
||||
player.player = player.item->addComponent<Player>();
|
||||
player.player->world = world;
|
||||
|
||||
player.mesh = std::make_shared<Mesh>();
|
||||
player.mesh->createBuffers(QUAD_VERTICE_COUNT, QUAD_INDICE_COUNT);
|
||||
|
@ -13,7 +13,6 @@
|
||||
namespace Dawn {
|
||||
struct PlayerPrefab : public Prefab {
|
||||
public:
|
||||
std::shared_ptr<Entity> entity;
|
||||
std::shared_ptr<Player> player;
|
||||
std::shared_ptr<Mesh> mesh;
|
||||
std::shared_ptr<MeshRenderer> meshRenderer;
|
||||
|
Reference in New Issue
Block a user