|
@@ -213,18 +213,25 @@ impl Board<CP437> {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
|
|
+type Updater = Box<dyn FnMut(&mut specs::World)>;
|
|
pub struct World<Idx> {
|
|
pub struct World<Idx> {
|
|
pub world: specs::World,
|
|
pub world: specs::World,
|
|
|
|
+ updater: Updater,
|
|
idx: std::marker::PhantomData<Idx>,
|
|
idx: std::marker::PhantomData<Idx>,
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+fn do_nothing() -> Updater {
|
|
|
|
+ Box::new(|_| {})
|
|
|
|
+}
|
|
|
|
+
|
|
impl <Idx: Tile + 'static> World<Idx> {
|
|
impl <Idx: Tile + 'static> World<Idx> {
|
|
pub fn new(board: Board<Idx>) -> World<Idx> {
|
|
pub fn new(board: Board<Idx>) -> World<Idx> {
|
|
let mut world = specs::World::new();
|
|
let mut world = specs::World::new();
|
|
world.insert(board);
|
|
world.insert(board);
|
|
|
|
+ let updater = do_nothing();
|
|
World {
|
|
World {
|
|
world,
|
|
world,
|
|
|
|
+ updater,
|
|
idx: std::marker::PhantomData,
|
|
idx: std::marker::PhantomData,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -246,6 +253,10 @@ impl <Idx: Tile + 'static> World<Idx> {
|
|
pub fn get(&self, at: impl Into<Coord>) -> Idx {
|
|
pub fn get(&self, at: impl Into<Coord>) -> Idx {
|
|
self.world.fetch::<Board<Idx>>().get(at)
|
|
self.world.fetch::<Board<Idx>>().get(at)
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ pub fn on_update(&mut self, update: impl FnMut(&mut specs::World) + 'static) {
|
|
|
|
+ self.updater = Box::new(update) as Updater;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
impl World<CP437> {
|
|
impl World<CP437> {
|
|
@@ -262,6 +273,10 @@ impl<Idx: 'static + Tile> ggez::event::EventHandler for World<Idx> {
|
|
}
|
|
}
|
|
|
|
|
|
fn update(&mut self, _ctx: &mut Context) -> Result<(), ggez::GameError> {
|
|
fn update(&mut self, _ctx: &mut Context) -> Result<(), ggez::GameError> {
|
|
|
|
+ let mut updater = do_nothing();
|
|
|
|
+ std::mem::swap(&mut updater, &mut self.updater);
|
|
|
|
+ updater(&mut self.world);
|
|
|
|
+ std::mem::swap(&mut updater, &mut self.updater);
|
|
Ok(())
|
|
Ok(())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -285,10 +300,26 @@ impl<Idx: Tile + 'static> Game<Idx> {
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ pub fn register<C>(&mut self)
|
|
|
|
+ where C: specs::Component, <C as specs::Component>::Storage: std::default::Default
|
|
|
|
+ {
|
|
|
|
+ self.world.world.register::<C>();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ pub fn insert(&mut self, r: impl specs::prelude::Resource) {
|
|
|
|
+ self.world.world.insert(r);
|
|
|
|
+ }
|
|
|
|
+
|
|
pub fn run(self) -> ggez::GameResult<()> {
|
|
pub fn run(self) -> ggez::GameResult<()> {
|
|
let Game { mut world, mut ctx, mut evloop } = self;
|
|
let Game { mut world, mut ctx, mut evloop } = self;
|
|
ggez::event::run(&mut ctx, &mut evloop, &mut world)
|
|
ggez::event::run(&mut ctx, &mut evloop, &mut world)
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ pub fn run_with_systems(self, update: impl FnMut(&mut specs::World) + 'static) -> ggez::GameResult<()> {
|
|
|
|
+ let Game { mut world, mut ctx, mut evloop } = self;
|
|
|
|
+ world.on_update(update);
|
|
|
|
+ ggez::event::run(&mut ctx, &mut evloop, &mut world)
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|