Skip to main content

sm64_binds/sm64/
game_state.rs

1#[derive(Debug)]
2pub struct GameState {
3    pub num_stars: i32,
4    pub pos: [f32; 3],
5    pub vel: [f32; 3],
6    pub lakitu_pos: [f32; 3],
7    pub lakitu_yaw: i32,
8    pub in_credits: i32,
9    pub course_num: i32,
10    pub act_num: i32,
11    pub area_index: i32,
12    pub level_num: i32,
13}
14
15impl GameState {
16    pub fn new(data: &[u8]) -> GameState {
17        let mut offset = 0;
18
19        let num_stars = i32::from_le_bytes(data[offset..offset + 4].try_into().unwrap());
20        offset += 4;
21
22        let pos = [
23            f32::from_le_bytes(data[offset..offset + 4].try_into().unwrap()),
24            f32::from_le_bytes(data[offset + 4..offset + 8].try_into().unwrap()),
25            f32::from_le_bytes(data[offset + 8..offset + 12].try_into().unwrap()),
26        ];
27        offset += 12;
28
29        let vel = [
30            f32::from_le_bytes(data[offset..offset + 4].try_into().unwrap()),
31            f32::from_le_bytes(data[offset + 4..offset + 8].try_into().unwrap()),
32            f32::from_le_bytes(data[offset + 8..offset + 12].try_into().unwrap()),
33        ];
34        offset += 12;
35
36        let lakitu_pos = [
37            f32::from_le_bytes(data[offset..offset + 4].try_into().unwrap()),
38            f32::from_le_bytes(data[offset + 4..offset + 8].try_into().unwrap()),
39            f32::from_le_bytes(data[offset + 8..offset + 12].try_into().unwrap()),
40        ];
41        offset += 12;
42
43        let lakitu_yaw = i32::from_le_bytes(data[offset..offset+4].try_into().unwrap());
44        offset += 4;
45
46        let in_credits = i32::from_le_bytes(data[offset..offset+4].try_into().unwrap());
47        offset += 4; 
48
49        let course_num = i32::from_le_bytes(data[offset..offset+4].try_into().unwrap());
50        offset += 4;
51
52        let act_num = i32::from_le_bytes(data[offset..offset+4].try_into().unwrap());
53        offset += 4;
54
55        let area_index = i32::from_le_bytes(data[offset..offset+4].try_into().unwrap());
56        offset += 4;
57        let level_num = i32::from_le_bytes(data[offset..offset+4].try_into().unwrap());
58        // offset += 4;
59
60        // Construct GameState
61        GameState {
62            num_stars,
63            pos,
64            vel,
65            lakitu_pos,
66            lakitu_yaw,
67            in_credits,
68            course_num,
69            act_num,
70            area_index,
71            level_num
72        }
73    }
74
75    pub fn has_won(&self) -> bool {
76        self.num_stars > 0
77    }
78
79    pub fn to_string(&self) -> String {
80        format!(
81            "GameState {{\n\
82                numStars: {},\n\
83                position: ({}, {}, {}),\n\
84                velocity: ({}, {}, {}),\n\
85                lakituPosition: ({}, {}, {}),\n\
86                lakituYaw: {},\n\
87                inCredits: {},\n\
88                courseNum: {},\n\
89                actNum: {},\n\
90                areaIndex: {},\n\
91                levelNum: {},\n\
92            }}",
93            self.num_stars,
94            self.pos[0], self.pos[1], self.pos[2],
95            self.vel[0], self.vel[1], self.vel[2],
96            self.lakitu_pos[0], self.lakitu_pos[1], self.lakitu_pos[2],
97            self.lakitu_yaw,
98            self.in_credits,
99            self.course_num,
100            self.act_num,
101            self.area_index,
102            self.level_num
103        )
104    }
105}