rename res to resources
Getty Ritter
5 years ago
6 | 6 | Context, ContextBuilder, GameResult, |
7 | 7 | }; |
8 | 8 | |
9 | mod nc { | |
10 | pub use ncollide2d::bounding_volume::*; | |
11 | pub use ncollide2d::broad_phase::*; | |
12 | pub use ncollide2d::narrow_phase::*; | |
13 | } | |
14 | 9 | use specs::world::WorldExt; |
15 | 10 | |
16 | 11 | pub mod components; |
17 | 12 | pub mod consts; |
18 | 13 | pub mod game; |
19 |
pub mod res |
|
14 | pub mod resources; | |
20 | 15 | pub mod sys; |
21 | 16 | pub mod types; |
22 | 17 | |
43 | 38 | if keycode == winit::VirtualKeyCode::Escape { |
44 | 39 | ggez::event::quit(ctx); |
45 | 40 | } |
46 |
self.world.write_resource::<res |
|
41 | self.world.write_resource::<resources::KeySet>().insert(keycode); | |
47 | 42 | } |
48 | 43 | |
49 | 44 | fn key_up_event( |
52 | 47 | keycode: winit::VirtualKeyCode, |
53 | 48 | _keymod: ggez::event::KeyMods, |
54 | 49 | ) { |
55 |
self.world.write_resource::<res |
|
50 | self.world.write_resource::<resources::KeySet>().remove(&keycode); | |
56 | 51 | } |
57 | 52 | } |
58 | 53 | |
61 | 56 | components::register(&mut world); |
62 | 57 | |
63 | 58 | world.insert(ncollide2d::world::CollisionWorld::<f32, specs::Entity>::new(0.1)); |
64 |
res |
|
59 | resources::world_from_file(&mut world, "assets/main.tmx"); | |
65 | 60 | |
66 | 61 | // Make a Context and an EventLoop. |
67 | 62 | let (mut ctx, mut evloop) = ContextBuilder::new("game", "me") |
82 | 77 | let mut sprites = ggez::graphics::spritebatch::SpriteBatch::new(image); |
83 | 78 | sprites.set_filter(ggez::graphics::FilterMode::Nearest); |
84 | 79 | world.insert(sprites); |
85 |
world.insert(res |
|
80 | world.insert(resources::KeySet::new()); | |
86 | 81 | |
87 | 82 | let mut my_game = MyGame { world }; |
88 | 83 |
1 | use crate::components::*; | |
2 | use crate::consts; | |
3 | use crate::types::World; | |
4 | ||
5 | use specs::world::{Builder, WorldExt}; | |
6 | use std::path::Path; | |
7 | ||
8 | #[derive(Debug, Default)] | |
9 | pub struct KeySet { | |
10 | keys: std::collections::HashSet<winit::VirtualKeyCode>, | |
11 | } | |
12 | ||
13 | impl KeySet { | |
14 | pub fn new() -> KeySet { | |
15 | KeySet { | |
16 | keys: std::collections::HashSet::new(), | |
17 | } | |
18 | } | |
19 | ||
20 | pub fn contains(&self, kc: &winit::VirtualKeyCode) -> bool { | |
21 | self.keys.contains(kc) | |
22 | } | |
23 | ||
24 | pub fn insert(&mut self, kc: winit::VirtualKeyCode) { | |
25 | self.keys.insert(kc); | |
26 | } | |
27 | ||
28 | pub fn remove(&mut self, kc: &winit::VirtualKeyCode) { | |
29 | self.keys.remove(kc); | |
30 | } | |
31 | } | |
32 | ||
33 | #[derive(Debug, Copy, Clone)] | |
34 | enum DrawLayer { | |
35 | Background, | |
36 | Foreground, | |
37 | Decoration, | |
38 | } | |
39 | ||
40 | static DRAW_LAYERS: [DrawLayer; 3] = [ | |
41 | DrawLayer::Background, | |
42 | DrawLayer::Foreground, | |
43 | DrawLayer::Decoration, | |
44 | ]; | |
45 | ||
46 | pub fn world_from_file<P: AsRef<Path>>(w: &mut specs::World, path: P) { | |
47 | let tiled::Map { | |
48 | layers, tilesets, .. | |
49 | } = tiled::parse_file(path.as_ref()).unwrap(); | |
50 | ||
51 | for (phase, layer) in DRAW_LAYERS.iter().zip(layers) { | |
52 | for (row, y) in layer.tiles.iter().zip(0..) { | |
53 | for (&n, x) in row.iter().zip(0..) { | |
54 | if n != 0 { | |
55 | let x = x as f32 * consts::TILE_SIZE; | |
56 | let y = y as f32 * consts::TILE_SIZE; | |
57 | let u = ((n - 1) % 32) as u8; | |
58 | let v = ((n - u as u32 - 1) / 32) as u8; | |
59 | let is_blocking = tilesets[0].tiles[(n-1) as usize].properties["pass"] | |
60 | == tiled::PropertyValue::BoolValue(false); | |
61 | let mut e = w | |
62 | .create_entity_unchecked() | |
63 | .with(Position { x, y }) | |
64 | .with(Sprite { u, v }); | |
65 | e = match phase { | |
66 | DrawLayer::Background => e.with(Background), | |
67 | DrawLayer::Foreground => e.with(Foreground), | |
68 | DrawLayer::Decoration => e.with(Decoration), | |
69 | }; | |
70 | ||
71 | let e = if is_blocking { | |
72 | let mut h = w.write_resource::<World>(); | |
73 | let entity = e.entity; | |
74 | e.with(Blocking::new_box(entity, &mut h)) | |
75 | } else { | |
76 | e | |
77 | }; | |
78 | e.build(); | |
79 | } | |
80 | } | |
81 | } | |
82 | } | |
83 | ||
84 | let e = w.create_entity_unchecked() | |
85 | .with(Position { | |
86 | x: 3.0 * consts::TILE_SIZE, | |
87 | y: 3.0 * consts::TILE_SIZE, | |
88 | }) | |
89 | .with(Sprite { u: 8, v: 0 }) | |
90 | .with(Velocity { dx: 0.0, dy: 0.0 }) | |
91 | .with(Foreground) | |
92 | .with(Controlled) | |
93 | .with(Collision { | |
94 | has_collision: false, | |
95 | }); | |
96 | ||
97 | let entity = e.entity; | |
98 | e.with({ | |
99 | let mut h = w.write_resource::<World>(); | |
100 | Blocking::new_ball(entity, &mut h) | |
101 | }) | |
102 | .build(); | |
103 | } |
1 | use crate::components::*; | |
2 | use crate::consts; | |
3 | use crate::types::World; | |
4 | ||
5 | use specs::world::{Builder, WorldExt}; | |
6 | use std::path::Path; | |
7 | ||
8 | #[derive(Debug, Default)] | |
9 | pub struct KeySet { | |
10 | keys: std::collections::HashSet<winit::VirtualKeyCode>, | |
11 | } | |
12 | ||
13 | impl KeySet { | |
14 | pub fn new() -> KeySet { | |
15 | KeySet { | |
16 | keys: std::collections::HashSet::new(), | |
17 | } | |
18 | } | |
19 | ||
20 | pub fn contains(&self, kc: &winit::VirtualKeyCode) -> bool { | |
21 | self.keys.contains(kc) | |
22 | } | |
23 | ||
24 | pub fn insert(&mut self, kc: winit::VirtualKeyCode) { | |
25 | self.keys.insert(kc); | |
26 | } | |
27 | ||
28 | pub fn remove(&mut self, kc: &winit::VirtualKeyCode) { | |
29 | self.keys.remove(kc); | |
30 | } | |
31 | } | |
32 | ||
33 | #[derive(Debug, Copy, Clone)] | |
34 | enum DrawLayer { | |
35 | Background, | |
36 | Foreground, | |
37 | Decoration, | |
38 | } | |
39 | ||
40 | static DRAW_LAYERS: [DrawLayer; 3] = [ | |
41 | DrawLayer::Background, | |
42 | DrawLayer::Foreground, | |
43 | DrawLayer::Decoration, | |
44 | ]; | |
45 | ||
46 | pub fn world_from_file<P: AsRef<Path>>(w: &mut specs::World, path: P) { | |
47 | let tiled::Map { | |
48 | layers, tilesets, .. | |
49 | } = tiled::parse_file(path.as_ref()).unwrap(); | |
50 | ||
51 | for (phase, layer) in DRAW_LAYERS.iter().zip(layers) { | |
52 | for (row, y) in layer.tiles.iter().zip(0..) { | |
53 | for (&n, x) in row.iter().zip(0..) { | |
54 | if n != 0 { | |
55 | let x = x as f32 * consts::TILE_SIZE; | |
56 | let y = y as f32 * consts::TILE_SIZE; | |
57 | let u = ((n - 1) % 32) as u8; | |
58 | let v = ((n - u as u32 - 1) / 32) as u8; | |
59 | let is_blocking = tilesets[0].tiles[(n-1) as usize].properties["pass"] | |
60 | == tiled::PropertyValue::BoolValue(false); | |
61 | let mut e = w | |
62 | .create_entity_unchecked() | |
63 | .with(Position { x, y }) | |
64 | .with(Sprite { u, v }); | |
65 | e = match phase { | |
66 | DrawLayer::Background => e.with(Background), | |
67 | DrawLayer::Foreground => e.with(Foreground), | |
68 | DrawLayer::Decoration => e.with(Decoration), | |
69 | }; | |
70 | ||
71 | let e = if is_blocking { | |
72 | let mut h = w.write_resource::<World>(); | |
73 | let entity = e.entity; | |
74 | e.with(Blocking::new_box(entity, &mut h)) | |
75 | } else { | |
76 | e | |
77 | }; | |
78 | e.build(); | |
79 | } | |
80 | } | |
81 | } | |
82 | } | |
83 | ||
84 | let e = w.create_entity_unchecked() | |
85 | .with(Position { | |
86 | x: 3.0 * consts::TILE_SIZE, | |
87 | y: 3.0 * consts::TILE_SIZE, | |
88 | }) | |
89 | .with(Sprite { u: 8, v: 0 }) | |
90 | .with(Velocity { dx: 0.0, dy: 0.0 }) | |
91 | .with(Foreground) | |
92 | .with(Controlled) | |
93 | .with(Collision { | |
94 | has_collision: false, | |
95 | }); | |
96 | ||
97 | let entity = e.entity; | |
98 | e.with({ | |
99 | let mut h = w.write_resource::<World>(); | |
100 | Blocking::new_ball(entity, &mut h) | |
101 | }) | |
102 | .build(); | |
103 | } |