~starkingdoms/starkingdoms

ref: 1018826380f46e306958358cc687d9a5302b2be6 starkingdoms/crates/unified/src/ship_editor/mod.rs -rw-r--r-- 5.0 KiB
10188263ghostly_zsh ship editor feat: selection and deselection 3 hours 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
pub mod ui;
pub mod input;
pub mod plugins;
pub mod components;
pub mod select;

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, OutlineCamera, Part, PartConfigHolder, PlayerPartRequest, Selectable, ShipEditorConfigHolder, SpawnPartRequest, GHOST_RENDER_LAYER, MAIN_RENDER_LAYER, OUTLINE_RENDER_LAYER};
use crate::ship_editor::input::input_plugin;
use crate::ship_editor::select::click_select;
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,
            clear_color: ClearColorConfig::Custom(colors::BASE),
            ..Default::default()
        },
        Transform::from_xyz(0.0, 0.0, 0.0),
        OutlineCamera,
        OUTLINE_RENDER_LAYER,
    ));
    commands.spawn((
        Camera2d,
        Camera {
            order: 1,
            clear_color: ClearColorConfig::None,
            ..Default::default()
        },
        Transform::from_xyz(0.0, 0.0, 0.0),
        IsDefaultUiCamera,
        MainCamera,
        MAIN_RENDER_LAYER,
    ));
    commands.spawn((
        Camera2d::default(),
        Camera {
            order: 2,
            clear_color: ClearColorConfig::None,
            ..Default::default()
        },
        GhostCamera,
        GHOST_RENDER_LAYER,
    ));
    let rectangle = meshes.add(Rectangle::new(50.0, 50.0));
    commands.spawn((
        PlayerPartRequest,
        MAIN_RENDER_LAYER,
        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()))
                .insert(Pickable::default())
                .insert(Selectable::default())
                .observe(click_select)
                .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));
    }
}