Commit 55ec109c authored by robert's avatar robert
Browse files

Merge branch 'rahix/player-spawns' into 'master'

Spawnpoints and Sanity tracking

Closes #11

See merge request !13
parents 45b6a639 fec0cb00
pub struct Player;
pub struct Player {
pub sanity: f32,
}
impl Player {
pub fn new() -> Player {
Player { sanity: 0.6 }
}
}
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::Player::new(),
components::Position::new(spawn.x, spawn.y),
components::Movable::new(),
colliders::Collider::new_circle_collider(50.0),
))
......
......@@ -17,6 +17,8 @@ pub struct InGameState {
impl InGameState {
pub fn new(level: svg_loader::SvgLevel) -> InGameState {
let sanity_bar = utils::get_element_by_id("sanity-amount").unwrap();
let mut rendering = resources::Rendering::new("game-canvas").unwrap();
let foreground = rendering.register_image(level.foreground_image.clone());
let background = rendering.register_image(level.background_image.clone());
......@@ -29,7 +31,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);
......@@ -40,10 +42,12 @@ impl InGameState {
.add_system(colliders::update_collision_world_system())
.flush()
.add_thread_local(systems::move_movable_system())
.add_thread_local(systems::sanity_goes_up_and_down_system())
.flush()
.add_thread_local(systems::move_camera_to_player_system())
.flush()
.add_thread_local(systems::camera_system())
.add_thread_local(systems::update_sanity_bar_system(sanity_bar))
.flush()
.add_thread_local(systems::draw_level_layer_system(background))
.add_thread_local(systems::draw_tmp_player_system())
......
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)
}
......
......@@ -2,10 +2,12 @@ mod camera;
mod draw_colliders;
mod level;
mod moving;
mod player;
mod tmp_player;
pub use camera::camera_system;
pub use draw_colliders::draw_debug_colliders_system;
pub use level::draw_level_layer_system;
pub use moving::{move_camera_to_player_system, move_movable_system};
pub use player::{sanity_goes_up_and_down_system, update_sanity_bar_system};
pub use tmp_player::draw_tmp_player_system;
use crate::components;
use crate::resources;
use legion::IntoQuery;
#[legion::system(for_each)]
#[read_component(components::Player)]
pub fn sanity_goes_up_and_down(
player: &mut components::Player,
#[resource] clock: &resources::Clock,
) {
player.sanity = (clock.wall_time() as f32).sin() * 0.5 + 0.5;
}
#[legion::system]
#[read_component(components::Player)]
pub fn update_sanity_bar(
world: &legion::world::SubWorld,
#[resource] player: &resources::Player,
#[state] sanity_bar: &mut web_sys::Element,
) {
let mut players = <&components::Player>::query();
let player = players.get(world, player.0).unwrap();
sanity_bar
.set_attribute("width", &(player.sanity * 1000.0).to_string())
.unwrap();
}
......@@ -12,8 +12,8 @@
<canvas id="game-canvas" width="1920" height="1080"></canvas>
<svg id="ingame-ui" class="game-gui" viewBox="0 0 1920 1080" style="display: none">
<text x="960" y="80" class="sanity">SANITY</text>
<rect x="460" y="100" width="600", height="40" class="sanity-amount" />
<rect x="460" y="100" width="1000", height="40" class="sanity-outline" />
<rect x="460" y="100" width="600" height="40" class="sanity-amount" id="sanity-amount" />
<rect x="460" y="100" width="1000" height="40" class="sanity-outline" />
</svg>
<svg id="heaven-ui" class="game-gui" viewBox="0 0 1920 1080" style="display: none">
<!-- GUI for the "heaven" -->
......
......@@ -248,9 +248,9 @@
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="0.30241212"
inkscape:cx="2411.5613"
inkscape:cy="1993.5803"
inkscape:zoom="0.60482424"
inkscape:cx="1890.0061"
inkscape:cy="501.65237"
inkscape:document-units="px"
inkscape:current-layer="layer4"
inkscape:document-rotation="0"
......@@ -276,7 +276,7 @@
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
......@@ -342,7 +342,8 @@
inkscape:groupmode="layer"
id="layer2"
inkscape:label="collider"
style="display:none;opacity:1">
style="display:none;opacity:1"
sodipodi:insensitive="true">
<circle
style="fill:#ff0000;fill-opacity:0.5;stroke:none;stroke-width:10;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
id="path1282"
......@@ -723,27 +724,27 @@
inkscape:groupmode="layer"
id="layer4"
inkscape:label="spawns"
style="display:none">
style="display:inline">
<text
xml:space="preserve"
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:40px;line-height:1.25;font-family:monospace;-inkscape-font-specification:monospace;letter-spacing:0px;word-spacing:0px;fill:#ffffff;fill-opacity:1;stroke:none"
x="260"
y="600"
x="300.26035"
y="600.03375"
id="text1116"><tspan
sodipodi:role="line"
id="tspan1114"
x="260"
y="600">player</tspan></text>
x="300.26035"
y="600.03375">player</tspan></text>
<text
xml:space="preserve"
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:40px;line-height:1.25;font-family:monospace;-inkscape-font-specification:monospace;letter-spacing:0px;word-spacing:0px;fill:#ffffff;fill-opacity:1;stroke:none"
x="2200.2112"
y="219.62509"
x="2200.0784"
y="240.31017"
id="text1120"><tspan
sodipodi:role="line"
id="tspan1118"
x="2200.2112"
y="219.62509">player</tspan></text>
x="2200.0784"
y="240.31017">player</tspan></text>
<text
xml:space="preserve"
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:40px;line-height:1.25;font-family:monospace;-inkscape-font-specification:monospace;letter-spacing:0px;word-spacing:0px;fill:#ffffff;fill-opacity:1;stroke:none"
......@@ -757,13 +758,13 @@
<text
xml:space="preserve"
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:40px;line-height:1.25;font-family:monospace;-inkscape-font-specification:monospace;letter-spacing:0px;word-spacing:0px;fill:#ffffff;fill-opacity:1;stroke:none"
x="260.11032"
y="3099.8606"
x="299.63376"
y="3100.7737"
id="text1128"><tspan
sodipodi:role="line"
id="tspan1126"
x="260.11032"
y="3099.8606">player</tspan></text>
x="299.63376"
y="3100.7737">player</tspan></text>
<text
xml:space="preserve"
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:40px;line-height:1.25;font-family:monospace;-inkscape-font-specification:monospace;letter-spacing:0px;word-spacing:0px;fill:#ffffff;fill-opacity:1;stroke:none"
......
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