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

Select level from assigned shift

Doing it this way, each shift can decide which levels are compatible
with it and this way we don't require that each level supports all
possible shifts.
parent b033aab2
pub struct BottleAngelShift {
hours: usize,
level: String,
}
impl BottleAngelShift {
pub fn generate(rng: &mut impl rand::Rng) -> BottleAngelShift {
BottleAngelShift {
hours: rng.gen_range(1, 3),
// TODO: When there are more levels, randomly select one here.
level: "assembly-hall-1.svg".to_owned(),
}
}
}
......@@ -19,4 +22,8 @@ impl super::AngelShiftImpl for BottleAngelShift {
hours: self.hours,
}
}
fn level_name(&self) -> &str {
&self.level
}
}
......@@ -3,6 +3,9 @@ pub trait AngelShiftImpl {
///
/// Used in heaven to display what task is up next.
fn metadata(&self) -> ShiftMetadata;
/// Return name of the selected level for this shift.
fn level_name(&self) -> &str;
}
pub struct ShiftMetadata {
......@@ -20,4 +23,8 @@ impl AngelShift {
pub fn metadata(&self) -> ShiftMetadata {
self.0.metadata()
}
pub fn level_name(&self) -> &str {
self.0.level_name()
}
}
......@@ -14,7 +14,7 @@ pub struct HeavenState {
node_schedule: legion::Schedule,
player: components::Player,
assigned_shift: angel_shifts::AngelShift,
assigned_shift: Option<angel_shifts::AngelShift>,
}
impl HeavenState {
......@@ -55,7 +55,7 @@ impl HeavenState {
resources,
node_schedule,
player,
assigned_shift,
assigned_shift: Some(assigned_shift),
}
}
}
......@@ -95,9 +95,10 @@ impl gamestate::State for HeavenState {
gamestate::Event::MouseClick {
target: "heaven-start-shift",
..
} => {
gamestate::Transition::replace(states::LevelLoadingState::new(self.player.clone()))
}
} => gamestate::Transition::replace(states::LevelLoadingState::new(
self.player.clone(),
self.assigned_shift.take().unwrap(),
)),
event => {
crate::console_log!("unknown event {:?}", event);
gamestate::Transition::Keep
......
use crate::angel_shifts;
use crate::colors;
use crate::components;
use crate::gamestate;
......@@ -12,10 +13,14 @@ pub struct LevelLoadingState {
node_schedule: legion::Schedule,
player: components::Player,
assigned_shift: Option<angel_shifts::AngelShift>,
}
impl LevelLoadingState {
pub fn new(player: components::Player) -> LevelLoadingState {
pub fn new(
player: components::Player,
assigned_shift: angel_shifts::AngelShift,
) -> LevelLoadingState {
let mut resources = legion::Resources::default();
resources.insert(resources::Clock::new());
resources.insert(resources::Rendering::new("game-canvas").unwrap());
......@@ -35,6 +40,7 @@ impl LevelLoadingState {
resources,
node_schedule,
player,
assigned_shift: Some(assigned_shift),
}
}
}
......@@ -44,10 +50,15 @@ impl gamestate::State for LevelLoadingState {
let mut rendering = resources::Rendering::new("game-canvas").unwrap();
let handle = init.get_handle();
let player = self.player.clone();
let assigned_shift = self.assigned_shift.take().unwrap();
wasm_bindgen_futures::spawn_local(async move {
let level = svg_loader::SvgLevel::load_from_svg_file("resources/levels/assembly-hall-1.svg")
.await
.unwrap();
let level_name = assigned_shift.level_name();
let level = svg_loader::SvgLevel::load_from_svg_file(&format!(
"resources/levels/{}",
level_name
))
.await
.unwrap();
sprites::Sprite::Player
.load_and_register(&mut rendering)
......
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