~starkingdoms/starkingdoms

ref: 0958067ee6628f0d20bb3efe520a7b0c30cc71ef starkingdoms/starkingdoms-client/src/lib.rs -rw-r--r-- 5.1 KiB
0958067e — ghostly_zsh rotation fixed, camera transform works, camera gui added 11 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
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 egui::{Context, DragValue};
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;

// 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,
        zoom: 1.0,
    });

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

    world.spawn(SpriteBundle {
        position: Translation {
            x: 100.0,
            y: 100.0
        },
        scale: Scale {
            width: 100.0,
            height: 100.0,
        },
        rotation: Rotation {
            radians: 45.0_f32.to_radians()
        },
        texture: SpriteTexture {
            texture: "happy-tree".to_string(),
        },
    });

    let event_loop = EventLoop::new().unwrap();
    event_loop.set_control_flow(ControlFlow::Poll);
    event_loop
        .run_app(&mut App::new(world, 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 = if raw_delta < 0.0 {
            raw_delta * -0.9
        } else {
            raw_delta * 1.1
        };

        if 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 Scale, &SpriteTexture, &mut Rotation)>();
                for (mut pos, 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("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("Camera Zoom");
                        ui.add(DragValue::new(&mut camera.zoom).speed(0.1));
                        ui.end_row();
                    });
            });
    }
}