summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/constants.rs1
-rw-r--r--src/lib.rs2
-rw-r--r--src/main.rs50
-rw-r--r--src/map.rs92
4 files changed, 115 insertions, 30 deletions
diff --git a/src/constants.rs b/src/constants.rs
new file mode 100644
index 0000000..8276340
--- /dev/null
+++ b/src/constants.rs
@@ -0,0 +1 @@
+pub const TILE_SCALE: f32 = 3.0;
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..32420a3
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,2 @@
+pub mod constants;
+pub mod map;
diff --git a/src/main.rs b/src/main.rs
index 1d4c606..bb85c0a 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,34 +1,31 @@
use ggez::conf::Conf;
use ggez::event::{self, EventHandler};
use ggez::filesystem;
-use ggez::graphics::{self, spritebatch::SpriteBatch, DrawParam, FilterMode, Image, Rect};
+use ggez::graphics::{self, spritebatch::SpriteBatch, DrawParam, FilterMode, Image};
use ggez::nalgebra::{Point2, Vector2};
use ggez::{Context, ContextBuilder, GameResult};
-use std::io::BufReader;
-use tiled::{parse, Map};
+use pax_romana::constants;
+use pax_romana::map::{Map, Tileset};
struct State {
- spritebatch: SpriteBatch,
map: Map,
+ tileset: Tileset,
+ spritebatch: SpriteBatch,
}
impl State {
fn new(context: &mut Context) -> GameResult<State> {
- let mut tileset = Image::new(context, "/tileset.png")?;
- tileset.set_filter(FilterMode::Nearest);
-
- let reader = BufReader::new(filesystem::open(context, "/map.tmx")?);
+ let mut image = Image::new(context, "/tileset.png")?;
+ image.set_filter(FilterMode::Nearest);
Ok(State {
- spritebatch: SpriteBatch::new(tileset),
- map: parse(reader).unwrap(),
+ map: Map::new(filesystem::open(context, "/map.tmx")?),
+ tileset: Tileset::new(filesystem::open(context, "/tileset.tsx")?),
+ spritebatch: SpriteBatch::new(image),
})
}
}
-const TILE_SIZE: f32 = 16.0;
-const TILE_SCALE: f32 = 3.0;
-
impl EventHandler for State {
fn update(&mut self, _context: &mut Context) -> GameResult {
Ok(())
@@ -37,24 +34,17 @@ impl EventHandler for State {
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()
- .dest(Point2::new(
- TILE_SIZE * TILE_SCALE * x as f32,
- TILE_SIZE * TILE_SCALE * y as f32,
- ))
- .scale(Vector2::new(TILE_SCALE, TILE_SCALE))
- .src(match layer.tiles[y as usize][x as usize] {
- 1 => Rect::new(0.0, 0.0, 1.0 / 3.0, 1.0),
- 2 => Rect::new(1.0 / 3.0, 0.0, 2.0 / 3.0, 1.0),
- 3 => Rect::new(2.0 / 3.0, 0.0, 1.0, 1.0),
- _ => Rect::zero(),
- });
+ for x in 0..self.map.width {
+ for y in 0..self.map.height {
+ let draw_param = DrawParam::default()
+ .src(self.tileset.tiles[self.map.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.spritebatch.add(draw_param);
- }
+ self.spritebatch.add(draw_param);
}
}
diff --git a/src/map.rs b/src/map.rs
new file mode 100644
index 0000000..2028b50
--- /dev/null
+++ b/src/map.rs
@@ -0,0 +1,92 @@
+use ggez::filesystem::File;
+use ggez::graphics::Rect;
+use std::io::BufReader;
+use xml::reader::{EventReader, XmlEvent};
+
+pub struct Map {
+ pub width: usize,
+ pub height: usize,
+ pub data: Vec<usize>,
+}
+
+impl Map {
+ pub fn new(file: File) -> Map {
+ let mut width = None;
+ let mut height = None;
+ let mut data: Option<Vec<usize>> = None;
+
+ for e in EventReader::new(BufReader::new(file)) {
+ if let Ok(XmlEvent::StartElement {
+ name, attributes, ..
+ }) = e
+ {
+ if name.local_name == "map" {
+ for attribute in attributes {
+ match attribute.name.local_name.as_str() {
+ "width" => width = Some(attribute.value.parse::<usize>().unwrap()),
+ "height" => height = Some(attribute.value.parse::<usize>().unwrap()),
+ _ => (),
+ }
+ }
+ }
+ } else if let Ok(XmlEvent::Characters(text)) = e {
+ data = Some(
+ text.replace("\n", "")
+ .split(',')
+ .map(|s| s.parse().unwrap())
+ .collect(),
+ );
+ }
+ }
+
+ Map {
+ data: data.unwrap(),
+ width: width.unwrap(),
+ height: height.unwrap(),
+ }
+ }
+}
+
+pub struct Tileset {
+ pub tiles: Vec<Rect>,
+ pub tile_width: f32,
+ pub tile_height: f32,
+}
+
+impl Tileset {
+ pub fn new(file: File) -> Tileset {
+ let mut tile_width = None;
+ let mut tile_height = None;
+ let mut columns = None;
+
+ for e in EventReader::new(BufReader::new(file)) {
+ if let Ok(XmlEvent::StartElement { attributes, .. }) = e {
+ for attribute in attributes {
+ match attribute.name.local_name.as_str() {
+ "columns" => columns = Some(attribute.value.parse::<usize>().unwrap()),
+ "tilewidth" => tile_width = Some(attribute.value.parse::<f32>().unwrap()),
+ "tileheight" => tile_height = Some(attribute.value.parse::<f32>().unwrap()),
+ _ => (),
+ }
+ }
+ }
+ }
+
+ let columns = columns.unwrap();
+
+ let mut tiles = Vec::new();
+ tiles.push(Rect::zero());
+
+ for c in 0..columns {
+ let x = c as f32 / columns as f32;
+ let w = (c as f32 + 1.0) / columns as f32;
+ tiles.push(Rect::new(x, 0.0, w, 1.0));
+ }
+
+ Tileset {
+ tiles,
+ tile_height: tile_height.unwrap(),
+ tile_width: tile_width.unwrap(),
+ }
+ }
+}