summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortom barrett <spalf0@gmail.com>2019-06-18 02:29:24 -0500
committertom barrett <spalf0@gmail.com>2019-06-18 02:29:24 -0500
commit2ad96b7f882ff962f67a13c7c56a46df86cfe6e3 (patch)
tree2bab6cc41f5b0888548dcc3d9a7fa02ef55645b6 /src
parentb4bd866cfadb1eae80f9d116364cab029a28f79b (diff)
added camera struct, moved map drawing to map struct, now draws a character that moves
Diffstat (limited to 'src')
-rw-r--r--src/camera.rs19
-rw-r--r--src/constants.rs2
-rw-r--r--src/lib.rs1
-rw-r--r--src/map.rs23
-rw-r--r--src/state.rs49
5 files changed, 68 insertions, 26 deletions
diff --git a/src/camera.rs b/src/camera.rs
new file mode 100644
index 0000000..1dc2db2
--- /dev/null
+++ b/src/camera.rs
@@ -0,0 +1,19 @@
+use ggez::nalgebra::Point2;
+
+pub struct Camera {
+ pub draw: Point2<f32>,
+}
+
+impl Camera {
+ pub fn new(draw: Point2<f32>) -> Camera {
+ Camera { draw }
+ }
+
+ pub fn give_center(&mut self, center: Point2<f32>) {}
+}
+
+impl Default for Camera {
+ fn default() -> Camera {
+ Camera::new(Point2::new(0.0, 0.0))
+ }
+}
diff --git a/src/constants.rs b/src/constants.rs
index dd61e38..ceb6c7f 100644
--- a/src/constants.rs
+++ b/src/constants.rs
@@ -1,2 +1,2 @@
pub const TILE_SCALE: f32 = 3.0;
-pub const CAMERA_MOVE: f32 = 5.0;
+pub const PLAYER_SPEED: f32 = 5.0;
diff --git a/src/lib.rs b/src/lib.rs
index c94378c..1cd8813 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,3 +1,4 @@
+pub mod camera;
pub mod constants;
pub mod map;
pub mod state;
diff --git a/src/map.rs b/src/map.rs
index ed90f1a..23833ab 100644
--- a/src/map.rs
+++ b/src/map.rs
@@ -1,7 +1,12 @@
use ggez::filesystem::File;
+use ggez::graphics::{spritebatch::SpriteBatch, DrawParam};
+use ggez::nalgebra::{Point2, Vector2};
use std::io::BufReader;
use xml::reader::{EventReader, XmlEvent};
+use crate::constants;
+use crate::tileset::Tileset;
+
pub struct Map {
pub width: usize,
pub height: usize,
@@ -39,6 +44,24 @@ impl Map {
height: height.unwrap(),
}
}
+
+ pub fn draw(&mut 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,
+ ))
+ .scale(Vector2::new(constants::TILE_SCALE, constants::TILE_SCALE));
+
+ spritebatch.add(draw_param);
+ }
+ }
+ }
+ }
}
pub struct Layer {
diff --git a/src/state.rs b/src/state.rs
index db17ee8..b659880 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -1,8 +1,9 @@
use ggez::event::{EventHandler, KeyCode, KeyMods};
-use ggez::graphics::{self, spritebatch::SpriteBatch, DrawParam, FilterMode, Image};
-use ggez::nalgebra::{Point2, Vector2};
+use ggez::graphics::{self, spritebatch::SpriteBatch, DrawParam, FilterMode, Image, Text};
+use ggez::nalgebra::Point2;
use ggez::{filesystem, Context, GameResult};
+use crate::camera::Camera;
use crate::constants;
use crate::map::Map;
use crate::tileset::Tileset;
@@ -11,7 +12,8 @@ pub struct State {
map: Map,
tileset: Tileset,
spritebatch: SpriteBatch,
- camera_point: (f32, f32),
+ camera: Camera,
+ player_position: Point2<f32>,
}
impl State {
@@ -23,51 +25,48 @@ 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),
+ camera: Camera::default(),
+ player_position: Point2::new(0.0, 0.0),
})
}
}
impl EventHandler for State {
fn update(&mut self, _: &mut Context) -> GameResult {
+ self.camera.give_center(self.player_position);
Ok(())
}
fn draw(&mut self, context: &mut Context) -> GameResult {
graphics::clear(context, graphics::BLACK);
- for layer in self.map.layers.iter() {
- for x in 0..self.map.width {
- for y in 0..self.map.height {
- let draw_param = DrawParam::default()
- .src(self.tileset.tiles[layer.data[x + (y * self.map.height)]])
- .dest(Point2::new(
- self.tileset.tile_width * constants::TILE_SCALE * x as f32,
- self.tileset.tile_height * constants::TILE_SCALE * y as f32,
- ))
- .scale(Vector2::new(constants::TILE_SCALE, constants::TILE_SCALE));
+ self.map.draw(&mut self.spritebatch, &self.tileset);
- self.spritebatch.add(draw_param);
- }
- }
- }
+ graphics::draw(
+ context,
+ &self.spritebatch,
+ DrawParam::default().dest(self.camera.draw),
+ )?;
- let draw_param =
- DrawParam::default().dest(Point2::new(self.camera_point.0, self.camera_point.1));
+ graphics::draw(
+ context,
+ &Text::new("@"),
+ DrawParam::default().dest(self.player_position),
+ )?;
- 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,
+ 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,
_ => (),
}
}