~starkingdoms/starkingdoms

ref: 494c0b1ca0e58be47c24cf6fd937b57dea1cd1f5 starkingdoms/crates/client/src/lib.rs -rw-r--r-- 6.4 KiB
494c0b1c — ghostly_zsh player and parts rendering, but jk it doesnt work 9 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use crate::ecs::{Camera, Translation, Rotation, Scale, SpriteBundle, SpriteTexture};
use crate::input::MouseWheelEvent;
use crate::rendering::ui::UiRenderable;
use crate::rendering::App;
use bevy_ecs::event::{EventReader, Events};
use bevy_ecs::schedule::Schedule;
use bevy_ecs::system::ResMut;
use bevy_ecs::world::World;
use ecs::{Player, RecvPacket, SendPacket, Shear};
use egui::{Context, DragValue};
use networking::ws::Ws;
use rendering::assets::Assets;
use starkingdoms_common::packet::Packet;
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 ecs;
pub mod input;
pub mod rendering;
pub mod networking;

// 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::<Events<MouseWheelEvent>>(Events::default());
    world.insert_resource(Camera {
        x: 0.0,
        y: 0.0,
        shear_x: 0.0,
        shear_y: 0.0,
        zoom: 1.0,
    });
    world.insert_resource(Assets::new());
    world.insert_resource(Ws::new());

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

    let mut start_schedule = Schedule::default();
    // Add startup things here
    // Caution: This will run before there are things on-screen
    start_schedule.run(&mut world);

    let mut update_schedule = Schedule::default();
    // Add things to run every frame here
    // Caution: This will run once before there are things on screen
    update_schedule.add_systems(zoom_camera_on_mouse_events);

    send_packet_events.send(SendPacket(Packet::ClientLogin {
        username: String::new(),
        save: None,
        jwt: None,
    }));

    world.spawn((SpriteBundle {
        position: Translation {
            x: 100.0,
            y: 100.0
        },
        shear: Shear {
            x: 0.0,
            y: 0.0,
        },
        scale: Scale {
            width: 100.0,
            height: 100.0,
        },
        rotation: Rotation {
            radians: 0.0_f32.to_radians()
        },
        texture: SpriteTexture {
            texture: "hearty".to_string(),
        },
    }, Player));

    let event_loop = EventLoop::new().unwrap();
    event_loop.set_control_flow(ControlFlow::Poll);
    event_loop
        .run_app(&mut App::new(world, send_packet_events, recv_packet_events, update_schedule, Gui {}))
        .unwrap();
}

fn zoom_camera_on_mouse_events(mut events: EventReader<MouseWheelEvent>, mut camera: ResMut<Camera>) {
    for event in events.read() {
        let raw_delta = match event {
            MouseWheelEvent::Line { y, .. } => *y,
            MouseWheelEvent::Pixel { y, ..} => *y,
        } as f32;

        let delta = 1.1;

        if raw_delta < 0.0 {
            camera.zoom *= 1.0 / delta;
        } else {
            camera.zoom *= delta;
        }
    }
}

pub struct Gui {}
impl UiRenderable for Gui {
    fn render(&mut self, ctx: &Context, world: &mut World) {
        egui::Window::new("Main Menu")
            .resizable(false)
            .show(ctx, |ui| {
                ui.heading("StarKingdoms.TK");
                ui.label("A game about floating through space");
                ui.separator();

                let mut sprites = world.query::<(&mut Translation, &mut Shear, &mut Scale, &SpriteTexture, &mut Rotation)>();
                /*for (mut pos, mut shear, mut scale, tex, mut rot) in sprites.iter_mut(world) {
                    ui.heading(&tex.texture);
                    
                    egui::Grid::new("sprite_grid")
                        .num_columns(2)
                        .spacing([40.0, 4.0])
                        .striped(true)
                        .show(ui, |ui| {
                            ui.label("X");
                            ui.add(DragValue::new(&mut pos.x).speed(0.1));
                            ui.end_row();

                            ui.label("Y");
                            ui.add(DragValue::new(&mut pos.y).speed(0.1));
                            ui.end_row();

                            ui.label("Shear X");
                            ui.add(DragValue::new(&mut shear.x).speed(0.1));
                            ui.end_row();

                            ui.label("Shear Y");
                            ui.add(DragValue::new(&mut shear.y).speed(0.1));
                            ui.end_row();

                            ui.label("Width");
                            ui.add(DragValue::new(&mut scale.width).speed(0.1));
                            ui.end_row();

                            ui.label("Height");
                            ui.add(DragValue::new(&mut scale.height).speed(0.1));
                            ui.end_row();

                            ui.label("Rotation");
                            ui.add(DragValue::new(&mut rot.radians).speed(0.1));
                            ui.end_row();
                        });
                }
                let mut camera = world.resource_mut::<Camera>();
                egui::Grid::new("camera_grid")
                    .num_columns(2)
                    .spacing([40.0, 4.0])
                    .show(ui, |ui| {
                        ui.label("Camera X");
                        ui.add(DragValue::new(&mut camera.x).speed(0.1));
                        ui.end_row();

                        ui.label("Camera Y");
                        ui.add(DragValue::new(&mut camera.y).speed(0.1));
                        ui.end_row();

                        ui.label("Shear X");
                        ui.add(DragValue::new(&mut camera.shear_x).speed(0.1));
                        ui.end_row();
                        ui.label("Shear Y");
                        ui.add(DragValue::new(&mut camera.shear_y).speed(0.1));
                        ui.end_row();

                        ui.label("Camera Zoom");
                        ui.add(DragValue::new(&mut camera.zoom).speed(0.1));
                        ui.end_row();
                    });*/
            });
    }
}