Skip to content

Commit 3edcdf6

Browse files
removed GC allocs inside of AIBrain
1 parent cea2a8a commit 3edcdf6

File tree

1 file changed

+15
-5
lines changed
  • Assets/BossRoom/Scripts/Server/Game/Character

1 file changed

+15
-5
lines changed

Assets/BossRoom/Scripts/Server/Game/Character/AIBrain.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ private enum AIStateType
1717
IDLE,
1818
}
1919

20+
static readonly AIStateType[] k_AIStates = { AIStateType.ATTACK , AIStateType.IDLE};
21+
2022
private ServerCharacter m_ServerCharacter;
2123
private ActionPlayer m_ActionPlayer;
2224
private AIStateType m_CurrentState;
@@ -53,7 +55,7 @@ public void Update()
5355
}
5456

5557
/// <summary>
56-
/// Called when we received some HP. Positive HP is healing, negative is damage.
58+
/// Called when we received some HP. Positive HP is healing, negative is damage.
5759
/// </summary>
5860
/// <param name="inflicter">The person who hurt or healed us. May be null. </param>
5961
/// <param name="amount">The amount of HP received. Negative is damage. </param>
@@ -69,13 +71,15 @@ private AIStateType FindBestEligibleAIState()
6971
{
7072
// for now we assume the AI states are in order of appropriateness,
7173
// which may be nonsensical when there are more states
72-
foreach (AIStateType type in Enum.GetValues(typeof(AIStateType)))
74+
for (int i = 0; i < k_AIStates.Length; i++)
7375
{
74-
if (m_Logics[type].IsEligible())
76+
var aiState = k_AIStates[i];
77+
if (m_Logics[aiState].IsEligible())
7578
{
76-
return type;
79+
return aiState;
7780
}
7881
}
82+
7983
Debug.LogError("No AI states are valid!?!");
8084
return AIStateType.IDLE;
8185
}
@@ -115,7 +119,13 @@ public void Hate(ServerCharacter character)
115119
public List<ServerCharacter> GetHatedEnemies()
116120
{
117121
// first we clean the list -- remove any enemies that have disappeared (became null), are dead, etc.
118-
m_HatedEnemies.RemoveAll(enemy => !IsAppropriateFoe(enemy));
122+
for (int i = m_HatedEnemies.Count - 1; i >= 0; i--)
123+
{
124+
if (!IsAppropriateFoe(m_HatedEnemies[i]))
125+
{
126+
m_HatedEnemies.RemoveAt(i);
127+
}
128+
}
119129
return m_HatedEnemies;
120130
}
121131

0 commit comments

Comments
 (0)