Arcade-like board.

Defenetly will rewrite the way how board is renders
This commit is contained in:
dan63047 2023-11-24 00:10:09 +03:00
parent dca0bb10be
commit 5c1f814cbb
8 changed files with 136 additions and 68 deletions

BIN
assets/board.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 234 KiB

BIN
assets/border.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

BIN
board.xcf Normal file

Binary file not shown.

BIN
border.xcf Normal file

Binary file not shown.

View File

@ -9,6 +9,9 @@ pub struct HUD {}
#[derive(Component)] #[derive(Component)]
pub struct LockDelayText {} pub struct LockDelayText {}
#[derive(Component)]
pub struct UImino {}
#[derive(Component, Clone, Copy)] #[derive(Component, Clone, Copy)]
pub struct Mino{ pub struct Mino{
pub color: Color pub color: Color

View File

@ -24,6 +24,8 @@ 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(OnEnter(GameStates::Gameplay), draw_next).
add_systems(OnExit(GameloopStates::Spawn), draw_next).
add_systems(Update, draw_board.run_if(in_state(GameStates::Gameplay))). 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))); add_systems(Update, receive_input_on_game_over.run_if(in_state(GameStates::GameOver)));
} }

View File

@ -1,8 +1,9 @@
use super::{resources::Engine, rotation_systems::LockDelayMode, GameStates, GameloopStates, randomizers::*}; use super::{resources::Engine, rotation_systems::LockDelayMode, GameStates, GameloopStates, randomizers::*};
use crate::engine::components::*; use crate::engine::components::*;
use bevy::{prelude::*, sprite::MaterialMesh2dBundle}; use bevy::{prelude::*, sprite::MaterialMesh2dBundle, render::view::ColorGrading};
const MINO_SIZE: f32 = 20.0; const MINO_SIZE: f32 = 20.0;
const SMALL_MINO_SIZE: f32 = 10.0;
pub fn reset_engine(mut commands: Commands){ pub fn reset_engine(mut commands: Commands){
commands.remove_resource::<Engine>(); commands.remove_resource::<Engine>();
@ -17,28 +18,48 @@ pub fn init_engine(
mut old_board: Query<Entity, With<BoardVisual>>, 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>>, mut game_next_state: ResMut<NextState<GameStates>>,
) { asset_server: Res<AssetServer>,
) { // despawn old board
for board in old_board.iter() { for board in old_board.iter() {
commands.entity(board).despawn(); commands.entity(board).despawn();
} }
commands.spawn(( // spawn board
MaterialMesh2dBundle { let board = commands.spawn((
mesh: meshes SpriteBundle {
.add(Mesh::from(shape::Quad { transform: Transform{
size: Vec2 { translation: Vec3 { x: 0.0, y: 0.0, z: -1.0},
x: engine.board.width as f32 * MINO_SIZE, rotation: Quat::default(),
y: engine.board.height as f32 * MINO_SIZE, scale: Vec3 { x: 0.5, y: 0.5, z: 1. },
}, },
flip: false, texture: asset_server.load("border.png"),
})) sprite: Sprite {
.into(), color: Color::Rgba { red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0 },
material: materials.add(ColorMaterial::from(Color::PURPLE)), ..default()
transform: Transform::from_xyz(0.0, 0.0, -1.0), },
..default() ..default()
}, },
BoardVisual {}, BoardVisual {},
)); ))
engine.init("ARS", Box::new(TGM::create())); .with_children( |parent| {
parent.spawn((SpriteBundle {
transform: Transform{
translation: Vec3 { x: 0.0, y: 487.0, z: -0.0},
rotation: Quat::default(),
scale: Vec3 { x: 1.0, y: 1.0, z: 1. },
},
texture: asset_server.load("board.png"),
sprite: Sprite {
color: Color::Rgba { red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0 },
..default()
},
..default()
},
BoardVisual{}));
})
.id();
// init engine
engine.init("ARS", Box::new(Bag::create()));
game_next_state.set(GameStates::Gameplay); game_next_state.set(GameStates::Gameplay);
next_state.set(GameloopStates::Falling); next_state.set(GameloopStates::Falling);
} }
@ -49,6 +70,7 @@ pub fn draw_board(
all_minos: Query<Entity, With<Mino>>, all_minos: Query<Entity, With<Mino>>,
asset_server: Res<AssetServer>, asset_server: Res<AssetServer>,
) { ) {
if !engine.is_changed(){return;}
for mino in all_minos.iter() { for mino in all_minos.iter() {
commands.entity(mino).despawn(); commands.entity(mino).despawn();
} }
@ -128,47 +150,6 @@ pub fn draw_board(
None => {} None => {}
} }
// draw next queue
if engine.board.show_next > 0 {
y = 8.0;
let mut drawed = 0;
for mino in &engine.next_queue {
for tile in &engine.rotation_system.pieces[mino.id]
[mino.rotation]
{
commands.spawn((
SpriteBundle {
transform: Transform::from_xyz(
11.0 * MINO_SIZE - (engine.board.width as f32) / 2.0 * MINO_SIZE
+ MINO_SIZE / 2.0
+ tile.0 as f32 * MINO_SIZE,
y * MINO_SIZE + MINO_SIZE / 2.0 + tile.1 as f32 * MINO_SIZE,
0.0,
),
texture: asset_server.load("default_mino.png"),
sprite: Sprite {
color: engine.rotation_system.colours[mino.id],
custom_size: Some(Vec2 {
x: MINO_SIZE,
y: MINO_SIZE,
}),
..default()
},
..default()
},
Mino {
color: engine.rotation_system.colours[mino.id],
},
));
}
y -= 4.0;
drawed += 1;
if drawed >= engine.board.show_next {
break;
}
}
}
// draw hold // draw hold
match engine.hold.as_ref() { match engine.hold.as_ref() {
Some(piece) => { Some(piece) => {
@ -176,27 +157,29 @@ pub fn draw_board(
commands.spawn(( commands.spawn((
SpriteBundle { SpriteBundle {
transform: Transform::from_xyz( transform: Transform::from_xyz(
-5.0 * MINO_SIZE - (engine.board.width as f32) / 2.0 * MINO_SIZE 0.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, + mino.0 as f32 * SMALL_MINO_SIZE + engine.rotation_system.spawn_offsets[piece.id].0 as f32 * SMALL_MINO_SIZE,
-2.0 * MINO_SIZE 1.0 * MINO_SIZE
+ (engine.board.height as f32) / 2.0 * MINO_SIZE + (engine.board.height as f32) / 2.0 * MINO_SIZE
+ MINO_SIZE / 2.0 + MINO_SIZE / 2.0
+ mino.1 as f32 * MINO_SIZE, + mino.1 as f32 * SMALL_MINO_SIZE + engine.rotation_system.spawn_offsets[piece.id].1 as f32 * SMALL_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: piece.color, color: piece.color,
custom_size: Some(Vec2 { custom_size: Some(Vec2 {
x: MINO_SIZE, x: SMALL_MINO_SIZE,
y: MINO_SIZE, y: SMALL_MINO_SIZE,
}), }),
..default() ..default()
}, },
..default() ..default()
}, },
Mino { color: piece.color }, Mino {
color: engine.rotation_system.colours[piece.id],
},
)); ));
} }
} }
@ -248,6 +231,82 @@ pub fn draw_board(
} }
} }
pub fn draw_next(
mut commands: Commands,
engine: Res<Engine>,
all_minos: Query<Entity, With<UImino>>,
asset_server: Res<AssetServer>,
){
for mino in all_minos.iter() {
commands.entity(mino).despawn();
}
let mut y: f32 = 11.0;
let mut x: f32 = 0.0;
// draw next queue
if engine.board.show_next > 0 {
let mut drawed = 0;
for mino in &engine.next_queue {
if drawed == 0 {
for tile in &engine.rotation_system.pieces[mino.id][mino.rotation]
{
commands.spawn((
SpriteBundle {
transform: Transform::from_xyz(
(x - 1.5) * MINO_SIZE + tile.0 as f32 * MINO_SIZE + engine.rotation_system.spawn_offsets[mino.id].0 as f32 * SMALL_MINO_SIZE,
y * MINO_SIZE + MINO_SIZE / 2.0 + tile.1 as f32 * MINO_SIZE + engine.rotation_system.spawn_offsets[mino.id].1 as f32 * SMALL_MINO_SIZE,
0.0,
),
texture: asset_server.load("default_mino.png"),
sprite: Sprite {
color: engine.rotation_system.colours[mino.id],
custom_size: Some(Vec2 {
x: MINO_SIZE,
y: MINO_SIZE,
}),
..default()
},
..default()
},
UImino{},
));
}
x += 7.0;
} else{
for tile in &engine.rotation_system.pieces[mino.id][mino.rotation]
{
commands.spawn((
SpriteBundle {
transform: Transform::from_xyz(
(x - 1.5) * SMALL_MINO_SIZE + tile.0 as f32 * SMALL_MINO_SIZE,
y * MINO_SIZE + MINO_SIZE / 2.0 + tile.1 as f32 * SMALL_MINO_SIZE,
0.0,
),
texture: asset_server.load("default_mino.png"),
sprite: Sprite {
color: engine.rotation_system.colours[mino.id],
custom_size: Some(Vec2 {
x: SMALL_MINO_SIZE,
y: SMALL_MINO_SIZE,
}),
..default()
},
..default()
},
UImino{},
));
}
x += 5.0;
}
drawed += 1;
if drawed >= engine.board.show_next {
break;
}
}
}
}
pub fn receive_input( pub fn receive_input(
keyboard_input: Res<Input<KeyCode>>, keyboard_input: Res<Input<KeyCode>>,
mut engine: ResMut<Engine>, mut engine: ResMut<Engine>,

View File

@ -1,12 +1,16 @@
mod engine; mod engine;
use bevy::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin};
use bevy::prelude::*; use bevy::prelude::*;
use engine::UBSGEngine; use engine::UBSGEngine;
fn main() { fn main() {
App::new() App::new()
.insert_resource(ClearColor(Color::DARK_GRAY)) //.insert_resource(ClearColor(Color::DARK_GRAY))
.add_plugins(DefaultPlugins) .insert_resource(Msaa::Off)
//.add_plugins(DefaultPlugins)
.add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest()))
.add_plugins(LogDiagnosticsPlugin::default())
.add_plugins(FrameTimeDiagnosticsPlugin::default())
.add_plugins(UBSGEngine) .add_plugins(UBSGEngine)
.add_systems(Startup, startup) .add_systems(Startup, startup)
//.add_systems(Update, gameloop) //.add_systems(Update, gameloop)