-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathsprite.rs
82 lines (72 loc) · 2.47 KB
/
sprite.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
extern crate piston_window;
extern crate ai_behavior;
extern crate sprite;
extern crate find_folder;
use std::rc::Rc;
use piston_window::*;
use sprite::*;
use ai_behavior::{
Action,
Sequence,
Wait,
WaitForever,
While,
};
fn main() {
let (width, height) = (300, 300);
let opengl = OpenGL::V3_2;
let mut window: PistonWindow =
WindowSettings::new("piston: sprite", (width, height))
.exit_on_esc(true)
.graphics_api(opengl)
.build()
.unwrap();
let assets = find_folder::Search::ParentsThenKids(3, 3)
.for_folder("assets").unwrap();
let id;
let mut scene = Scene::new();
let mut texture_context = TextureContext {
factory: window.factory.clone(),
encoder: window.factory.create_command_buffer().into()
};
let tex = Rc::new(Texture::from_path(
&mut texture_context,
assets.join("rust.png"),
Flip::None,
&TextureSettings::new()
).unwrap());
let mut sprite = Sprite::from_texture(tex);
sprite.set_position(width as f64 / 2.0, height as f64 / 2.0);
id = scene.add_child(sprite);
// Run a sequence of animations.
let seq = Sequence(vec![
Action(Ease(EaseFunction::CubicOut, Box::new(ScaleTo(2.0, 0.5, 0.5)))),
Action(Ease(EaseFunction::BounceOut, Box::new(MoveBy(1.0, 0.0, 100.0)))),
Action(Ease(EaseFunction::ElasticOut, Box::new(MoveBy(2.0, 0.0, -100.0)))),
Action(Ease(EaseFunction::BackInOut, Box::new(MoveBy(1.0, 0.0, -100.0)))),
Wait(0.5),
Action(Ease(EaseFunction::ExponentialInOut, Box::new(MoveBy(1.0, 0.0, 100.0)))),
Action(Blink(1.0, 5)),
While(Box::new(WaitForever), vec![
Action(Ease(EaseFunction::QuadraticIn, Box::new(FadeOut(1.0)))),
Action(Ease(EaseFunction::QuadraticOut, Box::new(FadeIn(1.0)))),
]),
]);
scene.run(id, &seq);
// This animation and the one above can run in parallel.
let rotate = Action(Ease(EaseFunction::ExponentialInOut,
Box::new(RotateTo(2.0, 360.0))));
scene.run(id, &rotate);
println!("Press any key to pause/resume the animation!");
while let Some(e) = window.next() {
scene.event(&e);
window.draw_2d(&e, |c, g, _| {
clear([1.0, 1.0, 1.0, 1.0], g);
scene.draw(c.transform, g);
});
if e.press_args().is_some() {
scene.toggle(id, &seq);
scene.toggle(id, &rotate);
}
}
}