1- namespace StateMachineLearn ;
1+ using System . Diagnostics ;
2+
3+ namespace StateMachineLearn ;
24using Location = ConstDefine . Location . LocationType ;
35
46/// <summary>
@@ -65,7 +67,8 @@ public override bool OnMessage(in Telegram message, Fly owner)
6567 }
6668
6769 // 2. 如果是通知在酒馆的状态 - 则随机按照一定概率去执行骚扰动作 - todo
68- owner . FSM . ChangState ( HarassmentState . Instance ) ;
70+ owner . FSM . ChangeState ( HarassmentState . Instance ) ;
71+
6972 return true ;
7073 }
7174
@@ -146,8 +149,12 @@ public override void Enter(Fly owner)
146149 /// <param name="owner"></param>
147150 public override void Execute ( Fly owner )
148151 {
149- WriteExt . WriteBgWhiteAndFgBlue ( $ "" +
150- $ "{ owner . Name } 运行苍蝇的骚扰人状态") ;
152+ // 1. 发消息给矿工,我要对你进行骚扰了
153+ MessageDispatcher . Instance . DispatchMessage ( EntityName . EntityMinerBob , EntityName . EntityFly ,
154+ ConstDefine . MessageType . MinerImFlyAttackU , 0 , null ) ;
155+
156+ // 2. 打日志
157+ WriteExt . WriteBgWhiteAndFgBlue ( $ "{ owner . Name } 发出了攻击矿工的消息") ;
151158 }
152159
153160 /// <summary>
@@ -173,10 +180,15 @@ public override bool OnMessage(in Telegram message, Fly owner)
173180 {
174181 return false ;
175182 }
183+
184+ // 2. 不是矿工的攻击消息,则返回 false
185+ if ( message . MessageType != ConstDefine . MessageType . FlyBeAttacked )
186+ {
187+ return false ;
188+ }
176189
177- // 2. 发信息给矿工
178- MessageDispatcher . Instance . DispatchMessage ( EntityName . EntityMinerBob , EntityName . EntityFly ,
179- ConstDefine . MessageType . MinerImFlyAttackU , 0 , null ) ;
190+ // 3. 切换状态到苍蝇的被攻击状态
191+ owner . FSM . ChangeState ( AttackedState . Instance ) ;
180192
181193 WriteExt . WriteBgWhiteAndFgBlue ( $ "{ owner . Name } 苍蝇正在骚扰矿工") ;
182194
@@ -218,6 +230,26 @@ public override void Execute(Fly owner)
218230 {
219231 // 1. 打日志
220232 WriteExt . WriteBgWhiteAndFgBlue ( $ "{ owner . Name } 苍蝇运行被攻击状态") ;
233+
234+
235+ // 2. 只有在酒馆中才能被攻击2. 只有在酒馆中才能被攻击
236+ Debug . Assert ( owner . CurrentLocation == Location . Saloon , "owner.CurrentLocation != Location.Saloon" ) ;
237+
238+ // 3. 未被击中 - 继续骚扰
239+ if ( DateTime . Now . Second % 2 != 0 )
240+ {
241+ return ;
242+ }
243+
244+ // 4 发信息通知矿工,不用继续攻击了,苍蝇投降了
245+ MessageDispatcher . Instance . DispatchMessage ( EntityName . EntityMinerBob , EntityName . EntityFly ,
246+ ConstDefine . MessageType . FlySurrender , 0 , null ) ;
247+
248+ // 5. 打日志
249+ WriteExt . WriteBgWhiteAndFgRed ( $ "{ owner . Name } 苍蝇被击中了,投降了") ;
250+
251+ // 6. 苍蝇对象的生命周期结束
252+ GameEntityManger . Instance . TryRemoveEntityByEntityInsId ( owner . InsId ) ;
221253 }
222254
223255 /// <summary>
@@ -238,27 +270,39 @@ public override void Exit(Fly owner)
238270 /// <returns></returns>
239271 public override bool OnMessage ( in Telegram message , Fly owner )
240272 {
241- if ( owner . CurrentLocation != Location . Saloon )
273+ if ( owner . CurrentLocation != Location . Saloon )
274+ {
275+ return false ;
276+ }
277+
278+ // 0. 消息不是矿工攻击苍蝇的消息,直接忽略
279+ if ( message . MessageType != ConstDefine . MessageType . FlyBeAttacked )
242280 {
243281 return false ;
244282 }
245283
246284 // 1. 被击中了
247- if ( DateTime . Now . Ticks % 2 == 0 )
285+ if ( DateTime . Now . Second % 2 == 0 )
248286 {
249287 // 1.1 切换状态到被击中状态
250288 // 1.1 发信息通知矿工,不用继续攻击了,苍蝇投降了
251289 MessageDispatcher . Instance . DispatchMessage ( EntityName . EntityMinerBob , EntityName . EntityFly ,
252290 ConstDefine . MessageType . FlySurrender , 0 , null ) ;
291+
292+ // 2. 打日志
253293 WriteExt . WriteBgWhiteAndFgRed ( $ "{ owner . Name } 苍蝇被击中了,投降了") ;
294+
295+ // 3. 苍蝇对象的生命周期结束
296+ GameEntityManger . Instance . TryRemoveEntityByEntityInsId ( owner . InsId ) ;
297+
254298 return true ;
255299 }
256300
257301 // 2. 未被击中
258302 if ( DateTime . Now . Ticks % 2 != 0 )
259303 {
260304 // 2.1 切换状态到躲避状态
261- owner . FSM . ChangState ( DodgeState . Instance ) ;
305+ owner . FSM . ChangeState ( DodgeState . Instance ) ;
262306 return true ;
263307 }
264308
@@ -332,9 +376,11 @@ public static HitBySomethingState Instance
332376 /// <param name="owner"></param>
333377 public override void Enter ( Fly owner )
334378 {
379+ /*
335380 // 1. 发消息给矿工,苍蝇被击中了
336381 MessageDispatcher.Instance.DispatchMessage(EntityName.EntityMinerBob, EntityName.EntityFly,
337382 ConstDefine.MessageType.FlySurrender, 0, null);
383+ */
338384
339385 // 2. 打日志
340386 WriteExt . WriteBgWhiteAndFgRed ( $ "{ owner . Name } 苍蝇被击中了,投降了") ;
@@ -346,8 +392,12 @@ public override void Enter(Fly owner)
346392 /// <param name="owner"></param>
347393 public override void Execute ( Fly owner )
348394 {
395+ // 0. 发消息给矿工 - 苍蝇被击落了
396+ MessageDispatcher . Instance . DispatchMessage ( EntityName . EntityMinerBob , EntityName . EntityFly ,
397+ ConstDefine . MessageType . FlySurrender , 0 , null ) ;
398+
349399 // 1. 更改为待机状态
350- owner . FSM . ChangState ( FlyGlobalState . Instance ) ;
400+ owner . FSM . ChangeState ( FlyGlobalState . Instance ) ;
351401
352402 // 2. 打日志
353403 WriteExt . WriteBgWhiteAndFgRed ( $ "{ owner . Name } 苍蝇待机,投降了") ;
@@ -396,7 +446,7 @@ public override void Enter(Fly owner)
396446 public override void Execute ( Fly owner )
397447 {
398448 // 1. 回到全局待机状态
399- owner . FSM . ChangState ( FlyGlobalState . Instance ) ;
449+ owner . FSM . ChangeState ( FlyGlobalState . Instance ) ;
400450
401451 // 2. 打日志
402452 WriteExt . WriteBgWhiteAndFgBlue ( $ "{ owner . Name } 苍蝇运行躲避状态") ;
0 commit comments