Skip to content

Commit

Permalink
fix todo: decouple spawning from eating
Browse files Browse the repository at this point in the history
Signed-off-by: Agustín Ramiro Díaz <agustin.ramiro.diaz@gmail.com>
  • Loading branch information
AgustinRamiroDiaz committed Jan 7, 2024
1 parent d35d5a7 commit de6f0a5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
34 changes: 17 additions & 17 deletions src/apple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@ use super::{
coordinate::Coordinate,
game_state::AppState,
schedule::InGameSet,
snake::{Depth, MyColor, Snake, SnakeSegment},
snake::{Depth, Snake},
HALF_LEN,
};

pub(crate) struct ApplePlugin;

impl Plugin for ApplePlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, setup).add_systems(
Update,
eat_apple
.in_set(InGameSet::SpawnDespawnEntities)
.run_if(in_state(AppState::InGame)),
);
app.add_event::<AppleEaten>()
.add_systems(Startup, setup)
.add_systems(
Update,
eat_apple
.in_set(InGameSet::SpawnDespawnEntities)
.run_if(in_state(AppState::InGame)),
);
}
}

Expand All @@ -48,31 +50,29 @@ fn spawn_apple(commands: &mut Commands, assets: &Res<SceneAssets>) {
#[derive(Component)]
pub(crate) struct Apple;

#[derive(Event)]
pub(crate) struct AppleEaten(pub(crate) Entity);

// TODO: decouple this logic into smaller units
// TODO: decouple spawning from eating
fn eat_apple(
mut commands: Commands,
mut snakes: Query<(&mut Snake, &MyColor)>,
mut snakes: Query<(Entity, &mut Snake)>,
coordinates: Query<&Coordinate>,
apples: Query<(Entity, &Coordinate), With<Apple>>,
assets: Res<SceneAssets>,
mut apple_eaten: EventWriter<AppleEaten>,
) {
let get_head = |snake: &Snake| {
let &head = snake.segments.front()?;
coordinates.get(head).ok()
};

for (mut snake, &color) in snakes.iter_mut() {
for (entity, snake) in snakes.iter_mut() {
for (apple, coord) in apples.iter() {
if coord == get_head(&snake).unwrap() {
if Some(coord) == get_head(&snake) {
commands.entity(apple).despawn();
spawn_apple(&mut commands, &assets);

let tail = commands
.spawn((color, SnakeSegment, snake.trail.clone(), Tile))
.id();

snake.segments.push_back(tail);
apple_eaten.send(AppleEaten(entity));
return;
}
}
Expand Down
24 changes: 20 additions & 4 deletions src/snake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::VecDeque;
use bevy::prelude::*;

use crate::{
coordinate::Coordinate, direction::Direction, game_state::AppState,
apple::AppleEaten, coordinate::Coordinate, direction::Direction, game_state::AppState,
main_menu::NumberOfPlayersSelected, schedule::InGameSet, BOARD_VIEWPORT_IN_WORLD_UNITS,
HALF_LEN, SIZE,
};
Expand All @@ -18,10 +18,11 @@ impl Plugin for SnakePlugin {
.add_systems(
Update,
(
grow_snake,
apply_deferred,
toroid_coordinates,
add_sprite_bundles,
// This is needed in order to render the sprites correctly, we need to flush the sprites into the world and then update their transforms
apply_deferred,
apply_deferred, // This is needed in order to render the sprites correctly, we need to flush the sprites into the world and then update their transforms
set_sprite_size,
update_local_coordinates_to_world_transforms,
)
Expand Down Expand Up @@ -158,6 +159,22 @@ pub(crate) struct Id(pub(crate) u8);
#[derive(Component)]
pub(crate) struct SnakeSegment;

fn grow_snake(
mut commands: Commands,
mut query: Query<(&mut Snake, &MyColor)>,
mut apple_eaten: EventReader<AppleEaten>,
) {
for AppleEaten(entity) in apple_eaten.read() {
if let Ok((mut snake, &color)) = query.get_mut(*entity) {
let tail = commands
.spawn((color, SnakeSegment, snake.trail.clone(), Tile))
.id();

snake.segments.push_back(tail);
}
}
}

fn toroid_coordinates(
mut query: Query<&mut Coordinate, (With<SnakeSegment>, Changed<Coordinate>)>,
) {
Expand Down Expand Up @@ -198,7 +215,6 @@ fn add_sprite_bundles(
for (entity, color) in query.iter() {
commands.entity(entity).insert(SpriteBundle {
sprite: Sprite {
// custom_size: Some(Vec2 { x: SIZE, y: SIZE }),
color: color.0,
..Default::default()
},
Expand Down

0 comments on commit de6f0a5

Please sign in to comment.