summaryrefslogtreecommitdiff
path: root/src/world.rs
diff options
context:
space:
mode:
authortom barrett <spalf0@gmail.com>2019-08-19 02:26:17 -0500
committertom barrett <spalf0@gmail.com>2019-08-19 02:26:17 -0500
commitebf13bcd8e8ab799c56961e6bc1779bd8031a8dc (patch)
tree914274f75890fb754e698be2b06a8b2a2ef9b1d1 /src/world.rs
parent7d2c0954cd95bdabcb7ecf26f9225382ab078289 (diff)
created abstraction between world and game, added temlate for dialogtrees
Diffstat (limited to 'src/world.rs')
-rw-r--r--src/world.rs119
1 files changed, 47 insertions, 72 deletions
diff --git a/src/world.rs b/src/world.rs
index abbb9c1..76048b9 100644
--- a/src/world.rs
+++ b/src/world.rs
@@ -1,102 +1,77 @@
-use ggez::event::{EventHandler, KeyCode, KeyMods};
-use ggez::graphics::{self, spritebatch::SpriteBatch, DrawParam, FilterMode, Image, WrapMode};
-use ggez::{filesystem, Context, GameResult};
+use ggez::event::KeyCode;
+use ggez::graphics::spritebatch::SpriteBatch;
+use ggez::nalgebra::distance;
+use ggez::{filesystem, Context};
-use crate::camera::Camera;
-use crate::dialogbox::DialogBox;
+use crate::constants;
+use crate::dialogbox::DialogTree;
use crate::entity::Operable;
use crate::map::Map;
-use crate::npc::NPC;
+use crate::npc::{Character, NPC};
use crate::player::Player;
use crate::tileset::Tileset;
+#[derive(Clone)]
pub struct World {
map: Map,
- spritebatch: SpriteBatch,
- dialogbox: DialogBox,
- camera: Camera,
- player: Player,
+ pub 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);
+impl Operable for World {
+ fn update(&mut self) {
+ self.map.update();
+ self.player.update();
+ for npc in self.npcs.iter_mut() {
+ npc.update();
+ }
+ }
- let tileset = Tileset::new(filesystem::open(context, "/tileset.tsx")?);
+ fn draw(&self, spritebatch: &mut SpriteBatch) {
+ self.map.draw(spritebatch);
+ self.player.draw(spritebatch);
+ for npc in self.npcs.iter() {
+ npc.draw(spritebatch);
+ }
+ }
+}
- let map = Map::new(filesystem::open(context, "/map.tmx")?, &tileset);
+impl World {
+ pub fn new(context: &mut Context) -> World {
+ let tileset = Tileset::new(filesystem::open(context, "/tileset.tsx").unwrap());
+ let map = Map::new(filesystem::open(context, "/map.tmx").unwrap(), &tileset);
- Ok(World {
+ World {
map: map.clone(),
- spritebatch: SpriteBatch::new(image),
- dialogbox: DialogBox::new(context),
- camera: Camera::new(map.get_dimensions()),
player: Player::new(
&tileset,
- map.get_spawn_points("player")[0],
+ map.get_spawn_points(Character::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();
+ npcs: NPC::build_npcs(context, &tileset, &map),
}
-
- 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);
+ pub fn get_dialogtree(&mut self) -> Option<DialogTree> {
+ let player_position = self.player.entity.position;
+ if let Some(npc) = self.npcs.iter_mut().find(|npc| {
+ constants::INTERACT_DISTANCE > distance(&player_position, &npc.entity.position)
+ }) {
+ Some(npc.get_dialogtree())
+ } else {
+ None
}
+ }
- graphics::draw(
- context,
- &self.spritebatch,
- DrawParam::default().dest(self.camera.draw),
- )?;
-
- self.dialogbox.draw(context)?;
-
- self.spritebatch.clear();
-
- graphics::present(context)?;
-
- Ok(())
+ pub fn give_key_up(&mut self, keycode: KeyCode) {
+ self.player.give_key_up(keycode);
}
- fn key_up_event(&mut self, _: &mut Context, keycode: KeyCode, _: KeyMods) {
- self.player.give_key_up(keycode)
+ pub fn give_key_down(&mut self, keycode: KeyCode) {
+ self.player.give_key_down(keycode);
}
- fn key_down_event(
- &mut self,
- context: &mut Context,
- keycode: KeyCode,
- _: KeyMods,
- repeat: bool,
- ) {
- if !repeat {
- match keycode {
- KeyCode::Q => context.continuing = false,
- KeyCode::E => self.dialogbox.visible = !self.dialogbox.visible,
- _ => self.player.give_key_down(keycode),
- }
- }
+ pub fn get_dimensions(&self) -> (f32, f32) {
+ self.map.get_dimensions()
}
}