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