gdritter repos wenaglia / edfcb36
Move some values into specs-based resources Getty Ritter 5 years ago
5 changed file(s) with 49 addition(s) and 37 deletion(s). Collapse all Expand all
1 use sdl2::keyboard as sdl;
1 // use sdl2::keyboard as sdl;
22
33 /// The shared values that the game state needs, mostly contained in
44 /// the specs ECS world
55 pub struct MyGame {
66 pub world: specs::World,
7 pub sprites: ggez::graphics::spritebatch::SpriteBatch,
8 pub keys: std::collections::HashSet<sdl::Keycode>,
7 // pub keys: std::collections::HashSet<sdl::Keycode>,
98 }
3939 if keycode == sdl::Keycode::Escape {
4040 ctx.quit().expect("Should never fail");
4141 }
42 self.keys.insert(keycode);
42 self.world.write_resource::<res::KeySet>().insert(keycode);
4343 }
4444
4545 fn key_up_event(
4949 _keymod: sdl::Mod,
5050 _repeat: bool,
5151 ) {
52 self.keys.remove(&keycode);
52 self.world.write_resource::<res::KeySet>().remove(&keycode);
5353 }
5454 }
5555
7676 let image = ggez::graphics::Image::new(&mut ctx, "/spritesheet.png")?;
7777 let mut sprites = ggez::graphics::spritebatch::SpriteBatch::new(image);
7878 sprites.set_filter(ggez::graphics::FilterMode::Nearest);
79 let keys = std::collections::HashSet::new();
79 world.add_resource(sprites);
80 world.add_resource(res::KeySet::new());
8081
81 let mut my_game = MyGame {
82 world,
83 sprites,
84 keys,
85 };
82 let mut my_game = MyGame { world };
8683
8784 event::run(&mut ctx, &mut my_game)
8885 }
44 use specs::world::Builder;
55 use std::path::Path;
66
7 use sdl2::keyboard as sdl;
8
9 #[derive(Debug, Default)]
10 pub struct KeySet {
11 keys: std::collections::HashSet<sdl::Keycode>,
12 }
13
14 impl KeySet {
15 pub fn new() -> KeySet {
16 KeySet {
17 keys: std::collections::HashSet::new(),
18 }
19 }
20
21 pub fn contains(&self, kc: &sdl::Keycode) -> bool {
22 self.keys.contains(kc)
23 }
24
25 pub fn insert(&mut self, kc: sdl::Keycode) {
26 self.keys.insert(kc);
27 }
28
29 pub fn remove(&mut self, kc: &sdl::Keycode) {
30 self.keys.remove(kc);
31 }
32 }
733
834 #[derive(Debug, Copy, Clone)]
935 enum DrawLayer {
1313 /// in.
1414 pub struct Draw<'t, Phase> {
1515 pub ctx: &'t mut Context,
16 pub sprites: &'t mut SpriteBatch,
16 //pub sprites: &'t mut SpriteBatch,
1717 pub _phase: Phase,
1818 }
1919
2222 specs::ReadStorage<'a, Position>,
2323 specs::ReadStorage<'a, Sprite>,
2424 specs::ReadStorage<'a, Phase>,
25 specs::WriteExpect<'a, SpriteBatch>,
2526 );
2627
27 fn run(&mut self, (positions, sprites, phase): Self::SystemData) {
28 fn run(&mut self, (positions, sprites, phase, mut batch): Self::SystemData) {
2829 use specs::Join;
2930
3031 for (pos, spr, _) in (&positions, &sprites, &phase).join() {
3435 scale: ggez::nalgebra::Point2::new(consts::SCALE, consts::SCALE),
3536 ..Default::default()
3637 };
37 self.sprites.add(param);
38 batch.add(param);
3839 graphics::draw(
3940 self.ctx,
40 self.sprites,
41 &*batch,
4142 ggez::nalgebra::Point2::new(0.0, 0.0),
4243 0.0).unwrap();
43 self.sprites.clear();
44 batch.clear();
4445 }
4546 }
4647 }
5253
5354 Draw {
5455 ctx,
55 sprites: &mut game.sprites,
5656 _phase: components::Background,
5757 }.run_now(&game.world.res);
5858 Draw {
5959 ctx,
60 sprites: &mut game.sprites,
6160 _phase: components::Foreground,
6261 }.run_now(&game.world.res);
6362 Draw {
6463 ctx,
65 sprites: &mut game.sprites,
6664 _phase: components::Decoration,
6765 }.run_now(&game.world.res);
6866
69 graphics::draw(
70 ctx,
71 &game.sprites,
72 ggez::nalgebra::Point2::new(0.0, 0.0),
73 0.0
74 )?;
75 game.sprites.clear();
7667 graphics::present(ctx);
7768 Ok(())
7869 }
11 use crate::components::{Controlled, Velocity};
22 use crate::game::MyGame;
3 use crate::res::KeySet;
34
45 use sdl2::keyboard as sdl;
56 use specs::RunNow;
67
7 pub struct Move<'t> {
8 keys: &'t std::collections::HashSet<sdl::Keycode>,
9 }
8 pub struct Move;
109
11 impl<'a, 't> specs::System<'a> for Move<'t> {
10 impl<'a> specs::System<'a> for Move {
1211 type SystemData = (
1312 specs::ReadStorage<'a, Controlled>,
1413 specs::WriteStorage<'a, Velocity>,
14 specs::Read<'a, KeySet>,
1515 );
1616
17 fn run(&mut self, (movable, mut velocity): Self::SystemData) {
17 fn run(&mut self, (movable, mut velocity, keys): Self::SystemData) {
1818 use specs::Join;
1919
2020 for (_, vel) in (&movable, &mut velocity).join() {
2121 vel.dx = 0.0;
2222 vel.dy = 0.0;
2323
24 if self.keys.contains(&sdl::Keycode::W) {
24 if keys.contains(&sdl::Keycode::W) {
2525 vel.dy -= 2.0;
2626 }
27 if self.keys.contains(&sdl::Keycode::A) {
27 if keys.contains(&sdl::Keycode::A) {
2828 vel.dx -= 2.0;
2929 }
30 if self.keys.contains(&sdl::Keycode::S) {
30 if keys.contains(&sdl::Keycode::S) {
3131 vel.dy += 2.0;
3232 }
33 if self.keys.contains(&sdl::Keycode::D) {
33 if keys.contains(&sdl::Keycode::D) {
3434 vel.dx += 2.0;
3535 }
3636 }
3737 }
3838 }
3939
40
4140 pub fn systems(game: &mut MyGame) {
42 Move { keys: &game.keys }.run_now(&game.world.res);
41 Move.run_now(&game.world.res);
4342 }