Add game over system
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use bevy::prelude::*;
|
||||
use std::ops::{Add, AddAssign};
|
||||
|
||||
type GridType = i16;
|
||||
pub type GridType = i16;
|
||||
|
||||
pub const GRID_SIZE: GridType = 21;
|
||||
pub const GRID_SEGMENT_SIZE: f32 = 20.;
|
||||
|
||||
15
src/main.rs
15
src/main.rs
@@ -41,12 +41,15 @@ fn main() {
|
||||
}
|
||||
|
||||
fn setup_system(mut commands: Commands) {
|
||||
let vertical_height = GRID_SIZE as f32 * GRID_SEGMENT_SIZE * 1.2;
|
||||
let grid_dimensions = GRID_SIZE as f32 * GRID_SEGMENT_SIZE * 1.2;
|
||||
|
||||
commands
|
||||
.spawn_bundle(Camera2dBundle {
|
||||
projection: OrthographicProjection {
|
||||
scaling_mode: ScalingMode::FixedVertical(vertical_height),
|
||||
scaling_mode: ScalingMode::Auto {
|
||||
min_width: grid_dimensions,
|
||||
min_height: grid_dimensions,
|
||||
},
|
||||
..Default::default()
|
||||
},
|
||||
..Default::default()
|
||||
@@ -66,16 +69,16 @@ fn camera_move_system(
|
||||
let mut delta = Vec3::ZERO;
|
||||
|
||||
if keypress.pressed(KeyCode::Up) {
|
||||
delta += Vec3::new(0., 1., 0.);
|
||||
delta += Vec3::new(0., 10., 0.);
|
||||
}
|
||||
if keypress.pressed(KeyCode::Down) {
|
||||
delta += Vec3::new(0., -1., 0.);
|
||||
delta += Vec3::new(0., -10., 0.);
|
||||
}
|
||||
if keypress.pressed(KeyCode::Left) {
|
||||
delta += Vec3::new(-1., 0., 0.);
|
||||
delta += Vec3::new(-10., 0., 0.);
|
||||
}
|
||||
if keypress.pressed(KeyCode::Right) {
|
||||
delta += Vec3::new(1., 0., 0.);
|
||||
delta += Vec3::new(10., 0., 0.);
|
||||
}
|
||||
|
||||
delta
|
||||
|
||||
26
src/snake.rs
26
src/snake.rs
@@ -1,5 +1,5 @@
|
||||
use crate::{
|
||||
grid::{GridCoordinate, GRID_SEGMENT_SIZE, GRID_SIZE},
|
||||
grid::{GridCoordinate, GridType, GRID_SEGMENT_SIZE, GRID_SIZE},
|
||||
AppState,
|
||||
};
|
||||
use bevy::prelude::*;
|
||||
@@ -28,9 +28,10 @@ impl Plugin for SnakePlugin {
|
||||
.with_stage(fixed_time_systems0)
|
||||
.with_stage(fixed_time_systems1),
|
||||
)
|
||||
.add_system(game_over_system.run_in_state(AppState::InGame))
|
||||
.add_system(add_direction_system.run_in_state(AppState::Begin))
|
||||
.add_system(direction_system.run_in_state(AppState::InGame))
|
||||
.add_system(grid_transform_system)
|
||||
.add_system(grid_transform_system.run_in_state(AppState::InGame))
|
||||
.add_system(add_tail_system);
|
||||
}
|
||||
}
|
||||
@@ -82,7 +83,7 @@ fn create_snake_segment(commands: &mut Commands, grid_position: GridCoordinate)
|
||||
.spawn_bundle(SpriteBundle {
|
||||
sprite: Sprite {
|
||||
color: Color::RED,
|
||||
custom_size: Some(Vec2::splat(GRID_SEGMENT_SIZE)*0.9),
|
||||
custom_size: Some(Vec2::splat(GRID_SEGMENT_SIZE) * 0.9),
|
||||
..Default::default()
|
||||
},
|
||||
transform: grid_position.into(),
|
||||
@@ -129,7 +130,7 @@ fn add_tail_system(
|
||||
mut snake_query: Query<(Entity, &mut SnakeSegments)>,
|
||||
) {
|
||||
if keypress.just_pressed(KeyCode::Space) {
|
||||
let segment = create_snake_segment(&mut commands, GridCoordinate::splat(-1));
|
||||
let segment = create_snake_segment(&mut commands, GridCoordinate::splat(GridType::MIN / 2));
|
||||
let (snake, mut snake_segments) = snake_query.single_mut();
|
||||
snake_segments.0.push(segment);
|
||||
commands.entity(snake).add_child(segment);
|
||||
@@ -186,3 +187,20 @@ fn segments_movement_system(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn game_over_system(
|
||||
mut commands: Commands,
|
||||
query: Query<(&GridCoordinate, &Direction), With<SnakeHead>>,
|
||||
) {
|
||||
const HIGH_BARRIER: GridType = GRID_SIZE - 1;
|
||||
|
||||
match query.single() {
|
||||
(GridCoordinate(_, 0), Direction::Down)
|
||||
| (GridCoordinate(_, HIGH_BARRIER), Direction::Up)
|
||||
| (GridCoordinate(0, _), Direction::Left)
|
||||
| (GridCoordinate(HIGH_BARRIER, _), Direction::Right) => {
|
||||
commands.insert_resource(NextState(AppState::End))
|
||||
}
|
||||
_ => (),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user