Skip to content

Commit e474d52

Browse files
committed
简单状态机学习
1 parent bd76823 commit e474d52

23 files changed

+915
-28
lines changed

StateMachineLearn/.idea/.idea.StateMachineLearn/.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

StateMachineLearn/Program.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
11
// See https://aka.ms/new-console-template for more information
22

3-
Console.WriteLine("Hello, World!");
3+
using StateMachineLearn;
4+
5+
var miner = new Miner(0)
6+
{
7+
CurrentLocation = ConstDefine.Location.MinerLocationType.None,
8+
StateMachine = new EnterMineAndDigForNuggetState()
9+
};
10+
11+
GameEntityManger.Instance.TryAddNewEntity(miner);
12+
13+
14+
int loopLimit = 100;
15+
while (loopLimit-- >0)
16+
{
17+
miner.Update();
18+
}

StateMachineLearn/WestWord1/BaseGameEntity.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,21 @@
66
public interface IBaseGameEntity
77
{
88
public void Update();
9+
10+
/// <summary>
11+
/// 实例 Id
12+
/// </summary>
13+
public int InsId { get; set; }
914
}
1015

1116
/// <summary>
1217
/// 基础游戏条目抽象类
1318
/// </summary>
14-
public abstract class BaseGameEntity : IBaseGameEntity
19+
public class BaseGameEntity : IBaseGameEntity
1520
{
1621
public class BaseGameEntityBuilder
1722
{
18-
private BaseGameEntity _baseGameEntity;
23+
private readonly BaseGameEntity m_baseGameEntity = new BaseGameEntity();
1924

2025
/// <summary>
2126
/// 构造游戏条目的实例 Id
@@ -26,18 +31,18 @@ public class BaseGameEntityBuilder
2631
public BaseGameEntityBuilder BuildEntityId(int id)
2732
{
2833
// 1. 检查传入的 Id 的有效性,如果 Id 无效,则抛出异常
29-
if (id < _nextValidId)
34+
if (id < NextValidId)
3035
{
3136
throw new ArgumentException("Invalid entity id " + id);
3237
}
3338

3439
// 2. 构造 Id
35-
_baseGameEntity.InsId = id;
40+
m_baseGameEntity.InsId = id;
3641

3742
// 3. 更新有效 id
3843
checked
3944
{
40-
++_nextValidId;
45+
++NextValidId;
4146
}
4247

4348
return this;
@@ -50,19 +55,19 @@ public BaseGameEntityBuilder BuildEntityId(int id)
5055
public BaseGameEntity()
5156
{
5257
}
53-
58+
5459
/// <summary>
5560
/// 当前对象的实例 Id
5661
/// </summary>
57-
public int InsId { get; private set; }
58-
62+
public int InsId { get; set; }
63+
5964
/// <summary>
6065
/// 生成对象序列中,下一个对象的有效 Id
6166
/// 该 Id 自动在对象创建的时候自增 1
6267
/// 如果在创建对象的时候传入的 InsId 初始值是否 大于等于 _nextValidId,如果不满足条件则抛出异常
6368
/// </summary>
6469
/// <exception cref="Exception"></exception>
65-
private static int _nextValidId { get; set; }
70+
protected static int NextValidId { get; set; }
6671

6772
#region Implementation of IBaseGameEntity
6873

StateMachineLearn/WestWord1/ConstDefine.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,72 @@ public static class MinerBehavior
2424
/// </summary>
2525
public const int TirednessThresholdMaxVal = 5;
2626
}
27+
28+
public static class Location
29+
{
30+
/// <summary>
31+
/// 位置
32+
/// </summary>
33+
public enum MinerLocationType
34+
{
35+
/// <summary>
36+
/// 无效位置 - 代码逻辑使用
37+
/// </summary>
38+
None,
39+
40+
/// <summary>
41+
/// 金矿
42+
/// </summary>
43+
Goldmine,
44+
45+
/// <summary>
46+
/// 酒吧
47+
/// </summary>
48+
Saloon,
49+
50+
/// <summary>
51+
/// 银行
52+
/// </summary>
53+
Bank,
54+
55+
/// <summary>
56+
/// 家里
57+
/// </summary>
58+
Home
59+
}
60+
}
61+
62+
public static class MinerState
63+
{
64+
/// <summary>
65+
/// 状态
66+
/// </summary>
67+
public enum MinerStateType
68+
{
69+
/// <summary>
70+
/// 无效状态 - 代码逻辑使用
71+
/// </summary>
72+
None,
73+
74+
/// <summary>
75+
/// 去矿洞挖矿
76+
/// </summary>
77+
EnterMineAndDigForNugget,
78+
79+
/// <summary>
80+
/// 去银行存钱
81+
/// </summary>
82+
VisitBankAndDepositGold,
83+
84+
/// <summary>
85+
/// 回家休息
86+
/// </summary>
87+
GoHomeAndSleepTilRested,
88+
89+
/// <summary>
90+
/// 解决口渴
91+
/// </summary>
92+
QuenchThirst
93+
}
94+
}
2795
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System.Collections.Concurrent;
2+
using System.Diagnostics;
3+
using System.Diagnostics.CodeAnalysis;
4+
5+
namespace StateMachineLearn;
6+
7+
public class GameEntityManger
8+
{
9+
private GameEntityManger()
10+
{
11+
12+
}
13+
14+
/// <summary>
15+
/// 添加新的实体
16+
/// </summary>
17+
/// <param name="entity"></param>
18+
/// <returns></returns>
19+
public bool TryAddNewEntity([NotNull]IBaseGameEntity entity)
20+
{
21+
Debug.Assert(entity!= null, "entity is null");
22+
23+
return GameEntities.TryAdd(entity.InsId, entity);
24+
}
25+
26+
/// <summary>
27+
/// 通过实体id获取实体
28+
/// </summary>
29+
/// <param name="insId"></param>
30+
/// <returns></returns>
31+
public IBaseGameEntity? TryGetEntity(int insId)
32+
{
33+
return GameEntities.TryGetValue(insId, out var entity) ? entity : null;
34+
}
35+
36+
#region Singleton
37+
38+
/// <summary>
39+
/// 对象缓存
40+
/// </summary>
41+
private static GameEntityManger? m_instance;
42+
43+
/// <summary>
44+
/// 获取实例
45+
/// </summary>
46+
public static GameEntityManger Instance
47+
{
48+
get
49+
{
50+
if (m_instance == null)
51+
{
52+
m_instance = new GameEntityManger();
53+
}
54+
return m_instance;
55+
}
56+
}
57+
58+
#endregion
59+
60+
#region 数据成员
61+
62+
private ConcurrentDictionary<int, IBaseGameEntity> GameEntities { get; set; } =
63+
new ConcurrentDictionary<int, IBaseGameEntity>();
64+
65+
#endregion
66+
}

0 commit comments

Comments
 (0)