(
state_shared: &mut EnemyStateShared,
dt: f32,
state_data: &mut EnemyStateNormal,
)
| 207 | } |
| 208 | |
| 209 | fn update_state_normal( |
| 210 | state_shared: &mut EnemyStateShared, |
| 211 | dt: f32, |
| 212 | state_data: &mut EnemyStateNormal, |
| 213 | ) -> Option<EnemyCommand> { |
| 214 | let angle_change_speed = std::f32::consts::PI * state_shared.angle_speed; |
| 215 | state_shared.angle += |
| 216 | (get_time() as f32 * angle_change_speed).sin() * std::f32::consts::PI * 2f32 * dt; |
| 217 | let dir = vec2(state_shared.angle.sin(), -state_shared.angle.cos()); |
| 218 | state_shared.pos += dir * ENEMY_SPEED * dt; |
| 219 | // state_shared.pos.x += rand::gen_range(-1f32, 1f32) * ENEMY_SPEED * dt; |
| 220 | // state_shared.pos.y += rand::gen_range(-1f32, 1f32) * ENEMY_SPEED * dt; |
| 221 | Self::clamp_in_view(&mut state_shared.pos); |
| 222 | state_shared.collision_rect.x = state_shared.pos.x - state_shared.texture.width() * 0.5f32; |
| 223 | state_shared.collision_rect.y = state_shared.pos.y; |
| 224 | |
| 225 | state_data.shoot_timer += dt; |
| 226 | // update timer for charging in on player |
| 227 | if let Some(charge_timer) = &mut state_shared.charge_timer_optional { |
| 228 | *charge_timer -= dt; |
| 229 | if *charge_timer <= 0f32 { |
| 230 | return Some(EnemyCommand::ChangeState(EnemyState::Homing( |
| 231 | EnemyStateHoming {}, |
| 232 | ))); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | state_shared.animation_timer += dt; |
| 237 | if state_shared.animation_timer > ENEMY_ANIM_TIME_FLAP * 4f32 { |
| 238 | state_shared.animation_timer -= ENEMY_ANIM_TIME_FLAP * 4f32; |
| 239 | } |
| 240 | if state_data.shoot_timer > ENEMY_SHOOT_TIME { |
| 241 | let shot_count = rand::gen_range(1, ENEMY_MAX_BURST_COUNT); |
| 242 | // every time we change state, the enemy will chose a random speed at which it changes its velocity |
| 243 | state_shared.angle_speed = |
| 244 | rand::gen_range(ENEMY_ANGLE_SPEED_RANGE.x, ENEMY_ANGLE_SPEED_RANGE.y); |
| 245 | state_shared.angle = rand::gen_range(-std::f32::consts::PI, std::f32::consts::PI); |
| 246 | return Some(EnemyCommand::ChangeState(EnemyState::Shooting( |
| 247 | EnemyStateShooting { |
| 248 | shoot_timer: ENEMY_SHOOT_BURST_TIME, |
| 249 | shots_left: shot_count, |
| 250 | }, |
| 251 | ))); |
| 252 | } |
| 253 | None |
| 254 | } |
| 255 | |
| 256 | fn update_state_shooting( |
| 257 | state_shared: &mut EnemyStateShared, |
nothing calls this directly
no outgoing calls
no test coverage detected