|
@@ -1,58 +1,32 @@
|
|
|
#[macro_use] extern crate specs_derive;
|
|
|
|
|
|
-use ggez::{Context, ContextBuilder, GameResult};
|
|
|
-use ggez::event::{self, EventHandler};
|
|
|
-use ggez::graphics;
|
|
|
-
|
|
|
-use specs::RunNow;
|
|
|
+use ggez::{
|
|
|
+ Context,
|
|
|
+ ContextBuilder,
|
|
|
+ GameResult,
|
|
|
+ event::{self, EventHandler},
|
|
|
+};
|
|
|
|
|
|
use sdl2::keyboard as sdl;
|
|
|
|
|
|
pub mod consts;
|
|
|
pub mod components;
|
|
|
+pub mod game;
|
|
|
pub mod res;
|
|
|
pub mod sys;
|
|
|
|
|
|
-struct MyGame {
|
|
|
- world: specs::World,
|
|
|
- sprites: graphics::spritebatch::SpriteBatch,
|
|
|
-}
|
|
|
+use game::MyGame;
|
|
|
|
|
|
impl EventHandler for MyGame {
|
|
|
|
|
|
fn update(&mut self, _ctx: &mut Context) -> GameResult<()> {
|
|
|
+ sys::input::systems(self);
|
|
|
+ sys::physics::systems(self);
|
|
|
Ok(())
|
|
|
}
|
|
|
|
|
|
fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
|
|
|
- ggez::graphics::set_background_color(ctx, ggez::graphics::BLACK);
|
|
|
- ggez::graphics::clear(ctx);
|
|
|
-
|
|
|
- sys::Draw {
|
|
|
- ctx,
|
|
|
- sprites: &mut self.sprites,
|
|
|
- _phase: components::Background,
|
|
|
- }.run_now(&self.world.res);
|
|
|
- sys::Draw {
|
|
|
- ctx,
|
|
|
- sprites: &mut self.sprites,
|
|
|
- _phase: components::Foreground,
|
|
|
- }.run_now(&self.world.res);
|
|
|
- sys::Draw {
|
|
|
- ctx,
|
|
|
- sprites: &mut self.sprites,
|
|
|
- _phase: components::Decoration,
|
|
|
- }.run_now(&self.world.res);
|
|
|
-
|
|
|
- ggez::graphics::draw(
|
|
|
- ctx,
|
|
|
- &self.sprites,
|
|
|
- ggez::nalgebra::Point2::new(0.0, 0.0),
|
|
|
- 0.0
|
|
|
- )?;
|
|
|
- self.sprites.clear();
|
|
|
- ggez::graphics::present(ctx);
|
|
|
- Ok(())
|
|
|
+ sys::drawing::systems(self, ctx)
|
|
|
}
|
|
|
|
|
|
fn key_down_event(
|
|
@@ -65,7 +39,17 @@ impl EventHandler for MyGame {
|
|
|
if keycode == sdl::Keycode::Escape {
|
|
|
ctx.quit().expect("Should never fail");
|
|
|
}
|
|
|
- sys::Move { keycode }.run_now(&self.world.res);
|
|
|
+ self.keys.insert(keycode);
|
|
|
+ }
|
|
|
+
|
|
|
+ fn key_up_event(
|
|
|
+ &mut self,
|
|
|
+ _ctx: &mut Context,
|
|
|
+ keycode: sdl::Keycode,
|
|
|
+ _keymod: sdl::Mod,
|
|
|
+ _repeat: bool,
|
|
|
+ ) {
|
|
|
+ self.keys.remove(&keycode);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -89,13 +73,15 @@ fn main() -> Result<(), ggez::error::GameError> {
|
|
|
..Default::default()
|
|
|
})
|
|
|
.build()?;
|
|
|
- let image = graphics::Image::new(&mut ctx, "/spritesheet.png")?;
|
|
|
- let mut sprites = graphics::spritebatch::SpriteBatch::new(image);
|
|
|
+ let image = ggez::graphics::Image::new(&mut ctx, "/spritesheet.png")?;
|
|
|
+ let mut sprites = ggez::graphics::spritebatch::SpriteBatch::new(image);
|
|
|
sprites.set_filter(ggez::graphics::FilterMode::Nearest);
|
|
|
+ let keys = std::collections::HashSet::new();
|
|
|
|
|
|
let mut my_game = MyGame {
|
|
|
world,
|
|
|
sprites,
|
|
|
+ keys,
|
|
|
};
|
|
|
|
|
|
event::run(&mut ctx, &mut my_game)
|