Skip to content

Commit

Permalink
添加状态机的消息处理 - 妻子煮肉
Browse files Browse the repository at this point in the history
  • Loading branch information
floatval committed Jul 8, 2022
1 parent 8e43b3d commit f35c897
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 55 deletions.
6 changes: 3 additions & 3 deletions StateMachineLearn/WestWord/BaseGameEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public interface IBaseGameEntity
/// 实例名字
/// </summary>
public EntityName Name { get; }

/// <summary>
/// 处理信息
/// </summary>
/// <param name="msg"></param>
public void HandleMessage(Telegram msg);
public void HandleMessage(in Telegram msg);
}

/// <summary>
Expand Down Expand Up @@ -53,7 +53,7 @@ protected BaseGameEntity(EntityName name)
/// 处理信息
/// </summary>
/// <param name="msg"></param>
public virtual void HandleMessage(Telegram msg)
public virtual void HandleMessage(in Telegram msg)
{
}

Expand Down
6 changes: 3 additions & 3 deletions StateMachineLearn/WestWord/MessageDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public bool DispatchMessage(int sendInsId, int receiverInsId, ConstDefine.Messag
.Build();

// 4. 发送消息
receiver!.HandleMessage(message);
receiver!.HandleMessage(in message);

return true;
}
Expand Down Expand Up @@ -110,7 +110,7 @@ public bool DispatchMessage(EntityName receiverName,
}

// 5. 发送消息
receiver.HandleMessage(message);
receiver.HandleMessage(in message);

return true;
}
Expand Down Expand Up @@ -243,7 +243,7 @@ private void DispatchMessage(Telegram message)
var receiver = EntityManger.TryGetEntity(message.ReceiverInsId);

// 2. 派发消息
receiver!.HandleMessage(message);
receiver!.HandleMessage(in message);
}

#endregion
Expand Down
9 changes: 9 additions & 0 deletions StateMachineLearn/WestWord/Miner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ public Miner(IState<Miner> initState, IState<Miner> preState, EntityName name) :

#region 覆写

/// <summary>
/// 处理信息
/// </summary>
/// <param name="msg"></param>
public override void HandleMessage(in Telegram msg)
{
FSM.HandleMessage(in msg);
}

/// <summary>
/// 刷新条目当前的状态 - 每帧(每次循环)调用
/// </summary>
Expand Down
11 changes: 7 additions & 4 deletions StateMachineLearn/WestWord/MinerOwnedState.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics;
using System.Globalization;

namespace StateMachineLearn;
using Behavior = ConstDefine.MinerBehavior;
Expand Down Expand Up @@ -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,回到家里,并告知妻子自己回来了");
}

/// <summary>
Expand Down Expand Up @@ -363,7 +366,7 @@ public override void Exit(Miner owner)
/// <param name="message"></param>
/// <param name="owner"></param>
/// <returns></returns>
public override bool OnMessage(Telegram message, Miner owner)
public override bool OnMessage(in Telegram message, Miner owner)
{
switch (message.MessageType)
{
Expand Down Expand Up @@ -564,7 +567,7 @@ public override void Exit(Miner owner)
/// <param name="message"></param>
/// <param name="owner"></param>
/// <returns></returns>
public override bool OnMessage(Telegram message, Miner owner)
public override bool OnMessage(in Telegram message, Miner owner)
{
throw new NotImplementedException();
}
Expand Down
6 changes: 3 additions & 3 deletions StateMachineLearn/WestWord/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface IState<in TOwner> where TOwner : class
/// <param name="message"></param>
/// <param name="owner"></param>
/// <returns></returns>
public bool OnMessage(Telegram message, Miner owner);
public bool OnMessage(in Telegram message, TOwner owner);
}

/// <summary>
Expand Down Expand Up @@ -66,9 +66,9 @@ public virtual void Exit(TOwner owner)
/// <param name="message"></param>
/// <param name="owner"></param>
/// <returns></returns>
public virtual bool OnMessage(Telegram message, Miner owner)
public virtual bool OnMessage(in Telegram message, TOwner owner)
{
return true;
return false;
}

#endregion
Expand Down
17 changes: 17 additions & 0 deletions StateMachineLearn/WestWord/StateMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,22 @@ public bool IsInState(IState<TOwner> state)
{
return ReferenceEquals(CurrentState, state);
}

/// <summary>
/// 处理消息
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
public bool HandleMessage(in Telegram msg)
{
// 1. 查看当前状态是否有效并且可以处理消息
if(CurrentState.OnMessage(in msg, Owner))
{
return true;
}

// 2. 全局状态是否可以处理消息
return GlobalState?.OnMessage(in msg, Owner) ?? false;
}

}
14 changes: 12 additions & 2 deletions StateMachineLearn/WestWord/Wife.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public interface IWife : IBaseGameEntity
/// </summary>
public StateMachine<Wife> FSM { get; set; }

/// <summary>
/// 是否在烹饪
/// </summary>
bool IsInCooking { get; set; }

#endregion

#region 方法成员
Expand Down Expand Up @@ -54,9 +59,9 @@ public Wife(IState<Wife> initState, IState<Wife> preState, EntityName name) : ba
/// 处理信息
/// </summary>
/// <param name="msg"></param>
public override void HandleMessage(Telegram msg)
public override void HandleMessage(in Telegram msg)
{
FSM.
FSM.HandleMessage(in msg);
}

#endregion
Expand Down Expand Up @@ -90,6 +95,11 @@ public override void Update()
/// </summary>
public StateMachine<Wife> FSM { get; set; }

/// <summary>
/// 是否在烹饪
/// </summary>
public bool IsInCooking { get; set; }

/// <summary>
/// 是否需要去洗手间
/// </summary>
Expand Down
83 changes: 43 additions & 40 deletions StateMachineLearn/WestWord/WifeOwnedState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}, 全局状态");
}

Expand All @@ -251,27 +236,6 @@ public override void Enter(Wife owner)
/// <param name="owner"></param>
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}, 全局状态解决完毕,切换状态到进入全局状态前的状态");
}

/// <summary>
Expand Down Expand Up @@ -308,6 +272,33 @@ public static WifeGlobalState Instance
}
}

#region Overrides of State<Wife>

/// <summary>
/// 处理消息
/// </summary>
/// <param name="message"></param>
/// <param name="owner"></param>
/// <returns></returns>
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
}

Expand All @@ -329,10 +320,20 @@ private CookStew()
/// <param name="owner"></param>
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}, 进入煮肉的状态");
}
Expand All @@ -343,9 +344,9 @@ public override void Enter(Wife owner)
/// <param name="owner"></param>
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}, 正在煮肉");
}
Expand All @@ -365,16 +366,18 @@ public override void Exit(Wife owner)
/// <param name="message"></param>
/// <param name="owner"></param>
/// <returns></returns>
public override bool OnMessage(Telegram message, Miner owner)
public override bool OnMessage(in Telegram message, Wife owner)
{
switch (message.MessageType)
{
// 1. 煮肉完成通知矿工
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:
Expand Down
Binary file modified StateMachineLearn/bin/Debug/net6.0/StateMachineLearn.dll
Binary file not shown.
Binary file modified StateMachineLearn/bin/Debug/net6.0/StateMachineLearn.pdb
Binary file not shown.
Binary file modified StateMachineLearn/bin/Debug/net6.0/ref/StateMachineLearn.dll
Binary file not shown.
Binary file modified StateMachineLearn/obj/Debug/net6.0/StateMachineLearn.dll
Binary file not shown.
Binary file modified StateMachineLearn/obj/Debug/net6.0/StateMachineLearn.pdb
Binary file not shown.
Binary file modified StateMachineLearn/obj/Debug/net6.0/ref/StateMachineLearn.dll
Binary file not shown.

0 comments on commit f35c897

Please sign in to comment.