From 8693a266c2c9a8448b70d87d487ed211c439cbc4 Mon Sep 17 00:00:00 2001
From: tom barrett <spalf0@gmail.com>
Date: Wed, 19 Jun 2019 04:45:46 -0500
Subject: camera now doesnt display unused map

---
 README.md        |  1 +
 src/camera.rs    | 32 +++++++++++++++++++++++++-------
 src/constants.rs |  3 +++
 src/lib.rs       |  1 +
 src/map.rs       | 15 ++++++++++-----
 src/player.rs    | 26 ++++++++++++++++++++++++++
 src/state.rs     | 35 +++++++++++++++--------------------
 src/tileset.rs   | 19 ++++---------------
 8 files changed, 85 insertions(+), 47 deletions(-)
 create mode 100644 src/player.rs

diff --git a/README.md b/README.md
index 77a2482..d4d212e 100644
--- a/README.md
+++ b/README.md
@@ -7,3 +7,4 @@
 * collision
 * sprites with movements (tile states)
 * fat binary (include-bytes!)
+* textbox (with dialog options)
diff --git a/src/camera.rs b/src/camera.rs
index 4ae0153..ed5a20a 100644
--- a/src/camera.rs
+++ b/src/camera.rs
@@ -1,23 +1,41 @@
 use ggez::nalgebra::Point2;
 use ggez::Context;
 
+use crate::constants;
+
 pub struct Camera {
     pub draw: Point2<f32>,
-    height: f32,
-    width: f32,
+    window_height: f32,
+    window_width: f32,
+    map_height: f32,
+    map_width: f32,
 }
 
 impl Camera {
-    pub fn new(context: &mut Context) -> Camera {
+    pub fn new(context: &mut Context, dimensions: (f32, f32)) -> Camera {
         Camera {
             draw: Point2::new(0.0, 0.0),
-            height: context.conf.window_mode.height,
-            width: context.conf.window_mode.width,
+            window_height: context.conf.window_mode.height,
+            window_width: context.conf.window_mode.width,
+            map_width: dimensions.0,
+            map_height: dimensions.1,
         }
     }
 
     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;
+        self.draw.x = ((self.window_width / 2.0) - center.x) - (constants::TILE_WIDTH);
+        self.draw.y = ((self.window_height / 2.0) - center.y) - (constants::TILE_HEIGHT);
+
+        if self.draw.x > 0.0 {
+            self.draw.x = 0.0;
+        } else if self.draw.x - self.window_width < -1.0 * self.map_width {
+            self.draw.x = -1.0 * (self.map_width - self.window_width);
+        }
+
+        if self.draw.y > 0.0 {
+            self.draw.y = 0.0;
+        } else if self.draw.y - self.window_height < -1.0 * self.map_height {
+            self.draw.y = -1.0 * (self.map_height - self.window_height);
+        }
     }
 }
diff --git a/src/constants.rs b/src/constants.rs
index ceb6c7f..1af4324 100644
--- a/src/constants.rs
+++ b/src/constants.rs
@@ -1,2 +1,5 @@
+pub const TILE_WIDTH: f32 = 16.0;
+pub const TILE_HEIGHT: f32 = 16.0;
 pub const TILE_SCALE: f32 = 3.0;
+
 pub const PLAYER_SPEED: f32 = 5.0;
diff --git a/src/lib.rs b/src/lib.rs
index cdf9a50..583c6f7 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,6 +1,7 @@
 pub mod camera;
 pub mod constants;
 pub mod map;
+pub mod player;
 pub mod state;
 pub mod tileset;
 pub mod xmlelements;
diff --git a/src/map.rs b/src/map.rs
index 21ebc5c..57b39f5 100644
--- a/src/map.rs
+++ b/src/map.rs
@@ -18,9 +18,7 @@ impl Map {
         let elements = XMLElements::new(file);
 
         let width = elements.get_element_attribute("map", "width").unwrap();
-
         let height = elements.get_element_attribute("map", "height").unwrap();
-
         let layers = elements
             .events
             .iter()
@@ -40,15 +38,15 @@ impl Map {
         }
     }
 
-    pub fn draw(&mut self, spritebatch: &mut SpriteBatch, tileset: &Tileset) {
+    pub fn draw(&self, spritebatch: &mut SpriteBatch, tileset: &Tileset) {
         for layer in self.layers.iter() {
             for x in 0..self.width {
                 for y in 0..self.height {
                     let draw_param = DrawParam::default()
                         .src(tileset.tiles[layer.data[x + (y * self.height)]])
                         .dest(Point2::new(
-                            tileset.tile_width * constants::TILE_SCALE * x as f32,
-                            tileset.tile_height * constants::TILE_SCALE * y as f32,
+                            constants::TILE_WIDTH * constants::TILE_SCALE * x as f32,
+                            constants::TILE_HEIGHT * constants::TILE_SCALE * y as f32,
                         ))
                         .scale(Vector2::new(constants::TILE_SCALE, constants::TILE_SCALE));
 
@@ -57,6 +55,13 @@ impl Map {
             }
         }
     }
+
+    pub fn get_dimensions(&self) -> (f32, f32) {
+        (
+            (constants::TILE_WIDTH * constants::TILE_SCALE) * self.width as f32,
+            (constants::TILE_HEIGHT * constants::TILE_SCALE) * self.height as f32,
+        )
+    }
 }
 
 pub struct Layer {
diff --git a/src/player.rs b/src/player.rs
new file mode 100644
index 0000000..66e7d28
--- /dev/null
+++ b/src/player.rs
@@ -0,0 +1,26 @@
+use ggez::graphics::{spritebatch::SpriteBatch, DrawParam};
+use ggez::nalgebra::{Point2, Vector2};
+
+use crate::constants;
+use crate::tileset::Tileset;
+
+pub struct Player {
+    pub position: Point2<f32>,
+}
+
+impl Player {
+    pub fn new() -> Player {
+        Player {
+            position: Point2::new(0.0, 0.0),
+        }
+    }
+
+    pub fn draw(&self, spritebatch: &mut SpriteBatch, tileset: &Tileset) {
+        let draw_param = DrawParam::default()
+            .src(tileset.tiles[1])
+            .dest(self.position)
+            .scale(Vector2::new(constants::TILE_SCALE, constants::TILE_SCALE));
+
+        spritebatch.add(draw_param);
+    }
+}
diff --git a/src/state.rs b/src/state.rs
index f335f49..d63a763 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -1,11 +1,11 @@
 use ggez::event::{EventHandler, KeyCode, KeyMods};
-use ggez::graphics::{self, spritebatch::SpriteBatch, DrawParam, FilterMode, Image, Text};
-use ggez::nalgebra::Point2;
+use ggez::graphics::{self, spritebatch::SpriteBatch, DrawParam, FilterMode, Image};
 use ggez::{filesystem, Context, GameResult};
 
 use crate::camera::Camera;
 use crate::constants;
 use crate::map::Map;
+use crate::player::Player;
 use crate::tileset::Tileset;
 
 pub struct State {
@@ -13,7 +13,7 @@ pub struct State {
     tileset: Tileset,
     spritebatch: SpriteBatch,
     camera: Camera,
-    player_position: Point2<f32>,
+    player: Player,
 }
 
 impl State {
@@ -21,19 +21,22 @@ impl State {
         let mut image = Image::new(context, "/tileset.png")?;
         image.set_filter(FilterMode::Nearest);
 
+        let map = Map::new(filesystem::open(context, "/map.tmx")?);
+        let map_dimensions = map.get_dimensions();
+
         Ok(State {
-            map: Map::new(filesystem::open(context, "/map.tmx")?),
+            map,
             tileset: Tileset::new(filesystem::open(context, "/tileset.tsx")?),
             spritebatch: SpriteBatch::new(image),
-            camera: Camera::new(context),
-            player_position: Point2::new(0.0, 0.0),
+            camera: Camera::new(context, map_dimensions),
+            player: Player::new(),
         })
     }
 }
 
 impl EventHandler for State {
     fn update(&mut self, _: &mut Context) -> GameResult {
-        self.camera.give_center(self.player_position);
+        self.camera.give_center(self.player.position);
         Ok(())
     }
 
@@ -41,6 +44,7 @@ impl EventHandler for State {
         graphics::clear(context, graphics::BLACK);
 
         self.map.draw(&mut self.spritebatch, &self.tileset);
+        self.player.draw(&mut self.spritebatch, &self.tileset);
 
         graphics::draw(
             context,
@@ -50,15 +54,6 @@ impl EventHandler for State {
 
         self.spritebatch.clear();
 
-        graphics::draw(
-            context,
-            &Text::new("@"),
-            DrawParam::default().dest(Point2::new(
-                self.player_position.x + self.camera.draw.x,
-                self.player_position.y + self.camera.draw.y,
-            )),
-        )?;
-
         graphics::present(context)?;
 
         Ok(())
@@ -66,10 +61,10 @@ impl EventHandler for State {
 
     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::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,
             _ => (),
         }
diff --git a/src/tileset.rs b/src/tileset.rs
index d4e652f..17f4f61 100644
--- a/src/tileset.rs
+++ b/src/tileset.rs
@@ -1,30 +1,23 @@
 use ggez::filesystem::File;
 use ggez::graphics::Rect;
 
+use crate::constants;
 use crate::xmlelements::XMLElements;
 
 pub struct Tileset {
     pub tiles: Vec<Rect>,
-    pub tile_width: f32,
-    pub tile_height: f32,
 }
 
 impl Tileset {
     pub fn new(file: File) -> Tileset {
         let elements = XMLElements::new(file);
 
+        let height = elements.get_element_attribute("image", "height").unwrap();
         let columns = elements
             .get_element_attribute("tileset", "columns")
             .unwrap();
-        let height = elements.get_element_attribute("image", "height").unwrap();
-        let tile_width = elements
-            .get_element_attribute("tileset", "tilewidth")
-            .unwrap() as f32;
-        let tile_height = elements
-            .get_element_attribute("tileset", "tileheight")
-            .unwrap() as f32;
 
-        let rows = height / (tile_height as usize);
+        let rows = height / (constants::TILE_HEIGHT as usize);
 
         let mut tiles = Vec::new();
         tiles.push(Rect::zero());
@@ -39,10 +32,6 @@ impl Tileset {
             }
         }
 
-        Tileset {
-            tiles,
-            tile_height,
-            tile_width,
-        }
+        Tileset { tiles }
     }
 }
-- 
cgit v1.2.3