Now can do a SLIDE motion that is a linear move to an x/y.

This commit is contained in:
Zed A. Shaw 2025-11-02 12:35:45 -05:00
parent f1f4cbc80f
commit 222c66a1f2
6 changed files with 58 additions and 48 deletions

View file

@ -35,46 +35,54 @@ namespace components {
} }
} }
void Animation::lerp(sf::Vector2f& scale_out, sf::Vector2f& pos_out) {
float tick = twitching();
if(stationary) {
switch(motion) {
case ease::SHAKE: {
pos_out.x += std::lerp(min_x, max_x, tick);
} break;
case ease::BOUNCE: {
pos_out.y -= std::lerp(min_y, max_y, tick);
} break;
case ease::RUSH: {
scale_out.x = std::lerp(min_x, max_x, tick);
scale_out.y = std::lerp(min_y, max_y, tick);
pos_out.y = pos_out.y - (pos_out.y * scale_out.y - pos_out.y);
} break;
case ease::SQUEEZE: {
scale_out.x *= std::lerp(min_x, max_x, tick);
} break;
case ease::SQUASH: {
scale_out.y *= std::lerp(min_y, max_y, tick);
} break;
case ease::STRETCH: {
scale_out.x = std::lerp(min_x, max_x, tick);
} break;
case ease::GROW: {
scale_out.y = std::lerp(min_y, max_y, tick);
} break;
case ease::SLIDE: {
pos_out.x += std::lerp(pos_out.x, pos_out.x + max_x, tick);
pos_out.y += std::lerp(pos_out.y, pos_out.y + max_y, tick);
} break;
default:
dbc::sentinel("Unknown animation.motion setting.");
}
} else {
scale_out.x = std::lerp(scale_out.x * min_x, scale_out.x * max_x, tick);
scale_out.y = std::lerp(scale_out.y * min_y, scale_out.y * max_y, tick);
}
}
void Animation::step(sf::Vector2f& scale_out, sf::Vector2f& pos_out, sf::IntRect& rect_out) { void Animation::step(sf::Vector2f& scale_out, sf::Vector2f& pos_out, sf::IntRect& rect_out) {
dbc::check(rect_out.size.x > 0, "step given rect_out with size.x <= 0, must be >"); dbc::check(rect_out.size.x > 0, "step given rect_out with size.x <= 0, must be >");
dbc::check(rect_out.size.y > 0, "step given rect_out with size.y <= 0, must be >"); dbc::check(rect_out.size.y > 0, "step given rect_out with size.y <= 0, must be >");
if(playing && current < frames) { if(playing && current < frames) {
float tick = twitching(); lerp(scale_out, pos_out);
if(stationary) {
switch(motion) {
case ease::SHAKE: {
pos_out.x += std::lerp(min_x, max_x, tick);
} break;
case ease::BOUNCE: {
pos_out.y -= std::lerp(min_y, max_y, tick);
} break;
case ease::RUSH: {
scale_out.x = std::lerp(min_x, max_x, tick);
scale_out.y = std::lerp(min_y, max_y, tick);
pos_out.y = pos_out.y - (pos_out.y * scale_out.y - pos_out.y);
} break;
case ease::SQUEEZE: {
scale_out.x *= std::lerp(min_x, max_x, tick);
} break;
case ease::SQUASH: {
scale_out.y *= std::lerp(min_y, max_y, tick);
} break;
case ease::STRETCH: {
scale_out.x = std::lerp(min_x, max_x, tick);
} break;
case ease::GROW: {
scale_out.y = std::lerp(min_y, max_y, tick);
} break;
default:
dbc::sentinel("Unknown animation.motion setting.");
}
} else {
scale_out.x = std::lerp(scale_out.x * min_x, scale_out.x * max_x, tick);
scale_out.y = std::lerp(scale_out.y * min_y, scale_out.y * max_y, tick);
}
if(!simple) { if(!simple) {
rect_out.position.x += current * frame_width; rect_out.position.x += current * frame_width;
@ -91,8 +99,7 @@ namespace components {
rect_out.position.x += current * frame_width; rect_out.position.x += current * frame_width;
} }
} else { } else {
scale_out.x = min_x; lerp(scale_out, pos_out);
scale_out.y = min_y;
playing = false; playing = false;
current = 0; current = 0;
subframe = 0.0f; subframe = 0.0f;
@ -112,7 +119,6 @@ namespace components {
sprite.setTextureRect(rect); sprite.setTextureRect(rect);
sprite.setPosition(pos); sprite.setPosition(pos);
// BUG: make this an option: apply_scale, apply_position and ranges for x y
if(scaled) { if(scaled) {
sprite.setScale(scale); sprite.setScale(scale);
} }
@ -127,6 +133,7 @@ namespace components {
step(scale, pos, ignored); step(scale, pos, ignored);
view_out.setCenter(pos); view_out.setCenter(pos);
// BUG: is this also needed in the other apply?
if(scaled) { if(scaled) {
view_out.setSize({size.x * scale.x, size.y * scale.y}); view_out.setSize({size.x * scale.x, size.y * scale.y});
} else { } else {

View file

@ -291,17 +291,17 @@
}, },
"test_zoom": { "test_zoom": {
"_type": "Animation", "_type": "Animation",
"easing": 6, "easing": 2,
"motion": 6, "motion": 7,
"ease_rate": 0.5, "ease_rate": 0.05,
"min_x": 0.8, "min_x": 0,
"min_y": 0.9, "min_y": 0,
"max_x": 1.1, "max_x": 150.0,
"max_y": 1.1, "max_y": 0.0,
"simple": true, "simple": true,
"frames": 1, "frames": 1,
"speed": 0.01, "speed": 0.01,
"scaled": true, "scaled": false,
"stationary": true, "stationary": true,
"toggled": false, "toggled": false,
"flipped": false, "flipped": false,

View file

@ -86,6 +86,7 @@ namespace boss {
$ui.status(L"PLAYER TURN"); $ui.status(L"PLAYER TURN");
const std::string& player_pos = run % 10 < 5 ? "player1" : "player2"; const std::string& player_pos = run % 10 < 5 ? "player1" : "player2";
$ui.move_actor("player", player_pos); $ui.move_actor("player", player_pos);
$ui.zoom(player_pos);
$ui.$zoom_anim.play(); $ui.$zoom_anim.play();
int attack_id = std::any_cast<int>(data); int attack_id = std::any_cast<int>(data);
boss::System::combat(attack_id); boss::System::combat(attack_id);

View file

@ -81,7 +81,7 @@ namespace boss {
$view_texture.setView(zoom); $view_texture.setView(zoom);
} else if($zoom_anim.playing) { } else if($zoom_anim.playing) {
auto& cell = $arena.$ui.cell_for(cell_name); auto& cell = $arena.$ui.cell_for(cell_name);
sf::Vector2f pos{float(cell.x), float(cell.y)}; sf::Vector2f pos{float(cell.x/2), float(cell.y/2)};
sf::View zoom; sf::View zoom;
$zoom_anim.apply(zoom, pos, {BOSS_VIEW_WIDTH/2, BOSS_VIEW_HEIGHT/2}); $zoom_anim.apply(zoom, pos, {BOSS_VIEW_WIDTH/2, BOSS_VIEW_HEIGHT/2});
$view_texture.setView(zoom); $view_texture.setView(zoom);

View file

@ -142,6 +142,7 @@ namespace components {
bool apply(sf::Sprite& target, sf::Vector2f pos); bool apply(sf::Sprite& target, sf::Vector2f pos);
bool apply(sf::View& view_out, sf::Vector2f pos, sf::Vector2f size); bool apply(sf::View& view_out, sf::Vector2f pos, sf::Vector2f size);
void lerp(sf::Vector2f& scale_out, sf::Vector2f& pos_out);
void play(); void play();
float twitching(); float twitching();

View file

@ -20,7 +20,8 @@ namespace ease {
SQUEEZE=3, SQUEEZE=3,
SQUASH=4, SQUASH=4,
STRETCH=5, STRETCH=5,
GROW=6 GROW=6,
SLIDE=7
}; };
inline double sine(double x) { inline double sine(double x) {