diff --git a/StateMachineLearn/WestWord/BaseGameEntity.cs b/StateMachineLearn/WestWord/BaseGameEntity.cs
index e270292..701e6ab 100644
--- a/StateMachineLearn/WestWord/BaseGameEntity.cs
+++ b/StateMachineLearn/WestWord/BaseGameEntity.cs
@@ -16,12 +16,12 @@ public interface IBaseGameEntity
/// 实例名字
///
public EntityName Name { get; }
-
+
///
/// 处理信息
///
///
- public void HandleMessage(Telegram msg);
+ public void HandleMessage(in Telegram msg);
}
///
@@ -53,7 +53,7 @@ protected BaseGameEntity(EntityName name)
/// 处理信息
///
///
- public virtual void HandleMessage(Telegram msg)
+ public virtual void HandleMessage(in Telegram msg)
{
}
diff --git a/StateMachineLearn/WestWord/MessageDispatcher.cs b/StateMachineLearn/WestWord/MessageDispatcher.cs
index 081c1ca..c05dfd6 100644
--- a/StateMachineLearn/WestWord/MessageDispatcher.cs
+++ b/StateMachineLearn/WestWord/MessageDispatcher.cs
@@ -73,7 +73,7 @@ public bool DispatchMessage(int sendInsId, int receiverInsId, ConstDefine.Messag
.Build();
// 4. 发送消息
- receiver!.HandleMessage(message);
+ receiver!.HandleMessage(in message);
return true;
}
@@ -110,7 +110,7 @@ public bool DispatchMessage(EntityName receiverName,
}
// 5. 发送消息
- receiver.HandleMessage(message);
+ receiver.HandleMessage(in message);
return true;
}
@@ -243,7 +243,7 @@ private void DispatchMessage(Telegram message)
var receiver = EntityManger.TryGetEntity(message.ReceiverInsId);
// 2. 派发消息
- receiver!.HandleMessage(message);
+ receiver!.HandleMessage(in message);
}
#endregion
diff --git a/StateMachineLearn/WestWord/Miner.cs b/StateMachineLearn/WestWord/Miner.cs
index 3bcd6ec..00f7080 100644
--- a/StateMachineLearn/WestWord/Miner.cs
+++ b/StateMachineLearn/WestWord/Miner.cs
@@ -81,6 +81,15 @@ public Miner(IState initState, IState preState, EntityName name) :
#region 覆写
+ ///
+ /// 处理信息
+ ///
+ ///
+ public override void HandleMessage(in Telegram msg)
+ {
+ FSM.HandleMessage(in msg);
+ }
+
///
/// 刷新条目当前的状态 - 每帧(每次循环)调用
///
diff --git a/StateMachineLearn/WestWord/MinerOwnedState.cs b/StateMachineLearn/WestWord/MinerOwnedState.cs
index 5de7255..9c7c663 100644
--- a/StateMachineLearn/WestWord/MinerOwnedState.cs
+++ b/StateMachineLearn/WestWord/MinerOwnedState.cs
@@ -1,5 +1,4 @@
using System.Diagnostics;
-using System.Globalization;
namespace StateMachineLearn;
using Behavior = ConstDefine.MinerBehavior;
@@ -303,8 +302,12 @@ public override void Enter(Miner owner)
return;
}
owner.CurrentLocation = Location.LocationType.Home;
+
+ // 2. 通知妻子自己回家了
+ MessageDispatcher.Instance.DispatchMessage(EntityName.EntityElsa, owner.Name,
+ ConstDefine.MessageType.HiHoneyImHome, 0, null);
- WriteExt.WriteBgWhiteAndFgYellow($"MinerId:{owner.InsId}, GoHomeAndSleepTilRestedState,回到家里");
+ WriteExt.WriteBgWhiteAndFgYellow($"MinerId:{owner.InsId}, GoHomeAndSleepTilRestedState,回到家里,并告知妻子自己回来了");
}
///
@@ -363,7 +366,7 @@ public override void Exit(Miner owner)
///
///
///
- public override bool OnMessage(Telegram message, Miner owner)
+ public override bool OnMessage(in Telegram message, Miner owner)
{
switch (message.MessageType)
{
@@ -564,7 +567,7 @@ public override void Exit(Miner owner)
///
///
///
- public override bool OnMessage(Telegram message, Miner owner)
+ public override bool OnMessage(in Telegram message, Miner owner)
{
throw new NotImplementedException();
}
diff --git a/StateMachineLearn/WestWord/State.cs b/StateMachineLearn/WestWord/State.cs
index e2edfd4..09da152 100644
--- a/StateMachineLearn/WestWord/State.cs
+++ b/StateMachineLearn/WestWord/State.cs
@@ -26,7 +26,7 @@ public interface IState where TOwner : class
///
///
///
- public bool OnMessage(Telegram message, Miner owner);
+ public bool OnMessage(in Telegram message, TOwner owner);
}
///
@@ -66,9 +66,9 @@ public virtual void Exit(TOwner owner)
///
///
///
- public virtual bool OnMessage(Telegram message, Miner owner)
+ public virtual bool OnMessage(in Telegram message, TOwner owner)
{
- return true;
+ return false;
}
#endregion
diff --git a/StateMachineLearn/WestWord/StateMachine.cs b/StateMachineLearn/WestWord/StateMachine.cs
index c26ffbe..64e1ae5 100644
--- a/StateMachineLearn/WestWord/StateMachine.cs
+++ b/StateMachineLearn/WestWord/StateMachine.cs
@@ -91,5 +91,22 @@ public bool IsInState(IState state)
{
return ReferenceEquals(CurrentState, state);
}
+
+ ///
+ /// 处理消息
+ ///
+ ///
+ ///
+ public bool HandleMessage(in Telegram msg)
+ {
+ // 1. 查看当前状态是否有效并且可以处理消息
+ if(CurrentState.OnMessage(in msg, Owner))
+ {
+ return true;
+ }
+
+ // 2. 全局状态是否可以处理消息
+ return GlobalState?.OnMessage(in msg, Owner) ?? false;
+ }
}
\ No newline at end of file
diff --git a/StateMachineLearn/WestWord/Wife.cs b/StateMachineLearn/WestWord/Wife.cs
index 9b2fb46..875c2e2 100644
--- a/StateMachineLearn/WestWord/Wife.cs
+++ b/StateMachineLearn/WestWord/Wife.cs
@@ -23,6 +23,11 @@ public interface IWife : IBaseGameEntity
///
public StateMachine FSM { get; set; }
+ ///
+ /// 是否在烹饪
+ ///
+ bool IsInCooking { get; set; }
+
#endregion
#region 方法成员
@@ -54,9 +59,9 @@ public Wife(IState initState, IState preState, EntityName name) : ba
/// 处理信息
///
///
- public override void HandleMessage(Telegram msg)
+ public override void HandleMessage(in Telegram msg)
{
- FSM.
+ FSM.HandleMessage(in msg);
}
#endregion
@@ -90,6 +95,11 @@ public override void Update()
///
public StateMachine FSM { get; set; }
+ ///
+ /// 是否在烹饪
+ ///
+ public bool IsInCooking { get; set; }
+
///
/// 是否需要去洗手间
///
diff --git a/StateMachineLearn/WestWord/WifeOwnedState.cs b/StateMachineLearn/WestWord/WifeOwnedState.cs
index 911e806..2774a29 100644
--- a/StateMachineLearn/WestWord/WifeOwnedState.cs
+++ b/StateMachineLearn/WestWord/WifeOwnedState.cs
@@ -227,21 +227,6 @@ public override void Enter(Wife owner)
return;
}
- // 1. 将妻子放置到洗手间
- if (owner.CurrentLocation != Location.BathRoom)
- {
-
- owner.CurrentLocation= Location.BathRoom;
- }
-
- // 2. 1/2 的概率来进入煮肉状态
- var random = new Random();
- int result = random.Next(0, 10);
- if (result % 2 == 0)
- {
- owner.FSM.ChangState(CookStew.Instance);
- }
-
WriteExt.WriteBgWhiteAndFgYellow($"wifeId:{entity.InsId}, 全局状态");
}
@@ -251,27 +236,6 @@ public override void Enter(Wife owner)
///
public override void Execute(Wife owner)
{
- // 2. 1/2 的概率来进入煮肉状态
- var random = new Random();
- int result = random.Next(0, 10);
- if (result % 2 == 0)
- {
- owner.FSM.ChangState(CookStew.Instance);
- }
-
- if (!owner.IsNeedToGoBathroom())
- {
- return;
- }
-
- // 1. 情况妻子的疲劳度
- owner.CurrentTirednessThreshold = 0;
-
- // 2. 回到进入全局状态前的状态
- owner.FSM.RevertToPreviousState();
-
- // 3. 打日志
- WriteExt.WriteBgWhiteAndFgBlue($"WifeId:{owner.InsId}, 全局状态解决完毕,切换状态到进入全局状态前的状态");
}
///
@@ -308,6 +272,33 @@ public static WifeGlobalState Instance
}
}
+ #region Overrides of State
+
+ ///
+ /// 处理消息
+ ///
+ ///
+ ///
+ ///
+ public override bool OnMessage(in Telegram message, Wife owner)
+ {
+ // 1. 不是矿工回家的消息
+ if (message.MessageType != ConstDefine.MessageType.HiHoneyImHome)
+ {
+ return false;
+ }
+
+ WriteExt.WriteBgWhiteAndFgYellow($"WifeId:{owner.InsId}, 收到消息,开始进入全局状态");
+
+ // 2. 更改状态到烹饪
+ owner.FSM.ChangState(CookStew.Instance);
+
+ return true;
+
+ }
+
+ #endregion
+
#endregion
}
@@ -329,10 +320,20 @@ private CookStew()
///
public override void Enter(Wife owner)
{
+ // 1. 检查当前状态
if(owner.CurrentLocation!= Location.Kitchen)
{
owner.CurrentLocation = Location.Kitchen;
}
+
+ // 2. 检查是否处于烹饪状态
+ if (owner.IsInCooking)
+ {
+ return;
+ }
+
+ // 3. 设置当前处于烹饪状态
+ owner.IsInCooking = true;
WriteExt.WriteBgWhiteAndFgYellow($"wifeId:{owner.InsId}, 进入煮肉的状态");
}
@@ -343,9 +344,9 @@ public override void Enter(Wife owner)
///
public override void Execute(Wife owner)
{
- // 1. 发送煮肉完成的消息给矿工
+ // 1. 开始烹饪,并在烹饪好的时候提醒自己
MessageDispatcher.Instance.DispatchMessage(owner.Name, EntityName.EntityElsa,
- ConstDefine.MessageType.StewReady, 0.0001, null);
+ ConstDefine.MessageType.StewReady, 0.01, null);
WriteExt.WriteBgWhiteAndFgBlue($"wifeId:{owner.InsId}, 正在煮肉");
}
@@ -365,7 +366,7 @@ public override void Exit(Wife owner)
///
///
///
- public override bool OnMessage(Telegram message, Miner owner)
+ public override bool OnMessage(in Telegram message, Wife owner)
{
switch (message.MessageType)
{
@@ -373,8 +374,10 @@ public override bool OnMessage(Telegram message, Miner owner)
case ConstDefine.MessageType.StewReady:
{
WriteExt.WriteBgWhiteAndFgRed($"wifeId:{owner.InsId}, 收到煮肉完成的消息");
- MessageDispatcher.Instance.DispatchMessage(owner.Name, EntityName.EntityMinerBob,
+ MessageDispatcher.Instance.DispatchMessage(EntityName.EntityMinerBob, owner.Name,
ConstDefine.MessageType.StewReady, 0, null);
+ owner.IsInCooking = false;
+ owner.FSM.ChangState(DoHouseWork.Instance);
return true;
}
case ConstDefine.MessageType.HiHoneyImHome:
diff --git a/StateMachineLearn/bin/Debug/net6.0/StateMachineLearn.dll b/StateMachineLearn/bin/Debug/net6.0/StateMachineLearn.dll
index 387d465..92e8b9c 100644
Binary files a/StateMachineLearn/bin/Debug/net6.0/StateMachineLearn.dll and b/StateMachineLearn/bin/Debug/net6.0/StateMachineLearn.dll differ
diff --git a/StateMachineLearn/bin/Debug/net6.0/StateMachineLearn.pdb b/StateMachineLearn/bin/Debug/net6.0/StateMachineLearn.pdb
index 0fe8570..8c98120 100644
Binary files a/StateMachineLearn/bin/Debug/net6.0/StateMachineLearn.pdb and b/StateMachineLearn/bin/Debug/net6.0/StateMachineLearn.pdb differ
diff --git a/StateMachineLearn/bin/Debug/net6.0/ref/StateMachineLearn.dll b/StateMachineLearn/bin/Debug/net6.0/ref/StateMachineLearn.dll
index 2ef41e1..53d9332 100644
Binary files a/StateMachineLearn/bin/Debug/net6.0/ref/StateMachineLearn.dll and b/StateMachineLearn/bin/Debug/net6.0/ref/StateMachineLearn.dll differ
diff --git a/StateMachineLearn/obj/Debug/net6.0/StateMachineLearn.dll b/StateMachineLearn/obj/Debug/net6.0/StateMachineLearn.dll
index 387d465..92e8b9c 100644
Binary files a/StateMachineLearn/obj/Debug/net6.0/StateMachineLearn.dll and b/StateMachineLearn/obj/Debug/net6.0/StateMachineLearn.dll differ
diff --git a/StateMachineLearn/obj/Debug/net6.0/StateMachineLearn.pdb b/StateMachineLearn/obj/Debug/net6.0/StateMachineLearn.pdb
index 0fe8570..8c98120 100644
Binary files a/StateMachineLearn/obj/Debug/net6.0/StateMachineLearn.pdb and b/StateMachineLearn/obj/Debug/net6.0/StateMachineLearn.pdb differ
diff --git a/StateMachineLearn/obj/Debug/net6.0/ref/StateMachineLearn.dll b/StateMachineLearn/obj/Debug/net6.0/ref/StateMachineLearn.dll
index 2ef41e1..53d9332 100644
Binary files a/StateMachineLearn/obj/Debug/net6.0/ref/StateMachineLearn.dll and b/StateMachineLearn/obj/Debug/net6.0/ref/StateMachineLearn.dll differ