From 02c34c4f2537386fb1087c1fb3528f96011a54e0 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Sun, 16 Jun 2019 12:26:06 -0500 Subject: movement of camera --- src/constants.rs | 1 + src/main.rs | 21 ++++++++++++++++++--- src/map.rs | 35 ++++++++++++++++------------------- 3 files changed, 35 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/constants.rs b/src/constants.rs index 8276340..dd61e38 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -1 +1,2 @@ pub const TILE_SCALE: f32 = 3.0; +pub const CAMERA_MOVE: f32 = 5.0; diff --git a/src/main.rs b/src/main.rs index 953d326..eaafa88 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use ggez::conf::Conf; -use ggez::event::{self, EventHandler}; +use ggez::event::{self, EventHandler, KeyCode, KeyMods}; use ggez::filesystem; use ggez::graphics::{self, spritebatch::SpriteBatch, DrawParam, FilterMode, Image}; use ggez::nalgebra::{Point2, Vector2}; @@ -12,6 +12,7 @@ struct State { map: Map, tileset: Tileset, spritebatch: SpriteBatch, + camera_point: (f32, f32), } impl State { @@ -23,12 +24,13 @@ impl State { map: Map::new(filesystem::open(context, "/map.tmx")?), tileset: Tileset::new(filesystem::open(context, "/tileset.tsx")?), spritebatch: SpriteBatch::new(image), + camera_point: (0.0, 0.0), }) } } impl EventHandler for State { - fn update(&mut self, _context: &mut Context) -> GameResult { + fn update(&mut self, _: &mut Context) -> GameResult { Ok(()) } @@ -51,12 +53,25 @@ impl EventHandler for State { } } - graphics::draw(context, &self.spritebatch, DrawParam::default())?; + let draw_param = + DrawParam::default().dest(Point2::new(self.camera_point.0, self.camera_point.1)); + + graphics::draw(context, &self.spritebatch, draw_param)?; self.spritebatch.clear(); graphics::present(context)?; Ok(()) } + + fn key_down_event(&mut self, _: &mut Context, keycode: KeyCode, _: KeyMods, _: bool) { + match keycode { + KeyCode::W => self.camera_point.1 += constants::CAMERA_MOVE, + KeyCode::A => self.camera_point.0 += constants::CAMERA_MOVE, + KeyCode::S => self.camera_point.1 -= constants::CAMERA_MOVE, + KeyCode::D => self.camera_point.0 -= constants::CAMERA_MOVE, + _ => (), + } + } } fn main() -> GameResult { diff --git a/src/map.rs b/src/map.rs index db79deb..ed90f1a 100644 --- a/src/map.rs +++ b/src/map.rs @@ -2,24 +2,6 @@ use ggez::filesystem::File; use std::io::BufReader; use xml::reader::{EventReader, XmlEvent}; -pub struct Layer { - pub id: usize, - pub data: Vec, -} - -impl Layer { - pub fn new(text: String, id: usize) -> Layer { - Layer { - id, - data: text - .replace("\n", "") - .split(',') - .map(|s| s.parse().unwrap()) - .collect(), - } - } -} - pub struct Map { pub width: usize, pub height: usize, @@ -47,7 +29,7 @@ impl Map { } } } else if let Ok(XmlEvent::Characters(text)) = e { - layers.push(Layer::new(text, layers.len() + 1)); + layers.push(Layer::new(text)); } } @@ -59,3 +41,18 @@ impl Map { } } +pub struct Layer { + pub data: Vec, +} + +impl Layer { + pub fn new(text: String) -> Layer { + Layer { + data: text + .replace("\n", "") + .split(',') + .map(|s| s.parse().unwrap()) + .collect(), + } + } +} -- cgit v1.2.3