4 个稳定版本
1.1.2 | 2022 年 6 月 8 日 |
---|---|
1.1.0 | 2022 年 6 月 7 日 |
1.0.0 | 2022 年 5 月 20 日 |
#8 in #minimum
12KB
216 行
沿线性数量运行缓动动画的状态。转折:您可以中断动画并指向不同的目标。使用简单的动画代码,这可能会导致突然跳跃,或者速度会突然变为零。InterruptableEaser 会在不产生任何突然震动或跳跃的情况下智能地重新定位。
它通过仅记住初始速度和初始位置来实现,而不是模拟一个小东西沿着移动。这可能不是实用的(这绝对不是最简单的方法,实现可能需要改进),但对于许多类型的动画来说,这是一个更好的 API。
使用 InterruptableEaser 制作动画生命条的相当真实示例
extern crate blibium; //note, blibium is my simple C++ game engine. There isn't a rust version. The following API might not be possible, but if it is, maybe it would be worth creating blibium rust, because this is better than the C++ version (because C++ doesn't have macros, so it ends up having to use a special langauge for resource stuff).
use blibium::{def, grab, rgb_hex, draw_rectangle, Rect, v2};
def!(health_bar_anim_dur, 0.2);
//ppmr is a unit representing the size of the screen, pixels per milli-radian. We can use it to do things in resolution-independent ways/allow the user to scale the UI.
def!(health_thickness, |ppmr| 0.8*ppmr);
def!(whether_rtl, false);
//resources can be grabbed by other resource definitions
def!(health_anchor, |whether_rtl, ppmr| v2(2.0*ppmr).mirror_x_if(whether_rtl));
def!(health_bar, InterruptableEaser::new(100.0));
def!(health_color, rgb_hex!(F21D40))
async fn take_damage(damage: f32){
grab!(mut health_bar, t, health_bar_anim_dur);
let new_health = (health_bar.end_value - damage).max(0.0);
health_bar.approach(new_health, t, health_bar_anim_dur);
}
async fn render_health(amount: f32){
grab!(health_bar, health_anchor, health_bar_direction, health_thickness, health_bar_anim_dur, health_color);
draw_rectangle(
&Rect::bl_ur(
health_anchor,
health_anchor + health_bar_direction*health_bar.v(t, health_bar_anim_dur) + v2(0.0, health_thickness),
),
health_color,
);
}