game states cleanup
This commit is contained in:
+1
-10
@@ -12,19 +12,11 @@ pub struct Collider {
|
|||||||
pub size: Vec2,
|
pub size: Vec2,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Resource, Default)]
|
|
||||||
pub struct HasCollided(pub bool);
|
|
||||||
|
|
||||||
// Check collision with Player
|
// Check collision with Player
|
||||||
pub fn check_collision_with_player(
|
pub fn check_collision_with_player(
|
||||||
player: Single<(&Transform, &Collider, &mut Sprite), With<Player>>,
|
player: Single<(&Transform, &Collider, &mut Sprite), With<Player>>,
|
||||||
obstacle: Query<(&Transform, &Collider), With<Obstacle>>,
|
obstacle: Query<(&Transform, &Collider), With<Obstacle>>,
|
||||||
mut has_collided: ResMut<HasCollided>,
|
|
||||||
) {
|
) {
|
||||||
if has_collided.0 {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let (player_transform, player_collider, mut _player_sprite) = player.into_inner();
|
let (player_transform, player_collider, mut _player_sprite) = player.into_inner();
|
||||||
|
|
||||||
// Check whether or not each obstacles overlap the player
|
// Check whether or not each obstacles overlap the player
|
||||||
@@ -36,7 +28,6 @@ pub fn check_collision_with_player(
|
|||||||
collider_collider.size / 2.0,
|
collider_collider.size / 2.0,
|
||||||
) {
|
) {
|
||||||
// player_sprite.color = Color::srgb(1.0, 0.0, 0.0); // Change sprite color
|
// player_sprite.color = Color::srgb(1.0, 0.0, 0.0); // Change sprite color
|
||||||
has_collided.0 = true;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -53,7 +44,7 @@ pub struct CollisionPlugin;
|
|||||||
|
|
||||||
impl Plugin for CollisionPlugin {
|
impl Plugin for CollisionPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.init_resource::<HasCollided>().add_systems(
|
app.add_systems(
|
||||||
Update,
|
Update,
|
||||||
check_collision_with_player
|
check_collision_with_player
|
||||||
.run_if(in_state(GameState::InGame))
|
.run_if(in_state(GameState::InGame))
|
||||||
|
|||||||
+3
-10
@@ -7,7 +7,6 @@ use crate::{camera::MainCamera, game_state::GameState};
|
|||||||
pub struct GameOver;
|
pub struct GameOver;
|
||||||
|
|
||||||
fn game_over(mut commands: Commands, mut main_camera: Single<&mut Transform, With<MainCamera>>) {
|
fn game_over(mut commands: Commands, mut main_camera: Single<&mut Transform, With<MainCamera>>) {
|
||||||
// Texts
|
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
GameOver,
|
GameOver,
|
||||||
Node {
|
Node {
|
||||||
@@ -19,6 +18,9 @@ fn game_over(mut commands: Commands, mut main_camera: Single<&mut Transform, Wit
|
|||||||
row_gap: Val::Px(30.0),
|
row_gap: Val::Px(30.0),
|
||||||
..default()
|
..default()
|
||||||
},
|
},
|
||||||
|
// Black background
|
||||||
|
BackgroundColor(Color::BLACK),
|
||||||
|
// Texts
|
||||||
children![
|
children![
|
||||||
(
|
(
|
||||||
Text::new("GAME OVER"),
|
Text::new("GAME OVER"),
|
||||||
@@ -36,15 +38,6 @@ fn game_over(mut commands: Commands, mut main_camera: Single<&mut Transform, Wit
|
|||||||
)
|
)
|
||||||
],
|
],
|
||||||
));
|
));
|
||||||
|
|
||||||
main_camera.translation = Vec3::ZERO;
|
|
||||||
|
|
||||||
// Background
|
|
||||||
commands.spawn((
|
|
||||||
GameOver,
|
|
||||||
Sprite::from_color(Color::BLACK, Vec2::new(1920.0, 1080.0)),
|
|
||||||
Transform::from_xyz(0.0, 0.0, -1.0),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cleanup_game_over(mut commands: Commands, game_over: Query<Entity, With<GameOver>>) {
|
fn cleanup_game_over(mut commands: Commands, game_over: Query<Entity, With<GameOver>>) {
|
||||||
|
|||||||
+3
-8
@@ -7,7 +7,6 @@ use crate::game_state::GameState;
|
|||||||
pub struct MainMenu;
|
pub struct MainMenu;
|
||||||
|
|
||||||
fn main_menu(mut commands: Commands) {
|
fn main_menu(mut commands: Commands) {
|
||||||
// Texts
|
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
MainMenu,
|
MainMenu,
|
||||||
Node {
|
Node {
|
||||||
@@ -19,6 +18,9 @@ fn main_menu(mut commands: Commands) {
|
|||||||
row_gap: Val::Px(30.0),
|
row_gap: Val::Px(30.0),
|
||||||
..default()
|
..default()
|
||||||
},
|
},
|
||||||
|
// Black background
|
||||||
|
BackgroundColor(Color::BLACK),
|
||||||
|
// Texts
|
||||||
children![
|
children![
|
||||||
(
|
(
|
||||||
Text::new("DOWN INTO THE VOID"),
|
Text::new("DOWN INTO THE VOID"),
|
||||||
@@ -36,13 +38,6 @@ fn main_menu(mut commands: Commands) {
|
|||||||
)
|
)
|
||||||
],
|
],
|
||||||
));
|
));
|
||||||
|
|
||||||
// Background
|
|
||||||
commands.spawn((
|
|
||||||
MainMenu,
|
|
||||||
Sprite::from_color(Color::BLACK, Vec2::new(1920.0, 1080.0)),
|
|
||||||
Transform::from_xyz(0.0, 0.0, -1.0),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait player to press enter for starting the fall
|
// Wait player to press enter for starting the fall
|
||||||
|
|||||||
+3
-4
@@ -1,7 +1,7 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
use crate::collision::Collider;
|
||||||
use crate::collision::check_collision_with_player;
|
use crate::collision::check_collision_with_player;
|
||||||
use crate::collision::{Collider, HasCollided};
|
|
||||||
use crate::game_state::GameState;
|
use crate::game_state::GameState;
|
||||||
|
|
||||||
const FALL_SPEED: f32 = 600.0; // Falling speed
|
const FALL_SPEED: f32 = 600.0; // Falling speed
|
||||||
@@ -88,7 +88,6 @@ pub(crate) fn move_player(
|
|||||||
pub fn reset_player(
|
pub fn reset_player(
|
||||||
player: Single<(&mut Transform, &mut Velocity, &mut Sprite), With<Player>>,
|
player: Single<(&mut Transform, &mut Velocity, &mut Sprite), With<Player>>,
|
||||||
input: Res<ButtonInput<KeyCode>>,
|
input: Res<ButtonInput<KeyCode>>,
|
||||||
mut has_collided: ResMut<HasCollided>,
|
|
||||||
mut fall_started: ResMut<FallStarted>,
|
mut fall_started: ResMut<FallStarted>,
|
||||||
// mut regenerate_obstacles: ResMut<RegenerateObstacles>,
|
// mut regenerate_obstacles: ResMut<RegenerateObstacles>,
|
||||||
mut next_state: ResMut<NextState<GameState>>, // For debug gameover state
|
mut next_state: ResMut<NextState<GameState>>, // For debug gameover state
|
||||||
@@ -96,13 +95,13 @@ pub fn reset_player(
|
|||||||
let (mut transform, mut velocity, mut sprite) = player.into_inner();
|
let (mut transform, mut velocity, mut sprite) = player.into_inner();
|
||||||
|
|
||||||
// R key for reset or player has collider
|
// R key for reset or player has collider
|
||||||
if input.just_pressed(KeyCode::KeyR) || has_collided.0 {
|
if input.just_pressed(KeyCode::KeyR) {
|
||||||
transform.translation = Vec3::new(START_POSITION.x, START_POSITION.y, START_POSITION.z);
|
transform.translation = Vec3::new(START_POSITION.x, START_POSITION.y, START_POSITION.z);
|
||||||
velocity.0 = Vec2::ZERO;
|
velocity.0 = Vec2::ZERO;
|
||||||
sprite.color = Color::WHITE;
|
sprite.color = Color::WHITE;
|
||||||
|
|
||||||
has_collided.0 = false;
|
|
||||||
fall_started.0 = false;
|
fall_started.0 = false;
|
||||||
|
next_state.set(GameState::GameOver);
|
||||||
// TODO: uncomment when obstacle chunk logic is implemented
|
// TODO: uncomment when obstacle chunk logic is implemented
|
||||||
// regenerate_obstacles.0 = true;
|
// regenerate_obstacles.0 = true;
|
||||||
} else if input.just_pressed(KeyCode::KeyG) {
|
} else if input.just_pressed(KeyCode::KeyG) {
|
||||||
|
|||||||
Reference in New Issue
Block a user