Smooth battle zone camera transitions

This commit is contained in:
2026-06-09 14:50:50 +08:00
parent 493b2bd3b8
commit 3e2e25eb93
5 changed files with 53 additions and 6 deletions
+3 -3
View File
@@ -112,17 +112,17 @@ LevelDefinition CreateWhiteboxLevel() {
level.battleZones = {
{"zone_01_intro",
frostbite2D::Rect(520.0f, 260.0f, 560.0f, 340.0f),
frostbite2D::Rect(320.0f, 0.0f, 1280.0f, 720.0f),
frostbite2D::Rect(0.0f, 0.0f, 1940.0f, 720.0f),
{{"zone_01_enemy_a", frostbite2D::Vec2(760.0f, 560.0f)},
{"zone_01_enemy_b", frostbite2D::Vec2(980.0f, 560.0f)}}},
{"zone_02_platform",
frostbite2D::Rect(1420.0f, 250.0f, 680.0f, 350.0f),
frostbite2D::Rect(1280.0f, 0.0f, 1280.0f, 720.0f),
frostbite2D::Rect(660.0f, 0.0f, 2300.0f, 720.0f),
{{"zone_02_enemy_a", frostbite2D::Vec2(1620.0f, 560.0f)},
{"zone_02_enemy_b", frostbite2D::Vec2(1900.0f, 560.0f)}}},
{"zone_03_exit",
frostbite2D::Rect(2380.0f, 250.0f, 720.0f, 350.0f),
frostbite2D::Rect(1920.0f, 0.0f, 1280.0f, 720.0f),
frostbite2D::Rect(1600.0f, 0.0f, 1600.0f, 720.0f),
{{"zone_03_enemy_a", frostbite2D::Vec2(2580.0f, 560.0f)},
{"zone_03_enemy_b", frostbite2D::Vec2(2860.0f, 560.0f)}}},
};
+40 -2
View File
@@ -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;
}
}
+8
View File
@@ -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_;