changing side colors, buggy but works

selection-refactor
Elijah Voigt 2 years ago
parent 2c17ecb1cd
commit 6dac7f5e4d

BIN
assets/images/sprites.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/images/sprites.xcf (Stored with Git LFS)

Binary file not shown.

@ -71,8 +71,6 @@ fn parse_credits(bytes: &[u8]) -> Result<CreditsText, Utf8Error> {
})
.collect();
info!("Split lines: {:?}", s);
Ok(CreditsText { sections })
}
@ -146,7 +144,7 @@ fn update_credits(
});
}
AssetEvent::Removed { .. } => {
info!("Removed Credit resource... we don't handle that...");
warn!("Removed Credit resource... we don't handle that...");
}
})
}

@ -44,7 +44,7 @@ impl DebugInfo {
self.0.insert(key, val)
}
pub fn get(&self, key: &String) -> Option<&String> {
pub fn _get(&self, key: &String) -> Option<&String> {
self.0.get(key)
}

@ -1,8 +1,5 @@
use bevy::{
input::{
mouse::{MouseButtonInput, MouseMotion},
ButtonState,
},
input::{mouse::MouseButtonInput, ButtonState},
window::{PrimaryWindow, WindowResized},
};
@ -12,7 +9,7 @@ use bevy::{
/// TODO: Handle Cursor!
///
use crate::{
game::{ActiveTile, Board, BoardIndex, Piece, Tile},
game::{ActiveTile, Board, BoardIndex, Piece, Side, Tile},
prelude::*,
};
@ -48,8 +45,8 @@ impl Plugin for Display2dPlugin {
.run_if(any_component_removed::<game::Selected>()),
update_background.run_if(on_event::<WindowResized>()),
// PERF: Only need to run when piece2d or tile2d added
set_sprite,
set_transform,
set_sprite.after(game::set_side),
set_transform.after(game::update_board::<Piece2d>),
),
)
.add_systems(
@ -110,7 +107,7 @@ fn load_spritesheet(
let atlas = TextureAtlas::from_grid(
server.load("images/sprites.png"),
Vec2::new(TILE_SIZE, TILE_SIZE),
5,
8,
1,
None,
None,
@ -173,6 +170,7 @@ fn initialize_board(board: Option<Res<Board>>, mut commands: Commands) {
Board2d,
))
.with_children(|parent| {
// Spawn tiles
for i in 0..32 {
let x = i % 8;
let y = i / 8;
@ -184,12 +182,15 @@ fn initialize_board(board: Option<Res<Board>>, mut commands: Commands) {
parent.spawn((tile, index, Tile2d, SpriteSheetBundle { ..default() }));
}
board.pieces().iter().for_each(|(piece, index)| {
// TODO: transform is slightly different, set sprite
// Spawn pieces
board.pieces().iter().for_each(|(index, piece)| {
let side = Board::side(index).expect("Spawn valid side");
parent.spawn((
piece.clone(),
Piece2d,
index.clone(),
side,
SpriteSheetBundle { ..default() },
));
});
@ -203,22 +204,28 @@ fn set_sprite(
&mut TextureAtlasSprite,
&mut Handle<TextureAtlas>,
Option<&Piece>,
Option<&Side>,
Option<&Tile>,
),
Or<(Added<Piece>, Added<Tile2d>)>,
Or<(Added<Piece>, Added<Tile2d>, Changed<Side>)>,
>,
sprite_sheet: Option<Res<SpriteSheet>>,
) {
if let Some(sprite_sheet) = sprite_sheet {
events
.iter_mut()
.for_each(|(mut sprite, mut texture_atlas, piece, tile)| {
.for_each(|(mut sprite, mut texture_atlas, piece, side, tile)| {
info!("Got sprite update event");
*texture_atlas = sprite_sheet.handle.clone();
let s = match piece {
Some(Piece::Queen) => 2,
Some(Piece::Drone) => 3,
Some(Piece::Pawn) => 4,
None => match tile {
let s = match (piece, side) {
(Some(Piece::Queen), Some(Side::A)) => 2,
(Some(Piece::Queen), Some(Side::B)) => 5,
(Some(Piece::Drone), Some(Side::A)) => 3,
(Some(Piece::Drone), Some(Side::B)) => 6,
(Some(Piece::Pawn), Some(Side::A)) => 4,
(Some(Piece::Pawn), Some(Side::B)) => 7,
_ => match tile {
Some(Tile::Dark) => 0,
Some(Tile::Light) => 1,
None => 99,
@ -244,7 +251,7 @@ fn set_transform(
let x = SCALE * 16.0 * ((i.x as f32) - 3.5);
let y = SCALE * 16.0 * ((i.y as f32) - 1.5);
*t = Transform::from_scale(Vec3::splat(SCALE)).with_translation(Vec3::new(x, y, 0.0));
info!("setting position of {:?} to {:?}", i, t);
debug!("setting position of {:?} to {:?}", i, t);
});
}
@ -379,6 +386,7 @@ fn place_piece(
}
Err(game::GameError::NullMove) => game_events.send(game::GameEvent::PlacePiece),
Err(game::GameError::InvalidMove) => warn!("Invalid move!"),
Err(game::GameError::InvalidIndex) => warn!("Invalid index!"),
})
}
@ -394,9 +402,10 @@ fn cancel_place(
(*button, *state) == (MouseButton::Right, ButtonState::Pressed)
})
.for_each(|_| {
let entity = current.single();
commands.entity(entity).remove::<game::Selected>();
writer.send(game::GameEvent::PlacePiece);
if let Ok(entity) = current.get_single() {
commands.entity(entity).remove::<game::Selected>();
writer.send(game::GameEvent::PlacePiece);
}
})
}

@ -10,11 +10,11 @@ impl Plugin for GamePlugin {
.add_systems(Startup, setup_board)
.add_systems(
Update,
update_board::<display2d::Piece2d>.run_if(on_event::<Move>()),
)
.add_systems(
Update,
update_board::<display3d::Piece3d>.run_if(on_event::<Move>()),
(
update_board::<display2d::Piece2d>.run_if(on_event::<Move>()),
update_board::<display3d::Piece3d>.run_if(on_event::<Move>()),
set_side.run_if(on_event::<GameEvent>()), // TODO: correct run_if?
),
)
.add_systems(
PostUpdate,
@ -56,6 +56,7 @@ impl std::fmt::Display for Piece {
pub(crate) enum GameError {
NullMove,
InvalidMove,
InvalidIndex,
}
#[derive(Debug, Event)]
@ -96,6 +97,12 @@ pub(crate) struct BoardIndex {
pub y: usize,
}
#[derive(Debug, Component, PartialEq)]
pub(crate) enum Side {
A,
B,
}
impl Board {
/// Returns the piece at the given location
pub(crate) fn at(&self, BoardIndex { x, y }: &BoardIndex) -> Option<Piece> {
@ -153,6 +160,14 @@ impl Board {
Err(GameError::InvalidMove)
}
}
pub(crate) fn side(&BoardIndex { x, .. }: &BoardIndex) -> Result<Side, GameError> {
match x {
0..=3 => Ok(Side::A),
4..=7 => Ok(Side::B),
_ => Err(GameError::InvalidIndex),
}
}
}
impl std::fmt::Display for Board {
@ -251,7 +266,7 @@ fn debug_board(board: Res<Board>, mut debug_info: ResMut<debug::DebugInfo>) {
/// Update this method to use a diff between the board and the state of the 2d/3d worlds
/// Only update the tiles without a corresponding board piece
fn update_board<T: Component>(
pub(crate) fn update_board<T: Component>(
mut events: EventReader<Move>,
mut pieces: Query<(Entity, &mut BoardIndex), With<T>>,
mut commands: Commands,
@ -269,3 +284,16 @@ fn update_board<T: Component>(
})
})
}
pub(crate) fn set_side(mut events: Query<(&mut Side, &BoardIndex), Changed<BoardIndex>>) {
events
.iter_mut()
.for_each(|(mut side, idx)| match Board::side(idx) {
Ok(s) => {
if *side != s {
*side = s
}
}
Err(e) => warn!("{:?}", e),
});
}

Loading…
Cancel
Save