~starkingdoms/starkingdoms

ref: b07b08dc56f85bd4f62f5873a1b96d506b321bb9 starkingdoms/crates/unified/src/client/ui.rs -rw-r--r-- 2.2 KiB
b07b08dcghostly_zsh fix: trying to fix gravity 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
59
60
61
62
63
64
use crate::prelude::*;

use crate::{
    client::colors,
    ecs::{FuelText, Player, PlayerStorage, PowerText, MainCamera, MAIN_LAYER},
};
use crate::client::rendering::setup_graphics;

pub fn ui_plugin(app: &mut App) {
    app.add_systems(Startup, setup_ui.after(setup_graphics))
        .add_systems(Update, update_ui);
}

fn setup_ui(camera: Single<Entity, (With<MainCamera>, With<Camera>)>, mut commands: Commands) {
    let ui_id = commands.spawn((
        UiTargetCamera(camera.into_inner()),
        Node {
            width: Val::Percent(100.0),
            height: Val::Percent(100.0),
            ..Default::default()
        },
        Transform::from_xyz(0.0, 0.0, 25.0),
        MAIN_LAYER,
        children![(
            Node {
                display: Display::Flex,
                position_type: PositionType::Absolute,
                width: Val::Px(200.0),
                height: Val::Px(150.0),
                top: Val::Px(5.0),
                right: Val::Px(5.0),
                ..Default::default()
            },
            MAIN_LAYER,
            BorderRadius::all(Val::Px(5.0)),
            BackgroundColor(colors::MANTLE),
            children![
                (TextColor(colors::PEACH), Text::new("Fuel: 25"), FuelText,),
                (TextColor(colors::PEACH), Text::new("Power: 25"), PowerText,),
                /*(
                    Node {
                        width: Val::Percent(100.0),
                        height: Val::Px(20.0),
                        margin: UiRect::all(Val::Px(5.0)),
                        ..Default::default()
                    },
                    BorderRadius::all(Val::Px(5.0)),
                    BackgroundColor(colors::CRUST),
                )*/
            ],
        )],
        Visibility::Visible
    )).id();
    debug!(?ui_id, "spawned ui component");
}

fn update_ui(
    mut fuel_text: Single<&mut Text, With<FuelText>>,
    mut power_text: Single<&mut Text, (With<PowerText>, Without<FuelText>)>,
    player: Single<&PlayerStorage, (With<Player>, Without<PowerText>)>,
) {
    fuel_text.0 = format!("Fuel: {}/{}", player.fuel, player.fuel_capacity);
    power_text.0 = format!("Power: {}/{}", player.power, player.power_capacity);
}