Commit 60d39bb0 authored by Rahix's avatar Rahix 🦀
Browse files

Load spawnpoints from map

Load all spawn positions from the `spawns` layer in the map SVG and
store them as level data.  As a first use, make the player spawn at one
of the player spawnpoints randomly.
parent 45b6a639
use crate::colliders;
use crate::components;
use crate::svg_loader;
pub fn create_player(world: &mut legion::World) -> legion::Entity {
use rand::seq::SliceRandom;
pub fn create_player(world: &mut legion::World, level: &svg_loader::SvgLevel) -> legion::Entity {
let spawn_locations = level
.spawnpoints
.get("player")
.expect("no player spawns in this map");
let spawn = spawn_locations.choose(&mut rand::thread_rng()).unwrap();
world.push((
components::Player,
components::Position::new(600.0, 600.0),
components::Position::new(spawn.x, spawn.y),
components::Movable::new(),
colliders::Collider::new_circle_collider(50.0),
))
......
......@@ -29,7 +29,7 @@ impl InGameState {
resources.insert(resources::Camera::new(1920.0, 1080.0));
resources.insert(colliders::CollisionWorld::new());
let player = entities::create_player(&mut world);
let player = entities::create_player(&mut world, &level);
resources.insert(resources::Player(player));
level.apply_colliders(&mut world);
......
use js_sys;
use std::collections;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use wasm_bindgen_futures;
......@@ -134,6 +135,7 @@ pub struct SvgLevel {
pub foreground_image: web_sys::HtmlImageElement,
pub background_image: web_sys::HtmlImageElement,
pub colliders: Vec<Collider>,
pub spawnpoints: collections::HashMap<String, Vec<nalgebra::Point2<f32>>>,
}
impl SvgLevel {
......@@ -159,10 +161,33 @@ impl SvgLevel {
let colliders = Collider::load_from_svg(&svg_loader);
let mut spawnpoints = collections::HashMap::new();
let nodes = svg_loader
.query_selector_all(r#"g[inkscape\:groupmode="layer"][inkscape\:label="spawns"] text"#);
for node in (0..nodes.length()).map(|i| nodes.get(i).unwrap()) {
let element: &web_sys::Element = node.dyn_ref().unwrap();
let x: f32 = element.get_attribute("x").unwrap().parse().unwrap();
let y: f32 = element.get_attribute("y").unwrap().parse().unwrap();
let pos = nalgebra::Point2::new(x, y);
let tspan: web_sys::Element = node
.first_child()
.expect("expected <tspan /> in <text />")
.dyn_into()
.unwrap();
let spawntype = tspan.inner_html();
spawnpoints
.entry(spawntype)
.or_insert_with(|| vec![])
.push(pos);
}
let svg_level = SvgLevel {
foreground_image,
background_image,
colliders,
spawnpoints,
};
Ok(svg_level)
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment