-
Notifications
You must be signed in to change notification settings - Fork 56
/
draw_bunnymark.rs
115 lines (100 loc) · 2.51 KB
/
draw_bunnymark.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
use notan::draw::*;
use notan::math::{vec2, Vec2};
use notan::prelude::*;
struct Bunny {
pos: Vec2,
speed: Vec2,
}
#[derive(AppState)]
struct State {
font: Font,
texture: Texture,
rng: Random,
bunnies: Vec<Bunny>,
}
impl State {
fn new(gfx: &mut Graphics) -> Self {
let texture = gfx
.create_texture()
.from_image(include_bytes!("assets/bunny.png"))
.build()
.unwrap();
let font = gfx
.create_font(include_bytes!("assets/Ubuntu-B.ttf"))
.unwrap();
Self {
font,
texture,
rng: Random::default(),
bunnies: vec![],
}
}
fn spawn(&mut self, n: i32) {
(0..n).for_each(|_| {
self.bunnies.push(Bunny {
pos: Vec2::ZERO,
speed: vec2(self.rng.gen_range(0.0..10.0), self.rng.gen_range(-5.0..5.0)),
})
});
}
}
fn init(gfx: &mut Graphics) -> State {
let mut state = State::new(gfx);
state.spawn(1);
state
}
fn update(app: &mut App, state: &mut State) {
if app.mouse.left_is_down() {
state.spawn(50);
}
let rng = &mut state.rng;
state.bunnies.iter_mut().for_each(|b| {
b.pos += b.speed;
b.speed.y += 0.75;
if b.pos.x > 800.0 {
b.speed.x *= -1.0;
b.pos.x = 800.0;
} else if b.pos.x < 0.0 {
b.speed.x *= -1.0;
b.pos.x = 0.0
}
if b.pos.y > 600.0 {
b.speed.y *= -0.85;
b.pos.y = 600.0;
if rng.gen::<bool>() {
b.speed.y -= rng.gen_range(0.0..6.0);
}
} else if b.pos.y < 0.0 {
b.speed.y = 0.0;
b.pos.y = 0.0;
}
});
}
fn draw(app: &mut App, gfx: &mut Graphics, state: &mut State) {
let mut draw = gfx.create_draw();
draw.clear([0.0, 0.0, 0.0, 1.0].into());
state.bunnies.iter().for_each(|b| {
draw.image(&state.texture).position(b.pos.x, b.pos.y);
});
draw.text(
&state.font,
&format!(
"{} -> {} ({:.6})",
app.timer.fps().round(),
state.bunnies.len(),
app.timer.delta_f32()
),
)
.position(10.0, 10.0)
.size(24.0);
gfx.render(&draw);
}
#[notan_main]
fn main() -> Result<(), String> {
notan::init_with(init)
.add_config(WindowConfig::new().set_vsync(true))
.add_config(DrawConfig)
.update(update)
.draw(draw)
.build()
}