Move some values into specs-based resources
Getty Ritter
5 years ago
1 |
|
|
1 | // use sdl2::keyboard as sdl; | |
2 | 2 | |
3 | 3 | /// The shared values that the game state needs, mostly contained in |
4 | 4 | /// the specs ECS world |
5 | 5 | pub struct MyGame { |
6 | 6 | 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>, | |
9 | 8 | } |
39 | 39 | if keycode == sdl::Keycode::Escape { |
40 | 40 | ctx.quit().expect("Should never fail"); |
41 | 41 | } |
42 |
self. |
|
42 | self.world.write_resource::<res::KeySet>().insert(keycode); | |
43 | 43 | } |
44 | 44 | |
45 | 45 | fn key_up_event( |
49 | 49 | _keymod: sdl::Mod, |
50 | 50 | _repeat: bool, |
51 | 51 | ) { |
52 |
self. |
|
52 | self.world.write_resource::<res::KeySet>().remove(&keycode); | |
53 | 53 | } |
54 | 54 | } |
55 | 55 | |
76 | 76 | let image = ggez::graphics::Image::new(&mut ctx, "/spritesheet.png")?; |
77 | 77 | let mut sprites = ggez::graphics::spritebatch::SpriteBatch::new(image); |
78 | 78 | sprites.set_filter(ggez::graphics::FilterMode::Nearest); |
79 |
|
|
79 | world.add_resource(sprites); | |
80 | world.add_resource(res::KeySet::new()); | |
80 | 81 | |
81 | let mut my_game = MyGame { | |
82 | world, | |
83 | sprites, | |
84 | keys, | |
85 |
|
|
82 | let mut my_game = MyGame { world }; | |
86 | 83 | |
87 | 84 | event::run(&mut ctx, &mut my_game) |
88 | 85 | } |
4 | 4 | use specs::world::Builder; |
5 | 5 | use std::path::Path; |
6 | 6 | |
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 | } | |
7 | 33 | |
8 | 34 | #[derive(Debug, Copy, Clone)] |
9 | 35 | enum DrawLayer { |
13 | 13 | /// in. |
14 | 14 | pub struct Draw<'t, Phase> { |
15 | 15 | pub ctx: &'t mut Context, |
16 |
|
|
16 | //pub sprites: &'t mut SpriteBatch, | |
17 | 17 | pub _phase: Phase, |
18 | 18 | } |
19 | 19 | |
22 | 22 | specs::ReadStorage<'a, Position>, |
23 | 23 | specs::ReadStorage<'a, Sprite>, |
24 | 24 | specs::ReadStorage<'a, Phase>, |
25 | specs::WriteExpect<'a, SpriteBatch>, | |
25 | 26 | ); |
26 | 27 | |
27 |
fn run(&mut self, (positions, sprites, phase |
|
28 | fn run(&mut self, (positions, sprites, phase, mut batch): Self::SystemData) { | |
28 | 29 | use specs::Join; |
29 | 30 | |
30 | 31 | for (pos, spr, _) in (&positions, &sprites, &phase).join() { |
34 | 35 | scale: ggez::nalgebra::Point2::new(consts::SCALE, consts::SCALE), |
35 | 36 | ..Default::default() |
36 | 37 | }; |
37 |
|
|
38 | batch.add(param); | |
38 | 39 | graphics::draw( |
39 | 40 | self.ctx, |
40 |
|
|
41 | &*batch, | |
41 | 42 | ggez::nalgebra::Point2::new(0.0, 0.0), |
42 | 43 | 0.0).unwrap(); |
43 |
|
|
44 | batch.clear(); | |
44 | 45 | } |
45 | 46 | } |
46 | 47 | } |
52 | 53 | |
53 | 54 | Draw { |
54 | 55 | ctx, |
55 | sprites: &mut game.sprites, | |
56 | 56 | _phase: components::Background, |
57 | 57 | }.run_now(&game.world.res); |
58 | 58 | Draw { |
59 | 59 | ctx, |
60 | sprites: &mut game.sprites, | |
61 | 60 | _phase: components::Foreground, |
62 | 61 | }.run_now(&game.world.res); |
63 | 62 | Draw { |
64 | 63 | ctx, |
65 | sprites: &mut game.sprites, | |
66 | 64 | _phase: components::Decoration, |
67 | 65 | }.run_now(&game.world.res); |
68 | 66 | |
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(); | |
76 | 67 | graphics::present(ctx); |
77 | 68 | Ok(()) |
78 | 69 | } |
1 | 1 | use crate::components::{Controlled, Velocity}; |
2 | 2 | use crate::game::MyGame; |
3 | use crate::res::KeySet; | |
3 | 4 | |
4 | 5 | use sdl2::keyboard as sdl; |
5 | 6 | use specs::RunNow; |
6 | 7 | |
7 | pub struct Move<'t> { | |
8 | keys: &'t std::collections::HashSet<sdl::Keycode>, | |
9 | } | |
8 | pub struct Move; | |
10 | 9 | |
11 |
impl<'a |
|
10 | impl<'a> specs::System<'a> for Move { | |
12 | 11 | type SystemData = ( |
13 | 12 | specs::ReadStorage<'a, Controlled>, |
14 | 13 | specs::WriteStorage<'a, Velocity>, |
14 | specs::Read<'a, KeySet>, | |
15 | 15 | ); |
16 | 16 | |
17 |
fn run(&mut self, (movable, mut velocity |
|
17 | fn run(&mut self, (movable, mut velocity, keys): Self::SystemData) { | |
18 | 18 | use specs::Join; |
19 | 19 | |
20 | 20 | for (_, vel) in (&movable, &mut velocity).join() { |
21 | 21 | vel.dx = 0.0; |
22 | 22 | vel.dy = 0.0; |
23 | 23 | |
24 |
if |
|
24 | if keys.contains(&sdl::Keycode::W) { | |
25 | 25 | vel.dy -= 2.0; |
26 | 26 | } |
27 |
if |
|
27 | if keys.contains(&sdl::Keycode::A) { | |
28 | 28 | vel.dx -= 2.0; |
29 | 29 | } |
30 |
if |
|
30 | if keys.contains(&sdl::Keycode::S) { | |
31 | 31 | vel.dy += 2.0; |
32 | 32 | } |
33 |
if |
|
33 | if keys.contains(&sdl::Keycode::D) { | |
34 | 34 | vel.dx += 2.0; |
35 | 35 | } |
36 | 36 | } |
37 | 37 | } |
38 | 38 | } |
39 | 39 | |
40 | ||
41 | 40 | pub fn systems(game: &mut MyGame) { |
42 |
Move |
|
41 | Move.run_now(&game.world.res); | |
43 | 42 | } |