|
| 1 | +use bevy::prelude::*; |
| 2 | + |
| 3 | +use crate::gameplay::{ |
| 4 | + FactorySystems, structure::Structure, world::terrain::Worldly, y_sort::YSort, |
| 5 | +}; |
| 6 | + |
| 7 | +pub(super) fn plugin(app: &mut App) { |
| 8 | + app.register_type::<Path>(); |
| 9 | + app.register_type::<Pathable>(); |
| 10 | + |
| 11 | + app.add_systems( |
| 12 | + Update, |
| 13 | + (build_paths,) |
| 14 | + .in_set(FactorySystems::Build) |
| 15 | + .run_if(on_event::<Pointer<DragDrop>>), |
| 16 | + ); |
| 17 | +} |
| 18 | + |
| 19 | +#[derive(Component, Reflect)] |
| 20 | +#[reflect(Component)] |
| 21 | +pub struct Path(Entity, Entity); |
| 22 | + |
| 23 | +#[derive(Component, Reflect, Default)] |
| 24 | +#[reflect(Component)] |
| 25 | +pub struct Pathable { |
| 26 | + pub walkable: bool, |
| 27 | +} |
| 28 | + |
| 29 | +impl Pathable { |
| 30 | + pub fn walkable() -> Self { |
| 31 | + Self { walkable: true } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +fn build_paths( |
| 36 | + mut events: EventReader<Pointer<DragDrop>>, |
| 37 | + asset_server: Res<AssetServer>, |
| 38 | + mut commands: Commands, |
| 39 | + pathables: Query<Entity, With<Pathable>>, |
| 40 | + transforms: Query<&Transform>, |
| 41 | +) { |
| 42 | + for event in events.read() { |
| 43 | + let target = event.target; |
| 44 | + let dropped = event.dropped; |
| 45 | + |
| 46 | + if !pathables.contains(target) || !pathables.contains(dropped) { |
| 47 | + continue; |
| 48 | + } |
| 49 | + |
| 50 | + let Ok(from) = transforms.get(target) else { |
| 51 | + continue; |
| 52 | + }; |
| 53 | + |
| 54 | + let Ok(to) = transforms.get(dropped) else { |
| 55 | + continue; |
| 56 | + }; |
| 57 | + |
| 58 | + let direction = to.translation - from.translation; |
| 59 | + let rotation = Quat::from_rotation_z(direction.xy().to_angle()); |
| 60 | + |
| 61 | + commands.spawn(( |
| 62 | + Name::new("Path"), |
| 63 | + Path(target, dropped), |
| 64 | + Worldly, |
| 65 | + Transform::default() |
| 66 | + .with_translation(from.translation.midpoint(to.translation)) |
| 67 | + .with_rotation(rotation), |
| 68 | + Sprite { |
| 69 | + image: asset_server.load("sprites/logistics/path.png"), |
| 70 | + custom_size: Some(Vec2::new( |
| 71 | + to.translation.distance(from.translation) - 64.0, |
| 72 | + 32.0, |
| 73 | + )), |
| 74 | + image_mode: SpriteImageMode::Sliced(TextureSlicer { |
| 75 | + border: BorderRect { |
| 76 | + left: 16.0, |
| 77 | + right: 16.0, |
| 78 | + top: 0.0, |
| 79 | + bottom: 0.0, |
| 80 | + }, |
| 81 | + center_scale_mode: SliceScaleMode::Tile { stretch_value: 1.0 }, |
| 82 | + sides_scale_mode: SliceScaleMode::Tile { stretch_value: 1.0 }, |
| 83 | + max_corner_scale: 1.0, |
| 84 | + }), |
| 85 | + ..default() |
| 86 | + }, |
| 87 | + YSort(0.5), |
| 88 | + Structure, |
| 89 | + Pickable::default(), |
| 90 | + )); |
| 91 | + } |
| 92 | +} |
0 commit comments