@@ -7,7 +7,8 @@ use crate::gameplay::{
77 assets:: { ItemDef , Transport } ,
88 inventory:: Inventory ,
99 } ,
10- logistics:: pathfinding:: { PorterPaths , WalkPath } ,
10+ people:: pathfinding:: PorterPaths ,
11+ people:: { Houses , Person } ,
1112 recipe:: { assets:: Recipe , select:: SelectedRecipe } ,
1213 sprite_sort:: { YSortSprite , ZIndexSprite } ,
1314} ;
@@ -25,46 +26,39 @@ pub(super) fn plugin(app: &mut App) {
2526#[ derive( Component , Reflect , Deref , DerefMut ) ]
2627#[ reflect( Component ) ]
2728#[ require( PorterSpawnOutputIndex ) ]
28- pub struct PorterSpawnTimer ( pub Timer ) ;
29+ pub struct PorterCooldown ( pub Timer ) ;
2930
3031#[ derive( Component , Reflect , Deref , DerefMut , Default ) ]
3132#[ reflect( Component ) ]
3233struct PorterSpawnOutputIndex ( usize ) ;
3334
34- #[ derive( Component , Reflect ) ]
35- #[ reflect( Component ) ]
36- #[ relationship_target( relationship = PorterOf ) ]
37- pub struct Porters ( Vec < Entity > ) ;
38-
39- #[ derive( Component , Reflect ) ]
40- #[ reflect( Component ) ]
41- #[ relationship( relationship_target = Porters ) ]
42- pub struct PorterOf ( pub Entity ) ;
43-
44- #[ derive( Component , Reflect ) ]
45- #[ reflect( Component ) ]
46- pub struct PorterTo ( pub Entity ) ;
47-
48- #[ derive( Component , Reflect ) ]
49- #[ reflect( Component ) ]
50- pub struct PortingItem ( pub AssetId < ItemDef > ) ;
51-
5235#[ derive( Message , Reflect , Debug ) ]
5336pub struct PorterArrival ( pub Entity ) ;
5437
5538#[ derive( Message , Reflect , Debug ) ]
5639pub struct PorterLost ( pub Entity ) ;
5740
41+ #[ derive( Component , Reflect , Debug ) ]
42+ #[ reflect( Component ) ]
43+ pub struct Porting {
44+ pub item : AssetId < ItemDef > ,
45+ pub source : Entity ,
46+ pub destination : Entity ,
47+ pub path : Vec < Entity > ,
48+ }
49+
5850fn spawn_porter (
5951 structure_query : Query < (
6052 Entity ,
6153 & Transform ,
62- & mut PorterSpawnTimer ,
54+ & mut PorterCooldown ,
6355 & mut PorterSpawnOutputIndex ,
6456 & SelectedRecipe ,
6557 & mut Inventory ,
6658 & mut PorterPaths ,
59+ & Houses ,
6760 ) > ,
61+ person_query : Query < ( ) , ( With < Person > , Without < Porting > ) > ,
6862 mut commands : Commands ,
6963 item_defs : Res < Assets < ItemDef > > ,
7064 recipes : Res < Assets < Recipe > > ,
@@ -79,12 +73,17 @@ fn spawn_porter(
7973 selected_recipe,
8074 mut inventory,
8175 mut porter_paths,
76+ houses,
8277 ) in structure_query
8378 {
8479 if !timer. tick ( time. delta ( ) ) . is_finished ( ) {
8580 continue ;
8681 }
8782
83+ let Some ( person) = houses. iter ( ) . find ( |e| person_query. contains ( * e) ) else {
84+ continue ;
85+ } ;
86+
8887 let Some ( recipe) = recipes. get ( & selected_recipe. 0 ) else {
8988 continue ;
9089 } ;
@@ -101,16 +100,15 @@ fn spawn_porter(
101100 continue ;
102101 }
103102
104- let Some ( ( input , path) ) = porter_paths. 0 . front ( ) else {
103+ let Some ( ( destination , path) ) = porter_paths. 0 . front ( ) else {
105104 continue ;
106105 } ;
107106
108107 let Some ( item_def) = item_defs. get ( * item_id) else {
109108 continue ;
110109 } ;
111110
112- commands. spawn ( (
113- Name :: new ( "Porter" ) ,
111+ commands. entity ( person) . insert ( (
114112 * transform,
115113 Sprite :: default ( ) ,
116114 Anchor ( Vec2 :: new ( 0.0 , -0.25 ) ) ,
@@ -121,13 +119,14 @@ fn spawn_porter(
121119 Transport :: Bag => Animation :: tag ( "walk_bag" ) ,
122120 } ,
123121 } ,
124- Inventory :: with_single ( * item_id, 1 ) ,
125122 YSortSprite ,
126123 ZIndexSprite ( 10 ) ,
127- PorterOf ( structure) ,
128- PorterTo ( * input) ,
129- PortingItem ( * item_id) ,
130- WalkPath ( path. clone ( ) ) ,
124+ Porting {
125+ item : * item_id,
126+ source : structure,
127+ destination : * destination,
128+ path : path. clone ( ) ,
129+ } ,
131130 ) ) ;
132131
133132 * quantity -= 1 ;
@@ -139,25 +138,27 @@ fn spawn_porter(
139138
140139fn despawn_lost_porters ( mut porter_losses : MessageReader < PorterLost > , mut commands : Commands ) {
141140 for PorterLost ( entity) in porter_losses. read ( ) {
142- commands. entity ( * entity) . despawn ( ) ;
141+ commands. entity ( * entity) . remove :: < ( Sprite , Porting ) > ( ) ;
143142 }
144143}
145144
146145fn drop_of_items (
147146 mut poter_arrivals : MessageReader < PorterArrival > ,
148- porters : Query < ( & PortingItem , & PorterTo ) > ,
147+ porters : Query < & Porting > ,
149148 mut structures : Query < & mut Inventory > ,
150149 mut commands : Commands ,
151150) {
152151 for PorterArrival ( porter) in poter_arrivals. read ( ) {
153- commands. entity ( * porter) . despawn ( ) ;
152+ commands. entity ( * porter) . remove :: < ( Sprite , Porting ) > ( ) ;
154153
155- let ( PortingItem ( item_id) , PorterTo ( structure) ) = porters. get ( * porter) . unwrap ( ) ;
154+ let Ok ( porting) = porters. get ( * porter) else {
155+ continue ;
156+ } ;
156157
157- if let Ok ( mut inventory) = structures. get_mut ( * structure ) {
158+ if let Ok ( mut inventory) = structures. get_mut ( porting . destination ) {
158159 inventory
159160 . items
160- . entry ( * item_id )
161+ . entry ( porting . item )
161162 . and_modify ( |q| * q += 1 )
162163 . or_insert ( 1 ) ;
163164 }
0 commit comments