Fix game-over bug

This commit is contained in:
2022-12-18 22:21:10 +01:00
parent 0e5114c3d9
commit def2337f0e
3 changed files with 174 additions and 183 deletions

View File

@@ -13,6 +13,11 @@ use iyes_loopless::prelude::*;
#[derive(Debug, Clone, PartialEq, Eq, Hash, StageLabel)]
struct MovementStage;
#[derive(Debug, Clone, PartialEq, Eq, Hash, SystemLabel)]
enum SystemLabel {
Collision,
}
pub struct SnakePlugin;
impl Plugin for SnakePlugin {
@@ -31,17 +36,20 @@ impl Plugin for SnakePlugin {
.add_system(
apply_direction_system
.run_in_state(AppState::InGame)
.run_if(tick_triggered),
.run_if(tick_triggered)
.before(SystemLabel::Collision),
)
.add_system(
game_over_system
.run_in_state(AppState::InGame)
.run_if(tick_triggered),
.run_if(tick_triggered)
.after(SystemLabel::Collision),
)
.add_system(
collision_system
.run_in_state(AppState::InGame)
.run_if(tick_triggered),
.run_if(tick_triggered)
.label(SystemLabel::Collision),
)
.add_system(add_direction_system.run_in_state(AppState::Begin))
.add_system(change_direction_system.run_in_state(AppState::InGame))
@@ -217,14 +225,14 @@ fn segments_movement_system(
continue;
}
// ... then the other elements
// Move other elements
for (&segment, &previous_segment) in segments.0.iter().rev().tuple_windows() {
let previous_coordinate = *segment_query.get(previous_segment).unwrap();
let mut coordinate = segment_query.get_mut(segment).unwrap();
*coordinate = previous_coordinate;
}
// First move head ...
// Move head
let head = *segments.0.first().expect("Snake has always a head");
let mut head_coordinate = segment_query.get_mut(head).unwrap();
*head_coordinate = next_grid_coordinate(&head_coordinate, direction);