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

Rahix's avatar
Rahix committed
10
11
12
13
pub struct LevelLoadingState {
    node_world: legion::World,
    resources: legion::Resources,
    node_schedule: legion::Schedule,
14
15

    player: components::Player,
Rahix's avatar
Rahix committed
16
    assigned_shift: Option<angel_shifts::AngelShift>,
Rahix's avatar
Rahix committed
17
18
19
}

impl LevelLoadingState {
Rahix's avatar
Rahix committed
20
21
22
23
    pub fn new(
        player: components::Player,
        assigned_shift: angel_shifts::AngelShift,
    ) -> LevelLoadingState {
Rahix's avatar
Rahix committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
        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,
42
            player,
Rahix's avatar
Rahix committed
43
            assigned_shift: Some(assigned_shift),
Rahix's avatar
Rahix committed
44
45
46
47
        }
    }
}

Rahix's avatar
Rahix committed
48
impl gamestate::State for LevelLoadingState {
Rahix's avatar
Rahix committed
49
50
51
    fn init(&mut self, init: gamestate::StateInitializer) -> gamestate::Transition {
        let mut rendering = resources::Rendering::new("game-canvas").unwrap();
        let handle = init.get_handle();
52
        let player = self.player.clone();
Rahix's avatar
Rahix committed
53
        let assigned_shift = self.assigned_shift.take().unwrap();
Rahix's avatar
Rahix committed
54
        wasm_bindgen_futures::spawn_local(async move {
Rahix's avatar
Rahix committed
55
56
57
58
59
60
61
            let level_name = assigned_shift.level_name();
            let level = svg_loader::SvgLevel::load_from_svg_file(&format!(
                "resources/levels/{}",
                level_name
            ))
            .await
            .unwrap();
Simon Goller's avatar
Simon Goller committed
62
63
64
65

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

            handle.do_transition(gamestate::Transition::replace(states::InGameState::new(
68
69
70
                level,
                rendering,
                player.clone(),
71
                assigned_shift,
Rahix's avatar
Rahix committed
72
73
            )));
        });
Rahix's avatar
Rahix committed
74
        gamestate::Transition::Loop
Rahix's avatar
Rahix committed
75
76
    }

Rahix's avatar
Rahix committed
77
    fn update(&mut self) -> gamestate::Transition {
Rahix's avatar
Rahix committed
78
79
        {
            let rendering = self.resources.get_mut::<resources::Rendering>().unwrap();
80
            rendering.set_fill_style(&colors::BACKGROUND);
81
            rendering.fill_rect(0.0, 0.0, 1920.0, 1080.0);
Rahix's avatar
Rahix committed
82
83
84
85
86
        }

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

Rahix's avatar
Rahix committed
87
        gamestate::Transition::Loop
Rahix's avatar
Rahix committed
88
89
    }
}