heaven.rs 4.34 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;
Rahix's avatar
Rahix committed
5
use crate::states;
6
7
8
9
use crate::utils;

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

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

    player: components::Player,
16
17
18
}

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

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

27
28
        HeavenState {
            gui_svg: utils::get_element_by_id("heaven-ui").unwrap(),
Rahix's avatar
Rahix committed
29
30
31
            node_world,
            resources,
            node_schedule,
32
            player,
33
34
35
36
        }
    }
}

Rahix's avatar
Rahix committed
37
impl gamestate::State for HeavenState {
38
    fn init(&mut self, mut init: gamestate::StateInitializer) -> gamestate::Transition {
39
40
41
42
43
        init.register_onclick("heaven-start-shift");
        self.gui_svg
            .style()
            .set_property("display", "block")
            .unwrap();
Rahix's avatar
Rahix committed
44
        gamestate::Transition::Loop
45
46
47
48
49
50
51
52
53
    }

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

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

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

Rahix's avatar
Rahix committed
64
        gamestate::Transition::Loop
Rahix's avatar
Rahix committed
65
66
    }

67
68
69
70
71
    fn event(&mut self, event: gamestate::Event) -> gamestate::Transition {
        match event {
            gamestate::Event::MouseClick {
                target: "heaven-start-shift",
                ..
72
73
74
            } => {
                gamestate::Transition::replace(states::LevelLoadingState::new(self.player.clone()))
            }
75
76
            event => {
                crate::console_log!("unknown event {:?}", event);
Rahix's avatar
Rahix committed
77
                gamestate::Transition::Keep
78
79
80
81
            }
        }
    }
}
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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

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)
}