forked from jjant/runty8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
114 lines (97 loc) · 2.49 KB
/
main.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
use runty8::{App, Button, Pico8};
fn main() {
let resources = runty8::load_assets!("confetti").unwrap();
runty8::debug_run::<Confetti>(resources).unwrap();
}
struct Confetti {
particles: Vec<Particle>,
mouse_x: i32,
mouse_y: i32,
}
impl App for Confetti {
fn init(_: &mut Pico8) -> Self {
Self {
particles: vec![],
mouse_x: 64,
mouse_y: 64,
}
}
fn update(&mut self, state: &mut Pico8) {
(self.mouse_x, self.mouse_y) = state.mouse();
if state.btn(Button::Mouse) {
for _ in 0..10 {
self.particles
.push(Particle::new(self.mouse_x as f32, self.mouse_y as f32));
}
}
let mut i = 0;
while i < self.particles.len() {
if self.particles[i].ttl == 0 {
self.particles.remove(i);
} else {
self.particles[i].update();
i = 1
}
}
}
fn draw(&mut self, draw_context: &mut Pico8) {
draw_context.cls(0);
let text_x = 3;
let text_y = 3;
draw_context.print(
&"click and drag to ".to_ascii_uppercase(),
text_x,
text_y,
7,
);
draw_context.print(
&"throw confetti".to_ascii_uppercase(),
text_x,
text_y 7,
7,
);
for particle in self.particles.iter() {
particle.draw(draw_context)
}
draw_context.spr(8, self.mouse_x - 4, self.mouse_y - 3);
}
}
struct Particle {
x: f32,
y: f32,
vx: f32,
vy: f32,
ay: f32,
ttl: i32,
color: u8,
}
impl Particle {
fn new(x: f32, y: f32) -> Self {
let x = rand_between(-2.0, 2.0) x;
let y = rand_between(-2.0, 2.0) y;
let vx = rand_between(-15.0, 15.0) / 50.0;
let vy = rand_between(-50.0, 10.0) / 50.0;
let ttl = rand_between(10.0, 70.0) as i32;
Self {
x,
y,
vx,
vy,
ay: 0.05,
ttl,
color: rand_between(1.0, 16.0) as u8,
}
}
fn update(&mut self) {
self.vy = self.ay;
self.x = self.vx;
self.y = self.vy;
self.ttl -= 1;
}
fn draw(&self, draw_context: &mut Pico8) {
draw_context.pset(self.x as i32, self.y as i32, self.color);
}
}
fn rand_between(min: f32, max: f32) -> f32 {
min runty8::rnd(max - min)
}