~starkingdoms/starkingdoms

ref: ship-editor starkingdoms/crates/unified/src/ship_editor/mod.rs -rw-r--r-- 4.3 KiB
f85bb4deghostly_zsh ship editor fix: thruster attachment and connected part sprite an hour 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
pub mod ui;
pub mod input;
pub mod plugins;
pub mod components;

use bevy::input_focus::InputFocus;
use crate::client::colors;
use crate::prelude::*;
use crate::shared::attachment::{Joint, JointId, JointOf, SnapOf, SnapOfJoint};
use crate::shared::config::part::{JointConfig, PartConfig};
use crate::shared::config::ship_editor::ShipEditorConfig;
use crate::ship_editor::components::{GhostCamera, MainCamera, Part, PartConfigHolder, PlayerPartRequest, ShipEditorConfigHolder, SpawnPartRequest, GHOST_RENDER_LAYER, MAIN_RENDER_LAYER};
use crate::ship_editor::input::input_plugin;
use crate::ship_editor::ui::{ui_plugin, PendingPart};

pub struct ShipEditorPlugin;

impl Plugin for ShipEditorPlugin {
    fn build(&self, app: &mut App) {
        app
            .init_resource::<InputFocus>()
            .add_systems(Startup, setup)
            .add_systems(Update, (spawn_player_part_request, spawn_parts))
            .add_plugins(input_plugin)
            .add_plugins(ui_plugin);
    }
}

fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<ColorMaterial>>,
) {
    commands.insert_resource(ClearColor(colors::BASE));
    commands.spawn((
        Camera2d,
        Camera {
            order: 0,
            ..Default::default()
        },
        Transform::from_xyz(0.0, 0.0, 0.0),
        MainCamera,
        IsDefaultUiCamera,
        MAIN_RENDER_LAYER,
    ));
    commands.spawn((
        Camera2d::default(),
        Camera {
            order: 1,
            ..Default::default()
        },
        GhostCamera,
        GHOST_RENDER_LAYER,
    ));
    let rectangle = meshes.add(Rectangle::new(50.0, 50.0));
    commands.spawn((
        PlayerPartRequest,
        Transform::from_xyz(0.0, 0.0, 0.0),
    ));
}

fn spawn_player_part_request(
    player_part_request_query: Query<Entity, With<PlayerPartRequest>>,
    ship_editor_config_holder: Res<ShipEditorConfigHolder>,
    ship_editor_config: Res<Assets<ShipEditorConfig>>,
    mut commands: Commands,
    asset_server: Res<AssetServer>,
) {
    let Some(handle) = ship_editor_config_holder.handle.clone() else { return };
    if let Some(strong_ship_editor_config) = ship_editor_config.get(&handle) {
        let player_part = &strong_ship_editor_config.general.player_part;
        for entity in player_part_request_query {
            commands.entity(entity)
                .insert(SpawnPartRequest(asset_server.load(format!("config/parts/{}.part.toml", player_part.to_lowercase()))))
                .remove::<PlayerPartRequest>();
        }
    }
}
fn spawn_parts(
    spawn_part_query: Query<(Entity, &SpawnPartRequest)>,
    part_config: Res<Assets<PartConfig>>,
    asset_server: Res<AssetServer>,
    mut commands: Commands,
) {
    for (entity, request) in spawn_part_query.iter() {
        if let Some(strong_part_config) = part_config.get(&request.0) {
            let mut sprite = Sprite::from_image(asset_server.load(strong_part_config.part.sprite_connected.clone()));
            sprite.custom_size = Some(vec2(strong_part_config.physics.width as f32, strong_part_config.physics.height as f32));

            commands.entity(entity)
                .insert(sprite)
                .insert(Part)
                .insert(PartConfigHolder(strong_part_config.clone()))
                .remove::<SpawnPartRequest>();
            debug!("spawned part");
            spawn_joints(strong_part_config, entity, commands.reborrow());
        }
    }
}

fn spawn_joint_bundle(joint: &JointConfig, part: &PartConfig, parent: &Entity) -> impl Bundle {
    let j_comp = Joint {
        id: JointId::from_part_and_joint_id(part.part.name.clone(), joint.id.clone()),
        transform: joint.target.into(),
    };
    let joint_transform: Transform = j_comp.transform;
    let joint_of = JointOf(*parent);

    (j_comp, joint_transform, joint_of)
}
fn spawn_snap_bundle(joint: &JointConfig, parent: &Entity, p_joint: &Entity) -> impl Bundle {
    let snap_transform: Transform = joint.snap.into();
    let snap_for = SnapOf(*parent);
    let snap_of = SnapOfJoint(*p_joint);

    (snap_transform, snap_for, snap_of)
}
fn spawn_joints(config: &PartConfig, parent: Entity, mut commands: Commands) {
    for joint in &config.joints {
        let joint_id = commands
            .spawn(spawn_joint_bundle(joint, config, &parent))
            .id();
        commands.spawn(spawn_snap_bundle(joint, &parent, &joint_id));
    }
}