Skip to content

Commit 2524d89

Browse files
committed
Disconnected tail
1 parent 238fe0b commit 2524d89

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

src/main.rs

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rand::prelude::random;
44

55
const SNAKE_HEAD_COLOR: Color = Color::rgb(0.7, 0.7, 0.7);
66
const FOOD_COLOR: Color = Color::rgb(1.0, 0.0, 1.0);
7+
const SNAKE_SEGMENT_COLOR: Color = Color::rgb(0.3, 0.3, 0.3);
78

89
const ARENA_HEIGHT: u32 = 10;
910
const ARENA_WIDTH: u32 = 10;
@@ -33,6 +34,12 @@ struct SnakeHead {
3334
direction: Direction,
3435
}
3536

37+
#[derive(Component)]
38+
struct SnakeSegment;
39+
40+
#[derive(Default, Deref, DerefMut)]
41+
struct SnakeSegments(Vec<Entity>);
42+
3643
#[derive(Component)]
3744
struct Food;
3845

@@ -59,24 +66,40 @@ fn setup_camera(mut commands: Commands) {
5966
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
6067
}
6168

62-
fn spawn_snake(mut commands: Commands) {
69+
fn spawn_snake(mut commands: Commands, mut segments: ResMut<SnakeSegments>) {
70+
*segments = SnakeSegments(vec![
71+
commands
72+
.spawn_bundle(SpriteBundle {
73+
sprite: Sprite {
74+
color: SNAKE_HEAD_COLOR,
75+
..default()
76+
},
77+
..default()
78+
})
79+
.insert(SnakeHead {
80+
direction: Direction::Up,
81+
})
82+
.insert(SnakeSegment)
83+
.insert(Position { x: 3, y: 3 })
84+
.insert(Size::square(0.8))
85+
.id(),
86+
spawn_segment(commands, Position { x: 3, y: 2 }),
87+
]);
88+
}
89+
90+
fn spawn_segment(mut commands: Commands, position: Position) -> Entity {
6391
commands
6492
.spawn_bundle(SpriteBundle {
6593
sprite: Sprite {
66-
color: SNAKE_HEAD_COLOR,
67-
..default()
68-
},
69-
transform: Transform {
70-
scale: Vec3::new(10.0, 10.0, 10.0),
94+
color: SNAKE_SEGMENT_COLOR,
7195
..default()
7296
},
7397
..default()
7498
})
75-
.insert(SnakeHead {
76-
direction: Direction::Up,
77-
})
78-
.insert(Position { x: 3, y: 3 })
79-
.insert(Size::square(0.8));
99+
.insert(SnakeSegment)
100+
.insert(position)
101+
.insert(Size::square(0.65))
102+
.id()
80103
}
81104

82105
fn snake_movement_input(keyboard_input: Res<Input<KeyCode>>, mut heads: Query<&mut SnakeHead>) {
@@ -177,6 +200,7 @@ fn main() {
177200
.with_run_criteria(FixedTimestep::step(0.150))
178201
.with_system(snake_movement),
179202
)
203+
.insert_resource(SnakeSegments::default())
180204
.add_system_set(
181205
SystemSet::new()
182206
.with_run_criteria(FixedTimestep::step(1.0))

0 commit comments

Comments
 (0)