Mercurial > touhou
comparison src/th06/enemy.rs @ 660:31fc0d881105
Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sat, 10 Aug 2019 14:41:30 +0200 |
parents | 3a9d82a02c88 |
children | 598f3125cbac |
comparison
equal
deleted
inserted
replaced
659:53786d834444 | 660:31fc0d881105 |
---|---|
9 use std::rc::{Rc, Weak}; | 9 use std::rc::{Rc, Weak}; |
10 | 10 |
11 /// The 2D position of an object in the game. | 11 /// The 2D position of an object in the game. |
12 #[derive(Debug, Clone, Copy, Default)] | 12 #[derive(Debug, Clone, Copy, Default)] |
13 pub struct Position { | 13 pub struct Position { |
14 x: f32, | 14 pub(crate) x: f32, |
15 y: f32, | 15 pub(crate) y: f32, |
16 } | 16 } |
17 | 17 |
18 /// An offset which can be added to a Position. | 18 /// An offset which can be added to a Position. |
19 #[derive(Debug, Clone, Copy, Default)] | 19 #[derive(Debug, Clone, Copy, Default)] |
20 pub struct Offset { | 20 pub struct Offset { |
58 /// God struct of our game. | 58 /// God struct of our game. |
59 pub struct Game { | 59 pub struct Game { |
60 enemies: Vec<Rc<RefCell<Enemy>>>, | 60 enemies: Vec<Rc<RefCell<Enemy>>>, |
61 anmrunners: Vec<Rc<RefCell<AnmRunner>>>, | 61 anmrunners: Vec<Rc<RefCell<AnmRunner>>>, |
62 prng: Rc<RefCell<Prng>>, | 62 prng: Rc<RefCell<Prng>>, |
63 rank: i32, | |
64 difficulty: i32, | |
63 } | 65 } |
64 | 66 |
65 impl Game { | 67 impl Game { |
66 /// Create said god struct. | 68 /// Create said god struct. |
67 pub fn new(prng: Rc<RefCell<Prng>>) -> Game { | 69 pub fn new(prng: Rc<RefCell<Prng>>) -> Game { |
68 Game { | 70 Game { |
69 enemies: Vec::new(), | 71 enemies: Vec::new(), |
70 anmrunners: Vec::new(), | 72 anmrunners: Vec::new(), |
71 prng, | 73 prng, |
74 rank: 0, | |
75 difficulty: 0, | |
72 } | 76 } |
73 } | 77 } |
74 | 78 |
75 /// Run the simulation for a single frame. | 79 /// Run the simulation for a single frame. |
76 pub fn run_frame(&mut self) { | 80 pub fn run_frame(&mut self) { |
107 | 111 |
108 /// The enemy struct, containing everything pertaining to an enemy. | 112 /// The enemy struct, containing everything pertaining to an enemy. |
109 #[derive(Default)] | 113 #[derive(Default)] |
110 pub struct Enemy { | 114 pub struct Enemy { |
111 // Common to all elements in game. | 115 // Common to all elements in game. |
112 pos: Position, | 116 pub(crate) pos: Position, |
113 removed: bool, | 117 pub(crate) removed: bool, |
114 anmrunner: Weak<RefCell<AnmRunner>>, | 118 pub(crate) anmrunner: Weak<RefCell<AnmRunner>>, |
115 | 119 |
116 // Specific to enemy. | 120 // Specific to enemy. |
117 // Floats. | 121 // Floats. |
118 z: f32, | 122 pub(crate) z: f32, |
119 angle: f32, | 123 pub(crate) angle: f32, |
120 speed: f32, | 124 pub(crate) speed: f32, |
121 rotation_speed: f32, | 125 pub(crate) rotation_speed: f32, |
122 acceleration: f32, | 126 pub(crate) acceleration: f32, |
123 | 127 |
124 // Ints. | 128 // Ints. |
125 type_: u32, | 129 pub(crate) type_: u32, |
126 bonus_dropped: u32, | 130 pub(crate) bonus_dropped: u32, |
127 die_score: u32, | 131 pub(crate) die_score: u32, |
128 frame: u32, | 132 /// XXX |
129 life: u32, | 133 pub frame: u32, |
130 death_flags: u32, | 134 pub(crate) life: u32, |
131 current_laser_id: u32, | 135 pub(crate) death_flags: u32, |
132 low_life_trigger: Option<u32>, | 136 pub(crate) current_laser_id: u32, |
133 timeout: Option<u32>, | 137 pub(crate) low_life_trigger: Option<u32>, |
134 remaining_lives: u32, | 138 pub(crate) timeout: Option<u32>, |
135 bullet_launch_interval: u32, | 139 pub(crate) remaining_lives: u32, |
136 bullet_launch_timer: u32, | 140 pub(crate) bullet_launch_interval: u32, |
137 death_anim: u32, | 141 pub(crate) bullet_launch_timer: u32, |
138 direction: u32, | 142 pub(crate) death_anim: u32, |
139 update_mode: u32, | 143 pub(crate) direction: u32, |
144 pub(crate) update_mode: u32, | |
140 | 145 |
141 // Bools. | 146 // Bools. |
142 visible: bool, | 147 pub(crate) visible: bool, |
143 was_visible: bool, | 148 pub(crate) was_visible: bool, |
144 touchable: bool, | 149 pub(crate) touchable: bool, |
145 collidable: bool, | 150 pub(crate) collidable: bool, |
146 damageable: bool, | 151 pub(crate) damageable: bool, |
147 boss: bool, | 152 pub(crate) boss: bool, |
148 automatic_orientation: bool, | 153 pub(crate) automatic_orientation: bool, |
149 delay_attack: bool, | 154 pub(crate) delay_attack: bool, |
150 | 155 |
151 // Tuples. | 156 // Tuples. |
152 difficulty_coeffs: (f32, f32, u32, u32, u32, u32), | 157 pub(crate) difficulty_coeffs: (f32, f32, u32, u32, u32, u32), |
153 extended_bullet_attributes: Option<(u32, u32, u32, u32, f32, f32, f32, f32)>, | 158 pub(crate) extended_bullet_attributes: Option<(u32, u32, u32, u32, f32, f32, f32, f32)>, |
154 bullet_attributes: Option<(i16, i16, u32, u32, u32, f32, f32, f32, f32, u32)>, | 159 pub(crate) bullet_attributes: Option<(i16, i16, u32, u32, u32, f32, f32, f32, f32, u32)>, |
155 bullet_launch_offset: Offset, | 160 pub(crate) bullet_launch_offset: Offset, |
156 movement_dependant_sprites: Option<(f32, f32, f32, f32)>, | 161 pub(crate) movement_dependant_sprites: Option<(f32, f32, f32, f32)>, |
157 screen_box: Option<(f32, f32, f32, f32)>, | 162 pub(crate) screen_box: Option<(f32, f32, f32, f32)>, |
158 | 163 |
159 // Callbacks. | 164 // Callbacks. |
160 death_callback: Option<Callback>, | 165 death_callback: Option<Callback>, |
161 boss_callback: Option<Callback>, | 166 boss_callback: Option<Callback>, |
162 low_life_callback: Option<Callback>, | 167 low_life_callback: Option<Callback>, |
168 // Options. | 173 // Options. |
169 // TODO: actually a 8 element array. | 174 // TODO: actually a 8 element array. |
170 options: Vec<Element>, | 175 options: Vec<Element>, |
171 | 176 |
172 // Interpolators. | 177 // Interpolators. |
173 interpolator: Option<Interpolator2<f32>>, | 178 pub(crate) interpolator: Option<Interpolator2<f32>>, |
174 speed_interpolator: Option<Interpolator1<f32>>, | 179 pub(crate) speed_interpolator: Option<Interpolator1<f32>>, |
175 | 180 |
176 // Misc stuff, do we need them? | 181 // Misc stuff, do we need them? |
177 anm0: Weak<RefCell<Anm0>>, | 182 pub(crate) anm0: Weak<RefCell<Anm0>>, |
178 process: Rc<RefCell<Process>>, | 183 process: Rc<RefCell<Process>>, |
179 game: Weak<RefCell<Game>>, | 184 pub(crate) game: Weak<RefCell<Game>>, |
180 prng: Weak<RefCell<Prng>>, | 185 pub(crate) prng: Weak<RefCell<Prng>>, |
181 hitbox_half_size: [f32; 2], | 186 pub(crate) hitbox_half_size: [f32; 2], |
182 } | 187 } |
183 | 188 |
184 impl Enemy { | 189 impl Enemy { |
185 /// Create a new enemy. | 190 /// Create a new enemy. |
186 pub fn new(pos: Position, life: i32, bonus_dropped: u32, die_score: u32, anm0: Weak<RefCell<Anm0>>, game: Weak<RefCell<Game>>) -> Enemy { | 191 pub fn new(pos: Position, life: i32, bonus_dropped: u32, die_score: u32, anm0: Weak<RefCell<Anm0>>, game: Weak<RefCell<Game>>) -> Rc<RefCell<Enemy>> { |
187 Enemy { | 192 let game_rc = game.upgrade().unwrap(); |
193 let enemy = Enemy { | |
188 pos, | 194 pos, |
189 anm0, | 195 anm0, |
190 game, | 196 game, |
191 visible: true, | 197 visible: true, |
192 bonus_dropped, | 198 bonus_dropped, |
195 touchable: true, | 201 touchable: true, |
196 collidable: true, | 202 collidable: true, |
197 damageable: true, | 203 damageable: true, |
198 difficulty_coeffs: (-0.5, 0.5, 0, 0, 0, 0), | 204 difficulty_coeffs: (-0.5, 0.5, 0, 0, 0, 0), |
199 ..Default::default() | 205 ..Default::default() |
200 } | 206 }; |
207 let enemy = Rc::new(RefCell::new(enemy)); | |
208 game_rc.borrow_mut().enemies.push(enemy.clone()); | |
209 enemy | |
201 } | 210 } |
202 | 211 |
203 /// Sets the animation to the one indexed by index in the current anm0. | 212 /// Sets the animation to the one indexed by index in the current anm0. |
204 pub fn set_anim(&mut self, index: u8) { | 213 pub fn set_anim(&mut self, index: u8) { |
205 let anm0 = self.anm0.upgrade().unwrap(); | 214 let anm0 = self.anm0.upgrade().unwrap(); |
209 let anmrunner = Rc::new(RefCell::new(anmrunner)); | 218 let anmrunner = Rc::new(RefCell::new(anmrunner)); |
210 self.anmrunner = Rc::downgrade(&anmrunner); | 219 self.anmrunner = Rc::downgrade(&anmrunner); |
211 (*game.borrow_mut()).anmrunners.push(anmrunner); | 220 (*game.borrow_mut()).anmrunners.push(anmrunner); |
212 } | 221 } |
213 | 222 |
223 /// Sets the current position of the enemy. | |
224 pub fn set_pos(&mut self, x: f32, y: f32, z: f32) { | |
225 self.pos.x = x; | |
226 self.pos.y = y; | |
227 self.z = z; | |
228 } | |
229 | |
214 /// Sets the hitbox around the enemy. | 230 /// Sets the hitbox around the enemy. |
215 pub fn set_hitbox(&mut self, width: f32, height: f32) { | 231 pub fn set_hitbox(&mut self, width: f32, height: f32) { |
216 self.hitbox_half_size = [width, height]; | 232 self.hitbox_half_size = [width, height]; |
233 } | |
234 | |
235 pub(crate) fn get_rank(&self) -> i32 { | |
236 let game = self.game.upgrade().unwrap(); | |
237 let game = game.borrow(); | |
238 game.rank | |
239 } | |
240 | |
241 pub(crate) fn get_difficulty(&self) -> i32 { | |
242 let game = self.game.upgrade().unwrap(); | |
243 let game = game.borrow(); | |
244 game.difficulty | |
217 } | 245 } |
218 } | 246 } |
219 | 247 |
220 #[cfg(test)] | 248 #[cfg(test)] |
221 mod tests { | 249 mod tests { |