diff options
author | tom barrett <spalf0@gmail.com> | 2019-06-17 03:39:16 -0500 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2019-06-17 03:39:16 -0500 |
commit | b4bd866cfadb1eae80f9d116364cab029a28f79b (patch) | |
tree | 4012e5351f261a2d8a8b42947e544186e6f3af65 /src | |
parent | b25da75be178e8f13198685a7d0f9b970fc0a717 (diff) |
moved state file
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 1 | ||||
-rw-r--r-- | src/main.rs | 76 | ||||
-rw-r--r-- | src/state.rs | 74 |
3 files changed, 77 insertions, 74 deletions
@@ -1,3 +1,4 @@ pub mod constants; pub mod map; +pub mod state; pub mod tileset; diff --git a/src/main.rs b/src/main.rs index eaafa88..0af3228 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,78 +1,6 @@ -use ggez::conf::Conf; -use ggez::event::{self, EventHandler, KeyCode, KeyMods}; -use ggez::filesystem; -use ggez::graphics::{self, spritebatch::SpriteBatch, DrawParam, FilterMode, Image}; -use ggez::nalgebra::{Point2, Vector2}; -use ggez::{Context, ContextBuilder, GameResult}; -use pax_romana::constants; -use pax_romana::map::Map; -use pax_romana::tileset::Tileset; +use ggez::{conf::Conf, event, ContextBuilder, GameResult}; -struct State { - map: Map, - tileset: Tileset, - spritebatch: SpriteBatch, - camera_point: (f32, f32), -} - -impl State { - fn new(context: &mut Context) -> GameResult<State> { - let mut image = Image::new(context, "/tileset.png")?; - image.set_filter(FilterMode::Nearest); - - Ok(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, _: &mut Context) -> GameResult { - 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.spritebatch.add(draw_param); - } - } - } - - 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, - _ => (), - } - } -} +use pax_romana::state::State; fn main() -> GameResult { let conf = Conf::new(); diff --git a/src/state.rs b/src/state.rs new file mode 100644 index 0000000..db17ee8 --- /dev/null +++ b/src/state.rs @@ -0,0 +1,74 @@ +use ggez::event::{EventHandler, KeyCode, KeyMods}; +use ggez::graphics::{self, spritebatch::SpriteBatch, DrawParam, FilterMode, Image}; +use ggez::nalgebra::{Point2, Vector2}; +use ggez::{filesystem, Context, GameResult}; + +use crate::constants; +use crate::map::Map; +use crate::tileset::Tileset; + +pub struct State { + map: Map, + tileset: Tileset, + spritebatch: SpriteBatch, + camera_point: (f32, f32), +} + +impl State { + pub fn new(context: &mut Context) -> GameResult<State> { + let mut image = Image::new(context, "/tileset.png")?; + image.set_filter(FilterMode::Nearest); + + Ok(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, _: &mut Context) -> GameResult { + 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.spritebatch.add(draw_param); + } + } + } + + 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, + _ => (), + } + } +} |