Do you wish to register an account?
Browse Source

Adding a tail

master
Timothy Warren 2 months ago
parent
commit
529fe3f2b1
1 changed files with 48 additions and 12 deletions
  1. +48
    -12
      src/main.rs

+ 48
- 12
src/main.rs View File

@ -30,11 +30,17 @@ struct SnakeHead {
}
struct Materials {
head_material: Handle<ColorMaterial>,
segment_material: Handle<ColorMaterial>,
food_material: Handle<ColorMaterial>,
}
struct SnakeMoveTimer(Timer);
struct SnakeSegment;
#[derive(Default)]
struct SnakeSegments(Vec<Entity>);
struct Food;
struct FoodSpawnTimer(Timer);
@ -66,22 +72,54 @@ fn setup(commands: &mut Commands, mut materials: ResMut<Assets<ColorMaterial>>)
commands.spawn(Camera2dBundle::default());
commands.insert_resource(Materials {
head_material: materials.add(Color::rgb(0.7, 0.7, 0.7).into()),
segment_material: materials.add(Color::rgb(0.3, 0.3, 0.3).into()),
food_material: materials.add(Color::rgb(1.0, 0.0, 1.0).into()),
});
}
fn spawn_snake(commands: &mut Commands, materials: Res<Materials>) {
fn spawn_snake(
commands: &mut Commands,
materials: Res<Materials>,
mut segments: ResMut<SnakeSegments>,
) {
segments.0 = vec![
commands
.spawn(SpriteBundle {
material: materials.head_material.clone(),
sprite: Sprite::new(Vec2::new(10.0, 10.0)),
..Default::default()
})
.with(SnakeHead {
direction: Direction::Up,
})
.with(SnakeSegment)
.with(Position { x: 3, y: 3 })
.with(Size::square(0.8))
.current_entity()
.unwrap(),
spawn_segment(
commands,
&materials.segment_material,
Position { x: 3, y: 2 },
),
];
}
fn spawn_segment(
commands: &mut Commands,
material: &Handle<ColorMaterial>,
position: Position,
) -> Entity {
commands
.spawn(SpriteBundle {
material: materials.head_material.clone(),
sprite: Sprite::new(Vec2::new(10.0, 10.0)),
material: material.clone(),
..Default::default()
})
.with(SnakeHead {
direction: Direction::Up,
})
.with(Position { x: 3, y: 3 })
.with(Size::square(0.8));
.with(SnakeSegment)
.with(position)
.with(Size::square(0.65))
.current_entity()
.unwrap()
}
fn snake_movement(
@ -186,10 +224,8 @@ fn main() {
height: 500.0,
..Default::default()
})
.add_resource(SnakeMoveTimer(Timer::new(
Duration::from_millis(150),
true,
)))
.add_resource(SnakeMoveTimer(Timer::new(Duration::from_millis(150), true)))
.add_resource(SnakeSegments::default())
.add_startup_system(setup.system())
.add_startup_stage("game_setup", SystemStage::single(spawn_snake.system()))
.add_system(snake_movement.system())


Loading…
Cancel
Save