comparison src/th06/enemy.rs @ 662:107bb5ca5cc8

Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 11 Aug 2019 18:14:01 +0200
parents 598f3125cbac
children f08e8e3c6196
comparison
equal deleted inserted replaced
661:598f3125cbac 662:107bb5ca5cc8
232 /// Sets the hitbox around the enemy. 232 /// Sets the hitbox around the enemy.
233 pub fn set_hitbox(&mut self, width: f32, height: f32) { 233 pub fn set_hitbox(&mut self, width: f32, height: f32) {
234 self.hitbox_half_size = [width, height]; 234 self.hitbox_half_size = [width, height];
235 } 235 }
236 236
237 /// Run all interpolators and such, and update internal variables once per
238 /// frame.
239 pub fn update(&mut self) {
240 let Position { mut x, mut y } = self.pos;
241
242 let speed = if self.update_mode == 1 {
243 0.
244 } else {
245 let speed = self.speed;
246 self.speed += self.acceleration;
247 self.angle += self.rotation_speed;
248 speed
249 };
250
251 let dx = self.angle.cos() * speed;
252 let dy = self.angle.sin() * speed;
253 if self.type_ & 2 != 0 {
254 x -= dx;
255 } else {
256 x += dx;
257 }
258 y += dy;
259
260 self.pos = Position { x, y };
261
262 self.frame += 1;
263 }
264
237 pub(crate) fn get_rank(&self) -> i32 { 265 pub(crate) fn get_rank(&self) -> i32 {
238 let game = self.game.upgrade().unwrap(); 266 let game = self.game.upgrade().unwrap();
239 let game = game.borrow(); 267 let game = game.borrow();
240 game.rank 268 game.rank
241 } 269 }