1
1
//! A simple example of a Flappy Bird game using surreal and hecs.
2
2
3
- use hecs:: { EntityBuilder , World } ;
3
+ use hecs:: World ;
4
4
use surreal:: {
5
5
backends:: sdl:: { Window , WindowSettings } ,
6
6
common:: { DeltaClock , StringName } ,
@@ -18,9 +18,11 @@ fn main() {
18
18
. unwrap ( ) ;
19
19
20
20
let graphics = GraphicsEngine :: opengl ( & window) ;
21
+ let mut queue = RenderQueue :: new ( ) ;
21
22
22
23
let mut game = Game :: new ( ) ;
23
- let mut queue = RenderQueue :: new ( ) ;
24
+
25
+ game. initialize ( ) ;
24
26
25
27
while window. update ( ) {
26
28
game. update ( ) ;
@@ -35,7 +37,6 @@ fn main() {
35
37
struct Game {
36
38
world : World ,
37
39
clock : DeltaClock ,
38
- sprite_cache : SpriteCache ,
39
40
}
40
41
41
42
struct Position {
@@ -53,46 +54,72 @@ struct Sprite {
53
54
tint : Color32 ,
54
55
}
55
56
56
- struct SpriteCache { }
57
+ struct Player { }
58
+
59
+ struct Obstacle { }
57
60
58
61
impl Game {
59
62
pub fn new ( ) -> Self {
60
- let mut world = World :: new ( ) ;
61
-
62
- // create entities
63
- for _ in 0 ..10 {
64
- let mut builder = EntityBuilder :: default ( ) ;
65
-
66
- builder. add ( Position { x : 0.0 , y : 0.0 } ) ;
67
- builder. add ( Velocity { x : 1.0 , y : 1.0 } ) ;
68
- builder. add ( Sprite {
69
- texture : "bird" . into ( ) ,
70
- tint : Color32 :: WHITE ,
71
- } ) ;
72
-
73
- world. spawn ( builder. build ( ) ) ;
74
- }
75
-
76
63
Self {
77
- world,
64
+ world : World :: new ( ) ,
78
65
clock : DeltaClock :: new ( ) ,
79
- sprite_cache : SpriteCache { } ,
80
66
}
81
67
}
82
68
69
+ pub fn initialize ( & mut self ) {
70
+ // spawn the player
71
+ self . world . spawn ( (
72
+ Position { x : 0.0 , y : 0.0 } ,
73
+ Velocity { x : 0.0 , y : 0.0 } ,
74
+ Sprite {
75
+ texture : "sprites/player.png" . into ( ) ,
76
+ tint : Color32 :: WHITE ,
77
+ } ,
78
+ Player { } ,
79
+ ) ) ;
80
+
81
+ // spawn the first obstacle
82
+ self . world . spawn ( (
83
+ Position { x : 0.0 , y : 0.0 } ,
84
+ Sprite {
85
+ texture : "sprites/obstacle.png" . into ( ) ,
86
+ tint : Color32 :: WHITE ,
87
+ } ,
88
+ Obstacle { } ,
89
+ ) ) ;
90
+ }
91
+
83
92
pub fn update ( & mut self ) {
84
93
let delta_time = self . clock . tick ( ) ;
85
94
86
- // update positions
95
+ self . update_physics ( delta_time) ;
96
+ self . update_player ( ) ;
97
+ }
98
+
99
+ fn update_physics ( & mut self , delta_time : f32 ) {
100
+ const GRAVITY : f32 = 9.8 ;
101
+
87
102
for ( _, ( position, velocity) ) in self . world . query_mut :: < ( & mut Position , & mut Velocity ) > ( ) {
88
103
position. x += velocity. x * delta_time;
89
104
position. y += velocity. y * delta_time;
105
+
106
+ velocity. y += GRAVITY * delta_time;
107
+ }
108
+ }
109
+
110
+ fn update_player ( & mut self ) {
111
+ for ( _, ( _velocity, _player) ) in self . world . query_mut :: < ( & mut Velocity , & Player ) > ( ) {
112
+ // TODO: player input
90
113
}
91
114
}
92
115
93
116
pub fn draw ( & self , queue : & mut RenderQueue ) {
94
117
queue. clear_color_buffer ( Color :: BLACK ) ;
95
118
119
+ self . draw_sprites ( queue) ;
120
+ }
121
+
122
+ fn draw_sprites ( & self , _queue : & mut RenderQueue ) {
96
123
for ( _, ( _position, _sprite) ) in & mut self . world . query :: < ( & Position , & Sprite ) > ( ) {
97
124
// queue
98
125
}
0 commit comments