Drawing pieces on the board!
Also states: 2d state, skeleton 3d state, menu state, loading state. Really leaning into the bevy system scheduling.selection-refactor
parent
21f5bded8a
commit
5f255275e0
@ -1,9 +1,170 @@
|
||||
use crate::prelude::*;
|
||||
use crate::{
|
||||
game::{Board, BoardIndex, Piece},
|
||||
prelude::*,
|
||||
};
|
||||
|
||||
const SCALE: f32 = 80.0;
|
||||
|
||||
pub struct Display2dPlugin;
|
||||
|
||||
impl Plugin for Display2dPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
todo!()
|
||||
app.add_systems(
|
||||
Startup,
|
||||
(initialize_camera, load_spritesheet).run_if(in_state(GameState::Loading)),
|
||||
)
|
||||
.add_systems(
|
||||
Update,
|
||||
(
|
||||
initialize_board.run_if(resource_added::<SpriteSheet>()),
|
||||
draw_board
|
||||
.run_if(resource_exists::<SpriteSheet>())
|
||||
.run_if(resource_changed::<Board>()) // TODO: run_if(in_state(Display2d))
|
||||
.run_if(any_with_component::<BoardIndex>()),
|
||||
),
|
||||
)
|
||||
.add_systems(OnEnter(GameState::Display2d), (activate, draw_board));
|
||||
}
|
||||
}
|
||||
|
||||
/// Sprite sheet Resource for later reference
|
||||
#[derive(Debug, Resource)]
|
||||
struct SpriteSheet {
|
||||
handle: Handle<TextureAtlas>,
|
||||
}
|
||||
|
||||
/// Marker component for the 2d board entity
|
||||
#[derive(Debug, Component)]
|
||||
struct Board2d;
|
||||
|
||||
/// Marker for 2d piece entities
|
||||
#[derive(Debug, Component)]
|
||||
struct Piece2d;
|
||||
|
||||
/// STARTUP: Initialize 2d gameplay Camera
|
||||
fn initialize_camera(mut commands: Commands) {
|
||||
commands.spawn(Camera2dBundle {
|
||||
camera: Camera {
|
||||
is_active: false,
|
||||
..default()
|
||||
},
|
||||
..default()
|
||||
});
|
||||
}
|
||||
|
||||
/// STARTUP: Load sprite sheet and insert texture atlas
|
||||
fn load_spritesheet(
|
||||
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
|
||||
server: Res<AssetServer>,
|
||||
mut commands: Commands,
|
||||
) {
|
||||
let atlas = TextureAtlas::from_grid(
|
||||
server.load("sprites.png"),
|
||||
Vec2::new(16.0, 16.0),
|
||||
5,
|
||||
1,
|
||||
None,
|
||||
None,
|
||||
);
|
||||
commands.insert_resource(SpriteSheet {
|
||||
handle: texture_atlases.add(atlas),
|
||||
});
|
||||
}
|
||||
|
||||
/// STARTUP: Initialize the board for representation
|
||||
fn initialize_board(sprite_sheet: Option<Res<SpriteSheet>>, mut commands: Commands) {
|
||||
if let Some(sprite_sheet) = sprite_sheet {
|
||||
commands
|
||||
.spawn((
|
||||
SpatialBundle {
|
||||
transform: Transform::from_xyz(-SCALE * 3.5, -SCALE * 1.5, 0.0),
|
||||
..default()
|
||||
},
|
||||
Board2d,
|
||||
))
|
||||
.with_children(|parent| {
|
||||
for i in 0..32 {
|
||||
let x = i % 8;
|
||||
let y = i / 8;
|
||||
let s = (x % 2) ^ (y % 2);
|
||||
|
||||
let transform = Transform::from_scale(Vec3::splat(5.0))
|
||||
.with_translation(Vec3::new(SCALE * x as f32, SCALE * y as f32, 0.0));
|
||||
|
||||
let sprite = TextureAtlasSprite::new(s);
|
||||
|
||||
let texture_atlas = sprite_sheet.handle.clone();
|
||||
|
||||
let index = BoardIndex { x, y };
|
||||
|
||||
// Rectangle
|
||||
parent.spawn((
|
||||
SpriteSheetBundle {
|
||||
texture_atlas,
|
||||
sprite,
|
||||
transform,
|
||||
..default()
|
||||
},
|
||||
index,
|
||||
));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_board(
|
||||
board: Option<Res<Board>>,
|
||||
mut commands: Commands,
|
||||
tiles: Query<(&Transform, &BoardIndex)>,
|
||||
pieces: Query<Entity, With<Piece2d>>,
|
||||
root: Query<Entity, With<Board2d>>,
|
||||
sprite_sheet: Option<Res<SpriteSheet>>,
|
||||
) {
|
||||
if let (Some(board), Some(sprite_sheet)) = (board, sprite_sheet) {
|
||||
commands.entity(root.single()).with_children(|parent| {
|
||||
info!("Board and sprite sheet ready, drawing board");
|
||||
board
|
||||
.pieces()
|
||||
.iter()
|
||||
.filter_map(|(board_index, piece)| {
|
||||
tiles.iter().find_map(|(transform, this_index)| {
|
||||
(*this_index == *board_index).then(|| (piece, transform))
|
||||
})
|
||||
})
|
||||
.for_each(|(piece, transform)| {
|
||||
let texture_atlas = sprite_sheet.handle.clone();
|
||||
let s = match piece {
|
||||
Piece::Queen => 2,
|
||||
Piece::Drone => 3,
|
||||
Piece::Pawn => 4,
|
||||
};
|
||||
let sprite = TextureAtlasSprite::new(s);
|
||||
// TODO: transform is slightly different, set sprite
|
||||
parent.spawn((
|
||||
piece.clone(),
|
||||
SpriteSheetBundle {
|
||||
texture_atlas,
|
||||
sprite,
|
||||
transform: Transform {
|
||||
..transform.clone()
|
||||
},
|
||||
..default()
|
||||
},
|
||||
Piece2d,
|
||||
));
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn activate(
|
||||
mut cameras: Query<&mut Camera, With<Camera2d>>,
|
||||
mut boards: Query<&mut Visibility, With<Board2d>>,
|
||||
) {
|
||||
cameras.iter_mut().for_each(|mut camera| {
|
||||
camera.is_active = true;
|
||||
});
|
||||
boards.iter_mut().for_each(|mut visibility| {
|
||||
*visibility = Visibility::Visible;
|
||||
});
|
||||
}
|
||||
|
||||
@ -1,2 +1,6 @@
|
||||
pub use crate::*;
|
||||
pub use bevy::asset::LoadState;
|
||||
pub use bevy::gltf::Gltf;
|
||||
pub use bevy::prelude::*;
|
||||
pub use bevy_fmod::prelude::AudioSource;
|
||||
pub use bevy_fmod::prelude::*;
|
||||
|
||||
Loading…
Reference in New Issue