Skip to content

Commit 757307a

Browse files
committed
Clean up
1 parent b23bd90 commit 757307a

File tree

1 file changed

+50
-23
lines changed

1 file changed

+50
-23
lines changed

samples/flappy-bird/src/main.rs

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! A simple example of a Flappy Bird game using surreal and hecs.
22
3-
use hecs::{EntityBuilder, World};
3+
use hecs::World;
44
use surreal::{
55
backends::sdl::{Window, WindowSettings},
66
common::{DeltaClock, StringName},
@@ -18,9 +18,11 @@ fn main() {
1818
.unwrap();
1919

2020
let graphics = GraphicsEngine::opengl(&window);
21+
let mut queue = RenderQueue::new();
2122

2223
let mut game = Game::new();
23-
let mut queue = RenderQueue::new();
24+
25+
game.initialize();
2426

2527
while window.update() {
2628
game.update();
@@ -35,7 +37,6 @@ fn main() {
3537
struct Game {
3638
world: World,
3739
clock: DeltaClock,
38-
sprite_cache: SpriteCache,
3940
}
4041

4142
struct Position {
@@ -53,46 +54,72 @@ struct Sprite {
5354
tint: Color32,
5455
}
5556

56-
struct SpriteCache {}
57+
struct Player {}
58+
59+
struct Obstacle {}
5760

5861
impl Game {
5962
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-
7663
Self {
77-
world,
64+
world: World::new(),
7865
clock: DeltaClock::new(),
79-
sprite_cache: SpriteCache {},
8066
}
8167
}
8268

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+
8392
pub fn update(&mut self) {
8493
let delta_time = self.clock.tick();
8594

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+
87102
for (_, (position, velocity)) in self.world.query_mut::<(&mut Position, &mut Velocity)>() {
88103
position.x += velocity.x * delta_time;
89104
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
90113
}
91114
}
92115

93116
pub fn draw(&self, queue: &mut RenderQueue) {
94117
queue.clear_color_buffer(Color::BLACK);
95118

119+
self.draw_sprites(queue);
120+
}
121+
122+
fn draw_sprites(&self, _queue: &mut RenderQueue) {
96123
for (_, (_position, _sprite)) in &mut self.world.query::<(&Position, &Sprite)>() {
97124
// queue
98125
}

0 commit comments

Comments
 (0)