~starkingdoms/starkingdoms

ref: ade2542fc7b644f54ab5b03a33cf11dca10a740c starkingdoms/crates/unified/src/client/starguide/orbit.rs -rw-r--r-- 2.0 KiB
ade2542fghostly_zsh fix: world config is on client too now 17 days 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
use bevy::window::WindowResized;

use crate::{ecs::{Me, StarguideCamera, StarguideOrbit, StarguideOrbitImage}, prelude::*};
use bevy::render::render_resource::Extent3d;

pub fn starguide_orbit_plugin(app: &mut App) {
    app
        .add_systems(Update, (update_orbits, window_resize));
}

fn update_orbits(
    orbit_image: Res<StarguideOrbitImage>,
    mut images: ResMut<Assets<Image>>,
    camera: Single<(&Camera, &GlobalTransform, &Projection), With<StarguideCamera>>,
    me: Single<&Transform, (With<Me>, Without<StarguideCamera>)>,
    orbit: Single<&Transform, (With<StarguideOrbit>, Without<Me>, Without<StarguideCamera>)>,
) {
    let Some(image) = images.get_mut(&orbit_image.0) else {
        error!("Orbit prediction image not found");
        return
    };
    let Projection::Orthographic(ref projection) = camera.2.clone() else { return };
    image.clear(&(Color::BLACK.to_srgba().to_u8_array()));

    let player_pos = me.translation - orbit.translation;
    let player_pos = Vec3::new(player_pos.x, -player_pos.y, player_pos.z);
    let player_pos = (player_pos / projection.scale).truncate();
    let player_pos = player_pos + image.size_f32()/2.0;
    let player_pos = player_pos.as_uvec2();

    for i in (player_pos.x)..(player_pos.x+100) {
        if !(i >= image.size().x || player_pos.y >= image.size().y) {
            image.set_color_at(i, player_pos.y, Color::linear_rgb(1.0, 0.0, 0.0)).unwrap();
        }
    }
}

fn window_resize(
    orbit_image: Res<StarguideOrbitImage>,
    mut images: ResMut<Assets<Image>>,
    mut resize_ev: MessageReader<WindowResized>,
    camera: Single<&Camera, With<StarguideCamera>>,
) {
    let Some(image) = images.get_mut(&orbit_image.0) else {
        error!("Orbit prediction image not found");
        return
    };
    for _ in resize_ev.read() {
        let Some(Vec2 {x: width, y: height}) = camera.logical_viewport_size() else {
            continue
        };
        image.resize(Extent3d {
            width: width as u32,
            height: height as u32,
            depth_or_array_layers: 1,
        });
    }
}