diff options
author | tom barrett <spalf0@gmail.com> | 2019-08-19 02:26:17 -0500 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2019-08-19 02:26:17 -0500 |
commit | ebf13bcd8e8ab799c56961e6bc1779bd8031a8dc (patch) | |
tree | 914274f75890fb754e698be2b06a8b2a2ef9b1d1 /src/game.rs | |
parent | 7d2c0954cd95bdabcb7ecf26f9225382ab078289 (diff) |
created abstraction between world and game, added temlate for dialogtrees
Diffstat (limited to 'src/game.rs')
-rw-r--r-- | src/game.rs | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/src/game.rs b/src/game.rs new file mode 100644 index 0000000..2eabcb2 --- /dev/null +++ b/src/game.rs @@ -0,0 +1,81 @@ +use ggez::event::{EventHandler, KeyCode, KeyMods}; +use ggez::graphics::{self, spritebatch::SpriteBatch, DrawParam, FilterMode, Image, WrapMode}; +use ggez::{Context, GameResult}; + +use crate::camera::Camera; +use crate::dialogbox::DialogBox; +use crate::entity::Operable; +use crate::world::World; + +pub struct Game { + world: World, + spritebatch: SpriteBatch, + dialogbox: DialogBox, + camera: Camera, +} + +impl Game { + pub fn new(context: &mut Context) -> GameResult<Game> { + let mut image = Image::new(context, "/tileset.png")?; + image.set_filter(FilterMode::Nearest); + image.set_wrap(WrapMode::Mirror, WrapMode::Mirror); + let world = World::new(context); + let dimensions = world.get_dimensions(); + + Ok(Game { + world, + spritebatch: SpriteBatch::new(image), + dialogbox: DialogBox::new(context), + camera: Camera::new(dimensions), + }) + } +} + +impl EventHandler for Game { + fn update(&mut self, _context: &mut Context) -> GameResult { + self.world.update(); + self.camera.give_center(self.world.player.get_position()); + self.dialogbox.update(); + Ok(()) + } + + fn draw(&mut self, context: &mut Context) -> GameResult { + graphics::clear(context, graphics::BLACK); + + self.world.draw(&mut self.spritebatch); + + graphics::draw( + context, + &self.spritebatch, + DrawParam::default().dest(self.camera.draw), + )?; + + self.dialogbox.draw(context)?; + + self.spritebatch.clear(); + + graphics::present(context)?; + + Ok(()) + } + + fn key_up_event(&mut self, _: &mut Context, keycode: KeyCode, _: KeyMods) { + self.world.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, + KeyCode::E => self.dialogbox.give_dialogtree(self.world.get_dialogtree()), + _ => self.world.give_key_down(keycode), + } + } + } +} |