Finished camera controller.
This commit is contained in:
@ -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([&](float_t delta) {
|
||||
glm::vec3 cameraTarget = player->transform->getLocalPosition();
|
||||
camera->transform->lookAt(
|
||||
cameraTarget + glm::vec3(0, 10.0f, 1.5f),
|
||||
cameraTarget
|
||||
);
|
||||
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){
|
||||
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);
|
||||
|
||||
|
Reference in New Issue
Block a user