rustic_mountain_core/objects/
lifeup.rs

1use std::{cell::RefCell, rc::Rc};
2
3use crate::{structures::*, Celeste};
4use serde::{Deserialize, Serialize};
5
6#[derive(Serialize, Deserialize)]
7pub struct LifeUp {
8    duration: f32,
9    flash: f32,
10}
11impl LifeUp {
12    pub fn init(_celeste: &mut Celeste, x: f32, y: f32) -> Object {
13        Object {
14            pos: Vector { x, y },
15            spd: Vector { x: 0.0, y: -0.25 },
16            rem: Vector { x: 0.0, y: 0.0 },
17            spr: 1,
18            hitbox: Rectangle {
19                x: -1.0,
20                y: -1.0,
21                w: 10.0,
22                h: 10.0,
23            },
24            flip: FlipState { x: false, y: false },
25            collidable: true,
26            solids: false,
27            obj_type: ObjectType::LifeUp(Rc::new(RefCell::new(Self {
28                duration: 30.0,
29                flash: 0.0,
30            }))),
31            draw: ObjFunc(Self::draw),
32            update: ObjFunc(Self::update),
33            name: "LifeUp",
34        }
35    }
36    pub fn update(obj: &mut Object, celeste: &mut Celeste) {
37        let tref = match &mut obj.obj_type {
38            ObjectType::LifeUp(p) => p.clone(),
39            _ => unreachable!(),
40        };
41        let mut this = tref.borrow_mut();
42        this.duration -= 1.0;
43        if this.duration <= 0.0 {
44            obj.destroy_self(celeste);
45        }
46    }
47    pub fn draw(obj: &mut Object, celeste: &mut Celeste) {
48        let tref = match &mut obj.obj_type {
49            ObjectType::LifeUp(p) => p.clone(),
50            _ => unreachable!(),
51        };
52        let mut this = tref.borrow_mut();
53        this.flash += 0.5;
54        celeste.mem.print(
55            "1000".into(),
56            obj.pos.x as i32 - 4,
57            obj.pos.y as i32 - 4,
58            7 + (this.flash % 2.0) as u8,
59        )
60    }
61}