@@ -23,7 +23,15 @@ use dogoap::prelude::*;
23
23
24
24
fn main ( ) {
25
25
let mut app = App :: new ( ) ;
26
- register_components ! ( app, vec![ Thirst , CarryingItem ] ) ;
26
+ // Customer components + actions
27
+ register_components ! ( app, vec![ Thirst , CarryingItem , PlacedOrder , OrderReady ] ) ;
28
+ register_actions ! (
29
+ app,
30
+ vec![ DrinkLemonade , PickupLemonade , WaitForOrder , PlaceOrder ]
31
+ ) ;
32
+ // Worker components + actions
33
+ register_components ! ( app, vec![ Energy ] ) ;
34
+ register_actions ! ( app, vec![ Rest ] ) ;
27
35
28
36
app. add_plugins ( DefaultPlugins . set ( WindowPlugin {
29
37
primary_window : Some ( Window {
@@ -40,21 +48,41 @@ fn main() {
40
48
. run ( ) ;
41
49
}
42
50
43
- // LocalFields
51
+ // LocalFields for customer
44
52
45
53
#[ derive( Component , Clone , DatumComponent ) ]
46
54
struct Thirst ( f64 ) ;
47
55
56
+ #[ derive( Component , Clone , EnumComponent ) ]
57
+ struct CarryingItem ( Item ) ;
58
+
59
+ #[ derive( Component , Clone , DatumComponent ) ]
60
+ struct PlacedOrder ( bool ) ;
61
+
48
62
#[ derive( Component , Clone , DatumComponent ) ]
49
- struct CarryingItem ( usize ) ; // `Items::Lemonade as usize` for example
63
+ struct OrderReady ( bool ) ;
64
+
65
+ // Actions for customer
50
66
51
- // Actions
67
+ #[ derive( Component , Clone , Default , ActionComponent ) ]
68
+ struct DrinkLemonade ;
69
+
70
+ #[ derive( Component , Clone , Default , ActionComponent ) ]
71
+ struct PickupLemonade ;
52
72
53
73
#[ derive( Component , Clone , Default , ActionComponent ) ]
54
- struct DrinkLemonade ( ) ;
74
+ struct WaitForOrder ;
55
75
56
76
#[ derive( Component , Clone , Default , ActionComponent ) ]
57
- struct PickupLemonade ( ) ;
77
+ struct PlaceOrder ;
78
+
79
+ // DatumComponents for worker
80
+
81
+ #[ derive( Component , Clone , DatumComponent ) ]
82
+ struct Energy ( f64 ) ;
83
+
84
+ #[ derive( Component , Clone , Default , ActionComponent ) ]
85
+ struct Rest ;
58
86
59
87
// Markers
60
88
@@ -67,7 +95,7 @@ struct Customer;
67
95
#[ derive( Component ) ]
68
96
struct Worker ;
69
97
70
- #[ derive( Clone , Default , Reflect ) ]
98
+ #[ derive( Clone , Default , Copy , Reflect ) ]
71
99
enum Item {
72
100
#[ default]
73
101
Nothing ,
@@ -87,59 +115,56 @@ struct StateDebugText;
87
115
fn setup ( mut commands : Commands ) {
88
116
// Spawn customers
89
117
for _i in 0 ..1 {
90
- let goal = Goal :: new ( ) . with_req ( & Thirst :: key ( ) , Compare :: LessThanEquals ( Datum :: F64 ( 1.0 ) ) ) ;
91
-
92
- let goals = vec ! [ goal. clone( ) ] ;
118
+ let goal = Goal :: from_reqs ( & [ Thirst :: is_less ( 1.0 ) ] ) ;
93
119
94
120
// Requires us to carry a lemonade, results in us having 10 less thirst + carrying Nothing
95
- let drink_lemonade_action = Action :: new ( & DrinkLemonade :: key ( ) )
96
- . with_precondition (
97
- & CarryingItem :: key ( ) ,
98
- Compare :: Equals ( Datum :: Enum ( Item :: Lemonade as usize ) ) ,
99
- )
100
- . with_effect (
101
- Effect :: new ( & DrinkLemonade :: key ( ) )
102
- . with_mutator ( Mutator :: Set (
103
- CarryingItem :: key ( ) ,
104
- Datum :: Enum ( Item :: Nothing as usize ) ,
105
- ) )
106
- . with_mutator ( Mutator :: Decrement ( Thirst :: key ( ) , Datum :: F64 ( 10.0 ) ) ) ,
107
- ) ;
121
+ let drink_lemonade_action = DrinkLemonade :: new ( )
122
+ . add_precondition ( CarryingItem :: is ( Item :: Lemonade ) )
123
+ . add_mutator ( CarryingItem :: set ( Item :: Nothing ) )
124
+ . add_mutator ( Thirst :: decrease ( 10.0 ) ) ;
108
125
109
126
// Requires us to not be carrying nothing, and leads to us having a lemonade
110
- let pickup_lemonade_action = Action :: new ( & PickupLemonade :: key ( ) )
111
- . with_precondition (
112
- & CarryingItem :: key ( ) ,
113
- Compare :: Equals ( Datum :: Enum ( Item :: Nothing as usize ) ) ,
114
- )
115
- . with_effect (
116
- Effect :: new ( & PickupLemonade :: key ( ) ) . with_mutator ( Mutator :: Set (
117
- CarryingItem :: key ( ) ,
118
- Datum :: Enum ( Item :: Lemonade as usize ) ,
119
- ) ) ,
120
- ) ;
121
-
122
- let actions_map = create_action_map ! (
123
- ( DrinkLemonade , drink_lemonade_action) ,
124
- ( PickupLemonade , pickup_lemonade_action)
125
- ) ;
126
-
127
- let initial_state = ( Thirst ( 0.0 ) , CarryingItem ( Item :: Nothing as usize ) ) ;
128
- let state = create_state ! ( Thirst ( 0.0 ) , CarryingItem ( Item :: Nothing as usize ) ) ;
129
-
130
- let mut planner = Planner :: new ( state, goals, actions_map) ;
127
+ let pickup_lemonade_action = PickupLemonade :: new ( )
128
+ . add_precondition ( CarryingItem :: is ( Item :: Nothing ) )
129
+ . add_mutator ( CarryingItem :: set ( Item :: Lemonade ) ) ;
130
+
131
+ let wait_for_order_action = WaitForOrder :: new ( )
132
+ . add_precondition ( PlacedOrder :: is ( true ) )
133
+ . add_precondition ( OrderReady :: is ( false ) )
134
+ . add_mutator ( OrderReady :: set ( true ) ) ;
135
+
136
+ let place_order_action = PlaceOrder :: new ( )
137
+ . add_precondition ( PlacedOrder :: is ( false ) )
138
+ . add_mutator ( PlacedOrder :: set ( true ) ) ;
139
+
140
+ // Drink Lemonade
141
+ // Pickup Lemonade
142
+ // Wait for Order
143
+ // Place Order
144
+ // Go To Order Desk
145
+
146
+ let ( mut planner, components) = create_planner ! ( {
147
+ actions: [
148
+ ( DrinkLemonade , drink_lemonade_action) ,
149
+ ( PickupLemonade , pickup_lemonade_action)
150
+ ] ,
151
+ state: [ Thirst ( 0.0 ) , CarryingItem ( Item :: Nothing ) ] ,
152
+ goals: [ goal] ,
153
+ } ) ;
131
154
132
155
planner. remove_goal_on_no_plan_found = false ; // Don't remove the goal
133
156
planner. always_plan = true ; // Re-calculate our plan whenever we can
134
157
planner. current_goal = Some ( goal. clone ( ) ) ;
135
158
159
+ let t = Transform :: from_scale ( Vec3 :: splat ( 2.0 ) ) ;
160
+
136
161
commands
137
162
. spawn ( (
138
163
Agent ,
139
164
Name :: new ( "Customer" ) ,
140
165
Customer ,
141
166
planner,
142
- initial_state ,
167
+ components ,
143
168
TransformBundle :: from ( Transform :: from_xyz ( -200.0 , -100.0 , 1.0 ) ) ,
144
169
) )
145
170
. with_children ( |subcommands| {
@@ -164,11 +189,25 @@ fn setup(mut commands: Commands) {
164
189
165
190
// Spawn worker
166
191
for _i in 0 ..1 {
192
+ let goal = Goal :: from_reqs ( & [ Energy :: is_more ( 1.0 ) ] ) ;
193
+
194
+ let rest_action = Rest :: new ( )
195
+ . add_precondition ( Energy :: is_less ( 10.0 ) )
196
+ . add_mutator ( Energy :: increase ( 50.0 ) ) ;
197
+
198
+ let ( planner, components) = create_planner ! ( {
199
+ actions: [ ( Rest , rest_action) ] ,
200
+ state: [ Energy ( 50.0 ) ] ,
201
+ goals: [ goal] ,
202
+ } ) ;
203
+
167
204
commands
168
205
. spawn ( (
169
206
Agent ,
170
207
Name :: new ( "Worker" ) ,
171
208
Worker ,
209
+ planner,
210
+ components,
172
211
TransformBundle :: from ( Transform :: from_xyz ( 0.0 , 0.0 , 1.0 ) ) ,
173
212
) )
174
213
. with_children ( |subcommands| {
@@ -214,7 +253,7 @@ fn handle_pickup_lemonade(
214
253
match progresses. get_mut ( & entity) {
215
254
Some ( progress) => {
216
255
if progress. tick ( time. delta ( ) ) . just_finished ( ) {
217
- state. 0 = Item :: Lemonade as usize ;
256
+ state. 0 = Item :: Lemonade ;
218
257
commands. entity ( entity) . remove :: < PickupLemonade > ( ) ;
219
258
progresses. remove ( & entity) ;
220
259
} else {
@@ -239,7 +278,7 @@ fn handle_drink_lemonade(
239
278
match progresses. get_mut ( & entity) {
240
279
Some ( progress) => {
241
280
if progress. tick ( time. delta ( ) ) . just_finished ( ) {
242
- state. 0 = Item :: Nothing as usize ;
281
+ state. 0 = Item :: Nothing ;
243
282
thirst. 0 = ( thirst. 0 - 5.0 ) . max ( 0.0 ) ;
244
283
245
284
commands. entity ( entity) . remove :: < DrinkLemonade > ( ) ;
@@ -266,9 +305,10 @@ fn update_thirst(time: Res<Time>, mut query: Query<&mut Thirst>) {
266
305
}
267
306
268
307
fn draw_state_debug (
269
- q_customers : Query < ( Entity , & Name , & Thirst , & Children ) , With < Customer > > ,
270
- q_customer_actions : Query < ( Option < & DrinkLemonade > , Option < & PickupLemonade > ) > ,
271
- q_workers : Query < ( Entity , & Name , & Children ) , With < Worker > > ,
308
+ q_planners : Query < ( Entity , & Name , & Children ) , With < Planner > > ,
309
+ // q_workers: Query<(Entity, &Name, &Children), With<Worker>>,
310
+ q_actions : Query < ( Entity , & dyn ActionComponent ) > ,
311
+ q_datums : Query < ( Entity , & dyn DatumComponent ) > ,
272
312
// q_worker_actions: Query<(Option<>)>
273
313
// q_actions: Query<(
274
314
// Option<&IsPlanning>,
@@ -278,42 +318,56 @@ fn draw_state_debug(
278
318
// )>,
279
319
mut q_child : Query < & mut Text , With < StateDebugText > > ,
280
320
) {
281
- for ( entity, name, thirst, children) in q_customers. iter ( ) {
282
- let thirst = thirst. 0 ;
283
-
321
+ for ( entity, name, children) in q_planners. iter ( ) {
284
322
let mut current_action = "Idle" ;
285
323
286
- let ( drink_lemonade_action, pickup_lemonade_action) =
287
- q_customer_actions. get ( entity) . unwrap ( ) ;
288
-
289
- if drink_lemonade_action. is_some ( ) {
290
- current_action = "Drinking Lemonade" ;
324
+ // Get current action, should always be one so grab the first one we find
325
+ for ( _entity, actions) in q_actions. get ( entity) . iter ( ) {
326
+ for action in actions. iter ( ) {
327
+ current_action = action. action_type_name ( ) ;
328
+ break ;
329
+ }
291
330
}
292
331
293
- if pickup_lemonade_action. is_some ( ) {
294
- current_action = "Picking up Lemonade" ;
332
+ // Concat all the found DatumComponents for this entity
333
+ let mut state: String = "" . to_string ( ) ;
334
+ for ( _entity, data) in q_datums. get ( entity) . iter ( ) {
335
+ for datum in data. iter ( ) {
336
+ state = format ! (
337
+ "{}\n {}: {}" ,
338
+ state,
339
+ datum. field_key( ) . to_string( ) ,
340
+ match datum. field_value( ) {
341
+ Datum :: Bool ( v) => v. to_string( ) ,
342
+ Datum :: F64 ( v) => format!( "{:.2}" , v) . to_string( ) ,
343
+ Datum :: I64 ( v) => format!( "{}" , v) . to_string( ) ,
344
+ Datum :: Enum ( v) => format!( "{}" , v) . to_string( ) ,
345
+ }
346
+ ) ;
347
+ }
295
348
}
296
349
350
+ // Render it out
297
351
for & child in children. iter ( ) {
298
352
let mut text = q_child. get_mut ( child) . unwrap ( ) ;
299
353
text. sections [ 0 ] . value =
300
- format ! ( "{name}\n {current_action}\n Thirst : {thirst:.2} \n Entity: {entity }" ) ;
354
+ format ! ( "{name}\n {current_action}\n Entity : {entity} \n --- \n {state }" ) ;
301
355
}
302
356
}
303
- for ( entity, name, children) in q_workers. iter ( ) {
304
- let current_action = "Idle" ;
357
+ // for (entity, name, children) in q_workers.iter() {
358
+ // let current_action = "Idle";
305
359
306
- // let (is_planning, eat, go_to_mushroom, replicate) = q_actions.get(entity).unwrap();
360
+ // // let (is_planning, eat, go_to_mushroom, replicate) = q_actions.get(entity).unwrap();
307
361
308
- // if is_planning.is_some() {
309
- // current_action = "Planning...";
310
- // }
362
+ // // if is_planning.is_some() {
363
+ // // current_action = "Planning...";
364
+ // // }
311
365
312
- for & child in children. iter ( ) {
313
- let mut text = q_child. get_mut ( child) . unwrap ( ) ;
314
- text. sections [ 0 ] . value = format ! ( "{name}\n {current_action}\n Entity: {entity}" ) ;
315
- }
316
- }
366
+ // for &child in children.iter() {
367
+ // let mut text = q_child.get_mut(child).unwrap();
368
+ // text.sections[0].value = format!("{name}\n{current_action}\nEntity: {entity}");
369
+ // }
370
+ // }
317
371
}
318
372
319
373
fn draw_ui (
0 commit comments