Skip to content

Commit

Permalink
Add WASD camera
Browse files Browse the repository at this point in the history
  • Loading branch information
TechPersonYT committed Dec 31, 2022
1 parent 05e48ac commit 0f1d958
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use ggez::winit::event::VirtualKeyCode;
use ggez::{event, mint};
use ggez::graphics::{self, Color, DrawParam, Canvas, Drawable, Rect, GraphicsContext, Mesh, Transform, Image};
use ggez::mint::Point2;
Expand All @@ -20,6 +21,26 @@ impl Camera {
visible.draw(canvas, gfx, &Point2{x: self.position.x, y: self.position.y}, self.rotation);
}
}

fn update(&mut self, ctx: &Context) {
const MOVEMENT_SPEED: f32 = 5.0;

if ctx.keyboard.is_key_pressed(VirtualKeyCode::W) {
self.position.y += MOVEMENT_SPEED
}

if ctx.keyboard.is_key_pressed(VirtualKeyCode::S) {
self.position.y -= MOVEMENT_SPEED
}

if ctx.keyboard.is_key_pressed(VirtualKeyCode::A) {
self.position.x += MOVEMENT_SPEED
}

if ctx.keyboard.is_key_pressed(VirtualKeyCode::D) {
self.position.x -= MOVEMENT_SPEED
}
}
}

trait Simulated {
Expand Down Expand Up @@ -303,9 +324,10 @@ impl MainState {
}

impl event::EventHandler<ggez::GameError> for MainState {
fn update(&mut self, _ctx: &mut Context) -> GameResult {
fn update(&mut self, ctx: &mut Context) -> GameResult {
self.player.update(&mut self.simulation);
self.simulation.update();
self.camera.update(ctx);
Ok(())
}

Expand Down

0 comments on commit 0f1d958

Please sign in to comment.