heaven.rs 5.58 KB
Newer Older
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;
Rahix's avatar
Rahix committed
6
use crate::states;
7
8
9
10
use crate::utils;

pub struct HeavenState {
    gui_svg: web_sys::SvgElement,
11

Rahix's avatar
Rahix committed
12
13
14
    node_world: legion::World,
    resources: legion::Resources,
    node_schedule: legion::Schedule,
15
16

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

impl HeavenState {
21
    pub fn new(player: Option<components::Player>) -> HeavenState {
22
        let (node_world, resources, node_schedule) = init_orbiting_nodes();
Rahix's avatar
Rahix committed
23

24
25
26
27
28
        let player = player.unwrap_or_else(|| {
            // Initialize the game as this is the first run.
            components::Player::new()
        });

Rahix's avatar
Rahix committed
29
30
31
32
33
34
35
36
        // Update the angel stats GUI elements
        utils::get_element_by_id::<web_sys::Element>("heaven-collected-hours")
            .unwrap()
            .set_inner_html(&format!("{}", player.collected_hours));
        utils::get_element_by_id::<web_sys::Element>("heaven-needed-hours")
            .unwrap()
            .set_inner_html(&format!("{}", 30 - player.collected_hours));

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
        // Assign a random shift
        let assigned_shift = angel_shifts::generate_random_shift(&mut rand::thread_rng());

        // Display the shift info
        let shift_meta = assigned_shift.metadata();
        utils::get_element_by_id::<web_sys::Element>("heaven-shift-title")
            .unwrap()
            .set_inner_html(&shift_meta.title);
        utils::get_element_by_id::<web_sys::Element>("heaven-shift-description")
            .unwrap()
            .set_inner_html(&shift_meta.description);
        utils::get_element_by_id::<web_sys::Element>("heaven-shift-hours")
            .unwrap()
            .set_inner_html(&format!("{}", shift_meta.hours));

52
53
        HeavenState {
            gui_svg: utils::get_element_by_id("heaven-ui").unwrap(),
Rahix's avatar
Rahix committed
54
55
56
            node_world,
            resources,
            node_schedule,
57
            player,
Rahix's avatar
Rahix committed
58
            assigned_shift: Some(assigned_shift),
59
60
61
62
        }
    }
}

Rahix's avatar
Rahix committed
63
impl gamestate::State for HeavenState {
64
    fn init(&mut self, mut init: gamestate::StateInitializer) -> gamestate::Transition {
65
66
67
68
69
        init.register_onclick("heaven-start-shift");
        self.gui_svg
            .style()
            .set_property("display", "block")
            .unwrap();
Rahix's avatar
Rahix committed
70
        gamestate::Transition::Loop
71
72
73
74
75
76
77
78
79
    }

    fn deinit(&mut self) {
        self.gui_svg
            .style()
            .set_property("display", "none")
            .unwrap();
    }

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

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

Rahix's avatar
Rahix committed
90
        gamestate::Transition::Loop
Rahix's avatar
Rahix committed
91
92
    }

93
94
95
96
97
    fn event(&mut self, event: gamestate::Event) -> gamestate::Transition {
        match event {
            gamestate::Event::MouseClick {
                target: "heaven-start-shift",
                ..
Rahix's avatar
Rahix committed
98
99
100
101
            } => gamestate::Transition::replace(states::LevelLoadingState::new(
                self.player.clone(),
                self.assigned_shift.take().unwrap(),
            )),
102
103
            event => {
                crate::console_log!("unknown event {:?}", event);
Rahix's avatar
Rahix committed
104
                gamestate::Transition::Keep
105
106
107
108
            }
        }
    }
}
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167

fn init_orbiting_nodes() -> (legion::World, legion::Resources, legion::Schedule) {
    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();
    let n0 = node_world.push((
        components::Node::new(),
        components::Position::new(800.0, 340.0),
        components::OrbitBody::new(5.0, 10.0),
        components::Gravity,
    ));
    let n1 = node_world.push((
        components::Node::new(),
        components::Position::new(120.0, 300.0),
        components::OrbitBody::new(5.0, -5.0),
        components::Gravity,
    ));
    let n2 = node_world.push((
        components::Node::new(),
        components::Position::new(700.0, 740.0),
        components::OrbitBody::new(8.0, -4.0),
        components::Gravity,
    ));
    let n3 = node_world.push((
        components::Node::new(),
        components::Position::new(340.0, 290.0),
        components::OrbitBody::new(10.0, 0.0),
        components::Gravity,
    ));
    let n4 = node_world.push((
        components::Node::new(),
        components::Position::new(300.0, 400.0),
        components::OrbitBody::new(0.0, 10.0),
        components::Gravity,
    ));

    node_world.push((components::Edge::new(n0, n1),));
    node_world.push((components::Edge::new(n1, n2),));
    node_world.push((components::Edge::new(n0, n2),));
    node_world.push((components::Edge::new(n2, n3),));
    node_world.push((components::Edge::new(n3, n4),));
    node_world.push((components::Edge::new(n0, n4),));

    node_world.push((components::TheSun, components::Position::new(600.0, 540.0)));

    let node_schedule = legion::Schedule::builder()
        .add_system(components::update_gravity_system())
        .add_system(components::update_movement_system())
        .add_thread_local(components::update_nodes_system())
        .flush()
        .add_thread_local(components::draw_edges_system())
        .add_thread_local(components::draw_nodes_system())
        .add_thread_local(components::draw_thesun_system())
        .build();

    (node_world, resources, node_schedule)
}