166 lines
4.2 KiB
C#
166 lines
4.2 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class GroupManager : MonoBehaviour
|
|
{
|
|
public Action DelSoldierDie;
|
|
public Action DelSoldierClear;
|
|
|
|
public int SoldierCount => mSoldierCount;
|
|
public int TotalDamage => mTotalDamage;
|
|
|
|
protected Dictionary<int, List<SoldierUnit>> mSoldierGroupDic;
|
|
protected List<SoldierUnit> mSoldierList;
|
|
protected int mSoldierCount = 0;
|
|
|
|
protected int mTotalDamage = 0;
|
|
|
|
private void Awake()
|
|
{
|
|
mSoldierGroupDic = new Dictionary<int, List<SoldierUnit>>();
|
|
mSoldierList = new List<SoldierUnit>();
|
|
}
|
|
|
|
public virtual void Init()
|
|
{
|
|
|
|
}
|
|
|
|
public WarUnit GetNearestTarget(Vector3 pRefPos, float pLimit = -1)
|
|
{
|
|
WarUnit tNearestUnit = null;
|
|
|
|
float tMinDisSqr = float.MaxValue;
|
|
float tCurDisSqr = 0;
|
|
Vector3 tTargetPos = Vector3.zero;
|
|
|
|
for (int i = 0; i < mSoldierList.Count; i++)
|
|
{
|
|
if (!mSoldierList[i].IsDead)
|
|
{
|
|
tTargetPos = mSoldierList[i].RelativePos(pRefPos);
|
|
tCurDisSqr = Mathf.Min(Mathf.Abs(tTargetPos.x - pRefPos.x), Mathf.Abs(tTargetPos.y - pRefPos.y));
|
|
if (pLimit > 0 && pLimit < tCurDisSqr )
|
|
continue;
|
|
|
|
tCurDisSqr = (pRefPos - tTargetPos).sqrMagnitude;
|
|
if (pLimit > 0 && (pLimit * pLimit) < tCurDisSqr)
|
|
continue;
|
|
|
|
if (tCurDisSqr < tMinDisSqr)
|
|
{
|
|
tMinDisSqr = tCurDisSqr;
|
|
tNearestUnit = mSoldierList[i];
|
|
}
|
|
}
|
|
}
|
|
|
|
return tNearestUnit;
|
|
}
|
|
|
|
public virtual void AddSoldier(SoldierUnit pUnit, GroupManager pAgainstGroup)
|
|
{
|
|
|
|
}
|
|
|
|
protected void AddSoldier(SoldierUnit pUnit)
|
|
{
|
|
pUnit.DelDie = OnSoldierDie;
|
|
pUnit.DelHurt = OnSoldierHurt;
|
|
pUnit.DelSplash = OnSoldierSplash;
|
|
|
|
if (!mSoldierGroupDic.ContainsKey(pUnit.GroupID))
|
|
{
|
|
mSoldierGroupDic[pUnit.GroupID] = new List<SoldierUnit>();
|
|
}
|
|
|
|
if(!mSoldierGroupDic[pUnit.GroupID].Contains(pUnit))
|
|
{
|
|
mSoldierGroupDic[pUnit.GroupID].Add(pUnit);
|
|
}
|
|
|
|
mSoldierList.Add(pUnit);
|
|
mSoldierCount++;
|
|
}
|
|
|
|
protected virtual void HandleSoldierClear()
|
|
{
|
|
|
|
}
|
|
|
|
private void OnSoldierDie(WarUnit pUnit)
|
|
{
|
|
if (mSoldierList.Contains(pUnit as SoldierUnit))
|
|
{
|
|
mSoldierList.Remove(pUnit as SoldierUnit);
|
|
}
|
|
|
|
mSoldierCount--;
|
|
DelSoldierDie?.Invoke();
|
|
if (mSoldierCount <= 0)
|
|
{
|
|
HandleSoldierClear();
|
|
DelSoldierClear?.Invoke();
|
|
}
|
|
}
|
|
|
|
private void OnSoldierSplash(WarUnit pUnit, int pOverflowDamage)
|
|
{
|
|
SoldierUnit tSoldier = pUnit as SoldierUnit;
|
|
if (mSoldierGroupDic.ContainsKey(tSoldier.GroupID) && mSoldierGroupDic[tSoldier.GroupID] != null && mSoldierGroupDic[tSoldier.GroupID].Count > 1)
|
|
{
|
|
List<SoldierUnit> tGroupList = mSoldierGroupDic[tSoldier.GroupID];
|
|
int tOverflowDamage = pOverflowDamage;
|
|
for (int i = 0; i < tGroupList.Count; i++)
|
|
{
|
|
if (tOverflowDamage <= 0)
|
|
break;
|
|
|
|
if (tGroupList[i] == tSoldier || tGroupList[i].IsDead)
|
|
continue;
|
|
|
|
tOverflowDamage = tGroupList[i].Hurt(tOverflowDamage, false);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnSoldierHurt(int pDamage)
|
|
{
|
|
mTotalDamage += pDamage;
|
|
}
|
|
|
|
public void ActiveAllSoldier()
|
|
{
|
|
foreach (SoldierUnit tUnit in mSoldierList)
|
|
{
|
|
if (tUnit.gameObject.activeSelf && !tUnit.IsDead)
|
|
{
|
|
tUnit.Active();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Win()
|
|
{
|
|
foreach (SoldierUnit tUnit in mSoldierList)
|
|
{
|
|
if (tUnit.gameObject.activeSelf && !tUnit.IsDead)
|
|
{
|
|
tUnit.Win();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Lose()
|
|
{
|
|
//foreach (SoldierUnit tUnit in mSoldierList)
|
|
//{
|
|
// if (tUnit.gameObject.activeSelf && !tUnit.IsDead)
|
|
// {
|
|
// tUnit.Die();
|
|
// }
|
|
//}
|
|
}
|
|
} |