diff --git a/assets/board.png b/assets/board.png new file mode 100644 index 0000000..8ccc178 Binary files /dev/null and b/assets/board.png differ diff --git a/assets/border.png b/assets/border.png new file mode 100644 index 0000000..da0f930 Binary files /dev/null and b/assets/border.png differ diff --git a/board.xcf b/board.xcf new file mode 100644 index 0000000..648768c Binary files /dev/null and b/board.xcf differ diff --git a/border.xcf b/border.xcf new file mode 100644 index 0000000..b44da85 Binary files /dev/null and b/border.xcf differ diff --git a/src/engine/components.rs b/src/engine/components.rs index 2d0d702..3ef2bac 100644 --- a/src/engine/components.rs +++ b/src/engine/components.rs @@ -9,6 +9,9 @@ pub struct HUD {} #[derive(Component)] pub struct LockDelayText {} +#[derive(Component)] +pub struct UImino {} + #[derive(Component, Clone, Copy)] pub struct Mino{ pub color: Color diff --git a/src/engine/mod.rs b/src/engine/mod.rs index af154c6..becf2d1 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -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(OnEnter(GameloopStates::AfterLocking), after_locking_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, receive_input_on_game_over.run_if(in_state(GameStates::GameOver))); } diff --git a/src/engine/systems.rs b/src/engine/systems.rs index c9d4cd1..1680f86 100644 --- a/src/engine/systems.rs +++ b/src/engine/systems.rs @@ -1,8 +1,9 @@ use super::{resources::Engine, rotation_systems::LockDelayMode, GameStates, GameloopStates, randomizers::*}; use crate::engine::components::*; -use bevy::{prelude::*, sprite::MaterialMesh2dBundle}; +use bevy::{prelude::*, sprite::MaterialMesh2dBundle, render::view::ColorGrading}; const MINO_SIZE: f32 = 20.0; +const SMALL_MINO_SIZE: f32 = 10.0; pub fn reset_engine(mut commands: Commands){ commands.remove_resource::(); @@ -17,28 +18,48 @@ pub fn init_engine( mut old_board: Query>, mut next_state: ResMut>, mut game_next_state: ResMut>, -) { + asset_server: Res, +) { // despawn old board for board in old_board.iter() { commands.entity(board).despawn(); } - commands.spawn(( - MaterialMesh2dBundle { - mesh: meshes - .add(Mesh::from(shape::Quad { - size: Vec2 { - x: engine.board.width as f32 * MINO_SIZE, - y: engine.board.height as f32 * MINO_SIZE, - }, - flip: false, - })) - .into(), - material: materials.add(ColorMaterial::from(Color::PURPLE)), - transform: Transform::from_xyz(0.0, 0.0, -1.0), + // spawn board + let board = commands.spawn(( + SpriteBundle { + transform: Transform{ + translation: Vec3 { x: 0.0, y: 0.0, z: -1.0}, + rotation: Quat::default(), + scale: Vec3 { x: 0.5, y: 0.5, z: 1. }, + }, + texture: asset_server.load("border.png"), + sprite: Sprite { + color: Color::Rgba { red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0 }, + ..default() + }, ..default() }, 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); next_state.set(GameloopStates::Falling); } @@ -49,6 +70,7 @@ pub fn draw_board( all_minos: Query>, asset_server: Res, ) { + if !engine.is_changed(){return;} for mino in all_minos.iter() { commands.entity(mino).despawn(); } @@ -128,47 +150,6 @@ pub fn draw_board( 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 match engine.hold.as_ref() { Some(piece) => { @@ -176,27 +157,29 @@ pub fn draw_board( commands.spawn(( SpriteBundle { 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.0 as f32 * MINO_SIZE, - -2.0 * MINO_SIZE + + mino.0 as f32 * SMALL_MINO_SIZE + engine.rotation_system.spawn_offsets[piece.id].0 as f32 * SMALL_MINO_SIZE, + 1.0 * MINO_SIZE + (engine.board.height as f32) / 2.0 * MINO_SIZE + 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, ), texture: asset_server.load("default_mino.png"), sprite: Sprite { color: piece.color, custom_size: Some(Vec2 { - x: MINO_SIZE, - y: MINO_SIZE, + x: SMALL_MINO_SIZE, + y: SMALL_MINO_SIZE, }), ..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, + all_minos: Query>, + asset_server: Res, +){ + 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( keyboard_input: Res>, mut engine: ResMut, diff --git a/src/main.rs b/src/main.rs index 3045427..2350057 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,16 @@ mod engine; - +use bevy::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}; use bevy::prelude::*; use engine::UBSGEngine; fn main() { App::new() - .insert_resource(ClearColor(Color::DARK_GRAY)) - .add_plugins(DefaultPlugins) + //.insert_resource(ClearColor(Color::DARK_GRAY)) + .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_systems(Startup, startup) //.add_systems(Update, gameloop)