Skip to content

Commit 8ef3470

Browse files
committed
妻子和矿工的互动状态BUG 解决
1 parent 391f230 commit 8ef3470

File tree

9 files changed

+55
-14
lines changed

9 files changed

+55
-14
lines changed

StateMachineLearn/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
{
99
FSM =
1010
{
11-
GlobalState = new WifeGlobalState()
11+
GlobalState = WifeGlobalState.Instance
1212
}
1313
};
1414

StateMachineLearn/WestWord/MinerOwnedState.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,9 @@ public override void Enter(Miner owner)
303303
}
304304
owner.CurrentLocation = Location.LocationType.Home;
305305

306-
/*
307-
错误的处理方式!!,不应该在 Enter 的时候去发消息,让其他关联对象改变状态,
308-
如果 Enter 的时候发消息了,会造成,一个 Update (逻辑帧) 里面处理多个状态的迁移,而应该在 Execute 里面进行处理
309306
// 2. 通知妻子自己回家了
310307
MessageDispatcher.Instance.DispatchMessage(EntityName.EntityElsa, owner.Name,
311308
ConstDefine.MessageType.HiHoneyImHome, 0, null);
312-
*/
313309

314310
WriteExt.WriteBgWhiteAndFgYellow($"MinerId:{owner.InsId}, GoHomeAndSleepTilRestedState,回到家里,并告知妻子自己回来了");
315311
}
@@ -326,10 +322,6 @@ public override void Execute(Miner owner)
326322
return;
327323
}
328324

329-
// 0. 发送回家的通知
330-
MessageDispatcher.Instance.DispatchMessage(owner.Name, EntityName.EntityElsa,
331-
ConstDefine.MessageType.HiHoneyImHome, 0, null); // todo:解决硬编码问题
332-
333325
owner.CurrentTirednessThreshold--;
334326
WriteExt.WriteBgWhiteAndFgBlue($"MinerId:{owner.InsId}, GoHomeAndSleepTilRestedState,在家里休息中");
335327

@@ -377,7 +369,7 @@ public override bool OnMessage(in Telegram message, Miner owner)
377369
// 1. 处理吃饭
378370
case ConstDefine.MessageType.StewReady:
379371
{
380-
WriteExt.WriteBgWhiteAndFgRed($"minderId:{owner.InsId}, 收到肉煮好的消息");
372+
WriteExt.WriteBgWhiteAndFgRed($"minderId:{owner.InsId}, 开始在家里吃肉");
381373
owner.FSM.ChangeState(EatStew.Instance);
382374
return true;
383375
}

StateMachineLearn/WestWord/StateMachine.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ public bool ChangeState(IState<TOwner> state)
7979
/// <returns></returns>
8080
public bool RevertToPreviousState()
8181
{
82-
return ChangeState(PreviousState);
82+
// 1. 之前的状态与当前状态不一样,则进行状态反转,并返回true
83+
return !IsInState(PreviousState) && ChangeState(PreviousState);
8384
}
8485

8586
/// <summary>

StateMachineLearn/WestWord/WifeOwnedState.cs

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ public static WifeInitState Instance
7676
/// </summary>
7777
public class VisitBathroom : State<Wife>
7878
{
79+
private VisitBathroom()
80+
{
81+
}
82+
7983
#region Implementation of IState<in Wife>
8084

8185
/// <summary>
@@ -91,7 +95,7 @@ public override void Enter(Wife owner)
9195
}
9296

9397
// 1. 将妻子放置到家里
94-
owner.CurrentLocation= Location.Home;
98+
owner.CurrentLocation= Location.BathRoom;
9599

96100
WriteExt.WriteBgWhiteAndFgYellow($"WifeId:{entity.InsId}, 初始化状态:在家里");
97101
}
@@ -102,7 +106,14 @@ public override void Enter(Wife owner)
102106
/// <param name="owner"></param>
103107
public override void Execute(Wife owner)
104108
{
105-
owner.CurrentTirednessThreshold++;
109+
// 1. 清空基类的疲劳度
110+
owner.CurrentTirednessThreshold = 0;
111+
112+
// 2. 反转回以前的状态
113+
owner.FSM.RevertToPreviousState();
114+
115+
// 3. 打日志
116+
WriteExt.WriteBgWhiteAndFgYellow($"WifeId:{owner.InsId}, 去洗手间");
106117
}
107118

108119
/// <summary>
@@ -111,10 +122,34 @@ public override void Execute(Wife owner)
111122
/// <param name="owner"></param>
112123
public override void Exit(Wife owner)
113124
{
114-
throw new NotImplementedException();
125+
WriteExt.WriteBgWhiteAndFgRed($"wifeId{owner.InsId} 退出去洗手间");
115126
}
116127

117128
#endregion
129+
130+
#region Singleton
131+
132+
/// <summary>
133+
/// 对象缓存
134+
/// </summary>
135+
private static VisitBathroom? m_instance;
136+
137+
/// <summary>
138+
/// 对象获取接口
139+
/// </summary>
140+
public static VisitBathroom Instance
141+
{
142+
get
143+
{
144+
if (m_instance == null)
145+
{
146+
m_instance = new VisitBathroom();
147+
}
148+
return m_instance;
149+
}
150+
}
151+
152+
#endregion
118153
}
119154

120155
/// <summary>
@@ -213,6 +248,11 @@ public static DoHouseWork Instance
213248
/// </summary>
214249
public class WifeGlobalState :State<Wife>
215250
{
251+
private WifeGlobalState()
252+
{
253+
m_instance = this;
254+
}
255+
216256
#region Implementation of IState<in Wife>
217257

218258
/// <summary>
@@ -236,6 +276,14 @@ public override void Enter(Wife owner)
236276
/// <param name="owner"></param>
237277
public override void Execute(Wife owner)
238278
{
279+
// 1. 检查是否需要切换状态
280+
if (owner.IsNeedToGoBathroom())
281+
{
282+
owner.FSM.ChangeState(VisitBathroom.Instance);
283+
}
284+
285+
// 2. 打日志
286+
WriteExt.WriteBgWhiteAndFgBlue($"WifeId:{owner.InsId}, 全局状态");
239287
}
240288

241289
/// <summary>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)