Skip to content

Commit 75d942c

Browse files
committed
矿工妻子全局状态及反转状态处理逻辑添加
1 parent 2c167c7 commit 75d942c

21 files changed

+443
-51
lines changed

StateMachineLearn/Program.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,29 @@
44

55
var miner = new Miner(new InitState(), new InitState());
66

7+
var wife = new Wife(new WifeInitState(), new WifeInitState())
8+
{
9+
FSM =
10+
{
11+
GlobalState = new WifeGlobalState()
12+
}
13+
};
14+
715
GameEntityManger.Instance.TryAddNewEntity(miner);
16+
GameEntityManger.Instance.TryAddNewEntity(wife);
17+
18+
var gameEntities = new List<BaseGameEntity>
19+
{
20+
miner,
21+
wife
22+
};
823

924

1025
int loopLimit = 100;
1126
while (loopLimit-- >0)
1227
{
13-
miner.Update();
14-
}
28+
gameEntities.ForEach(entity =>
29+
{
30+
entity.Update();
31+
});
32+
}

StateMachineLearn/StateMachineLearn.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,4 @@
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

10-
<ItemGroup>
11-
<Folder Include="WestWord2" />
12-
</ItemGroup>
13-
1410
</Project>

StateMachineLearn/WestWord1/BaseGameEntity.cs renamed to StateMachineLearn/WestWord/BaseGameEntity.cs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,13 @@ public interface IBaseGameEntity
1818
/// </summary>
1919
public class BaseGameEntity : IBaseGameEntity
2020
{
21-
public class BaseGameEntityBuilder
22-
{
23-
private readonly BaseGameEntity m_baseGameEntity = new();
24-
25-
/// <summary>
26-
/// 构造游戏条目的实例 Id
27-
/// </summary>
28-
/// <returns></returns>
29-
/// <exception cref="ArgumentException">id 无效时抛出异常</exception>
30-
public BaseGameEntityBuilder BuildEntityId()
31-
{
32-
// 2. 构造 Id
33-
m_baseGameEntity.InsId = NextValidId;
34-
35-
// 3. 更新有效 id
36-
checked
37-
{
38-
++NextValidId;
39-
}
40-
41-
return this;
42-
}
43-
}
44-
4521
/// <summary>
4622
/// 防止外部绕过 Builder 创建对象
4723
/// </summary>
4824
protected BaseGameEntity()
4925
{
26+
InsId = NextValidId;
27+
++NextValidId;
5028
}
5129

5230
/// <summary>

StateMachineLearn/WestWord1/ConstDefine.cs renamed to StateMachineLearn/WestWord/ConstDefine.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static class Location
3030
/// <summary>
3131
/// 位置
3232
/// </summary>
33-
public enum MinerLocationType
33+
public enum LocationType
3434
{
3535
/// <summary>
3636
/// 无效位置 - 代码逻辑使用
@@ -55,7 +55,17 @@ public enum MinerLocationType
5555
/// <summary>
5656
/// 家里
5757
/// </summary>
58-
Home
58+
Home,
59+
60+
/// <summary>
61+
/// 洗手间
62+
/// </summary>
63+
BathRoom,
64+
65+
/// <summary>
66+
/// 客厅
67+
/// </summary>
68+
LivingRoom
5969
}
6070
}
6171

File renamed without changes.
File renamed without changes.
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
namespace StateMachineLearn;
2-
using Location = ConstDefine.Location.MinerLocationType;
32

43
/// <summary>
54
/// 矿工接口
@@ -26,7 +25,7 @@ public interface IMiner : IBaseGameEntity
2625
/// <summary>
2726
/// 当前位置
2827
/// </summary>
29-
public Location CurrentLocation { get; set; }
28+
public ConstDefine.Location.LocationType CurrentLocation { get; set; }
3029

3130
/// <summary>
3231
/// 状态机
@@ -116,7 +115,7 @@ public override void Update()
116115
/// <summary>
117116
/// 当前位置
118117
/// </summary>
119-
public Location CurrentLocation { get; set; } = Location.None;
118+
public ConstDefine.Location.LocationType CurrentLocation { get; set; } = ConstDefine.Location.LocationType.None;
120119

121120
/// <summary>
122121
/// 状态机

StateMachineLearn/WestWord1/MinerOwnedState.cs renamed to StateMachineLearn/WestWord/MinerOwnedState.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ public void Enter(Miner owner)
2626
}
2727

2828
// 1. 将矿工放置到矿洞里面
29-
owner.CurrentLocation= Location.MinerLocationType.Goldmine;
29+
owner.CurrentLocation= Location.LocationType.Goldmine;
3030

31-
WriteExt.WriteBgWhiteAndFgYellow($"MinerId:{entity.InsId}, EnterMineAndDigForNuggetState,初始化状态:进入金矿");
31+
WriteExt.WriteBgWhiteAndFgYellow($"MinerId:{entity.InsId}, 初始化状态:进入金矿");
3232
}
3333

3434
/// <summary>
@@ -71,11 +71,11 @@ public void Enter(Miner owner)
7171
}
7272

7373
// 1. 已经在矿洞里了,不进行额外的处理
74-
if (owner.CurrentLocation == Location.MinerLocationType.Goldmine)
74+
if (owner.CurrentLocation == Location.LocationType.Goldmine)
7575
{
7676
return;
7777
}
78-
owner.CurrentLocation= Location.MinerLocationType.Goldmine;
78+
owner.CurrentLocation= Location.LocationType.Goldmine;
7979

8080
// 2. 不在矿洞里面则进行移动到矿洞的操作
8181
WriteExt.WriteBgWhiteAndFgYellow($"MinerId:{entity.InsId}, EnterMineAndDigForNuggetState,进入金矿");
@@ -185,11 +185,11 @@ public void Enter(Miner owner)
185185
}
186186

187187
// 1. 切换矿工的位置
188-
if(owner.CurrentLocation == Location.MinerLocationType.Bank)
188+
if(owner.CurrentLocation == Location.LocationType.Bank)
189189
{
190190
return;
191191
}
192-
owner.CurrentLocation = Location.MinerLocationType.Bank;
192+
owner.CurrentLocation = Location.LocationType.Bank;
193193

194194
WriteExt.WriteBgWhiteAndFgYellow($"MinerId:{owner.InsId}, VisitBankAndDepositGoldState,进入银行");
195195
}
@@ -296,11 +296,11 @@ public void Enter(Miner owner)
296296
}
297297

298298
// 1. 切换矿工位置
299-
if(owner.CurrentLocation == Location.MinerLocationType.Home)
299+
if(owner.CurrentLocation == Location.LocationType.Home)
300300
{
301301
return;
302302
}
303-
owner.CurrentLocation = Location.MinerLocationType.Home;
303+
owner.CurrentLocation = Location.LocationType.Home;
304304

305305
WriteExt.WriteBgWhiteAndFgYellow($"MinerId:{owner.InsId}, GoHomeAndSleepTilRestedState,回到家里");
306306
}
@@ -403,11 +403,11 @@ public void Enter(Miner owner)
403403
}
404404

405405
// 1. 切换位置
406-
if(owner.CurrentLocation == Location.MinerLocationType.Saloon)
406+
if(owner.CurrentLocation == Location.LocationType.Saloon)
407407
{
408408
return;
409409
}
410-
owner.CurrentLocation = Location.MinerLocationType.Saloon;
410+
owner.CurrentLocation = Location.LocationType.Saloon;
411411

412412
WriteExt.WriteBgWhiteAndFgYellow($"MinerId:{owner.InsId}, QuenchThirstState,进入酒吧");
413413
}
@@ -495,11 +495,11 @@ public void Enter(Miner owner)
495495
}
496496

497497
// 1. 切换位置
498-
if(owner.CurrentLocation == Location.MinerLocationType.Saloon)
498+
if(owner.CurrentLocation == Location.LocationType.Saloon)
499499
{
500500
return;
501501
}
502-
owner.CurrentLocation = Location.MinerLocationType.Saloon;
502+
owner.CurrentLocation = Location.LocationType.Saloon;
503503

504504
WriteExt.WriteBgWhiteAndFgYellow($"MinerId:{owner.InsId}, QuenchThirstState,进入酒吧");
505505
}
File renamed without changes.

0 commit comments

Comments
 (0)