I'm converting an old 2015 tutorials http://piston-tutorial.logdown.com/posts/306682 (about Rust game dev using Piston) to 2020 version to learning Rust better, and hoping to push the updates.
I converted Part 1 and am working on Part 2.
I know to convert the:
for e in window {
to:
window.draw_2d(&event, |c, g, _device| {
but I'm having trouble with:
match e.event {
Some(Event::Update(upd)) => {
game.on_update(upd);
}
Some(Event::Render(ren)) => {
game.on_draw(ren, e);
}
Some(Event::Input(inp)) => {
game.on_input(inp);
}
_ => {
}
}
Here is the full code:
extern crate piston_window;
use piston_window::*;
struct Game {
rotation: f64,
x: f64,
y: f64,
up_d: bool, down_d: bool, left_d: bool, right_d: bool
}
impl Game {
fn new() -> Game {
Game { rotation : 0.0, x : 0.0, y : 0.0, up_d: false, down_d: false, left_d: false, right_d: false }
}
fn on_update(&mut self, upd: UpdateArgs) {
self.rotation += 3.0 * upd.dt;
if self.up_d {
self.y += (-50.0) * upd.dt;
}
if self.down_d {
self.y += (50.0) * upd.dt;
}
if self.left_d {
self.x += (-50.0) * upd.dt;
}
if self.right_d {
self.x += (50.0) * upd.dt;
}
}
fn on_draw(&mut self, ren: RenderArgs, e: PistonWindow) {
e.draw_2d(|c, g| {
clear([0.0, 0.0, 0.0, 1.0], g);
let center = c.transform.trans((ren.width / 2) as f64, (ren.height / 2) as f64);
let square = rectangle::square(0.0, 0.0, 100.0);
let red = [1.0, 0.0, 0.0, 1.0];
rectangle(red, square, center.trans(self.x, self.y).rot_rad(self.rotation).trans(-50.0, -50.0), g); // We translate the rectangle slightly so that it's centered; otherwise only the top left corner would be centered
});
}
fn on_input(&mut self, inp: Input) {
match inp {
Input::Press(but) => {
match but {
Button::Keyboard(Key::Up) => {
self.up_d = true;
}
Button::Keyboard(Key::Down) => {
self.down_d = true;
}
Button::Keyboard(Key::Left) => {
self.left_d = true;
}
Button::Keyboard(Key::Right) => {
self.right_d = true;
}
_ => {}
}
}
Input::Release(but) => {
match but {
Button::Keyboard(Key::Up) => {
self.up_d = false;
}
Button::Keyboard(Key::Down) => {
self.down_d = false;
}
Button::Keyboard(Key::Left) => {
self.left_d = false;
}
Button::Keyboard(Key::Right) => {
self.right_d = false;
}
_ => {}
}
}
_ => {}
}
}
}
fn main() {
let window: PistonWindow = WindowSettings::new(
"piston-tutorial",
[600, 600]
)
.exit_on_esc(true)
.build()
.unwrap();
let mut game = Game::new();
for e in window {
match e.event {
Some(Event::Update(upd)) => {
game.on_update(upd);
}
Some(Event::Render(ren)) => {
game.on_draw(ren, e);
}
Some(Event::Input(inp)) => {
game.on_input(inp);
}
_ => {
}
}
}
}