Finished camera controller.
This commit is contained in:
@ -8,6 +8,7 @@
|
||||
#include "assert/assert.hpp"
|
||||
#include "util/flag.hpp"
|
||||
#include "event/Event.hpp"
|
||||
#include "state/StateEvent.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class SceneItem;
|
||||
@ -40,7 +41,8 @@ namespace Dawn {
|
||||
|
||||
public:
|
||||
// I have no idea if I'm keeping this
|
||||
Event<> eventTransformUpdated;
|
||||
// Event<> eventTransformUpdated;
|
||||
StateEvent<> eventTransformUpdated;
|
||||
SceneItem *item;
|
||||
|
||||
/**
|
||||
|
@ -1,27 +0,0 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "Prefab.hpp"
|
||||
#include "ui/UIComponent.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
template<class T, class P>
|
||||
class UIPrefab :
|
||||
public Prefab<T, UICanvas, P>
|
||||
{
|
||||
public:
|
||||
static void apply(T *item) {
|
||||
assertNotNull(item);
|
||||
P::prefabApply(&item->getGame()->assetManager , item);
|
||||
}
|
||||
|
||||
static T * prefabCreate(UICanvas *canvas) {
|
||||
T * item = canvas->addElement<T>();
|
||||
P::apply(item);
|
||||
return item;
|
||||
}
|
||||
};
|
||||
}
|
@ -59,6 +59,8 @@ void CharacterController2D::onStart() {
|
||||
}
|
||||
}
|
||||
|
||||
if(mathAbs<float_t>(moveAmount.x) <= 0.001f && mathAbs<float_t>(moveAmount.y) <= 0.001f) return;
|
||||
|
||||
transform->setLocalPosition(
|
||||
transform->getLocalPosition() + (glm::vec3(moveAmount.x, 0, moveAmount.y) * delta)
|
||||
);
|
||||
|
52
src/dawnrose/prefabs/PlayerPrefab.hpp
Normal file
52
src/dawnrose/prefabs/PlayerPrefab.hpp
Normal file
@ -0,0 +1,52 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "prefab/SceneItemPrefab.hpp"
|
||||
#include "scene/components/display/MeshHost.hpp"
|
||||
#include "scene/components/display/MeshRenderer.hpp"
|
||||
#include "scene/components/display/material/SimpleTexturedMaterial.hpp"
|
||||
#include "scene/components/physics/2d/CharacterController2D.hpp"
|
||||
#include "scene/components/physics/2d/BoxCollider.hpp"
|
||||
#include "scene/components/PlayerController.hpp"
|
||||
#include "display/mesh/CubeMesh.hpp"
|
||||
#include "display/mesh/CapsuleMesh.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class PlayerPrefab : public SceneItemPrefab<PlayerPrefab>, public StateOwner {
|
||||
public:
|
||||
static std::vector<Asset*> prefabAssets(AssetManager *man) {
|
||||
return {};
|
||||
}
|
||||
|
||||
PlayerController *player;
|
||||
|
||||
PlayerPrefab(Scene *s, sceneitemid_t i) : SceneItemPrefab(s, i) {}
|
||||
|
||||
void prefabInit(AssetManager *man) override {
|
||||
auto characterController = this->addComponent<CharacterController2D>();
|
||||
auto hitbox = this->addComponent<BoxCollider>();
|
||||
auto meshHost = this->addComponent<MeshHost>();
|
||||
auto meshRenderer = this->addComponent<MeshRenderer>();
|
||||
auto material = this->addComponent<SimpleTexturedMaterial>();
|
||||
|
||||
CapsuleMesh::create(&meshHost->mesh, 0.5f, 1.5f);
|
||||
|
||||
auto nose = scene->createSceneItem();
|
||||
auto noseMeshHost = nose->addComponent<MeshHost>();
|
||||
auto noseMeshRenderer = nose->addComponent<MeshRenderer>();
|
||||
auto noseMaterial = nose->addComponent<SimpleTexturedMaterial>();
|
||||
noseMeshHost->mesh.createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT);
|
||||
glm::vec3 noseSize = glm::vec3(0.25f, 0.25f, 0.25f);
|
||||
CubeMesh::buffer(&noseMeshHost->mesh, -(noseSize * 0.5f), noseSize, 0, 0);
|
||||
noseMaterial->color = COLOR_RED;
|
||||
nose->transform.setParent(&this->transform);
|
||||
nose->transform.setLocalPosition(glm::vec3(0, 0, 0.5f));
|
||||
|
||||
|
||||
player = this->addComponent<PlayerController>();
|
||||
}
|
||||
};
|
||||
}
|
@ -4,6 +4,7 @@
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "GameCamera.hpp"
|
||||
#include "game/DawnGame.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
@ -19,11 +20,24 @@ void GameCamera::onStart() {
|
||||
assertNotNull(this->camera);
|
||||
assertNotNull(this->player);
|
||||
|
||||
useEvent([&]{
|
||||
glm::vec3 lookOffset = player->transform->getLocalRotation() * glm::vec3(0, 0, 1.0f);
|
||||
glm::vec2 target = glm::vec2(lookOffset.x, lookOffset.z) * lookOffsetScale;
|
||||
slowTarget += (target - slowTarget) * getGame()->timeManager.delta * movementScrollSpeed;
|
||||
}, this->player->transform->eventTransformUpdated);
|
||||
|
||||
useEvent([&](float_t delta){
|
||||
glm::vec3 cameraTarget = player->transform->getLocalPosition();
|
||||
camera->transform->lookAt(
|
||||
cameraTarget + glm::vec3(0, 10.0f, 1.5f),
|
||||
cameraTarget
|
||||
if(current != slowTarget) {
|
||||
float_t m = 6.0f;
|
||||
float_t s = delta * 3.0f;
|
||||
current += glm::vec2(
|
||||
mathClamp<float_t>((slowTarget.x - current.x) * s, -m, m),
|
||||
mathClamp<float_t>((slowTarget.y - current.y) * s, -m, m)
|
||||
);
|
||||
}
|
||||
|
||||
glm::vec3 current3 = glm::vec3(current.x, 0, current.y);
|
||||
glm::vec3 t0 = player->transform->getLocalPosition();
|
||||
camera->transform->lookAt(t0 + current3 + zoomOffset, t0 + current3);
|
||||
}, getScene()->eventSceneUpdate);
|
||||
}
|
@ -11,8 +11,14 @@ namespace Dawn {
|
||||
class GameCamera : public SceneItemComponent {
|
||||
protected:
|
||||
Camera *camera = nullptr;
|
||||
glm::vec2 slowTarget = glm::vec2(0, 0);
|
||||
glm::vec2 current = glm::vec2(0, 0);
|
||||
|
||||
public:
|
||||
glm::vec2 lookOffsetScale = glm::vec2(6.0f, 3.0f);
|
||||
float_t movementScrollSpeed = 0.5f;
|
||||
glm::vec3 zoomOffset = glm::vec3(0, 30.0f, 7.5f);
|
||||
|
||||
PlayerController *player = nullptr;
|
||||
|
||||
GameCamera(SceneItem *item);
|
||||
|
@ -22,9 +22,6 @@ void PlayerController::onStart() {
|
||||
assertNotNull(this->characterController);
|
||||
|
||||
useEvent([&](float_t delta){
|
||||
// auto camera = getScene()->findComponent<Camera>();
|
||||
// assertNotNull(camera);
|
||||
|
||||
// Movement
|
||||
auto inputMove = getGame()->inputManager.getAxis2D(
|
||||
INPUT_BIND_NEGATIVE_X, INPUT_BIND_POSITIVE_X,
|
||||
@ -34,6 +31,9 @@ void PlayerController::onStart() {
|
||||
if(inputMove.x != 0 || inputMove.y != 0) {
|
||||
float_t angle = atan2(inputMove.y, inputMove.x);
|
||||
glm::vec2 movement(cos(angle), sin(angle));
|
||||
|
||||
transform->setLocalRotation(glm::quat(glm::vec3(0, -angle + 1.5708f, 0)));
|
||||
|
||||
characterController->velocity += movement * delta * moveSpeed;
|
||||
}
|
||||
}, getScene()->eventSceneUpdate);
|
||||
|
@ -12,7 +12,7 @@ namespace Dawn {
|
||||
CharacterController2D *characterController;
|
||||
|
||||
public:
|
||||
float_t moveSpeed = 40.0f;
|
||||
float_t moveSpeed = 80.0f;
|
||||
|
||||
PlayerController(SceneItem *item);
|
||||
|
||||
|
@ -5,16 +5,8 @@
|
||||
|
||||
#pragma once
|
||||
#include "scene/Scene.hpp"
|
||||
#include "scene/components/PlayerController.hpp"
|
||||
#include "scene/components/GameCamera.hpp"
|
||||
#include "scene/components/GameCamera.hpp"
|
||||
#include "scene/components/display/MeshRenderer.hpp"
|
||||
#include "scene/components/display/MeshHost.hpp"
|
||||
#include "scene/components/display/material/SimpleTexturedMaterial.hpp"
|
||||
#include "scene/components/example/ExampleSpin.hpp"
|
||||
#include "scene/components/physics/2d/BoxCollider.hpp"
|
||||
#include "scene/components/physics/2d/CharacterController2D.hpp"
|
||||
#include "display/mesh/CubeMesh.hpp"
|
||||
#include "prefabs/PlayerPrefab.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class HelloWorldScene : public Scene {
|
||||
@ -22,10 +14,7 @@ namespace Dawn {
|
||||
Camera *camera;
|
||||
|
||||
void stage() override {
|
||||
auto playerItem = this->createSceneItem();
|
||||
auto player = playerItem->addComponent<PlayerController>();
|
||||
auto hitbox = playerItem->addComponent<BoxCollider>();
|
||||
playerItem->addComponent<CharacterController2D>();
|
||||
auto player = PlayerPrefab::create(this);
|
||||
|
||||
auto wallBox = this->createSceneItem()->addComponent<BoxCollider>();
|
||||
wallBox->min = glm::vec2(-4, -3);
|
||||
@ -36,9 +25,10 @@ namespace Dawn {
|
||||
wallBox2->max = glm::vec2(3, -3);
|
||||
|
||||
camera = Camera::create(this);
|
||||
camera->fov = 0.436332f;
|
||||
camera->transform->lookAt(glm::vec3(10, 10, 10), glm::vec3(0, 0, 0));
|
||||
// auto gameCamera = camera->item->addComponent<GameCamera>();
|
||||
// gameCamera->player = player;
|
||||
auto gameCamera = camera->item->addComponent<GameCamera>();
|
||||
gameCamera->player = player->player;
|
||||
}
|
||||
|
||||
std::vector<Asset*> getRequiredAssets() override {
|
||||
|
Reference in New Issue
Block a user