diff options
author | tom barrett <spalf0@gmail.com> | 2019-06-16 12:38:05 -0500 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2019-06-16 12:38:05 -0500 |
commit | 6162da14c62b1f2e23428dac40ad608bd9bb5e7d (patch) | |
tree | 3bb32148741333e9f1b1cb70866933f56489951d | |
parent | 8bb97d08ce435b207419e8c3502ab1fb76a21d2f (diff) | |
parent | 02c34c4f2537386fb1087c1fb3528f96011a54e0 (diff) |
Merge branch 'master' into feature/tiles
-rw-r--r-- | src/constants.rs | 1 | ||||
-rw-r--r-- | src/main.rs | 21 | ||||
-rw-r--r-- | src/map.rs | 35 |
3 files changed, 35 insertions, 22 deletions
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 { @@ -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<usize>, -} - -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<usize>, +} + +impl Layer { + pub fn new(text: String) -> Layer { + Layer { + data: text + .replace("\n", "") + .split(',') + .map(|s| s.parse().unwrap()) + .collect(), + } + } +} |