Skip to content

Commit

Permalink
Disconnected tail
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusbuffett committed Dec 23, 2020
1 parent 7a78670 commit 2d625c9
Showing 1 changed file with 46 additions and 8 deletions.
54 changes: 46 additions & 8 deletions src/main.rs
Expand Up @@ -30,11 +30,16 @@ 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);
Expand Down Expand Up @@ -67,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(
Expand Down Expand Up @@ -190,6 +227,7 @@ fn main() {
Duration::from_millis(150. as u64),
true,
)))
.add_resource(SnakeSegments::default())
.add_startup_system(setup.system())
.add_startup_stage("game_setup", SystemStage::single(spawn_snake.system()))
.add_system(snake_timer.system())
Expand Down

0 comments on commit 2d625c9

Please sign in to comment.