Skip to content

Commit

Permalink
Use component hook to fill list of Diplopod segments
Browse files Browse the repository at this point in the history
  • Loading branch information
tehlers committed Jan 25, 2025
1 parent dead164 commit 51c2252
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 44 deletions.
12 changes: 11 additions & 1 deletion src/game/components.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
use bevy::prelude::*;
use bevy::{
ecs::{component::ComponentId, world::DeferredWorld},
prelude::*,
};

use crate::prelude::CONSUMABLE_SCALE_FACTOR;

use super::DiplopodSegments;

#[derive(Component)]
pub struct DiplopodHead {
pub direction: Vec2,
}

#[derive(Component)]
#[component(on_add=on_add_diplopod_segment)]
pub struct DiplopodSegment;

fn on_add_diplopod_segment(mut world: DeferredWorld, entity: Entity, _: ComponentId) {
world.resource_mut::<DiplopodSegments>().0.push(entity);
}

#[derive(Component, Default, Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub struct DiplopodPosition {
pub x: i32,
Expand Down
80 changes: 37 additions & 43 deletions src/game/systems/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ use super::graphics::diplopod_position2translation;
use super::graphics::position2translation;
use super::graphics::TILE_SIZE;

pub fn init_diplopod(mut commands: Commands, mut segments: ResMut<DiplopodSegments>) {
spawn_diplopod(&mut commands, &mut segments);
pub fn init_diplopod(mut commands: Commands) {
spawn_diplopod(&mut commands);
}

fn spawn_diplopod(commands: &mut Commands, segments: &mut ResMut<DiplopodSegments>) {
fn spawn_diplopod(commands: &mut Commands) {
let shape = shapes::Rectangle {
extents: Vec2::splat(TILE_SIZE),
origin: shapes::RectangleOrigin::Center,
Expand All @@ -31,45 +31,41 @@ fn spawn_diplopod(commands: &mut Commands, segments: &mut ResMut<DiplopodSegment
y: ARENA_HEIGHT / 2,
};

segments.0 = vec![commands
.spawn((
ShapeBundle {
path: GeometryBuilder::build_as(&shape),
transform: Transform::from_translation(diplopod_position2translation(&position)),
..default()
},
Fill::color(DIPLOPOD_COLOR),
Stroke::color(DIPLOPOD_COLOR),
DiplopodHead {
direction: Vec2::ZERO,
},
position,
DiplopodSegment,
OnGameScreen,
))
.id()];
commands.spawn((
ShapeBundle {
path: GeometryBuilder::build_as(&shape),
transform: Transform::from_translation(diplopod_position2translation(&position)),
..default()
},
Fill::color(DIPLOPOD_COLOR),
Stroke::color(DIPLOPOD_COLOR),
DiplopodHead {
direction: Vec2::ZERO,
},
position,
DiplopodSegment,
OnGameScreen,
));
}

fn spawn_segment(
commands: &mut Commands,
color: Color,
position: DiplopodPosition,
shape: &Rectangle,
) -> Entity {
commands
.spawn((
ShapeBundle {
path: GeometryBuilder::build_as(shape),
transform: Transform::from_translation(diplopod_position2translation(&position)),
..default()
},
Fill::color(color),
Stroke::color(color),
DiplopodSegment,
position,
OnGameScreen,
))
.id()
) {
commands.spawn((
ShapeBundle {
path: GeometryBuilder::build_as(shape),
transform: Transform::from_translation(diplopod_position2translation(&position)),
..default()
},
Fill::color(color),
Stroke::color(color),
DiplopodSegment,
position,
OnGameScreen,
));
}

pub fn init_wall(mut commands: Commands, mut free_positions: ResMut<FreePositions>) {
Expand Down Expand Up @@ -517,7 +513,6 @@ pub fn eat(
pub fn growth(
mut commands: Commands,
last_tail_position: Res<LastTailPosition>,
mut segments: ResMut<DiplopodSegments>,
mut growth_reader: EventReader<Growth>,
immunity_time: Res<ImmunityTime>,
) {
Expand All @@ -529,7 +524,7 @@ pub fn growth(

if let Some(growth) = growth_reader.read().next() {
for _ in 0..growth.0 {
segments.0.push(spawn_segment(
spawn_segment(
&mut commands,
if immunity_time.0 > 0 {
ANTIDOTE_COLOR
Expand All @@ -538,7 +533,7 @@ pub fn growth(
},
last_tail_position.0.unwrap(),
&shape,
));
);
}
}
}
Expand Down Expand Up @@ -603,7 +598,7 @@ pub fn control_antidote_sound(
pub fn game_over(
mut commands: Commands,
mut reader: EventReader<GameOver>,
segments: Query<Entity, With<DiplopodSegment>>,
mut segments: ResMut<DiplopodSegments>,
mut free_positions: ResMut<FreePositions>,
mut last_special_spawn: ResMut<LastSpecialSpawn>,
mut immunity_time: ResMut<ImmunityTime>,
Expand All @@ -618,10 +613,7 @@ pub fn game_over(
PlaybackSettings::DESPAWN,
));

lastscore.0 = 0;
for _ in segments.iter() {
lastscore.0 += 1;
}
lastscore.0 = segments.0.len() as u16;

if lastscore.0 > highscore.0 {
highscore.0 = lastscore.0;
Expand All @@ -632,6 +624,8 @@ pub fn game_over(
last_special_spawn.0 = 0;
immunity_time.0 = 0;

segments.0 = Vec::new();

game_state.set(GameState::Highscore);
}
}

0 comments on commit 51c2252

Please sign in to comment.