2023-10-19 15:00:19 +08:00
|
|
|
|
using System;
|
2023-08-22 19:08:45 +08:00
|
|
|
|
using System.Collections.Generic;
|
2023-12-13 16:20:41 +08:00
|
|
|
|
using Gameplay.Unit;
|
2023-08-22 19:08:45 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Gameplay.Character
|
|
|
|
|
|
{
|
2023-12-14 19:10:53 +08:00
|
|
|
|
public class CharacterManager
|
2023-08-22 19:08:45 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
2023-10-19 15:00:19 +08:00
|
|
|
|
private readonly Dictionary<int, Troops> _troopDict = new Dictionary<int, Troops>();
|
2023-08-22 19:08:45 +08:00
|
|
|
|
|
2023-12-14 19:00:39 +08:00
|
|
|
|
public void AddCharacter(Character character)
|
2023-08-22 19:08:45 +08:00
|
|
|
|
{
|
|
|
|
|
|
_AddToTroop(character);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-14 14:42:27 +08:00
|
|
|
|
public void RemoveCharacter(Character c)
|
2023-08-22 19:08:45 +08:00
|
|
|
|
{
|
|
|
|
|
|
_RemoveFromTroop(c);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void _RemoveFromTroop(Character c)
|
|
|
|
|
|
{
|
|
|
|
|
|
var teamId = c.commonData.troopId;
|
|
|
|
|
|
if (_troopDict.TryGetValue(teamId, out Troops team))
|
|
|
|
|
|
{
|
|
|
|
|
|
team.characters.Remove(c);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void _AddToTroop(Character c)
|
|
|
|
|
|
{
|
|
|
|
|
|
var teamId = c.commonData.troopId;
|
|
|
|
|
|
if (_troopDict.TryGetValue(teamId, out Troops team))
|
|
|
|
|
|
{
|
|
|
|
|
|
team.characters.Add(c);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
team = new Troops(teamId);
|
|
|
|
|
|
team.characters.Add(c);
|
|
|
|
|
|
_troopDict.Add(teamId, team);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-14 19:10:53 +08:00
|
|
|
|
public bool Contains(uint id)
|
|
|
|
|
|
{
|
|
|
|
|
|
return GameUnitManager.instance.infightUnits.Contains(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-22 19:08:45 +08:00
|
|
|
|
public Character Get(uint id)
|
|
|
|
|
|
{
|
2023-12-14 14:42:27 +08:00
|
|
|
|
var c = GameUnitManager.instance.infightUnits.Search(id) as Character;
|
|
|
|
|
|
return c;
|
2023-08-22 19:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void SearchEnemies(GameUnit self,
|
|
|
|
|
|
List<GameUnit> enemyList)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (self == null) return;
|
|
|
|
|
|
|
|
|
|
|
|
if (self.controlData.followUnit != null
|
|
|
|
|
|
&& Level.Level.IsEnemy(self.controlData.followUnit.commonData.troopId, self.commonData.troopId))
|
|
|
|
|
|
{
|
|
|
|
|
|
enemyList.Add(self.controlData.followUnit);
|
|
|
|
|
|
}
|
|
|
|
|
|
foreach (var team in _troopDict)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Level.Level.IsEnemy(team.Key, self.commonData.troopId))
|
|
|
|
|
|
{
|
|
|
|
|
|
enemyList.AddRange(team.Value.characters);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-12-14 14:42:27 +08:00
|
|
|
|
|
2023-08-22 19:08:45 +08:00
|
|
|
|
public void SearchFriends(GameUnit self,
|
|
|
|
|
|
List<GameUnit> friendList)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (self == null) return;
|
|
|
|
|
|
|
|
|
|
|
|
if (self.controlData.followUnit != null
|
|
|
|
|
|
&& !Level.Level.IsEnemy(self.controlData.followUnit.commonData.troopId, self.commonData.troopId))
|
|
|
|
|
|
{
|
|
|
|
|
|
friendList.Add(self.controlData.followUnit);
|
|
|
|
|
|
}
|
|
|
|
|
|
foreach (var team in _troopDict)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!Level.Level.IsEnemy(team.Key, self.commonData.troopId))
|
|
|
|
|
|
{
|
|
|
|
|
|
friendList.AddRange(team.Value.characters);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void ForEach(Action<Character> action)
|
|
|
|
|
|
{
|
2023-12-14 19:10:53 +08:00
|
|
|
|
foreach (var unit in GameUnitManager.instance.infightUnits.unitList)
|
2023-08-22 19:08:45 +08:00
|
|
|
|
{
|
2023-12-14 19:10:53 +08:00
|
|
|
|
var charactor = unit as Character;
|
|
|
|
|
|
if (charactor != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
action(charactor);
|
|
|
|
|
|
}
|
2023-08-22 19:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public List<Character> FindAll(Func<Character, bool> conditions)
|
|
|
|
|
|
{
|
|
|
|
|
|
List<Character> result = new List<Character>();
|
2023-12-14 19:10:53 +08:00
|
|
|
|
foreach (var unit in GameUnitManager.instance.infightUnits.unitList)
|
2023-08-22 19:08:45 +08:00
|
|
|
|
{
|
2023-12-14 19:10:53 +08:00
|
|
|
|
var charactor = unit as Character;
|
2023-12-14 14:42:27 +08:00
|
|
|
|
if (conditions(charactor))
|
2023-08-22 19:08:45 +08:00
|
|
|
|
{
|
2023-12-14 14:42:27 +08:00
|
|
|
|
result.Add(charactor);
|
2023-08-22 19:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Character FindOne(Func<Character, bool> conditions)
|
|
|
|
|
|
{
|
2023-12-14 19:10:53 +08:00
|
|
|
|
foreach (var unit in GameUnitManager.instance.infightUnits.unitList)
|
2023-08-22 19:08:45 +08:00
|
|
|
|
{
|
2023-12-14 19:10:53 +08:00
|
|
|
|
var charactor = unit as Character;
|
2023-12-14 14:42:27 +08:00
|
|
|
|
if (conditions(charactor))
|
2023-08-22 19:08:45 +08:00
|
|
|
|
{
|
2023-12-14 14:42:27 +08:00
|
|
|
|
return charactor;
|
2023-08-22 19:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|