level_loading.rs 2.3 KB
Newer Older
1
2
3
use std::cell;
use std::rc;

4
use crate::colors;
Rahix's avatar
Rahix committed
5
use crate::components;
Rahix's avatar
Rahix committed
6
use crate::gamestate;
Rahix's avatar
Rahix committed
7
use crate::resources;
Rahix's avatar
Rahix committed
8
use crate::states;
9
use crate::svg_loader;
Rahix's avatar
Rahix committed
10
11
12
13
14

pub struct LevelLoadingState {
    node_world: legion::World,
    resources: legion::Resources,
    node_schedule: legion::Schedule,
15
    svg_level: rc::Rc<cell::RefCell<Option<svg_loader::SvgLevel>>>,
Rahix's avatar
Rahix committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
}

impl LevelLoadingState {
    pub fn new() -> LevelLoadingState {
        let mut resources = legion::Resources::default();
        resources.insert(resources::Clock::new());
        resources.insert(resources::Rendering::new("game-canvas").unwrap());

        let mut node_world = legion::World::default();
        node_world.push((
            components::TheSun,
            components::Position::new(1920.0 / 2.0, 1080.0 / 2.0),
        ));

        let node_schedule = legion::Schedule::builder()
            .add_thread_local(components::draw_thesun_system())
            .build();

34
35
36
37
        let svg_level = rc::Rc::new(cell::RefCell::new(None));

        let svg_level_cell = svg_level.clone();
        wasm_bindgen_futures::spawn_local(async move {
38
            let level = svg_loader::SvgLevel::load_from_svg_file("resources/testlevel.svg")
39
40
41
42
43
                .await
                .unwrap();
            *svg_level_cell.borrow_mut() = Some(level);
        });

Rahix's avatar
Rahix committed
44
45
46
47
        LevelLoadingState {
            node_world,
            resources,
            node_schedule,
48
            svg_level,
Rahix's avatar
Rahix committed
49
50
51
52
        }
    }
}

Rahix's avatar
Rahix committed
53
54
55
impl gamestate::State for LevelLoadingState {
    fn init(&mut self, _init: gamestate::StateInitializer) -> gamestate::Transition {
        gamestate::Transition::Loop
Rahix's avatar
Rahix committed
56
57
    }

Rahix's avatar
Rahix committed
58
    fn update(&mut self) -> gamestate::Transition {
Rahix's avatar
Rahix committed
59
60
        {
            let rendering = self.resources.get_mut::<resources::Rendering>().unwrap();
61
            rendering.set_fill_style(&colors::BACKGROUND);
62
            rendering.fill_rect(0.0, 0.0, 1920.0, 1080.0);
Rahix's avatar
Rahix committed
63
64
65
66
67
        }

        self.node_schedule
            .execute(&mut self.node_world, &mut self.resources);

68
        // TODO: Polling the future/promise here is really ugly ...
Rahix's avatar
Rahix committed
69
        if let Some(level) = self.svg_level.borrow_mut().take() {
70
            crate::console_log!("Level was loaded!");
Rahix's avatar
Rahix committed
71
            gamestate::Transition::replace(states::InGameState::new(level))
72
        } else {
Rahix's avatar
Rahix committed
73
            gamestate::Transition::Loop
74
        }
Rahix's avatar
Rahix committed
75
76
    }
}