rustic_mountain_core/objects/
spring.rs

1use std::cell::RefCell;
2use std::rc::Rc;
3
4use crate::{structures::*, Celeste};
5use serde::{Deserialize, Serialize};
6
7#[derive(Serialize, Deserialize)]
8pub struct Spring {
9    pub hide_in: u8,
10    hide_for: u8,
11    delay: u8,
12}
13impl Spring {
14    pub fn init(_celeste: &mut Celeste, x: f32, y: f32) -> Object {
15        Object {
16            pos: Vector { x, y },
17            spd: Vector { x: 0.0, y: 0.0 },
18            rem: Vector { x: 0.0, y: 0.0 },
19            spr: 18,
20            hitbox: Rectangle {
21                x: 0.0,
22                y: 0.0,
23                w: 8.0,
24                h: 8.0,
25            },
26            flip: FlipState { x: false, y: false },
27            solids: true,
28            collidable: true,
29            obj_type: ObjectType::Spring(Rc::new(RefCell::new(Self {
30                hide_in: 0,
31                hide_for: 0,
32                delay: 0,
33            }))),
34            draw: ObjFunc(Self::draw),
35            update: ObjFunc(Self::update),
36            name: "Spring",
37        }
38    }
39    pub fn update(obj: &mut Object, celeste: &mut Celeste) {
40        let tref = match &mut obj.obj_type {
41            ObjectType::Spring(p) => p.clone(),
42            _ => unreachable!(),
43        };
44        let mut this = tref.borrow_mut();
45
46        if this.hide_for > 0 {
47            this.hide_for -= 1;
48            if this.hide_for <= 0 {
49                obj.spr = 18;
50                this.delay = 0;
51            }
52        } else if obj.spr == 18 {
53            let hit = obj.check(celeste, "Player", 0.0, 0.0);
54            // dbg!(&hit);
55            match hit {
56                Some(i) => {
57                    // panic!();'
58                    let jref = celeste.objects[i].clone();
59                    let mut playerobj = jref.borrow_mut();
60                    let pref = match &mut playerobj.obj_type {
61                        ObjectType::Player(p) => p.clone(),
62                        _ => unreachable!(),
63                    };
64
65                    let mut player = pref.borrow_mut();
66                    if playerobj.spd.y >= 0.0 {
67                        obj.spr = 19;
68                        playerobj.pos.y = obj.pos.y - 4.0;
69                        playerobj.spd.x *= 0.2;
70                        playerobj.spd.y = -3.0;
71                        player.djump = celeste.max_djump;
72                        this.delay = 10;
73                        obj.init_smoke(celeste, 0.0, 0.0);
74
75                        let floordex = obj.check(celeste, "FallFloor", 0.0, 1.0);
76                        if let Some(i) = floordex {
77                            let oref = celeste.objects[i].clone();
78                            let mut floorobj = oref.borrow_mut();
79                            let fref = match &mut floorobj.obj_type {
80                                ObjectType::FallFloor(p) => p.clone(),
81                                _ => unreachable!(),
82                            };
83                            let mut floor = fref.borrow_mut();
84                            this.hide_in = 15; // TODO: innacuracy: break_floor doesn't hide it
85                                               // because then they would both have a mut ref, impossible in safe rust
86                            floor.break_floor(&mut floorobj, celeste);
87                        }
88                        // psfx 8
89                    }
90                }
91                None => (),
92            }
93        } else if this.delay > 0 {
94            this.delay -= 1;
95            if this.delay <= 0 {
96                obj.spr = 18;
97            }
98        }
99        if this.hide_in > 0 {
100            this.hide_in -= 1;
101            if this.hide_in <= 0 {
102                this.hide_for = 60;
103                obj.spr = 0;
104            }
105        }
106    }
107    pub fn draw(obj: &mut Object, celeste: &mut Celeste) {
108        let tref = match &mut obj.obj_type {
109            ObjectType::Spring(p) => p.clone(),
110            _ => unreachable!(),
111        };
112        let _this = tref.borrow_mut();
113
114        obj.draw_sprite(celeste);
115    }
116}