Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkleiny committed Apr 6, 2024
1 parent b23bd90 commit 757307a
Showing 1 changed file with 50 additions and 23 deletions.
73 changes: 50 additions & 23 deletions samples/flappy-bird/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! A simple example of a Flappy Bird game using surreal and hecs.
use hecs::{EntityBuilder, World};
use hecs::World;
use surreal::{
backends::sdl::{Window, WindowSettings},
common::{DeltaClock, StringName},
Expand All @@ -18,9 +18,11 @@ fn main() {
.unwrap();

let graphics = GraphicsEngine::opengl(&window);
let mut queue = RenderQueue::new();

let mut game = Game::new();
let mut queue = RenderQueue::new();

game.initialize();

while window.update() {
game.update();
Expand All @@ -35,7 +37,6 @@ fn main() {
struct Game {
world: World,
clock: DeltaClock,
sprite_cache: SpriteCache,
}

struct Position {
Expand All @@ -53,46 +54,72 @@ struct Sprite {
tint: Color32,
}

struct SpriteCache {}
struct Player {}

struct Obstacle {}

impl Game {
pub fn new() -> Self {
let mut world = World::new();

// create entities
for _ in 0..10 {
let mut builder = EntityBuilder::default();

builder.add(Position { x: 0.0, y: 0.0 });
builder.add(Velocity { x: 1.0, y: 1.0 });
builder.add(Sprite {
texture: "bird".into(),
tint: Color32::WHITE,
});

world.spawn(builder.build());
}

Self {
world,
world: World::new(),
clock: DeltaClock::new(),
sprite_cache: SpriteCache {},
}
}

pub fn initialize(&mut self) {
// spawn the player
self.world.spawn((
Position { x: 0.0, y: 0.0 },
Velocity { x: 0.0, y: 0.0 },
Sprite {
texture: "sprites/player.png".into(),
tint: Color32::WHITE,
},
Player {},
));

// spawn the first obstacle
self.world.spawn((
Position { x: 0.0, y: 0.0 },
Sprite {
texture: "sprites/obstacle.png".into(),
tint: Color32::WHITE,
},
Obstacle {},
));
}

pub fn update(&mut self) {
let delta_time = self.clock.tick();

// update positions
self.update_physics(delta_time);
self.update_player();
}

fn update_physics(&mut self, delta_time: f32) {
const GRAVITY: f32 = 9.8;

for (_, (position, velocity)) in self.world.query_mut::<(&mut Position, &mut Velocity)>() {
position.x += velocity.x * delta_time;
position.y += velocity.y * delta_time;

velocity.y += GRAVITY * delta_time;
}
}

fn update_player(&mut self) {
for (_, (_velocity, _player)) in self.world.query_mut::<(&mut Velocity, &Player)>() {
// TODO: player input
}
}

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

self.draw_sprites(queue);
}

fn draw_sprites(&self, _queue: &mut RenderQueue) {
for (_, (_position, _sprite)) in &mut self.world.query::<(&Position, &Sprite)>() {
// queue
}
Expand Down

0 comments on commit 757307a

Please sign in to comment.