Better working camera that is constrained in the bounds, but the animations don't follow the bounding.

This commit is contained in:
Zed A. Shaw 2026-01-01 12:59:39 -05:00
parent 6dc9d564c6
commit 51bb74e2d7
9 changed files with 93 additions and 33 deletions

View file

@ -107,6 +107,7 @@ namespace boss {
state(State::END); state(State::END);
} else { } else {
$ui.status(L"PLAYER REQUESTS", L"COMMIT"); $ui.status(L"PLAYER REQUESTS", L"COMMIT");
$ui.reset_camera();
$battle.ap_refresh(); $battle.ap_refresh();
$battle.clear_requests(); $battle.clear_requests();
state(State::PLAYER_REQUESTS); state(State::PLAYER_REQUESTS);

View file

@ -106,12 +106,18 @@ namespace boss {
$arena.play_animations(); $arena.play_animations();
} }
void UI::damage(const std::string& actor, const std::string& target, int amount) { void UI::damage(const string& actor, const std::string& target, int amount) {
if(amount > 0) { if(amount > 0) {
$arena.attach_text(target, fmt::format("{}", amount)); $arena.attach_text(target, fmt::format("{}", amount));
$arena.apply_effect(actor, "flame"); $arena.apply_effect(actor, "flame");
fmt::println("CAMERA zooming by 0.7 to {}", target);
$arena.zoom(target, 0.8f);
} else { } else {
$arena.attach_text(actor, "MISSED"); $arena.attach_text(actor, "MISSED");
} }
} }
void UI::reset_camera() {
$arena.reset($view_texture);
}
} }

View file

@ -39,5 +39,6 @@ namespace boss {
void update_stats(); void update_stats();
void play_animations(); void play_animations();
void damage(const std::string& actor, const std::string& target, int amount); void damage(const std::string& actor, const std::string& target, int amount);
void reset_camera();
}; };
} }

View file

@ -4,9 +4,10 @@
#include <unordered_map> #include <unordered_map>
#include "components.hpp" #include "components.hpp"
#include "config.hpp" #include "config.hpp"
#include <algorithm>
namespace cinematic { namespace cinematic {
using components::Animation, std::string; using components::Animation, std::string, std::min, std::clamp;
struct CameraManager { struct CameraManager {
std::unordered_map<string, Animation> animations; std::unordered_map<string, Animation> animations;
@ -27,13 +28,38 @@ namespace cinematic {
} }
} }
Camera::Camera() : Camera::Camera(sf::Vector2f size) :
anim(MGR.animations.at("pan")) anim(MGR.animations.at("shake")),
base_size(size),
size(size)
{ {
} }
void Camera::resize(float width, float height) { void Camera::update_camera_bounds(sf::Vector2f size) {
size = {width, height}; // camera bounds now constrains the x/y so that the mid-point
// of the size won't go too far outside of the frame
camera_bounds = {
{size.x / 2.0f, size.y / 2.0f},
{base_size.x - size.x / 2.0f, base_size.y - size.y / 2.0f}
};
fmt::println("!!!!! CAMERA BOUNDS camera_bounds={},{},{},{}",
camera_bounds.position.x, camera_bounds.position.y,
camera_bounds.size.x, camera_bounds.size.y);
}
void Camera::scale(float ratio) {
size.x = base_size.x * ratio;
size.y = base_size.y * ratio;
update_camera_bounds(size);
}
void Camera::resize(float width) {
dbc::check(width <= base_size.x, "invalid width for camera");
size.x = width;
size.y = base_size.y * (width / base_size.x);
update_camera_bounds(size);
} }
void Camera::style(const std::string &name) { void Camera::style(const std::string &name) {
@ -41,11 +67,23 @@ namespace cinematic {
} }
void Camera::position(float x, float y) { void Camera::position(float x, float y) {
aimed_at = {x, y}; aimed_at.x = clamp(x, camera_bounds.position.x, camera_bounds.size.x);
aimed_at.y = clamp(y, camera_bounds.position.y, camera_bounds.size.y);
fmt::println("!!! CAMERA POSITION aimed_at={},{}; x/y={},{}; camera_bounds={},{},{},{}",
aimed_at.x, aimed_at.y, x, y,
camera_bounds.position.x, camera_bounds.position.y,
camera_bounds.size.x, camera_bounds.size.y);
} }
void Camera::move(float x, float y) { void Camera::move(float x, float y) {
going_to = {x, y}; going_to.x = clamp(x, camera_bounds.position.x, camera_bounds.size.x);
going_to.y = clamp(y, camera_bounds.position.y, camera_bounds.size.y);
fmt::println("!!!! CAMERA MOVE going_to={},{}; x/y={},{}; camera_bounds={},{},{},{}",
going_to.x, going_to.y, x, y,
camera_bounds.position.x, camera_bounds.position.y,
camera_bounds.size.x, camera_bounds.size.y);
// BUG: annoying special case // BUG: annoying special case
if(anim.motion == ease::SLIDE) { if(anim.motion == ease::SLIDE) {
@ -56,11 +94,14 @@ namespace cinematic {
} }
} }
void Camera::reset(sf::RenderTexture& target, float width, float height) { void Camera::reset(sf::RenderTexture& target) {
size = {width, height}; size = {base_size.x, base_size.y};
aimed_at = {width/2, height/2}; aimed_at = {base_size.x/2, base_size.y/2};
going_to = {width/2, height/2}; going_to = {base_size.x/2, base_size.y/2};
view = {aimed_at, size}; view = {aimed_at, size};
camera_bounds = {{0,0}, base_size};
// BUG: is getDefaultView different from view?
target.setView(target.getDefaultView()); target.setView(target.getDefaultView());
} }

View file

@ -6,20 +6,24 @@ namespace cinematic {
struct Camera { struct Camera {
components::Animation anim; components::Animation anim;
sf::View view; sf::View view;
sf::Vector2f base_size{SCREEN_WIDTH, SCREEN_HEIGHT};
sf::Vector2f size{SCREEN_WIDTH, SCREEN_HEIGHT}; sf::Vector2f size{SCREEN_WIDTH, SCREEN_HEIGHT};
sf::FloatRect camera_bounds{{0,0},{SCREEN_WIDTH, SCREEN_HEIGHT}};
sf::Vector2f aimed_at{0,0}; sf::Vector2f aimed_at{0,0};
sf::Vector2f going_to{0,0}; sf::Vector2f going_to{0,0};
Camera(); Camera(sf::Vector2f base_size);
void resize(float width, float height); void resize(float width);
void scale(float ratio);
void position(float x, float y); void position(float x, float y);
void move(float x, float y); void move(float x, float y);
bool playing(); bool playing();
void render(sf::RenderTexture& target); void render(sf::RenderTexture& target);
void play(); void play();
void style(const std::string &name); void style(const std::string &name);
void reset(sf::RenderTexture& target, float width, float height); void reset(sf::RenderTexture& target);
void update_camera_bounds(sf::Vector2f size);
}; };
void init(); void init();

View file

@ -36,7 +36,7 @@ namespace scene {
Engine::Engine(components::AnimatedScene& scene) : Engine::Engine(components::AnimatedScene& scene) :
$scene(scene) $scene(scene)
{ {
$camera.style("shake"); $camera.style("dolly");
for(auto& config : $scene.actors) { for(auto& config : $scene.actors) {
auto element = config_scene_element(config, false, false); auto element = config_scene_element(config, false, false);
@ -161,17 +161,26 @@ namespace scene {
return pos; return pos;
} }
void Engine::zoom(int mid_x, int mid_y, int width, int height) { void Engine::zoom(float mid_x, float mid_y, float scale) {
$camera.resize(float(width), float(height)); $camera.scale(scale);
$camera.move(float(mid_x), float(mid_y)); $camera.move(mid_x, mid_y);
$camera.play();
} }
void Engine::zoom(const std::string &cell_name) { void Engine::zoom(const std::string &actor, float scale) {
auto& cell = $ui.cell_for(cell_name); auto& config = actor_config(actor);
zoom(cell.w, cell.h, cell.mid_x, cell.mid_y); auto bounds = config.st.sprite->getGlobalBounds();
float mid_x = config.pos.x + bounds.size.x / 2.0f;
float mid_y = config.pos.y + bounds.size.y / 2.0f;
fmt::println("CAMERA in scene::Engine I'm zooming {} by {} @ {},{} size {},{}; going_to mid={},{}",
actor, scale, config.pos.x, config.pos.y,
bounds.size.x, bounds.size.y, mid_x, mid_y);
zoom(mid_x, mid_y, scale);
} }
void Engine::reset(sf::RenderTexture& view, float width, float height) { void Engine::reset(sf::RenderTexture& view) {
$camera.reset(view, width, height); $camera.reset(view);
} }
} }

View file

@ -34,7 +34,7 @@ namespace scene {
std::unordered_map<std::string, int> $actor_name_ids; std::unordered_map<std::string, int> $actor_name_ids;
std::vector<Element> $fixtures; std::vector<Element> $fixtures;
std::vector<Element> $actors; std::vector<Element> $actors;
cinematic::Camera $camera; cinematic::Camera $camera{{BOSS_VIEW_WIDTH, BOSS_VIEW_HEIGHT}};
Engine(components::AnimatedScene& scene); Engine(components::AnimatedScene& scene);
@ -51,8 +51,8 @@ namespace scene {
void play_animations(); void play_animations();
void apply_effect(const std::string& actor, const std::string& shader); void apply_effect(const std::string& actor, const std::string& shader);
Element& actor_config(const std::string& actor); Element& actor_config(const std::string& actor);
void zoom(const std::string& cell); void zoom(const std::string& actor, float scale=0.9f);
void reset(sf::RenderTexture& view, float width, float height); void zoom(float mid_x, float mid_y, float scale);
void zoom(int mid_x, int mid_y, int width, int height); void reset(sf::RenderTexture& view);
}; };
} }

View file

@ -93,13 +93,11 @@ namespace storyboard {
void UI::zoom(const std::string &cell_name) { void UI::zoom(const std::string &cell_name) {
auto& cell = $ui.cell_for(cell_name); auto& cell = $ui.cell_for(cell_name);
$camera.resize(float(cell.w), float(cell.h)); $camera.resize(float(cell.w));
$camera.move(float(cell.mid_x), float(cell.mid_y)); $camera.move(float(cell.mid_x), float(cell.mid_y));
} }
void UI::reset() { void UI::reset() {
sf::FloatRect where{{0,0},{SCREEN_WIDTH, SCREEN_HEIGHT}}; $camera.reset($view_texture);
sf::View view{where};
$view_texture.setView(view);
} }
} }

View file

@ -10,7 +10,7 @@ namespace storyboard {
guecs::UI $ui; guecs::UI $ui;
sf::RenderTexture $view_texture; sf::RenderTexture $view_texture;
sf::Sprite $view_sprite; sf::Sprite $view_sprite;
cinematic::Camera $camera; cinematic::Camera $camera{{SCREEN_WIDTH, SCREEN_HEIGHT}};
std::shared_ptr<sf::Sound> $audio; std::shared_ptr<sf::Sound> $audio;
std::string $zoom_target = "a"; std::string $zoom_target = "a";
bool $moving = false; bool $moving = false;