ingame.rs 5.95 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, &level, player);
Grisu's avatar
Grisu committed
42
        let obstacles = entities::create_stationary_obstacles(&mut world, &level);
Simon Goller's avatar
Simon Goller committed
43

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

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

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

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

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

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

91
        init.register_keyevents();
Rahix's avatar
Rahix committed
92
        gamestate::Transition::Loop
Rahix's avatar
Rahix committed
93
94
95
    }

    fn deinit(&mut self) {
96
97
98
99
100
101
        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
102
103
104
105
106
        self.gui_svg
            .style()
            .set_property("display", "none")
            .unwrap();
    }
107

108
    fn event(&mut self, event: gamestate::Event) -> gamestate::Transition {
109
110
111
112
113
114
        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();
115
        match event {
Simon Goller's avatar
Simon Goller committed
116
            gamestate::Event::KeyDown("w") => player_movable.velocity.y = -300.0,
117
            gamestate::Event::KeyUp("w") => player_movable.velocity.y = 0.0,
Simon Goller's avatar
Simon Goller committed
118
            gamestate::Event::KeyDown("a") => player_movable.velocity.x = -300.0,
119
            gamestate::Event::KeyUp("a") => player_movable.velocity.x = 0.0,
Simon Goller's avatar
Simon Goller committed
120
            gamestate::Event::KeyDown("s") => player_movable.velocity.y = 300.0,
121
            gamestate::Event::KeyUp("s") => player_movable.velocity.y = 0.0,
Simon Goller's avatar
Simon Goller committed
122
            gamestate::Event::KeyDown("d") => player_movable.velocity.x = 300.0,
123
            gamestate::Event::KeyUp("d") => player_movable.velocity.x = 0.0,
124
125
            _ => (),
        }
Simon Goller's avatar
Simon Goller committed
126
127
128
        if player_movable.velocity.x != 0.0 || player_movable.velocity.y != 0.0 {
            player_movable.velocity = player_movable.velocity.normalize() * 300.0;
        }
Rahix's avatar
Rahix committed
129
        gamestate::Transition::Keep
130
131
    }

Simon Goller's avatar
Simon Goller committed
132
    fn update(&mut self, timestamp: f64) -> gamestate::Transition {
Rahix's avatar
Rahix committed
133
134
        use legion::IntoQuery;

Simon Goller's avatar
Simon Goller committed
135
136
137
138
139
        self.resources
            .get_mut::<resources::Clock>()
            .unwrap()
            .update(timestamp);

140
141
        {
            let rendering = self.resources.get::<resources::Rendering>().unwrap();
Rahix's avatar
Rahix committed
142
            rendering.set_transform(1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
143
            rendering.set_fill_style(&colors::BACKGROUND);
144
145
            rendering.fill_rect(0.0, 0.0, 1920.0, 1080.0);
        }
146
        self.schedule.execute(&mut self.world, &mut self.resources);
Rahix's avatar
Rahix committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161

        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
        }
162
    }
Rahix's avatar
Rahix committed
163
}