|
@@ -1,6 +1,7 @@
|
|
|
use ggez::graphics::spritebatch::{SpriteBatch, SpriteIdx};
|
|
|
use ggez::graphics::{DrawParam, Drawable, Image};
|
|
|
use ggez::{Context, GameError};
|
|
|
+use specs::WorldExt;
|
|
|
use std::path::Path;
|
|
|
|
|
|
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
|
|
@@ -26,7 +27,7 @@ impl From<Color> for ggez::graphics::Color {
|
|
|
/// identifiers we care about should at least be `Clone`, but often
|
|
|
/// they'll be (wrappers over) small identifiers (like a `u8` or a
|
|
|
/// `u16`) and could probably be `Copy`.
|
|
|
-pub trait Tile: Clone {
|
|
|
+pub trait Tile: Clone + Send + Sync {
|
|
|
fn to_location(self) -> [f32;4];
|
|
|
fn blank() -> Self;
|
|
|
}
|
|
@@ -190,7 +191,7 @@ impl<Idx: Tile> Board<Idx> {
|
|
|
self.tileset.batch.set(self.contents[idx].1, param).unwrap();
|
|
|
}
|
|
|
|
|
|
- pub fn get(&mut self, at: impl Into<Coord>) -> Idx {
|
|
|
+ pub fn get(&self, at: impl Into<Coord>) -> Idx {
|
|
|
let at = at.into();
|
|
|
let idx = at.x + at.y * self.size.width;
|
|
|
self.contents[idx].0.clone()
|
|
@@ -217,3 +218,60 @@ impl Board<CP437> {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+pub struct Game<Idx> {
|
|
|
+ pub world: specs::World,
|
|
|
+ idx: std::marker::PhantomData<Idx>,
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+impl<Idx: Tile + 'static> Game<Idx> {
|
|
|
+ pub fn create(_name: &str, _author: &str, board: Board<Idx>) -> Result<Game<Idx>, ggez::GameError> {
|
|
|
+ let mut world = specs::World::new();
|
|
|
+ world.insert(board);
|
|
|
+ Ok(Game {
|
|
|
+ world,
|
|
|
+ idx: std::marker::PhantomData,
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ pub fn set(&mut self, at: impl Into<Coord>, ch: Idx) {
|
|
|
+ self.world.fetch_mut::<Board<Idx>>().set(at, ch)
|
|
|
+ }
|
|
|
+
|
|
|
+ pub fn set_with_color(
|
|
|
+ &mut self,
|
|
|
+ at: impl Into<Coord>,
|
|
|
+ ch: Idx,
|
|
|
+ color: impl Into<ggez::graphics::Color>,
|
|
|
+ ) {
|
|
|
+ self.world
|
|
|
+ .fetch_mut::<Board<Idx>>()
|
|
|
+ .set_with_color(at, ch, color)
|
|
|
+ }
|
|
|
+
|
|
|
+ pub fn get(&self, at: impl Into<Coord>) -> Idx {
|
|
|
+ self.world.fetch::<Board<Idx>>().get(at)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+impl Game<CP437> {
|
|
|
+ pub fn print(&mut self, at: impl Into<Coord>, msg: &str) {
|
|
|
+ self.world.fetch_mut::<Board<CP437>>().print(at, msg)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+impl<Idx: 'static + Tile> ggez::event::EventHandler for Game<Idx> {
|
|
|
+ fn draw(&mut self, ctx: &mut Context) -> Result<(), ggez::GameError> {
|
|
|
+ ggez::graphics::clear(ctx, ggez::graphics::BLACK);
|
|
|
+ self.world.fetch::<Board<Idx>>().draw(ctx)?;
|
|
|
+ ggez::graphics::present(ctx)
|
|
|
+ }
|
|
|
+
|
|
|
+ fn update(&mut self, _ctx: &mut Context) -> Result<(), ggez::GameError> {
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+}
|