diff --git a/src/game.rs b/src/game.rs index 578005f..cfbfa9a 100644 --- a/src/game.rs +++ b/src/game.rs @@ -93,7 +93,6 @@ impl Plugin for GamePlugin { ) .add_systems(OnExit(GameState::Game), despawn_screen::) .insert_resource(DiplopodSegments::default()) - .insert_resource(LastTailPosition::default()) .insert_resource(LastSpecialSpawn::default()) .insert_resource(ImmunityTime::default()) .insert_resource(FreePositions::new(CONSUMABLE_WIDTH, CONSUMABLE_HEIGHT)) diff --git a/src/game/resources.rs b/src/game/resources.rs index 4332d2c..3c77fc0 100644 --- a/src/game/resources.rs +++ b/src/game/resources.rs @@ -2,7 +2,7 @@ use bevy::prelude::*; use rand::seq::SliceRandom; use rand::thread_rng; -use crate::game::components::{DiplopodPosition, Position}; +use crate::game::components::Position; #[derive(Default, Resource)] pub struct DiplopodSegments(pub Vec); @@ -57,9 +57,6 @@ impl FreePositions { } } -#[derive(Default, Resource)] -pub struct LastTailPosition(pub Option); - #[derive(Default, Resource)] pub struct LastSpecialSpawn(pub u32); diff --git a/src/game/systems/control.rs b/src/game/systems/control.rs index ade52a8..af88f55 100644 --- a/src/game/systems/control.rs +++ b/src/game/systems/control.rs @@ -15,9 +15,7 @@ use super::graphics::diplopod_position2translation; use super::graphics::position2translation; use super::graphics::TILE_SIZE; -struct SpawnDiplopodSegment { - position: DiplopodPosition, -} +struct SpawnDiplopodSegment; impl Command for SpawnDiplopodSegment { fn apply(self, world: &mut World) { @@ -27,7 +25,20 @@ impl Command for SpawnDiplopodSegment { radii: None, }; - let is_head = world.resource::().0.is_empty(); + let segments = &world.resource::().0; + let is_head = segments.is_empty(); + + let position = if is_head { + DiplopodPosition { + x: ARENA_WIDTH / 2, + y: ARENA_HEIGHT / 2, + } + } else { + *world + .get::(*segments.last().unwrap()) + .unwrap() + }; + let color = if world.resource::().0 > 0 { ANTIDOTE_COLOR } else { @@ -37,15 +48,13 @@ impl Command for SpawnDiplopodSegment { let mut segment = world.spawn(( ShapeBundle { path: GeometryBuilder::build_as(&shape), - transform: Transform::from_translation(diplopod_position2translation( - &self.position, - )), + transform: Transform::from_translation(diplopod_position2translation(&position)), ..default() }, Fill::color(color), Stroke::color(color), DiplopodSegment, - self.position, + position, OnGameScreen, )); @@ -58,12 +67,7 @@ impl Command for SpawnDiplopodSegment { } pub fn init_diplopod(mut commands: Commands) { - commands.queue(SpawnDiplopodSegment { - position: DiplopodPosition { - x: ARENA_WIDTH / 2, - y: ARENA_HEIGHT / 2, - }, - }); + commands.queue(SpawnDiplopodSegment); } pub fn init_wall(mut commands: Commands, mut free_positions: ResMut) { @@ -374,7 +378,6 @@ pub fn movement( mut heads: Query<(Entity, &DiplopodHead)>, mut positions: Query<&mut DiplopodPosition>, segments: ResMut, - mut last_tail_position: ResMut, mut game_over_writer: EventWriter, ) { if let Some((head_entity, head)) = heads.iter_mut().next() { @@ -400,8 +403,6 @@ pub fn movement( .for_each(|(pos, segment)| { *positions.get_mut(*segment).unwrap() = *pos; }); - - last_tail_position.0 = Some(*segment_positions.last().unwrap()); } } @@ -508,16 +509,10 @@ pub fn eat( } } -pub fn growth( - mut commands: Commands, - last_tail_position: Res, - mut growth_reader: EventReader, -) { +pub fn growth(mut commands: Commands, mut growth_reader: EventReader) { if let Some(growth) = growth_reader.read().next() { for _ in 0..growth.0 { - commands.queue(SpawnDiplopodSegment { - position: last_tail_position.0.unwrap(), - }); + commands.queue(SpawnDiplopodSegment); } } }