summaryrefslogtreecommitdiff
path: root/src/world.rs
diff options
context:
space:
mode:
authortom barrett <spalf0@gmail.com>2019-07-23 00:54:35 -0500
committertom barrett <spalf0@gmail.com>2019-07-23 03:48:58 -0500
commit2ee665ef6954f6eab9f0398c19284b3ea4c8246d (patch)
treea64d57f2fecbded780d93f464b36354f432c3033 /src/world.rs
parentf351a002b66ae8a4b73588b7ffdffc69623181f3 (diff)
state -> world, moved struct members to private
Diffstat (limited to 'src/world.rs')
-rw-r--r--src/world.rs96
1 files changed, 96 insertions, 0 deletions
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<NPC>,
+}
+
+impl World {
+ pub fn new(context: &mut Context) -> GameResult<World> {
+ 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),
+ }
+ }
+ }
+}