level_loading.rs 2.11 KB
Newer Older
1
use crate::colors;
Rahix's avatar
Rahix committed
2
use crate::components;
Rahix's avatar
Rahix committed
3
use crate::gamestate;
Rahix's avatar
Rahix committed
4
use crate::resources;
Simon Goller's avatar
Simon Goller committed
5
use crate::sprites;
Rahix's avatar
Rahix committed
6
use crate::states;
7
use crate::svg_loader;
Simon Goller's avatar
Simon Goller committed
8

Rahix's avatar
Rahix committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
pub struct LevelLoadingState {
    node_world: legion::World,
    resources: legion::Resources,
    node_schedule: legion::Schedule,
}

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();

        LevelLoadingState {
            node_world,
            resources,
            node_schedule,
        }
    }
}

Rahix's avatar
Rahix committed
39
impl gamestate::State for LevelLoadingState {
Rahix's avatar
Rahix committed
40
41
42
43
44
45
46
    fn init(&mut self, init: gamestate::StateInitializer) -> gamestate::Transition {
        let mut rendering = resources::Rendering::new("game-canvas").unwrap();
        let handle = init.get_handle();
        wasm_bindgen_futures::spawn_local(async move {
            let level = svg_loader::SvgLevel::load_from_svg_file("resources/testlevel.svg")
                .await
                .unwrap();
Simon Goller's avatar
Simon Goller committed
47
48
49
50

            sprites::Sprite::Player
                .load_and_register(&mut rendering)
                .await;
Rahix's avatar
Rahix committed
51
52

            handle.do_transition(gamestate::Transition::replace(states::InGameState::new(
Simon Goller's avatar
Simon Goller committed
53
                level, rendering,
Rahix's avatar
Rahix committed
54
55
            )));
        });
Rahix's avatar
Rahix committed
56
        gamestate::Transition::Loop
Rahix's avatar
Rahix committed
57
58
    }

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

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

Rahix's avatar
Rahix committed
69
        gamestate::Transition::Loop
Rahix's avatar
Rahix committed
70
71
    }
}