From b64a6998a341f169dbb3ee60b515dd755a9f354f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Agust=C3=ADn=20Ramiro=20D=C3=ADaz?= Date: Sun, 7 Jan 2024 13:58:48 -0300 Subject: [PATCH] fix todo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Agustín Ramiro Díaz --- src/apple.rs | 13 +++++-------- src/snake.rs | 24 ++++++++++++++++++++---- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/apple.rs b/src/apple.rs index f350e20..4bf5016 100644 --- a/src/apple.rs +++ b/src/apple.rs @@ -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; @@ -36,15 +38,10 @@ fn spawn_apple(commands: &mut Commands, assets: &Res) { 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, )); } @@ -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); diff --git a/src/snake.rs b/src/snake.rs index 15d3b60..22f895a 100644 --- a/src/snake.rs +++ b/src/snake.rs @@ -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) @@ -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() @@ -72,7 +75,7 @@ fn spawn_snakes(mut commands: Commands, number_of_players: Res, Without)>, + query: Query<(Entity, &MyColor), (Changed, Without)>, 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() }, @@ -189,6 +193,18 @@ fn add_sprite_bundles( } } +#[derive(Component)] +pub(crate) struct Tile; + +fn set_sprite_size(mut query: Query<&mut Sprite, Added>) { + 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, Changed)>, ) {