Commit ff0b5af8 authored by Rahix's avatar Rahix 🦀
Browse files

Pass Player component between gamestates

The player component hold information which should stay persistent
across the game-runs and thus needs to be passed through the different
game-states.  Do this by instanciating it in the heaven and then passing
it through the level-loading state into the in-game state.
parent 1ae2c0d4
#[derive(Debug, Clone)]
pub struct Player {
pub sanity: f32,
}
......
......@@ -10,6 +10,7 @@ pub fn create_player(
world: &mut legion::World,
resources: &legion::Resources,
level: &svg_loader::SvgLevel,
player: components::Player,
) -> legion::Entity {
let spawn_locations = level
.spawnpoints
......@@ -21,7 +22,7 @@ pub fn create_player(
let center = rendering.get_image_size(&image).unwrap() / 2.0;
world.push((
components::Player::new(),
player,
components::Position::new(spawn.x, spawn.y),
components::Movable::new(),
colliders::Collider::new_player(50.0),
......
......@@ -7,20 +7,29 @@ use crate::utils;
pub struct HeavenState {
gui_svg: web_sys::SvgElement,
node_world: legion::World,
resources: legion::Resources,
node_schedule: legion::Schedule,
player: components::Player,
}
impl HeavenState {
pub fn new() -> HeavenState {
pub fn new(player: Option<components::Player>) -> HeavenState {
let (node_world, resources, node_schedule) = init_orbiting_nodes();
let player = player.unwrap_or_else(|| {
// Initialize the game as this is the first run.
components::Player::new()
});
HeavenState {
gui_svg: utils::get_element_by_id("heaven-ui").unwrap(),
node_world,
resources,
node_schedule,
player,
}
}
}
......@@ -60,7 +69,9 @@ impl gamestate::State for HeavenState {
gamestate::Event::MouseClick {
target: "heaven-start-shift",
..
} => gamestate::Transition::push(states::LevelLoadingState::new()),
} => {
gamestate::Transition::replace(states::LevelLoadingState::new(self.player.clone()))
}
event => {
crate::console_log!("unknown event {:?}", event);
gamestate::Transition::Keep
......
......@@ -17,7 +17,11 @@ pub struct InGameState {
}
impl InGameState {
pub fn new(level: svg_loader::SvgLevel, mut rendering: resources::Rendering) -> InGameState {
pub fn new(
level: svg_loader::SvgLevel,
mut rendering: resources::Rendering,
player: components::Player,
) -> InGameState {
let sanity_bar = utils::get_element_by_id("sanity-amount").unwrap();
let foreground = rendering.register_image(level.foreground_image.clone());
......@@ -31,7 +35,7 @@ impl InGameState {
resources.insert(resources::Camera::new(1920.0, 1080.0));
resources.insert(colliders::CollisionWorld::new());
let player = entities::create_player(&mut world, &resources, &level);
let player = entities::create_player(&mut world, &resources, &level, player);
resources.insert(resources::Player(player));
entities::create_drop_points(&mut world, &level);
......
......@@ -10,10 +10,12 @@ pub struct LevelLoadingState {
node_world: legion::World,
resources: legion::Resources,
node_schedule: legion::Schedule,
player: components::Player,
}
impl LevelLoadingState {
pub fn new() -> LevelLoadingState {
pub fn new(player: components::Player) -> LevelLoadingState {
let mut resources = legion::Resources::default();
resources.insert(resources::Clock::new());
resources.insert(resources::Rendering::new("game-canvas").unwrap());
......@@ -32,6 +34,7 @@ impl LevelLoadingState {
node_world,
resources,
node_schedule,
player,
}
}
}
......@@ -40,6 +43,7 @@ impl gamestate::State for LevelLoadingState {
fn init(&mut self, init: gamestate::StateInitializer) -> gamestate::Transition {
let mut rendering = resources::Rendering::new("game-canvas").unwrap();
let handle = init.get_handle();
let player = self.player.clone();
wasm_bindgen_futures::spawn_local(async move {
let level = svg_loader::SvgLevel::load_from_svg_file("resources/testlevel.svg")
.await
......@@ -50,7 +54,9 @@ impl gamestate::State for LevelLoadingState {
.await;
handle.do_transition(gamestate::Transition::replace(states::InGameState::new(
level, rendering,
level,
rendering,
player.clone(),
)));
});
gamestate::Transition::Loop
......
......@@ -25,7 +25,7 @@ impl gamestate::State for MainMenuState {
// In debug builds, skip the main menu and go straight to heaven. This eases iterative
// development because one does not need to click "Play Game" every time ...
if cfg!(debug_assertions) {
gamestate::Transition::push(states::HeavenState::new())
gamestate::Transition::push(states::HeavenState::new(None))
} else {
gamestate::Transition::Sleep
}
......@@ -43,7 +43,7 @@ impl gamestate::State for MainMenuState {
gamestate::Event::MouseClick {
target: "main-menu-play",
..
} => gamestate::Transition::push(states::HeavenState::new()),
} => gamestate::Transition::push(states::HeavenState::new(None)),
event => {
crate::console_log!("unknown event {:?}", event);
gamestate::Transition::Keep
......
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