diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/camera.rs | 20 | ||||
-rw-r--r-- | src/state.rs | 14 |
2 files changed, 21 insertions, 13 deletions
diff --git a/src/camera.rs b/src/camera.rs index 1dc2db2..4ae0153 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -1,19 +1,23 @@ use ggez::nalgebra::Point2; +use ggez::Context; pub struct Camera { pub draw: Point2<f32>, + height: f32, + width: f32, } impl Camera { - pub fn new(draw: Point2<f32>) -> Camera { - Camera { draw } + pub fn new(context: &mut Context) -> Camera { + Camera { + draw: Point2::new(0.0, 0.0), + height: context.conf.window_mode.height, + width: context.conf.window_mode.width, + } } - pub fn give_center(&mut self, center: Point2<f32>) {} -} - -impl Default for Camera { - fn default() -> Camera { - Camera::new(Point2::new(0.0, 0.0)) + pub fn give_center(&mut self, center: Point2<f32>) { + self.draw.x = (self.width / 2.0) - center.x; + self.draw.y = (self.height / 2.0) - center.y; } } diff --git a/src/state.rs b/src/state.rs index b659880..f335f49 100644 --- a/src/state.rs +++ b/src/state.rs @@ -25,7 +25,7 @@ impl State { map: Map::new(filesystem::open(context, "/map.tmx")?), tileset: Tileset::new(filesystem::open(context, "/tileset.tsx")?), spritebatch: SpriteBatch::new(image), - camera: Camera::default(), + camera: Camera::new(context), player_position: Point2::new(0.0, 0.0), }) } @@ -48,25 +48,29 @@ impl EventHandler for State { DrawParam::default().dest(self.camera.draw), )?; + self.spritebatch.clear(); + graphics::draw( context, &Text::new("@"), - DrawParam::default().dest(self.player_position), + DrawParam::default().dest(Point2::new( + self.player_position.x + self.camera.draw.x, + self.player_position.y + self.camera.draw.y, + )), )?; - self.spritebatch.clear(); - graphics::present(context)?; Ok(()) } - fn key_down_event(&mut self, _: &mut Context, keycode: KeyCode, _: KeyMods, _: bool) { + fn key_down_event(&mut self, context: &mut Context, keycode: KeyCode, _: KeyMods, _: bool) { match keycode { KeyCode::W => self.player_position.y -= constants::PLAYER_SPEED, KeyCode::A => self.player_position.x -= constants::PLAYER_SPEED, KeyCode::S => self.player_position.y += constants::PLAYER_SPEED, KeyCode::D => self.player_position.x += constants::PLAYER_SPEED, + KeyCode::Q => context.continuing = false, _ => (), } } |