@@ -29,11 +29,32 @@ impl Size {
2929}
3030
3131#[ derive( Component ) ]
32- struct SnakeHead ;
32+ struct SnakeHead {
33+ direction : Direction ,
34+ }
3335
3436#[ derive( Component ) ]
3537struct Food ;
3638
39+ #[ derive( PartialEq , Copy , Clone ) ]
40+ enum Direction {
41+ Left ,
42+ Up ,
43+ Right ,
44+ Down ,
45+ }
46+
47+ impl Direction {
48+ fn opposite ( self ) -> Self {
49+ match self {
50+ Self :: Left => Self :: Right ,
51+ Self :: Right => Self :: Left ,
52+ Self :: Up => Self :: Down ,
53+ Self :: Down => Self :: Up ,
54+ }
55+ }
56+ }
57+
3758fn setup_camera ( mut commands : Commands ) {
3859 commands. spawn_bundle ( OrthographicCameraBundle :: new_2d ( ) ) ;
3960}
@@ -51,31 +72,51 @@ fn spawn_snake(mut commands: Commands) {
5172 } ,
5273 ..default ( )
5374 } )
54- . insert ( SnakeHead )
75+ . insert ( SnakeHead {
76+ direction : Direction :: Up ,
77+ } )
5578 . insert ( Position { x : 3 , y : 3 } )
5679 . insert ( Size :: square ( 0.8 ) ) ;
5780}
5881
59- fn snake_movement (
60- keyboard_input : Res < Input < KeyCode > > ,
61- mut head_positions : Query < & mut Position , With < SnakeHead > > ,
62- ) {
63- for mut pos in head_positions. iter_mut ( ) {
64- if keyboard_input. pressed ( KeyCode :: Left ) {
65- pos. x -= 1 ;
66- }
67- if keyboard_input. pressed ( KeyCode :: Right ) {
68- pos. x += 1 ;
69- }
70- if keyboard_input. pressed ( KeyCode :: Down ) {
71- pos. y -= 1 ;
72- }
73- if keyboard_input. pressed ( KeyCode :: Up ) {
74- pos. y += 1 ;
82+ fn snake_movement_input ( keyboard_input : Res < Input < KeyCode > > , mut heads : Query < & mut SnakeHead > ) {
83+ if let Some ( mut head) = heads. iter_mut ( ) . next ( ) {
84+ let dir: Direction = if keyboard_input. pressed ( KeyCode :: Left ) {
85+ Direction :: Left
86+ } else if keyboard_input. pressed ( KeyCode :: Down ) {
87+ Direction :: Down
88+ } else if keyboard_input. pressed ( KeyCode :: Up ) {
89+ Direction :: Up
90+ } else if keyboard_input. pressed ( KeyCode :: Right ) {
91+ Direction :: Right
92+ } else {
93+ head. direction
94+ } ;
95+ if dir != head. direction . opposite ( ) {
96+ head. direction = dir;
7597 }
7698 }
7799}
78100
101+ fn snake_movement ( mut heads : Query < ( & mut Position , & SnakeHead ) > ) {
102+ if let Some ( ( mut head_pos, head) ) = heads. iter_mut ( ) . next ( ) {
103+ match & head. direction {
104+ Direction :: Left => {
105+ head_pos. x -= 1 ;
106+ }
107+ Direction :: Right => {
108+ head_pos. x += 1 ;
109+ }
110+ Direction :: Up => {
111+ head_pos. y += 1 ;
112+ }
113+ Direction :: Down => {
114+ head_pos. y -= 1 ;
115+ }
116+ } ;
117+ }
118+ }
119+
79120fn size_scaling ( windows : Res < Windows > , mut q : Query < ( & Size , & mut Transform ) > ) {
80121 let window = windows. get_primary ( ) . unwrap ( ) ;
81122 for ( sprite_size, mut transform) in q. iter_mut ( ) {
@@ -130,7 +171,12 @@ fn main() {
130171 } )
131172 . add_startup_system ( setup_camera)
132173 . add_startup_system ( spawn_snake)
133- . add_system ( snake_movement)
174+ . add_system ( snake_movement_input. before ( snake_movement) )
175+ . add_system_set (
176+ SystemSet :: new ( )
177+ . with_run_criteria ( FixedTimestep :: step ( 0.150 ) )
178+ . with_system ( snake_movement) ,
179+ )
134180 . add_system_set (
135181 SystemSet :: new ( )
136182 . with_run_criteria ( FixedTimestep :: step ( 1.0 ) )
0 commit comments