gdritter repos wenaglia / 9686767
rename res to resources Getty Ritter 5 years ago
4 changed file(s) with 109 addition(s) and 114 deletion(s). Collapse all Expand all
66 Context, ContextBuilder, GameResult,
77 };
88
9 mod nc {
10 pub use ncollide2d::bounding_volume::*;
11 pub use ncollide2d::broad_phase::*;
12 pub use ncollide2d::narrow_phase::*;
13 }
149 use specs::world::WorldExt;
1510
1611 pub mod components;
1712 pub mod consts;
1813 pub mod game;
19 pub mod res;
14 pub mod resources;
2015 pub mod sys;
2116 pub mod types;
2217
4338 if keycode == winit::VirtualKeyCode::Escape {
4439 ggez::event::quit(ctx);
4540 }
46 self.world.write_resource::<res::KeySet>().insert(keycode);
41 self.world.write_resource::<resources::KeySet>().insert(keycode);
4742 }
4843
4944 fn key_up_event(
5247 keycode: winit::VirtualKeyCode,
5348 _keymod: ggez::event::KeyMods,
5449 ) {
55 self.world.write_resource::<res::KeySet>().remove(&keycode);
50 self.world.write_resource::<resources::KeySet>().remove(&keycode);
5651 }
5752 }
5853
6156 components::register(&mut world);
6257
6358 world.insert(ncollide2d::world::CollisionWorld::<f32, specs::Entity>::new(0.1));
64 res::world_from_file(&mut world, "assets/main.tmx");
59 resources::world_from_file(&mut world, "assets/main.tmx");
6560
6661 // Make a Context and an EventLoop.
6762 let (mut ctx, mut evloop) = ContextBuilder::new("game", "me")
8277 let mut sprites = ggez::graphics::spritebatch::SpriteBatch::new(image);
8378 sprites.set_filter(ggez::graphics::FilterMode::Nearest);
8479 world.insert(sprites);
85 world.insert(res::KeySet::new());
80 world.insert(resources::KeySet::new());
8681
8782 let mut my_game = MyGame { world };
8883
+0
-103
src/res.rs less more
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 }
11 use crate::components::{Controlled, Velocity};
22 use crate::game::MyGame;
3 use crate::res::KeySet;
3 use crate::resources::KeySet;
44
55 use specs::RunNow;
66