~starkingdoms/starkingdoms

ref: 103444e57478b2f702c26a6dfb9a62b34a60e5fe starkingdoms/crates/client/src/lib.rs -rw-r--r-- 3.2 KiB
103444e5 — core chore(wasm)!: fix compilations on webassembly 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
use crate::components::PlayerResources;
use crate::components::Schedule::{Startup, Update};
use crate::networking::handlers::{
    handle_crafting_ui, handle_despawn_part, handle_energy_update, handle_existing_players_list,
    handle_login_response, handle_message, handle_part_positions, handle_player_leave,
    handle_spawn_part, handle_spawn_player,
};
use crate::networking::websocket::Websocket;
use crate::systems::create_hearty;
use bevy_ecs::schedule::Schedule;
use bevy_ecs::{event::Events, world::World};
use components::{Camera, Chat, RecvPacket, SendPacket};
use networking::handlers::{handle_planet_positions, handle_spawn_planet};
use platform::assets::Assets;
use platform::websocket::Ws;
use rendering::assets::AssetLoader;
use rendering::App;
use tracing::info;
use winit::event_loop::{ControlFlow, EventLoop};

#[cfg(target_arch = "wasm32")]
#[path = "wasm/mod.rs"]
pub mod platform;
#[cfg(not(target_arch = "wasm32"))]
#[path = "native/mod.rs"]
pub mod platform;

pub mod components;
pub mod networking;
pub mod rendering;
pub mod systems;
pub mod ui;

// Hi, you've found the real main function! This is called AFTER platform-specific initialization code.
pub fn start() {
    info!(
        "Hello, world! StarKingdoms.TK v{} says hello, running on {}",
        env!("CARGO_PKG_VERSION"),
        if cfg!(target_arch = "wasm32") {
            "wasm"
        } else {
            "native"
        }
    );

    info!("Creating the ECS world...");
    let mut world = World::new();

    world.insert_resource(Camera {
        x: 0.0,
        y: 0.0,
        zoom: 1.0,
        width: 0,
        height: 0,
    });

    world.insert_resource(Assets::new());
    world.insert_resource(Ws::new());
    world.insert_resource(PlayerResources {
        fuel_amount: 0,
        fuel_max: 0,
    });
    world.insert_resource(Chat {
        messages: vec![],
        textbox: String::new(),
    });

    let send_packet_events = Events::<SendPacket>::default();
    let recv_packet_events = Events::<RecvPacket>::default();

    starkingdoms_common::packet::register_packet_events(&mut world);

    let mut startup_schedule = Schedule::new(Startup);

    startup_schedule.add_systems(create_hearty);

    let mut update_schedule = Schedule::new(Update);

    update_schedule.add_systems(handle_existing_players_list);
    update_schedule.add_systems(handle_spawn_player);
    update_schedule.add_systems(handle_spawn_part);
    update_schedule.add_systems(handle_part_positions);
    update_schedule.add_systems(handle_spawn_planet);
    update_schedule.add_systems(handle_planet_positions);
    update_schedule.add_systems(handle_energy_update);
    update_schedule.add_systems(handle_crafting_ui);
    update_schedule.add_systems(handle_message);
    update_schedule.add_systems(handle_player_leave);
    update_schedule.add_systems(handle_despawn_part);
    update_schedule.add_systems(handle_login_response);

    // add systems here

    world.add_schedule(startup_schedule);
    world.add_schedule(update_schedule);

    world.run_schedule(Startup);

    let event_loop = EventLoop::new().unwrap();
    event_loop.set_control_flow(ControlFlow::Wait);

    event_loop
        .run_app(&mut App::new(world, send_packet_events, recv_packet_events))
        .unwrap();
}