Skip out-of-bounds whip frames

This commit is contained in:
Alex Page 2022-01-31 18:49:59 -05:00
parent 770793ea68
commit 1c54755039
2 changed files with 88 additions and 60 deletions

View file

@ -172,19 +172,37 @@ impl State {
pub fn draw_whip(&self, ctx: &mut BTerm) {
let positions = self.ecs.read_storage::<Position>();
let wants_to_whips = self.ecs.read_storage::<WantsToWhip>();
let map = self.ecs.read_resource::<Map>();
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(

View file

@ -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::<oddio::Stop<_>, _>().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::<oddio::Stop<_>, _>().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);