~starkingdoms/starkingdoms

ref: 8cd3d04f86d10b98bf70ca240fd96acd3f55e253 starkingdoms/crates/unified/src/client/starguide/init.rs -rw-r--r-- 2.9 KiB
8cd3d04fghostly_zsh fix: orbit prediction follows hill spheres 16 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use bevy::anti_alias::fxaa::Fxaa;
use bevy::asset::RenderAssetUsages;
use bevy::core_pipeline::tonemapping::DebandDither;
use bevy::post_process::bloom::Bloom;
use bevy::render::render_resource::{Extent3d, TextureDimension, TextureFormat};
use bevy::window::PrimaryWindow;
use crate::prelude::*;
use crate::ecs::{Me, ORBIT_LAYER, OrbitCamera, Part, STARGUIDE_LAYER, StarguideCamera, StarguideMe, StarguideOrbit, StarguideOrbitImage};

pub fn starguide_init_plugin(app: &mut App) {
    app
        .add_systems(Startup, init_starguide)
        .add_systems(Update, player_init)
        .add_systems(Update, player_position_update);
}

pub fn init_starguide(
    mut images: ResMut<Assets<Image>>,
    window: Single<&Window, With<PrimaryWindow>>,
    mut commands: Commands,
) {
    commands.spawn((Camera2d::default(), Camera {
        is_active: false,
        clear_color: ClearColorConfig::Custom(Color::linear_rgba(0.0, 0.0, 0.0, 1.0)),
        order: 0,
        ..default()
    }))
    .insert(ORBIT_LAYER)
    .insert(OrbitCamera);

    commands.spawn((Camera2d::default(), Camera {
        is_active: false,
        //clear_color: ClearColorConfig::None,
        clear_color: ClearColorConfig::Custom(Color::linear_rgba(0.0, 0.0, 0.0, 1.0)),
        order: 1,
        ..default()
    }))
    .insert(Bloom::default())
    .insert(DebandDither::Enabled)
    .insert(Fxaa::default())
    .insert(STARGUIDE_LAYER)
    .insert(StarguideCamera);

    let image = Image::new_fill(
        Extent3d {
            width: window.width() as u32,
            height: window.height() as u32,
            depth_or_array_layers: 1,
        }, TextureDimension::D2,
        &(Color::BLACK.to_srgba().to_u8_array()),
        TextureFormat::Rgba8UnormSrgb,
        RenderAssetUsages::MAIN_WORLD | RenderAssetUsages::RENDER_WORLD,
    );
    let handle = images.add(image);
    commands.spawn((Sprite::from_image(handle.clone()), STARGUIDE_LAYER, StarguideOrbit))
        .insert(Transform::from_xyz(0.0, 0.0, -10.0));
    commands.insert_resource(StarguideOrbitImage(handle));
}

pub fn player_init(
    me: Single<(Entity, &Sprite, &Part), Added<Me>>,
    mut commands: Commands,
    asset_server: Res<AssetServer>,
) {
    let mut sprite = Sprite::from_image(asset_server.load(&me.2.strong_config.part.sprite_connected));
    sprite.custom_size = Some(Vec2::new(
        me.2.strong_config.physics.width,
        me.2.strong_config.physics.height,
    ));
    commands.spawn((sprite, StarguideMe, STARGUIDE_LAYER,
            Transform::from_scale(Vec3::splat(10.0))));
}

fn player_position_update(
    me: Single<&Transform, (Changed<Transform>, With<Me>)>,
    //mut orbit: Single<&mut Transform, (With<StarguideOrbit>, Without<Me>)>,
    mut starguide_me: Single<&mut Transform, (With<StarguideMe>, Without<StarguideOrbit>, Without<Me>)>,
) {
    starguide_me.translation = me.translation;
    starguide_me.rotation = me.rotation;
}