diff --git a/src/main.rs b/src/main.rs index d5a4886..50cd612 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ use bevy::{ }; use bevy_ecs_tilemap::prelude::*; -use pawns::{make_pawn, pawn_control, pawn_motion}; +use pawns::{make_pawn, pawn_control, pawn_motion, pawn_transform}; use terrain::make_ground_layer; mod pawns; @@ -75,5 +75,6 @@ fn main() { .add_system(mouse_motion) .add_system(pawn_control) .add_system(pawn_motion) + .add_system(pawn_transform) .run(); } diff --git a/src/pawns.rs b/src/pawns.rs index 4951073..040e6d2 100644 --- a/src/pawns.rs +++ b/src/pawns.rs @@ -21,6 +21,13 @@ pub struct PawnDest { pub y: u32, } +#[derive(Component, Default, Clone, Copy, Debug)] +pub struct PawnNextTile { + pub x: u32, + pub y: u32, + pub progress: f32, +} + #[derive(Bundle, Default)] struct PawnBundle { position: PawnPos, @@ -88,10 +95,13 @@ pub fn pawn_control( if let Some(tile_pos) = TilePos::from_world_pos(&tilemap_pos, map_size, map_grid_size, map_type) { - commands.entity(entity).insert(PawnDest { - x: tile_pos.x, - y: tile_pos.y, - }); + commands + .entity(entity) + .insert(PawnDest { + x: tile_pos.x, + y: tile_pos.y, + }) + .remove::(); } } }); @@ -100,31 +110,52 @@ pub fn pawn_control( } } -type ChangedPawns = Or<(Changed, With)>; +type PawnsToPath = (Without, With); pub fn pawn_motion( mut commands: Commands, - mut pawn_q: Query<(Entity, &mut PawnPos, &mut Transform, Option<&PawnDest>), ChangedPawns>, - tilemap_q: Query<&TilemapTileSize>, + mut pawn_q: Query<(Entity, &PawnPos, &PawnDest), PawnsToPath>, ) { - let map_tile_size = tilemap_q.single(); - pawn_q.for_each_mut(|(entity, mut pos, mut transform, dest)| match dest { - Some(d) => { - pos.x = d.x; - pos.y = d.y; - transform.translation = Vec3::from(( - (pos.x as f32) * map_tile_size.x, - (pos.y as f32) * map_tile_size.y, - 1.0, - )); + pawn_q.for_each_mut(|(entity, pos, dest)| { + if pos.x == dest.x && pos.y == dest.y { commands.entity(entity).remove::(); - } - None => { - transform.translation = Vec3::from(( - (pos.x as f32) * map_tile_size.x, - (pos.y as f32) * map_tile_size.y, - 1.0, - )); + } else { + let next_tile = { TilePos::new(dest.x, dest.y) }; + commands.entity(entity).insert(PawnNextTile { + x: next_tile.x, + y: next_tile.y, + progress: 0.0, + }); } }) } + +pub fn pawn_transform( + mut commands: Commands, + mut pawn_q: Query<(Entity, &mut PawnPos, &mut Transform, &mut PawnNextTile)>, + tilemap_q: Query<&TilemapTileSize>, + time: Res