//! Minimal Mawida example: a colored quad you move with the arrow keys.
//!
//! Run with: `cargo run --example hello_quad`
//!
//! This is the whole programming model in one file: implement `Game`, hold your
//! state, move it in `update`, draw it in `draw`. No editor, no engine config,
//! just Rust.

use mawida::{
    run, AppConfig, Camera, Color, Context, Game, GameTime, Key, OrthographicCamera, Result,
};
use mawida::drawable::Sprite;

struct Pong {
    x: f32,
    y: f32,
    speed: f32,
    camera: OrthographicCamera,
}

impl Game for Pong {
    fn init(&mut self, ctx: &mut Context) -> Result<()> {
        ctx.set_clear_color(mawida::CORNFLOWER_BLUE);
        let (w, h) = ctx.size();
        self.camera = OrthographicCamera::new(w as f32, h as f32);
        Ok(())
    }

    fn update(&mut self, ctx: &mut Context, time: GameTime) -> Result<()> {
        let dt = time.delta_seconds();
        let input = ctx.input();
        let mut dx = 0.0;
        let mut dy = 0.0;
        if input.is_down(Key::Left) {
            dx -= 1.0;
        }
        if input.is_down(Key::Right) {
            dx += 1.0;
        }
        if input.is_down(Key::Up) {
            dy -= 1.0; // y-down screen space
        }
        if input.is_down(Key::Down) {
            dy += 1.0;
        }
        self.x += dx * self.speed * dt;
        self.y += dy * self.speed * dt;

        // keep the camera matched to the window if it was resized
        let (w, h) = ctx.size();
        self.camera.width = w as f32;
        self.camera.height = h as f32;
        Ok(())
    }

    fn draw(&mut self, ctx: &mut Context) -> Result<()> {
        let player = Sprite::solid(Color::WHITE, self.x, self.y, 64.0, 64.0);
        ctx.draw(&player, &self.camera as &dyn Camera)?;
        Ok(())
    }
}

fn main() -> Result<()> {
    let game = Pong {
        x: 100.0,
        y: 100.0,
        speed: 300.0,
        camera: OrthographicCamera::new(800.0, 600.0),
    };
    run(
        game,
        AppConfig {
            title: "Mawida - hello_quad".to_string(),
            width: 800,
            height: 600,
            ..Default::default()
        },
    )
}
