-
-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathaction.rs
134 lines (120 loc) · 3.74 KB
/
action.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use std::time::Duration;
use mq::{
camera::{set_camera, Camera2D},
color::{Color, BLACK},
math::{Rect, Vec2},
text,
texture::{self, Texture2D},
time, window,
};
use zscene::{self, action, Action, Boxed, Layer, Scene, Sprite};
#[derive(Debug)]
pub enum Err {
File(mq::file::FileError),
Font(mq::text::FontError),
}
impl From<mq::file::FileError> for Err {
fn from(err: mq::file::FileError) -> Self {
Err::File(err)
}
}
impl From<mq::text::FontError> for Err {
fn from(err: mq::text::FontError) -> Self {
Err::Font(err)
}
}
#[derive(Debug, Clone, Default)]
pub struct Layers {
pub bg: Layer,
pub fg: Layer,
}
impl Layers {
fn sorted(self) -> Vec<Layer> {
vec![self.bg, self.fg]
}
}
struct Assets {
font: text::Font,
texture: Texture2D,
}
impl Assets {
async fn load() -> Result<Self, Err> {
let font = text::load_ttf_font("zscene/assets/Karla-Regular.ttf").await?;
let texture = texture::load_texture("zscene/assets/fire.png").await?;
Ok(Self { font, texture })
}
}
struct State {
assets: Assets,
scene: Scene,
layers: Layers,
}
impl State {
fn new(assets: Assets) -> Self {
let layers = Layers::default();
let scene = Scene::new(layers.clone().sorted());
update_aspect_ratio();
Self {
assets,
scene,
layers,
}
}
fn action_demo_move(&self) -> Box<dyn Action> {
let mut sprite = Sprite::from_texture(self.assets.texture, 0.5);
sprite.set_pos(Vec2::new(0.0, -1.0));
let delta = Vec2::new(0.0, 1.5);
let move_duration = Duration::from_millis(2_000);
let action = action::Sequence::new(vec![
action::Show::new(&self.layers.fg, &sprite).boxed(),
action::MoveBy::new(&sprite, delta, move_duration).boxed(),
]);
action.boxed()
}
fn action_demo_show_hide(&self) -> Box<dyn Action> {
let mut sprite = {
let mut sprite = Sprite::from_text(("some text", self.assets.font), 0.1);
sprite.set_pos(Vec2::new(0.0, 0.0));
sprite.set_scale(2.0); // just testing set_size method
let scale = sprite.scale();
assert!((scale - 2.0).abs() < 0.001);
sprite
};
let visible = Color::new(0.0, 1.0, 0.0, 1.0);
let invisible = Color::new(0.0, 1.0, 0.0, 0.0);
sprite.set_color(invisible);
let t = Duration::from_millis(1_000);
let action = action::Sequence::new(vec![
action::Show::new(&self.layers.bg, &sprite).boxed(),
action::ChangeColorTo::new(&sprite, visible, t).boxed(),
action::Sleep::new(t).boxed(),
action::ChangeColorTo::new(&sprite, invisible, t).boxed(),
action::Hide::new(&self.layers.bg, &sprite).boxed(),
]);
action.boxed()
}
}
fn update_aspect_ratio() {
let aspect_ratio = window::screen_width() / window::screen_height();
let coordinates = Rect::new(-aspect_ratio, -1.0, aspect_ratio * 2.0, 2.0);
set_camera(&Camera2D::from_display_rect(coordinates));
}
#[mq::main("ZScene: Actions Demo")]
#[macroquad(crate_rename = "mq")]
async fn main() {
let assets = Assets::load().await.expect("Can't load assets");
let mut state = State::new(assets);
{
// Run two demo demo actions in parallel.
state.scene.add_action(state.action_demo_move());
state.scene.add_action(state.action_demo_show_hide());
}
loop {
window::clear_background(BLACK);
update_aspect_ratio();
let dtime = time::get_frame_time();
state.scene.tick(Duration::from_secs_f32(dtime));
state.scene.draw();
window::next_frame().await;
}
}