1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- use crate::consts;
- use crate::com::*;
- use specs::world::Builder;
- use std::path::Path;
- use sdl2::keyboard as sdl;
- #[derive(Debug, Default)]
- pub struct KeySet {
- keys: std::collections::HashSet<sdl::Keycode>,
- }
- impl KeySet {
- pub fn new() -> KeySet {
- KeySet {
- keys: std::collections::HashSet::new(),
- }
- }
- pub fn contains(&self, kc: &sdl::Keycode) -> bool {
- self.keys.contains(kc)
- }
- pub fn insert(&mut self, kc: sdl::Keycode) {
- self.keys.insert(kc);
- }
- pub fn remove(&mut self, kc: &sdl::Keycode) {
- self.keys.remove(kc);
- }
- }
- #[derive(Debug, Copy, Clone)]
- enum DrawLayer {
- Background,
- Foreground,
- Decoration,
- }
- static DRAW_LAYERS: [DrawLayer;3] = [
- DrawLayer::Background,
- DrawLayer::Foreground,
- DrawLayer::Decoration,
- ];
- pub fn world_from_file<P: AsRef<Path>>(w: &mut specs::World, path: P) {
- let tiled::Map {
- layers,
- tilesets,
- ..
- } = tiled::parse_file(path.as_ref()).unwrap();
- for (phase, layer) in DRAW_LAYERS.iter().zip(layers) {
- for (row, y) in layer.tiles.iter().zip(0..) {
- for (&n, x) in row.iter().zip(0..) {
- if n != 0 {
- let x = x as f32 * consts::TILE_SIZE;
- let y = y as f32 * consts::TILE_SIZE;
- let u = ((n - 1) % 32) as u8;
- let v = ((n - u as u32 - 1) / 32) as u8;
- let mut e = w.create_entity()
- .with(Position { x, y })
- .with(Sprite { u, v });
- e = match phase {
- DrawLayer::Background => e.with(Background),
- DrawLayer::Foreground => e.with(Foreground),
- DrawLayer::Decoration => e.with(Decoration),
- };
- let e = if tilesets[0].tiles[n as usize].properties["pass"] ==
- tiled::PropertyValue::BoolValue(false) {
- e.with(Blocking)
- } else {
- e
- };
- e.build();
- }
- }
- }
- }
- // create the player
- w.create_entity()
- .with(Position {
- x: 3.0 * consts::TILE_SIZE,
- y: 3.0 * consts::TILE_SIZE,
- })
- .with(Sprite { u: 8, v: 0 })
- .with(Velocity { dx: 0.0, dy: 0.0 })
- .with(Foreground)
- .with(Controlled)
- .with(Collision { has_collision: false })
- .build();
- }
|