Created
May 26, 2018 18:42
-
-
Save glfmn/f6f2b1ba1541410ab1c7485be4e8fc1c to your computer and use it in GitHub Desktop.
Terrible endless runner game prototype using ggez
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//! The simplest possible example that does something. | |
extern crate ggez; | |
use ggez::{GameResult, Context}; | |
use ggez::graphics::{self, DrawMode}; | |
use ggez::conf; | |
use ggez::event; | |
#[derive(Copy, Clone, Debug)] | |
struct Pad { | |
pos_x: f32, | |
} | |
impl Pad { | |
pub fn new(pos_x: f32) -> Pad { | |
Pad { pos_x } | |
} | |
pub fn integrate(&mut self, vel_x: f32) { | |
self.pos_x = self.pos_x + vel_x; | |
} | |
} | |
#[derive(Copy, Clone, Debug)] | |
enum PlayerState { | |
Jump { | |
t0: f32, | |
v0: f32, | |
}, | |
Wait, | |
} | |
#[derive(Copy, Clone, Debug)] | |
struct Player { | |
pos_y: f32, | |
state: PlayerState, | |
} | |
impl Player { | |
pub fn new(pos_y: f32) -> Player { | |
Player { pos_y, state: PlayerState::Wait } | |
} | |
pub fn jump(&mut self, v0: f32) { | |
match self.state { | |
PlayerState::Wait => self.state = PlayerState::Jump { t0: 0.0, v0 }, | |
PlayerState::Jump { t0: _, v0: _ } => {}, | |
} | |
} | |
pub fn update(&mut self, dt: f32) { | |
self.state = match self.state { | |
PlayerState::Jump {t0, v0} => { | |
let t = t0 + dt; | |
self.pos_y = (v0*t0 - 0.5 * 9.2 * t0*t0).max(-0.1); | |
if self.pos_y < 0.0 { | |
PlayerState::Wait | |
} else { | |
PlayerState::Jump {t0: t, v0} | |
} | |
}, | |
PlayerState::Wait => return, | |
} | |
} | |
} | |
struct MainState { | |
scroll_vel: f32, | |
pads: Vec<Pad>, | |
ground_height: f32, | |
player: Player, | |
time: f32, | |
time_step: f32, | |
} | |
impl MainState { | |
fn new(_ctx: &mut Context) -> GameResult<MainState> { | |
let s = MainState{ | |
scroll_vel: 5.0, | |
ground_height: 500.0, | |
pads: vec![ | |
Pad::new(800.0), | |
Pad::new(1000.0), | |
Pad::new(1300.0), | |
], | |
player: Player::new(0.0), | |
time: 0.0, | |
time_step: 0.5, | |
}; | |
Ok(s) | |
} | |
} | |
impl event::EventHandler for MainState { | |
fn update(&mut self, _ctx: &mut Context) -> GameResult<()> { | |
self.time = self.time + self.time_step; | |
for pad in &mut self.pads { | |
pad.integrate(-self.scroll_vel); | |
if pad.pos_x < 60.0 { | |
self.player.jump(50.0); | |
} | |
} | |
self.pads = self.pads.iter().filter(|p| p.pos_x > 0.0).cloned().collect(); | |
if self.pads.len() < 1 { | |
self.pads.push(Pad::new(850.0)); | |
} | |
self.player.update(self.time_step); | |
Ok(()) | |
} | |
fn draw(&mut self, ctx: &mut Context) -> GameResult<()> { | |
graphics::clear(ctx); | |
graphics::set_background_color(ctx, graphics::Color { r: 1.0, g: 1.0, b: 1.0, a: 1.0 }); | |
graphics::set_color(ctx, graphics::Color { r: 0.0, g: 0.0, b: 0.0, a: 1.0 })?; | |
graphics::rectangle( | |
ctx, | |
DrawMode::Fill, | |
graphics::Rect::new(0.0, self.ground_height, 800.0, 500.0), | |
)?; | |
graphics::set_color(ctx, graphics::Color { r: 1.0, g: 1.0, b: 0.0, a: 1.0 })?; | |
for pad in &self.pads { | |
graphics::rectangle( | |
ctx, | |
DrawMode::Fill, | |
graphics::Rect::new(pad.pos_x, self.ground_height, 20.0, 10.0), | |
)?; | |
} | |
graphics::set_color(ctx, graphics::Color { r: 0.0, g: 1.0, b: 0.0, a: 1.0 })?; | |
graphics::rectangle( | |
ctx, | |
DrawMode::Fill, | |
graphics::Rect::new(50.0, self.ground_height - self.player.pos_y - 10.0, 20.0, 10.0), | |
)?; | |
graphics::present(ctx); | |
Ok(()) | |
} | |
} | |
pub fn main() { | |
let c = conf::Conf::new(); | |
let ctx = &mut Context::load_from_conf("super_simple", "ggez", c).unwrap(); | |
let state = &mut MainState::new(ctx).unwrap(); | |
event::run(ctx, state).unwrap(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment