res.rs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. use crate::consts;
  2. use crate::com::*;
  3. use specs::world::{Builder,WorldExt};
  4. use std::path::Path;
  5. #[derive(Debug, Default)]
  6. pub struct KeySet {
  7. keys: std::collections::HashSet<winit::VirtualKeyCode>,
  8. }
  9. impl KeySet {
  10. pub fn new() -> KeySet {
  11. KeySet {
  12. keys: std::collections::HashSet::new(),
  13. }
  14. }
  15. pub fn contains(&self, kc: &winit::VirtualKeyCode) -> bool {
  16. self.keys.contains(kc)
  17. }
  18. pub fn insert(&mut self, kc: winit::VirtualKeyCode) {
  19. self.keys.insert(kc);
  20. }
  21. pub fn remove(&mut self, kc: &winit::VirtualKeyCode) {
  22. self.keys.remove(kc);
  23. }
  24. }
  25. #[derive(Debug, Copy, Clone)]
  26. enum DrawLayer {
  27. Background,
  28. Foreground,
  29. Decoration,
  30. }
  31. static DRAW_LAYERS: [DrawLayer;3] = [
  32. DrawLayer::Background,
  33. DrawLayer::Foreground,
  34. DrawLayer::Decoration,
  35. ];
  36. pub fn world_from_file<P: AsRef<Path>>(w: &mut specs::World, path: P) {
  37. let tiled::Map {
  38. layers,
  39. tilesets,
  40. ..
  41. } = tiled::parse_file(path.as_ref()).unwrap();
  42. for (phase, layer) in DRAW_LAYERS.iter().zip(layers) {
  43. for (row, y) in layer.tiles.iter().zip(0..) {
  44. for (&n, x) in row.iter().zip(0..) {
  45. if n != 0 {
  46. let x = x as f32 * consts::TILE_SIZE;
  47. let y = y as f32 * consts::TILE_SIZE;
  48. let u = ((n - 1) % 32) as u8;
  49. let v = ((n - u as u32 - 1) / 32) as u8;
  50. let mut e = w.create_entity()
  51. .with(Position { x, y })
  52. .with(Sprite { u, v });
  53. e = match phase {
  54. DrawLayer::Background => e.with(Background),
  55. DrawLayer::Foreground => e.with(Foreground),
  56. DrawLayer::Decoration => e.with(Decoration),
  57. };
  58. let e = if tilesets[0].tiles[n as usize].properties["pass"] ==
  59. tiled::PropertyValue::BoolValue(false) {
  60. e.with(Blocking::new())
  61. } else {
  62. e
  63. };
  64. e.build();
  65. }
  66. }
  67. }
  68. }
  69. // create the player
  70. w.create_entity()
  71. .with(Position {
  72. x: 3.0 * consts::TILE_SIZE,
  73. y: 3.0 * consts::TILE_SIZE,
  74. })
  75. .with(Sprite { u: 8, v: 0 })
  76. .with(Velocity { dx: 0.0, dy: 0.0 })
  77. .with(Foreground)
  78. .with(Controlled)
  79. .with(Collision { has_collision: false })
  80. .with(Blocking {
  81. volume: Box::new(ncollide2d::shape::Ball::new(10.0)),
  82. })
  83. .build();
  84. }