309 lines
11 KiB
C#
309 lines
11 KiB
C#
using Cal.DataTable;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace ET
|
|
{
|
|
|
|
public class TeamComponentAwakeSystem : AwakeSystem<TeamComponent>
|
|
{
|
|
public override void Awake(TeamComponent self)
|
|
{
|
|
TeamComponent.Instance = self;
|
|
}
|
|
}
|
|
|
|
public class TeamComponentDestroySystem : DestroySystem<TeamComponent>
|
|
{
|
|
public override void Destroy(TeamComponent self)
|
|
{
|
|
self.teamDic.Clear();
|
|
}
|
|
}
|
|
|
|
public static class TeamComponentSystem
|
|
{
|
|
private const int MaxMemberCount = 5;
|
|
public static Team CreateTeam(this TeamComponent self, long id)
|
|
{
|
|
Team team = EntityFactory.CreateWithParent<Team>(self);
|
|
team.LeaderId = id;
|
|
Unit unit = MapUnitComponent.Instance.Get(id);
|
|
team.Add(unit);
|
|
self.teamDic[team.LeaderId] = team;
|
|
|
|
MainStoryMap.Instance.InitBattleInteractiveInfo(id);
|
|
return team;
|
|
}
|
|
/// <summary>
|
|
/// 判断能否进入对方队伍
|
|
/// </summary>
|
|
/// <param name="self"></param>
|
|
/// <param name="learId">对方Id</param>
|
|
/// <returns></returns>
|
|
public static string CheckCanRequest(this TeamComponent self, Unit unit, Unit targetUnit)
|
|
{
|
|
Team selfTeam = self.Get(unit.TeamLeaderId);
|
|
if (selfTeam == null)
|
|
{
|
|
Log.Error($"【{UserComponent.Instance.Get(unit.Id)?.NickName}({unit.Id})】没有队伍");
|
|
return "系统错误,自己队伍消失";
|
|
}
|
|
string canTeamRet = CanTeam(unit);
|
|
if (canTeamRet != null)
|
|
{
|
|
return canTeamRet;
|
|
}
|
|
if (selfTeam.MemberCount > 1)
|
|
{
|
|
return "自己有队伍了,不能加入其他队伍!";
|
|
}
|
|
Team team = self.Get(targetUnit.TeamLeaderId);
|
|
if (team == null)
|
|
{
|
|
Log.Error($"【{UserComponent.Instance.Get(targetUnit.Id)?.NickName}({targetUnit.Id})】没有队伍");
|
|
return "对方没有队伍,无法进入!";
|
|
}
|
|
if (team.Contains(unit.Id))
|
|
{
|
|
return "对方同队!";
|
|
}
|
|
if (team.MemberCount < 0 || team.MemberCount >= MaxMemberCount)
|
|
{
|
|
return "对方队伍已满,无法进入!";
|
|
}
|
|
if (team.TeamState == TeamState.Fight)
|
|
{
|
|
return "对方在战斗中,无法进入!";
|
|
}
|
|
return null;
|
|
}
|
|
private static string CanTeam(Unit unit)
|
|
{
|
|
|
|
UnitScene unitScene = unit.GetComponent<UnitScene>();
|
|
if (unitScene == null)
|
|
{
|
|
Log.Error($"uniscene == null where id = {unit.Id}");
|
|
return "系统错误";
|
|
}
|
|
Sys_Scene scene = Sys_SceneCategory.Instance.Get(unitScene.MapId / 100);
|
|
if (scene == null)
|
|
{
|
|
return "系统错误";
|
|
}
|
|
if (!scene.CanTeam)
|
|
{
|
|
return "此地图禁止组队!";
|
|
}
|
|
return null;
|
|
}
|
|
/// <summary>
|
|
/// 判断是否可以邀请对方
|
|
/// </summary>
|
|
/// <param name="self"></param>
|
|
/// <param name="learId">对方Id</param>
|
|
/// <returns></returns>
|
|
public static string CheckCanInvite(this TeamComponent self, Unit unit)
|
|
{
|
|
Team team = self.Get(unit.TeamLeaderId);
|
|
if (team == null)
|
|
{
|
|
Log.Error($"【{UserComponent.Instance.Get(unit.Id)?.NickName}({unit.Id})】没有队伍");
|
|
return null;
|
|
}
|
|
string canTeamRet = CanTeam(unit);
|
|
if (canTeamRet != null)
|
|
{
|
|
return canTeamRet;
|
|
}
|
|
if (team.MemberCount > 1)
|
|
{
|
|
return "对方已有队伍,无法邀请!";
|
|
}
|
|
if (team.TeamState == TeamState.Fight)
|
|
{
|
|
return "对方在战斗中,无法邀请!";
|
|
}
|
|
return null;
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="self"></param>
|
|
/// <param name="learId">自己的Id</param>
|
|
/// <param name="targetUnit">对方</param>
|
|
public static void InviteTeam(this TeamComponent self, long learId, Unit targetUnit)
|
|
{
|
|
MessageHelper.SendActor(targetUnit, new M2C_InviteList() { UnitId = learId, TimeOut = 10000 });
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="self"></param>
|
|
/// <param name="learId">对方的ID</param>
|
|
/// <param name="myUnit">自己</param>
|
|
public static void RequestTeam(this TeamComponent self, long learId, Unit myUnit)
|
|
{
|
|
MessageHelper.SendActor(MapUnitComponent.Instance.Get(learId), new M2C_RequestList() { UnitId = myUnit.Id, TimeOut = 10000 });
|
|
}
|
|
/// <summary>
|
|
/// 将别人加入自己队伍
|
|
/// </summary>
|
|
/// <param name="self"></param>
|
|
/// <param name="unit">自己</param>
|
|
/// <param name="targetId">目标玩家</param>
|
|
/// <returns></returns>
|
|
public static Team HandleRequestTeam(this TeamComponent self, Unit unit, long targetId)
|
|
{
|
|
Team team = self.Get(unit.TeamLeaderId);
|
|
if (team == null) return null;
|
|
string canTeamRet = CanTeam(unit);
|
|
if (canTeamRet != null)
|
|
{
|
|
return null;
|
|
}
|
|
if (team.TeamState == TeamState.Fight)
|
|
return null;
|
|
if (!team.CanInvite(MaxMemberCount)) return null;
|
|
if (team.Contains(targetId))
|
|
return null;
|
|
Unit targetUnit = MapUnitComponent.Instance.Get(targetId);
|
|
if (targetUnit == null)
|
|
return null;
|
|
Team targetTeam = TeamComponent.Instance.Get(targetUnit.TeamLeaderId);
|
|
if (targetTeam == null) return null;
|
|
if (targetTeam.TeamState == TeamState.Fight)
|
|
return null;
|
|
if (!targetTeam.CanBeInvite()) return null;
|
|
self.Remove(targetUnit.TeamLeaderId);
|
|
team.Add(targetUnit);
|
|
return team;
|
|
}
|
|
/// <summary>
|
|
/// 将自己加入别人队伍
|
|
/// </summary>
|
|
/// <param name="self"></param>
|
|
/// <param name="unit">被邀请人</param>
|
|
/// <param name="targetId">目标队长</param>
|
|
/// <returns></returns>
|
|
public static Team HandleIniviteTeam(this TeamComponent self, Unit unit, long targetId)
|
|
{
|
|
//!targetId == targetLeaderId
|
|
Team team = self.Get(targetId);
|
|
if (team == null) return null;
|
|
string canTeamRet = CanTeam(unit);
|
|
if (canTeamRet != null)
|
|
{
|
|
return null;
|
|
}
|
|
if (team.TeamState == TeamState.Fight)
|
|
return null;
|
|
Team selfTeam = self.Get(unit.TeamLeaderId);
|
|
if (selfTeam == null) return null;
|
|
if (selfTeam.TeamState == TeamState.Fight)
|
|
return null;
|
|
if (!selfTeam.CanBeInvite()) return null;
|
|
if (!team.CanInvite(MaxMemberCount)) return null;
|
|
if (team.Contains(unit.Id))
|
|
return null;
|
|
self.Remove(unit.TeamLeaderId);
|
|
team.Add(unit);
|
|
return team;
|
|
}
|
|
public static Team Get(this TeamComponent self, long learId)
|
|
{
|
|
self.teamDic.TryGetValue(learId, out Team team);
|
|
return team;
|
|
}
|
|
public static void Update(this TeamComponent self, long oldLearId, Team team)
|
|
{
|
|
self.teamDic.Remove(oldLearId);
|
|
self.teamDic.Add(team.LeaderId, team);
|
|
}
|
|
public static void Remove(this TeamComponent self, long leaderId)
|
|
{
|
|
self.teamDic.Remove(leaderId);
|
|
MainStoryMap.Instance.RemoveBattleInteractiveInfo(leaderId);
|
|
}
|
|
|
|
public static void QuitTeam(this TeamComponent self, Unit unit, Team team = null)
|
|
{
|
|
team ??= self.Get(unit.TeamLeaderId);
|
|
if (team == null) return;
|
|
team.Remove(unit.Id);
|
|
//队长特殊处理
|
|
if (unit.Id == unit.TeamLeaderId)
|
|
{
|
|
MainStoryAIComponent.instance.DestoryAI(unit);
|
|
LinkedList<Unit> unitIds = team.GetUnits();
|
|
long leaderId = unitIds.First.Value.Id;
|
|
team.LeaderId = leaderId;
|
|
//更新信息
|
|
self.Update(unit.Id, team);
|
|
foreach (Unit item in team.GetUnits())
|
|
{
|
|
item.TeamLeaderId = leaderId;
|
|
}
|
|
team.ChangeLeader(leaderId);
|
|
//更新旧队伍交互信息
|
|
MainStoryMap.Instance.UpdateBattleInteractiveInfo(unit.TeamLeaderId, team.LeaderId);
|
|
}
|
|
//创建新队伍
|
|
Team newTeam = self.CreateTeam(unit.Id);
|
|
|
|
TeamHelper.SendTeamMember(newTeam);
|
|
TeamHelper.SendTeamMember(team);
|
|
}
|
|
public static void DestroyTeam(this TeamComponent self, Unit unit, Team team = null)
|
|
{
|
|
team ??= self.Get(unit.TeamLeaderId);
|
|
if (team == null) return;
|
|
//如果人数==0 ,自动清理
|
|
team.Remove(unit.Id);
|
|
if(team)
|
|
{
|
|
//队长特殊处理,移除了自己,所以只要还有人就做处理
|
|
if (unit.Id == unit.TeamLeaderId)
|
|
{
|
|
MainStoryAIComponent.instance.DestoryAI(unit);
|
|
LinkedList<Unit> unitIds = team.GetUnits();
|
|
long leaderId = unitIds.First.Value.Id;
|
|
team.LeaderId = leaderId;
|
|
//更新信息
|
|
self.Update(unit.Id, team);
|
|
foreach (Unit item in team.GetUnits())
|
|
{
|
|
item.TeamLeaderId = leaderId;
|
|
}
|
|
team.ChangeLeader(leaderId);
|
|
//更新旧队伍交互信息
|
|
MainStoryMap.Instance.UpdateBattleInteractiveInfo(unit.TeamLeaderId, team.LeaderId);
|
|
}
|
|
TeamHelper.SendTeamMember(team);
|
|
}
|
|
BattleIdleMap.Instance.Remove(unit.TeamLeaderId);
|
|
}
|
|
public static void RemoveOffLineId(this TeamComponent self, Team team, Unit unit)
|
|
{
|
|
team.offLineUnits.Remove(unit);
|
|
}
|
|
public static async ETTask RemoveAllOffLineId(this TeamComponent self, Team team)
|
|
{
|
|
using ListComponent<Unit> listComponent = ListComponent<Unit>.Create();
|
|
listComponent.List.AddRange(team.offLineUnits);
|
|
foreach (Unit unit in listComponent.List)
|
|
{
|
|
if (unit.IsOffline == false)
|
|
continue;
|
|
if (team.MemberCount == 1)
|
|
_=true;
|
|
else
|
|
self.QuitTeam(unit);
|
|
await Game.EventSystem.Publish(new EventType.UnitOffline { unit = unit });
|
|
}
|
|
}
|
|
}
|
|
}
|