Smooth battle zone camera transitions
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#include <frostbite2D/event/key_event.h>
|
||||
#include <frostbite2D/graphics/camera.h>
|
||||
#include <frostbite2D/graphics/renderer.h>
|
||||
#include <algorithm>
|
||||
|
||||
namespace ns_game {
|
||||
|
||||
@@ -26,6 +27,7 @@ void WhiteboxScene::onEnter() {
|
||||
void WhiteboxScene::Update(float deltaTime) {
|
||||
frostbite2D::Scene::Update(deltaTime);
|
||||
updateBattleZones();
|
||||
updateCameraBoundsBlend(deltaTime);
|
||||
updateCamera(deltaTime);
|
||||
}
|
||||
|
||||
@@ -93,6 +95,7 @@ void WhiteboxScene::configureCamera() {
|
||||
config.viewportWidth = static_cast<float>(config::kVirtualWidth);
|
||||
config.viewportHeight = static_cast<float>(config::kVirtualHeight);
|
||||
cameraController_.Configure(config);
|
||||
currentCameraBounds_ = level_.cameraBounds;
|
||||
|
||||
if (player_) {
|
||||
cameraController_.SnapToTarget(*camera, player_->GetWorldBounds());
|
||||
@@ -111,6 +114,14 @@ void WhiteboxScene::toggleDebugOverlay() {
|
||||
debugFlags_.showSpawnPoints = enabled;
|
||||
}
|
||||
|
||||
void WhiteboxScene::beginCameraBoundsBlend(
|
||||
const frostbite2D::Rect& targetBounds) {
|
||||
cameraBoundsBlendStart_ = currentCameraBounds_;
|
||||
cameraBoundsBlendTarget_ = targetBounds;
|
||||
cameraBoundsBlendTimer_ = 0.0f;
|
||||
cameraBoundsBlendActive_ = true;
|
||||
}
|
||||
|
||||
void WhiteboxScene::clearActiveBattleZone() {
|
||||
if (activeBattleZoneIndex_ < 0 ||
|
||||
activeBattleZoneIndex_ >= static_cast<int>(battleZoneStates_.size())) {
|
||||
@@ -119,7 +130,34 @@ void WhiteboxScene::clearActiveBattleZone() {
|
||||
|
||||
battleZoneStates_[static_cast<size_t>(activeBattleZoneIndex_)].cleared = true;
|
||||
activeBattleZoneIndex_ = -1;
|
||||
cameraController_.SetWorldBounds(level_.cameraBounds);
|
||||
beginCameraBoundsBlend(level_.cameraBounds);
|
||||
}
|
||||
|
||||
void WhiteboxScene::updateCameraBoundsBlend(float deltaTime) {
|
||||
if (!cameraBoundsBlendActive_) {
|
||||
return;
|
||||
}
|
||||
|
||||
cameraBoundsBlendTimer_ += deltaTime;
|
||||
const float t = cameraBoundsBlendDuration_ > 0.0f
|
||||
? std::min(cameraBoundsBlendTimer_ /
|
||||
cameraBoundsBlendDuration_,
|
||||
1.0f)
|
||||
: 1.0f;
|
||||
const auto lerp = [t](float from, float to) {
|
||||
return from + (to - from) * t;
|
||||
};
|
||||
|
||||
currentCameraBounds_ = frostbite2D::Rect(
|
||||
lerp(cameraBoundsBlendStart_.left(), cameraBoundsBlendTarget_.left()),
|
||||
lerp(cameraBoundsBlendStart_.top(), cameraBoundsBlendTarget_.top()),
|
||||
lerp(cameraBoundsBlendStart_.width(), cameraBoundsBlendTarget_.width()),
|
||||
lerp(cameraBoundsBlendStart_.height(), cameraBoundsBlendTarget_.height()));
|
||||
cameraController_.SetWorldBounds(currentCameraBounds_);
|
||||
|
||||
if (t >= 1.0f) {
|
||||
cameraBoundsBlendActive_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
void WhiteboxScene::updateBattleZones() {
|
||||
@@ -146,7 +184,7 @@ void WhiteboxScene::updateBattleZones() {
|
||||
|
||||
runtime.triggered = true;
|
||||
activeBattleZoneIndex_ = static_cast<int>(i);
|
||||
cameraController_.SetWorldBounds(level_.battleZones[i].cameraBounds);
|
||||
beginCameraBoundsBlend(level_.battleZones[i].cameraBounds);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,9 @@ private:
|
||||
std::vector<BattleZoneDebugInfo> buildBattleZoneDebugInfo() const;
|
||||
void configureCamera();
|
||||
void toggleDebugOverlay();
|
||||
void beginCameraBoundsBlend(const frostbite2D::Rect& targetBounds);
|
||||
void clearActiveBattleZone();
|
||||
void updateCameraBoundsBlend(float deltaTime);
|
||||
void updateBattleZones();
|
||||
void updateCamera(float deltaTime);
|
||||
|
||||
@@ -39,6 +41,12 @@ private:
|
||||
DebugFlags debugFlags_;
|
||||
std::vector<BattleZoneRuntime> battleZoneStates_;
|
||||
int activeBattleZoneIndex_ = -1;
|
||||
bool cameraBoundsBlendActive_ = false;
|
||||
float cameraBoundsBlendTimer_ = 0.0f;
|
||||
float cameraBoundsBlendDuration_ = 0.35f;
|
||||
frostbite2D::Rect cameraBoundsBlendStart_;
|
||||
frostbite2D::Rect cameraBoundsBlendTarget_;
|
||||
frostbite2D::Rect currentCameraBounds_;
|
||||
CameraController cameraController_;
|
||||
StageRenderer stageRenderer_;
|
||||
DebugOverlay debugOverlay_;
|
||||
|
||||
Reference in New Issue
Block a user