Add the tail

This commit is contained in:
Timothy Warren 2020-11-05 11:09:57 -05:00
parent feb2fc0ce6
commit 676dbf0a96
1 changed files with 27 additions and 4 deletions

View File

@ -43,10 +43,11 @@ impl Direction {
}
}
}
struct SegmentMaterial(Handle<ColorMaterial>);
#[derive(Default)]
struct SnakeSegment {
next_segment: Option<Entity>
next_segment: Option<Entity>,
}
struct SnakeHead {
@ -56,6 +57,17 @@ struct SnakeHead {
struct SnakeMoveTimer(Timer);
struct HeadMaterial(Handle<ColorMaterial>);
fn spawn_segment(commands: &mut Commands, material: Handle<ColorMaterial>, position: Position) {
commands
.spawn(SpriteComponents {
material,
..Default::default()
})
.with(SnakeSegment { next_segment: None })
.with(position)
.with(Size::square(0.65));
}
fn snake_movement(
time: Res<Time>,
keyboard_input: Res<Input<KeyCode>>,
@ -128,16 +140,27 @@ fn setup(mut commands: Commands, mut materials: ResMut<Assets<ColorMaterial>>) {
commands.insert_resource(HeadMaterial(
materials.add(Color::rgb(0.7, 0.7, 0.7).into()),
));
commands.insert_resource(SegmentMaterial(
materials.add(Color::rgb(0.3, 0.3, 0.3).into()),
));
}
fn game_setup(mut commands: Commands, head_material: Res<HeadMaterial>) {
fn game_setup(
mut commands: Commands,
head_material: Res<HeadMaterial>,
segment_material: Res<SegmentMaterial>,
) {
spawn_segment(&mut commands, segment_material.0, Position { x: 10, y: 9 });
let first_segment = commands.current_entity().unwrap();
commands
.spawn(SpriteComponents {
material: head_material.0,
sprite: Sprite::new(Vec2::new(10.0, 10.0)),
..Default::default()
})
.with(SnakeHead { direction: Direction::Up })
.with(SnakeHead {
direction: Direction::Up,
next_segment: first_segment,
})
.with(Position { x: 10, y: 10 })
.with(Size::square(0.8));
}