ingame.rs 5.46 KB
Newer Older
1
use crate::angel_shifts;
Rahix's avatar
Rahix committed
2
use crate::colliders;
3
use crate::colors;
4
use crate::components;
Rahix's avatar
Rahix committed
5
use crate::entities;
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;
Rahix's avatar
Rahix committed
9
use crate::svg_loader;
10
use crate::systems;
Rahix's avatar
Rahix committed
11
12
13
14
use crate::utils;

pub struct InGameState {
    gui_svg: web_sys::SvgElement,
15
16
17
    world: legion::World,
    resources: legion::Resources,
    schedule: legion::Schedule,
18
    registered_images: Vec<resources::ImageHandle>,
Rahix's avatar
Rahix committed
19
20
21
}

impl InGameState {
22
23
24
25
    pub fn new(
        level: svg_loader::SvgLevel,
        mut rendering: resources::Rendering,
        player: components::Player,
26
        assigned_shift: angel_shifts::AngelShift,
27
    ) -> InGameState {
Simon Goller's avatar
Simon Goller committed
28
29
        let sanity_bar = utils::get_element_by_id("sanity-amount").unwrap();

Rahix's avatar
Rahix committed
30
31
        let foreground = rendering.register_image(level.foreground_image.clone());
        let background = rendering.register_image(level.background_image.clone());
32

33
        let mut resources = legion::Resources::default();
Rahix's avatar
Rahix committed
34
35
        let mut world = legion::World::default();

36
        resources.insert(resources::Clock::new());
37
38
        resources.insert(rendering);
        resources.insert(resources::Camera::new(1920.0, 1080.0));
Rahix's avatar
Rahix committed
39
        resources.insert(colliders::CollisionWorld::new());
40

41
        let player = entities::create_player(&mut world, &resources, &level, player);
Simon Goller's avatar
Simon Goller committed
42

Rahix's avatar
Rahix committed
43
        resources.insert(resources::Player(player));
Rahix's avatar
Rahix committed
44
        resources.insert(resources::GameManager::new());
Rahix's avatar
Rahix committed
45
        level.apply_colliders(&mut world);
46

47
48
        let mut schedule_builder = legion::Schedule::builder();
        schedule_builder
Rahix's avatar
Rahix committed
49
50
51
52
            .add_system(colliders::synchronize_collisision_world_system())
            .flush()
            .add_system(colliders::update_collision_world_system())
            .flush()
53
            .add_thread_local(systems::move_movable_system())
54
55
56
57
58
            .flush();

        assigned_shift.init_gameworld(&mut world, &mut resources, &mut schedule_builder, &level);

        schedule_builder
59
            .flush()
60
            .add_thread_local(systems::move_camera_to_player_system())
Rahix's avatar
Rahix committed
61
            .add_thread_local(systems::camera_system())
Rahix's avatar
Rahix committed
62
            .add_thread_local(systems::update_sanity_bar_system(sanity_bar))
63
            .add_thread_local(systems::draw_level_layer_system(background))
Simon Goller's avatar
Simon Goller committed
64
            .add_thread_local(systems::draw_sprites_system())
65
            .add_thread_local(systems::draw_level_layer_system(foreground))
robert's avatar
robert committed
66
            .add_thread_local(systems::draw_tmp_matebottledrop_system())
67
            // .add_thread_local(systems::draw_debug_colliders_system())
68
69
            ;
        let schedule = schedule_builder.build();
70

Rahix's avatar
Rahix committed
71
72
        InGameState {
            gui_svg: utils::get_element_by_id("ingame-ui").unwrap(),
73
74
75
            world,
            resources,
            schedule,
76
            registered_images: vec![foreground, background],
Rahix's avatar
Rahix committed
77
78
79
80
        }
    }
}

Rahix's avatar
Rahix committed
81
impl gamestate::State for InGameState {
82
    fn init(&mut self, mut init: gamestate::StateInitializer) -> gamestate::Transition {
Rahix's avatar
Rahix committed
83
84
85
86
87
        self.gui_svg
            .style()
            .set_property("display", "block")
            .unwrap();

88
        init.register_keyevents();
Rahix's avatar
Rahix committed
89
        gamestate::Transition::Loop
Rahix's avatar
Rahix committed
90
91
92
    }

    fn deinit(&mut self) {
93
94
95
96
97
98
        let mut rendering = self.resources.get_mut::<resources::Rendering>().unwrap();
        for image in &self.registered_images {
            rendering.deregister_image(image);
        }

        rendering.set_transform(1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
Rahix's avatar
Rahix committed
99
100
101
102
103
        self.gui_svg
            .style()
            .set_property("display", "none")
            .unwrap();
    }
104

105
    fn event(&mut self, event: gamestate::Event) -> gamestate::Transition {
106
107
108
109
110
111
        use legion::IntoQuery;

        let player = self.resources.get::<resources::Player>().unwrap().0.clone();
        let player_movable = <&mut components::Movable>::query()
            .get_mut(&mut self.world, player)
            .unwrap();
112
        match event {
113
            gamestate::Event::KeyDown("w") => player_movable.velocity.y = -50.0,
114
            gamestate::Event::KeyUp("w") => player_movable.velocity.y = 0.0,
115
            gamestate::Event::KeyDown("a") => player_movable.velocity.x = -50.0,
116
            gamestate::Event::KeyUp("a") => player_movable.velocity.x = 0.0,
117
            gamestate::Event::KeyDown("s") => player_movable.velocity.y = 50.0,
118
            gamestate::Event::KeyUp("s") => player_movable.velocity.y = 0.0,
119
            gamestate::Event::KeyDown("d") => player_movable.velocity.x = 50.0,
120
            gamestate::Event::KeyUp("d") => player_movable.velocity.x = 0.0,
121
122
            _ => (),
        }
Rahix's avatar
Rahix committed
123
        gamestate::Transition::Keep
124
125
    }

Rahix's avatar
Rahix committed
126
    fn update(&mut self) -> gamestate::Transition {
Rahix's avatar
Rahix committed
127
128
        use legion::IntoQuery;

129
130
        {
            let rendering = self.resources.get::<resources::Rendering>().unwrap();
Rahix's avatar
Rahix committed
131
            rendering.set_transform(1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
132
            rendering.set_fill_style(&colors::BACKGROUND);
133
134
            rendering.fill_rect(0.0, 0.0, 1920.0, 1080.0);
        }
135
        self.schedule.execute(&mut self.world, &mut self.resources);
Rahix's avatar
Rahix committed
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150

        if self
            .resources
            .get::<resources::GameManager>()
            .unwrap()
            .wants_return_to_heaven()
        {
            let player_ent = self.resources.get::<resources::Player>().unwrap().0.clone();
            let player = <&components::Player>::query()
                .get(&self.world, player_ent)
                .unwrap();
            gamestate::Transition::replace(states::HeavenState::new(Some(player.clone())))
        } else {
            gamestate::Transition::Loop
        }
151
    }
Rahix's avatar
Rahix committed
152
}