Skip to content

Commit

Permalink
fix todo
Browse files Browse the repository at this point in the history
Signed-off-by: Agustín Ramiro Díaz <[email protected]>
  • Loading branch information
AgustinRamiroDiaz committed Jan 7, 2024
1 parent 9d1cafd commit b64a699
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
13 changes: 5 additions & 8 deletions src/apple.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use bevy::prelude::*;
use rand::Rng;

use crate::snake::Tile;

use super::{
asset_loader::SceneAssets,
coordinate::Coordinate,
game_state::AppState,
schedule::InGameSet,
snake::{Depth, MyColor, Snake, SnakeSegment},
HALF_LEN, SIZE,
HALF_LEN,
};

pub(crate) struct ApplePlugin;
Expand Down Expand Up @@ -36,15 +38,10 @@ fn spawn_apple(commands: &mut Commands, assets: &Res<SceneAssets>) {
rand::thread_rng().gen_range(-HALF_LEN..HALF_LEN) as f32,
)),
SpriteBundle {
// TODO: remove the need to specify size here
// This should be handled by `add_sprite_bundles`
sprite: Sprite {
custom_size: Some(Vec2 { x: SIZE, y: SIZE }),
..Default::default()
},
texture: assets.apple.clone(),
..default()
},
Tile,
));
}

Expand Down Expand Up @@ -72,7 +69,7 @@ fn eat_apple(
spawn_apple(&mut commands, &assets);

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

snake.segments.push_back(tail);
Expand Down
24 changes: 20 additions & 4 deletions src/snake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use crate::{

pub(crate) struct SnakePlugin;

const TILE_SIZE: f32 = 1.1;

impl Plugin for SnakePlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, setup)
Expand All @@ -20,6 +22,7 @@ impl Plugin for SnakePlugin {
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,
set_sprite_size,
update_local_coordinates_to_world_transforms,
)
.chain()
Expand Down Expand Up @@ -72,7 +75,7 @@ fn spawn_snakes(mut commands: Commands, number_of_players: Res<NumberOfPlayersSe
let mut spawn_snake =
|id, spawn_coord: Coordinate, direction: Direction, color: MyColor, name: String| {
let head_a = commands
.spawn((color, SnakeSegment, spawn_coord.clone()))
.spawn((color, SnakeSegment, spawn_coord.clone(), Tile))
.id();

commands.spawn((
Expand All @@ -86,6 +89,7 @@ fn spawn_snakes(mut commands: Commands, number_of_players: Res<NumberOfPlayersSe
name,
},
color,
Tile,
));
};

Expand Down Expand Up @@ -173,13 +177,13 @@ pub(crate) struct Depth(pub(crate) f32);

// TODO: we assume that Transform == SpriteBundle
fn add_sprite_bundles(
mut query: Query<(Entity, &MyColor), (Changed<Coordinate>, Without<Transform>)>,
query: Query<(Entity, &MyColor), (Changed<Coordinate>, Without<Transform>)>,
mut commands: Commands,
) {
for (entity, color) in query.iter_mut() {
for (entity, color) in query.iter() {
commands.entity(entity).insert(SpriteBundle {
sprite: Sprite {
custom_size: Some(Vec2 { x: SIZE, y: SIZE }),
// custom_size: Some(Vec2 { x: SIZE, y: SIZE }),
color: color.0,
..Default::default()
},
Expand All @@ -189,6 +193,18 @@ fn add_sprite_bundles(
}
}

#[derive(Component)]
pub(crate) struct Tile;

fn set_sprite_size(mut query: Query<&mut Sprite, Added<Tile>>) {
for mut sprite in query.iter_mut() {
sprite.custom_size = Some(Vec2 {
x: TILE_SIZE,
y: TILE_SIZE,
});
}
}

fn toroid_coordinates(
mut query: Query<&mut Coordinate, (With<SnakeSegment>, Changed<Coordinate>)>,
) {
Expand Down

0 comments on commit b64a699

Please sign in to comment.