Dawn/src/dawnrose/scene/components/GameCamera.cpp
2023-03-22 18:54:22 -07:00

43 lines
1.3 KiB
C++

// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "GameCamera.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn;
GameCamera::GameCamera(SceneItem *i) : SceneItemComponent(i) {}
std::vector<SceneItemComponent*> GameCamera::getDependencies() {
return {
(this->camera = item->getComponent<Camera>())
};
}
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){
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);
}