Reset button now works

This commit is contained in:
dan63047 2023-11-16 18:54:36 +03:00
parent fbd10833b2
commit 18410079c6
2 changed files with 43 additions and 12 deletions

View File

@ -16,7 +16,7 @@ impl Plugin for UBSGEngine{
add_state::<GameStates>(). add_state::<GameStates>().
add_state::<GameloopStates>(). add_state::<GameloopStates>().
insert_resource(Engine::default()). insert_resource(Engine::default()).
add_systems(Startup, init_engine.run_if(in_state(GameStates::Gameplay))). //add_systems(Startup, init_engine).
add_systems(Startup, spawn_hud). add_systems(Startup, spawn_hud).
add_systems(OnEnter(GameloopStates::Init), init_engine). add_systems(OnEnter(GameloopStates::Init), init_engine).
add_systems(Update, receive_input.run_if(in_state(GameStates::Gameplay))). add_systems(Update, receive_input.run_if(in_state(GameStates::Gameplay))).
@ -24,14 +24,15 @@ impl Plugin for UBSGEngine{
add_systems(FixedUpdate, gameloop.run_if(in_state(GameStates::Gameplay)).run_if(in_state(GameloopStates::Falling))). add_systems(FixedUpdate, gameloop.run_if(in_state(GameStates::Gameplay)).run_if(in_state(GameloopStates::Falling))).
add_systems(OnEnter(GameloopStates::AfterLocking), after_locking_routine). add_systems(OnEnter(GameloopStates::AfterLocking), after_locking_routine).
add_systems(OnEnter(GameloopStates::Spawn), spawn_routine). add_systems(OnEnter(GameloopStates::Spawn), spawn_routine).
add_systems(Update, draw_board); add_systems(Update, draw_board.run_if(in_state(GameStates::Gameplay))).
add_systems(Update, receive_input_on_game_over.run_if(in_state(GameStates::GameOver)));
} }
} }
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)] #[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)]
pub enum GameStates{ pub enum GameStates{
Init,
#[default] #[default]
Init,
Gameplay, Gameplay,
Pause, Pause,
GameOver GameOver

View File

@ -4,14 +4,23 @@ use bevy::{prelude::*, sprite::MaterialMesh2dBundle};
const MINO_SIZE: f32 = 20.0; const MINO_SIZE: f32 = 20.0;
pub fn reset_engine(mut commands: Commands){
commands.remove_resource::<Engine>();
commands.insert_resource::<Engine>(Engine::default());
}
pub fn init_engine( pub fn init_engine(
mut commands: Commands, mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>, mut materials: ResMut<Assets<ColorMaterial>>,
mut engine: ResMut<Engine>, mut engine: ResMut<Engine>,
mut old_board: Query<Entity, With<BoardVisual>>,
mut next_state: ResMut<NextState<GameloopStates>>, mut next_state: ResMut<NextState<GameloopStates>>,
mut game_next_state: ResMut<NextState<GameStates>>,
) { ) {
//commands.insert_resource(Engine::default()); for board in old_board.iter() {
commands.entity(board).despawn();
}
commands.spawn(( commands.spawn((
MaterialMesh2dBundle { MaterialMesh2dBundle {
mesh: meshes mesh: meshes
@ -29,7 +38,8 @@ pub fn init_engine(
}, },
BoardVisual {}, BoardVisual {},
)); ));
engine.init("SRS", Box::new(BagX2::create())); engine.init("NRS", Box::new(RandomWithoutDirectRepetition::create()));
game_next_state.set(GameStates::Gameplay);
next_state.set(GameloopStates::Falling); next_state.set(GameloopStates::Falling);
} }
@ -121,22 +131,23 @@ pub fn draw_board(
// draw next queue // draw next queue
if engine.board.show_next > 0 { if engine.board.show_next > 0 {
y = 8.0; y = 8.0;
for i in 0..engine.board.show_next as usize { let mut drawed = 0;
for mino in &engine.rotation_system.pieces[engine.next_queue[i].id] for mino in &engine.next_queue {
[engine.next_queue[i].rotation] for tile in &engine.rotation_system.pieces[mino.id]
[mino.rotation]
{ {
commands.spawn(( commands.spawn((
SpriteBundle { SpriteBundle {
transform: Transform::from_xyz( transform: Transform::from_xyz(
11.0 * MINO_SIZE - (engine.board.width as f32) / 2.0 * MINO_SIZE 11.0 * MINO_SIZE - (engine.board.width as f32) / 2.0 * MINO_SIZE
+ MINO_SIZE / 2.0 + MINO_SIZE / 2.0
+ mino.0 as f32 * MINO_SIZE, + tile.0 as f32 * MINO_SIZE,
y * MINO_SIZE + MINO_SIZE / 2.0 + mino.1 as f32 * MINO_SIZE, y * MINO_SIZE + MINO_SIZE / 2.0 + tile.1 as f32 * MINO_SIZE,
0.0, 0.0,
), ),
texture: asset_server.load("default_mino.png"), texture: asset_server.load("default_mino.png"),
sprite: Sprite { sprite: Sprite {
color: engine.rotation_system.colours[engine.next_queue[i].id], color: engine.rotation_system.colours[mino.id],
custom_size: Some(Vec2 { custom_size: Some(Vec2 {
x: MINO_SIZE, x: MINO_SIZE,
y: MINO_SIZE, y: MINO_SIZE,
@ -146,11 +157,15 @@ pub fn draw_board(
..default() ..default()
}, },
Mino { Mino {
color: engine.rotation_system.colours[engine.next_queue[i].id], color: engine.rotation_system.colours[mino.id],
}, },
)); ));
} }
y -= 4.0; y -= 4.0;
drawed += 1;
if drawed >= engine.board.show_next {
break;
}
} }
} }
@ -238,8 +253,11 @@ pub fn receive_input(
mut engine: ResMut<Engine>, mut engine: ResMut<Engine>,
state: Res<State<GameloopStates>>, state: Res<State<GameloopStates>>,
mut next_state: ResMut<NextState<GameloopStates>>, mut next_state: ResMut<NextState<GameloopStates>>,
mut game_next_state: ResMut<NextState<GameStates>>,
mut commands: Commands
) { ) {
if keyboard_input.just_pressed(KeyCode::R) && state.get() != &GameloopStates::Init { if keyboard_input.just_pressed(KeyCode::R) && state.get() != &GameloopStates::Init {
reset_engine(commands);
next_state.set(GameloopStates::Init); next_state.set(GameloopStates::Init);
} }
if keyboard_input.any_just_pressed([KeyCode::Up, KeyCode::X]) if keyboard_input.any_just_pressed([KeyCode::Up, KeyCode::X])
@ -284,6 +302,18 @@ pub fn receive_input(
} }
} }
pub fn receive_input_on_game_over(
keyboard_input: Res<Input<KeyCode>>,
state: Res<State<GameloopStates>>,
mut next_state: ResMut<NextState<GameloopStates>>,
mut commands: Commands
){
if keyboard_input.just_pressed(KeyCode::R) && state.get() != &GameloopStates::Init {
reset_engine(commands);
next_state.set(GameloopStates::Init);
}
}
pub fn das_and_arr(mut engine: ResMut<Engine>, time: Res<Time>, state: Res<State<GameloopStates>>) { pub fn das_and_arr(mut engine: ResMut<Engine>, time: Res<Time>, state: Res<State<GameloopStates>>) {
let direction = engine.handling.movement_tick(time.delta_seconds() * 1000.0); let direction = engine.handling.movement_tick(time.delta_seconds() * 1000.0);
if state.get() == &GameloopStates::Falling { if state.get() == &GameloopStates::Falling {