forked from CleanCut/invaders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shot.rs
43 lines (40 loc) · 998 Bytes
/
shot.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
use crate::frame::{Drawable, Frame};
use rusty_time::timer::Timer;
use std::time::Duration;
pub struct Shot {
pub x: usize,
pub y: usize,
pub exploding: bool,
timer: Timer,
}
impl Shot {
pub fn new(x: usize, y: usize) -> Self {
Self {
x,
y,
exploding: false,
timer: Timer::from_millis(50),
}
}
pub fn update(&mut self, delta: Duration) {
self.timer.update(delta);
if self.timer.ready && !self.exploding {
if self.y > 0 {
self.y -= 1;
}
self.timer.reset();
}
}
pub fn explode(&mut self) {
self.exploding = true;
self.timer = Timer::from_millis(250);
}
pub fn dead(&self) -> bool {
(self.exploding && self.timer.ready) || (self.y == 0)
}
}
impl Drawable for Shot {
fn draw(&self, frame: &mut Frame) {
frame[self.x][self.y] = if self.exploding { '*' } else { '|' };
}
}