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