diff --git a/src/state.rs b/src/state.rs index 54d3ea5..9327caa 100644 --- a/src/state.rs +++ b/src/state.rs @@ -172,19 +172,37 @@ impl State { pub fn draw_whip(&self, ctx: &mut BTerm) { let positions = self.ecs.read_storage::(); let wants_to_whips = self.ecs.read_storage::(); + let map = self.ecs.read_resource::(); let mut rng = RandomNumberGenerator::new(); for (position, wants_to_whip) in (&positions, &wants_to_whips).join() { let color = RGB::named(vga::get_by_index(rng.range(1, 16))); - let frame_data = match wants_to_whip.frame { - 0 => Some((-1, -1, '\\')), - 1 => Some((-1, 0, '─')), - 2 => Some((-1, 1, '/')), - 3 => Some((0, 1, '│')), - 4 => Some((1, 1, '\\')), - 5 => Some((1, 0, '─')), - 6 => Some((1, -1, '/')), - 7 => Some((0, -1, '│')), - _ => None, + let mut rendered_frame = wants_to_whip.frame; + let frame_data = loop { + let frame_data = match rendered_frame { + 0 => Some((-1, -1, '\\')), + 1 => Some((-1, 0, '─')), + 2 => Some((-1, 1, '/')), + 3 => Some((0, 1, '│')), + 4 => Some((1, 1, '\\')), + 5 => Some((1, 0, '─')), + 6 => Some((1, -1, '/')), + 7 => Some((0, -1, '│')), + _ => None, + }; + + if let Some(data) = frame_data { + let dest = Point { + x: position.x + data.0, + y: position.y + data.1, + }; + if map.in_bounds(dest) { + break frame_data; + } + rendered_frame += 1; + if rendered_frame > 7 { + break None; + } + } }; if let Some(data) = frame_data { ctx.set( diff --git a/src/systems/whip_system.rs b/src/systems/whip_system.rs index 3b17ac5..911c583 100644 --- a/src/systems/whip_system.rs +++ b/src/systems/whip_system.rs @@ -43,65 +43,75 @@ impl<'a> System<'a> for WhipSystem { stop_clock.0 = true; let now = Instant::now(); if now - wants_to_whip.last_frame > Duration::from_secs_f32(0.1) { - let destination = match wants_to_whip.frame { - 0 => Some(Point { - x: position.x - 1, - y: position.y - 1, - }), - 1 => Some(Point { - x: position.x - 1, - y: position.y, - }), - 2 => Some(Point { - x: position.x - 1, - y: position.y + 1, - }), - 3 => Some(Point { - x: position.x, - y: position.y + 1, - }), - 4 => Some(Point { - x: position.x + 1, - y: position.y + 1, - }), - 5 => Some(Point { - x: position.x + 1, - y: position.y, - }), - 6 => Some(Point { - x: position.x + 1, - y: position.y - 1, - }), - 7 => Some(Point { - x: position.x, - y: position.y - 1, - }), - _ => None, + let destination = loop { + let destination = match wants_to_whip.frame { + 0 => Some(Point { + x: position.x - 1, + y: position.y - 1, + }), + 1 => Some(Point { + x: position.x - 1, + y: position.y, + }), + 2 => Some(Point { + x: position.x - 1, + y: position.y + 1, + }), + 3 => Some(Point { + x: position.x, + y: position.y + 1, + }), + 4 => Some(Point { + x: position.x + 1, + y: position.y + 1, + }), + 5 => Some(Point { + x: position.x + 1, + y: position.y, + }), + 6 => Some(Point { + x: position.x + 1, + y: position.y - 1, + }), + 7 => Some(Point { + x: position.x, + y: position.y - 1, + }), + _ => None, + }; + + if let Some(dest) = destination { + if map.in_bounds(dest) { + break destination; + } + wants_to_whip.frame += 1; + if wants_to_whip.frame > 7 { + break None; + } + } }; if let Some(dest) = destination { - if map.in_bounds(dest) { - if let Some(e) = map.get_tile_content(map.point2d_to_index(dest)) { - if let Some(_monster) = monsters.get(e) { - let _ = entities.delete(e); - if let Some(sound) = &mut wants_to_whip.sound { - sound.control::, _>().stop(); - } - if wants_to_whip.frame == 7 { - wants_to_whip.sound = None; - sound_output.play_sound(sound_effects.whipping_hit_end.clone()); - } else { - wants_to_whip.sound = Some( - sound_output.play_sound(sound_effects.whipping_hit.clone()), - ); - } + if let Some(e) = map.get_tile_content(map.point2d_to_index(dest)) { + if let Some(_monster) = monsters.get(e) { + let _ = entities.delete(e); + if let Some(sound) = &mut wants_to_whip.sound { + sound.control::, _>().stop(); + } + if wants_to_whip.frame == 7 { + wants_to_whip.sound = None; + sound_output.play_sound(sound_effects.whipping_hit_end.clone()); + } else { + wants_to_whip.sound = Some( + sound_output.play_sound(sound_effects.whipping_hit.clone()), + ); } } } } if wants_to_whip.frame < 7 { - (*wants_to_whip).frame += 1; + wants_to_whip.frame += 1; wants_to_whip.last_frame = now; } else { entities_to_remove.push(entity);