From 2ee665ef6954f6eab9f0398c19284b3ea4c8246d Mon Sep 17 00:00:00 2001 From: tom barrett Date: Tue, 23 Jul 2019 00:54:35 -0500 Subject: state -> world, moved struct members to private --- src/animations.rs | 10 +++--- src/cell.rs | 2 +- src/entity.rs | 2 +- src/lib.rs | 2 +- src/main.rs | 4 +-- src/state.rs | 96 ------------------------------------------------------- src/world.rs | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 106 insertions(+), 106 deletions(-) delete mode 100644 src/state.rs create mode 100644 src/world.rs (limited to 'src') diff --git a/src/animations.rs b/src/animations.rs index e2cc009..b82b42e 100644 --- a/src/animations.rs +++ b/src/animations.rs @@ -10,9 +10,9 @@ use crate::tileset::Tileset; #[derive(Debug, Clone, PartialEq)] pub struct Animation { - pub frames: Vec, - pub timer: Instant, - pub current: Tile, + frames: Vec, + timer: Instant, + current: Tile, } impl Animation { @@ -55,8 +55,8 @@ impl Animation { #[derive(Debug, Clone, PartialEq)] pub struct Animations { - pub available: HashMap, - pub current: Animation, + available: HashMap, + current: Animation, } impl Animations { diff --git a/src/cell.rs b/src/cell.rs index 9d257ff..bce744b 100644 --- a/src/cell.rs +++ b/src/cell.rs @@ -9,8 +9,8 @@ use crate::tileset::Tileset; #[derive(Debug, Clone)] pub struct Cell { pub id: usize, - pub animation: Animation, pub destination: Point2, + animation: Animation, } impl Operable for Cell { diff --git a/src/entity.rs b/src/entity.rs index ae3a236..3489671 100644 --- a/src/entity.rs +++ b/src/entity.rs @@ -13,7 +13,7 @@ pub struct Entity { pub position: Point2, pub spawn: Point2, pub action: Action, - pub map_dimensions: (f32, f32), + map_dimensions: (f32, f32), } impl Entity { diff --git a/src/lib.rs b/src/lib.rs index 4ecd991..a95028d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,7 +7,7 @@ pub mod layer; pub mod map; pub mod npc; pub mod player; -pub mod state; pub mod tile; pub mod tileset; +pub mod world; pub mod xmlelements; diff --git a/src/main.rs b/src/main.rs index 6f16771..78c60f9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use ggez::conf::{NumSamples, WindowSetup}; use ggez::{event, ContextBuilder, GameResult}; -use pax_romana::state::State; +use pax_romana::world::World; fn main() -> GameResult { let (ref mut context, ref mut event_loop) = ContextBuilder::new("pax-romana", "tom barrett") @@ -13,7 +13,7 @@ fn main() -> GameResult { .add_resource_path("./resources") .build()?; - let state = &mut State::new(context)?; + let state = &mut World::new(context)?; event::run(context, event_loop, state) } diff --git a/src/state.rs b/src/state.rs deleted file mode 100644 index 98ad422..0000000 --- a/src/state.rs +++ /dev/null @@ -1,96 +0,0 @@ -use ggez::event::{EventHandler, KeyCode, KeyMods}; -use ggez::graphics::{self, spritebatch::SpriteBatch, DrawParam, FilterMode, Image, WrapMode}; -use ggez::{filesystem, Context, GameResult}; - -use crate::camera::Camera; -use crate::entity::Operable; -use crate::map::Map; -use crate::npc::NPC; -use crate::player::Player; -use crate::tileset::Tileset; - -pub struct State { - map: Map, - spritebatch: SpriteBatch, - camera: Camera, - player: Player, - npcs: Vec, -} - -impl State { - pub fn new(context: &mut Context) -> GameResult { - let mut image = Image::new(context, "/tileset.png")?; - image.set_filter(FilterMode::Nearest); - image.set_wrap(WrapMode::Mirror, WrapMode::Mirror); - - let tileset = Tileset::new(filesystem::open(context, "/tileset.tsx")?); - - let map = Map::new(filesystem::open(context, "/map.tmx")?, &tileset); - - Ok(State { - map: map.clone(), - spritebatch: SpriteBatch::new(image), - camera: Camera::new(context, map.get_dimensions()), - player: Player::new( - &tileset, - map.get_spawn_points("player")[0], - map.get_dimensions(), - ), - npcs: NPC::build_npcs(&tileset, &map), - }) - } -} - -impl EventHandler for State { - fn update(&mut self, _context: &mut Context) -> GameResult { - self.map.update(); - self.player.update(); - for npc in self.npcs.iter_mut() { - npc.update(); - } - - self.camera.give_center(self.player.get_position()); - Ok(()) - } - - fn draw(&mut self, context: &mut Context) -> GameResult { - graphics::clear(context, graphics::BLACK); - - self.map.draw(&mut self.spritebatch); - self.player.draw(&mut self.spritebatch); - for npc in self.npcs.iter_mut() { - npc.draw(&mut self.spritebatch); - } - - graphics::draw( - context, - &self.spritebatch, - DrawParam::default().dest(self.camera.draw), - )?; - - self.spritebatch.clear(); - - graphics::present(context)?; - - Ok(()) - } - - fn key_up_event(&mut self, _: &mut Context, keycode: KeyCode, _: KeyMods) { - self.player.give_key_up(keycode) - } - - fn key_down_event( - &mut self, - context: &mut Context, - keycode: KeyCode, - _: KeyMods, - repeat: bool, - ) { - if !repeat { - match keycode { - KeyCode::Q => context.continuing = false, - _ => self.player.give_key_down(keycode), - } - } - } -} diff --git a/src/world.rs b/src/world.rs new file mode 100644 index 0000000..0de8726 --- /dev/null +++ b/src/world.rs @@ -0,0 +1,96 @@ +use ggez::event::{EventHandler, KeyCode, KeyMods}; +use ggez::graphics::{self, spritebatch::SpriteBatch, DrawParam, FilterMode, Image, WrapMode}; +use ggez::{filesystem, Context, GameResult}; + +use crate::camera::Camera; +use crate::entity::Operable; +use crate::map::Map; +use crate::npc::NPC; +use crate::player::Player; +use crate::tileset::Tileset; + +pub struct World { + map: Map, + spritebatch: SpriteBatch, + camera: Camera, + player: Player, + npcs: Vec, +} + +impl World { + pub fn new(context: &mut Context) -> GameResult { + let mut image = Image::new(context, "/tileset.png")?; + image.set_filter(FilterMode::Nearest); + image.set_wrap(WrapMode::Mirror, WrapMode::Mirror); + + let tileset = Tileset::new(filesystem::open(context, "/tileset.tsx")?); + + let map = Map::new(filesystem::open(context, "/map.tmx")?, &tileset); + + Ok(World { + map: map.clone(), + spritebatch: SpriteBatch::new(image), + camera: Camera::new(context, map.get_dimensions()), + player: Player::new( + &tileset, + map.get_spawn_points("player")[0], + map.get_dimensions(), + ), + npcs: NPC::build_npcs(&tileset, &map), + }) + } +} + +impl EventHandler for World { + fn update(&mut self, _context: &mut Context) -> GameResult { + self.map.update(); + self.player.update(); + for npc in self.npcs.iter_mut() { + npc.update(); + } + + self.camera.give_center(self.player.get_position()); + Ok(()) + } + + fn draw(&mut self, context: &mut Context) -> GameResult { + graphics::clear(context, graphics::BLACK); + + self.map.draw(&mut self.spritebatch); + self.player.draw(&mut self.spritebatch); + for npc in self.npcs.iter_mut() { + npc.draw(&mut self.spritebatch); + } + + graphics::draw( + context, + &self.spritebatch, + DrawParam::default().dest(self.camera.draw), + )?; + + self.spritebatch.clear(); + + graphics::present(context)?; + + Ok(()) + } + + fn key_up_event(&mut self, _: &mut Context, keycode: KeyCode, _: KeyMods) { + self.player.give_key_up(keycode) + } + + fn key_down_event( + &mut self, + context: &mut Context, + keycode: KeyCode, + _: KeyMods, + repeat: bool, + ) { + if !repeat { + match keycode { + KeyCode::Q => context.continuing = false, + _ => self.player.give_key_down(keycode), + } + } + } +} -- cgit v1.2.3