~starkingdoms/starkingdoms

ref: aa2aadee626d8d90bc9fb67699cda2a588a6a992 starkingdoms/starkingdoms-client/src/lib.rs -rw-r--r-- 3.0 KiB
aa2aadee — core switch to matricies for sprite positioning 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
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;
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");
            });
    }
}