/// <summary> /// 根据坐标 即时 向地图固定格子 里删除 unitId /// </summary> /// <param name="aoiUnit"></param> public static void RemoveUnitId(this AoiGridComponent self, AoiUnitComponent aoiUnit) { AoiGrid aoiGrid = self.Get(aoiUnit.gridId); if (aoiGrid != null) { UnitType unitType = aoiUnit.GetParent <Unit>().UnitType; switch (unitType) { case UnitType.Player: if (aoiGrid.players.Contains(aoiUnit.GetParent <Unit>().Id)) { aoiGrid.players.Remove(aoiUnit.GetParent <Unit>().Id); } break; case UnitType.Monster: if (aoiGrid.enemys.Contains(aoiUnit.GetParent <Unit>().Id)) { aoiGrid.enemys.Remove(aoiUnit.GetParent <Unit>().Id); } break; case UnitType.Npcer: if (aoiGrid.npcers.Contains(aoiUnit.GetParent <Unit>().Id)) { aoiGrid.npcers.Remove(aoiUnit.GetParent <Unit>().Id); } break; } } }
/// <summary> /// 更新 AoiUnitInfo ,当换格时 /// </summary> /// <param name="aoiUnit"></param> public static void UpdateAoiUnitInfo(this AoiGridComponent self, AoiUnitComponent aoiUnit) { // 把新的AOI节点转移到旧的节点里 aoiUnit.playerIds.OldMovesSet = aoiUnit.playerIds.MovesSet.Select(d => d).ToHashSet(); aoiUnit.enemyIds.OldMovesSet = aoiUnit.enemyIds.MovesSet.Select(d => d).ToHashSet(); aoiUnit.npcerIds.OldMovesSet = aoiUnit.npcerIds.MovesSet.Select(d => d).ToHashSet(); //// 移动到新的位置 self.MoveToChangeAoiGrid(aoiUnit); // 查找 周围 可见九宫格内单元 unitId self.FindAoi(aoiUnit); // 差集计算 aoiUnit.playerIds.Enters = aoiUnit.playerIds.MovesSet.Except(aoiUnit.playerIds.OldMovesSet).ToHashSet(); aoiUnit.playerIds.Leaves = aoiUnit.playerIds.OldMovesSet.Except(aoiUnit.playerIds.MovesSet).ToHashSet(); aoiUnit.enemyIds.Enters = aoiUnit.enemyIds.MovesSet.Except(aoiUnit.enemyIds.OldMovesSet).ToHashSet(); aoiUnit.enemyIds.Leaves = aoiUnit.enemyIds.OldMovesSet.Except(aoiUnit.enemyIds.MovesSet).ToHashSet(); aoiUnit.npcerIds.Enters = aoiUnit.npcerIds.MovesSet.Except(aoiUnit.npcerIds.OldMovesSet).ToHashSet(); aoiUnit.npcerIds.Leaves = aoiUnit.npcerIds.OldMovesSet.Except(aoiUnit.npcerIds.MovesSet).ToHashSet(); ///20190715 AoiPlayerComponent aoiPlayer = aoiUnit.GetParent <Unit>().GetComponent <AoiPlayerComponent>(); if (aoiPlayer != null) { aoiPlayer.UpdateAddRemove(); } ///将进入自己视野的人 加进来;将自己加入别的人视野 aoiUnit.UpdateEnters(); ///将离开自己视野的人 删除掉;同时将自己从别人的视野里删除 aoiUnit.UpdateLeaves(); }
/// <summary> /// 得到本人的 九宫格内 最新的 所有单元的 unitId /// </summary> /// <param name="aoiUnit"></param> static void FindAoi(this AoiGridComponent self, AoiUnitComponent aoiUnit) { aoiUnit.playerIds.MovesSet.Clear(); aoiUnit.enemyIds.MovesSet.Clear(); aoiUnit.npcerIds.MovesSet.Clear(); AoiGrid mygrid = self.Get(aoiUnit.gridId); foreach (long gid in mygrid.seeGrids) { AoiGrid nineGrid = self.Get(gid); if (nineGrid.players.Count > 0) { foreach (long unitId1 in nineGrid.players) { aoiUnit.playerIds.MovesSet.Add(unitId1); } } if (nineGrid.enemys.Count > 0) { foreach (long unitId2 in nineGrid.enemys) { aoiUnit.enemyIds.MovesSet.Add(unitId2); } } if (nineGrid.npcers.Count > 0) { foreach (long unitId3 in nineGrid.npcers) { aoiUnit.npcerIds.MovesSet.Add(unitId3); } } } }
public static void SqrDistance(this SqrDistanceComponent self) { AoiUnitComponent aoiUnit = self.GetParent <Unit>().GetComponent <AoiUnitComponent>(); UnitType unitType = self.GetParent <Unit>().UnitType; switch (unitType) { case UnitType.Player: if (aoiUnit.enemyIds.MovesSet.Count > 0) { Unit[] units1 = Game.Scene.GetComponent <MonsterUnitComponent>().GetAllByIds(aoiUnit.enemyIds.MovesSet.ToArray()); //Console.WriteLine(" SqrDistance-19-Player:" + aoiUnit.enemyIds.MovesSet.Count + " / " + units1.Length); self.SqrDistance(units1); } break; case UnitType.Monster: if (aoiUnit.playerIds.MovesSet.Count > 0) { Unit[] units2 = Game.Scene.GetComponent <UnitComponent>().GetAllByIds(aoiUnit.playerIds.MovesSet.ToArray()); //Console.WriteLine(" SqrDistance-25-Monster:" + aoiUnit.playerIds.MovesSet.Count + " / " + units2.Length); self.SqrDistance(units2); } break; default: break; } }
public static long GetGridId(this AoiGridComponent self, AoiUnitComponent aoiUnit) { float x = aoiUnit.GetParent <Unit>().Position.x; float y = aoiUnit.GetParent <Unit>().Position.z; long id = self.GetDridX(x) + self.GetDridY(y) * self.rcCount; return(id); }
/// <summary> /// 根据坐标 更换地图固定格子 注册注销 unitId /// </summary> /// <param name="aoiUnit"></param> public static void MoveToChangeAoiGrid(this AoiGridComponent self, AoiUnitComponent aoiUnit) { long oldid = aoiUnit.gridId; self.RemoveUnitId(aoiUnit); aoiUnit.gridId = self.GetGridId(aoiUnit); self.AddUnitId(aoiUnit); Console.WriteLine(" AoiGridComponentHelper-119-Id: " + aoiUnit.GetParent <Unit>().Id + " " + aoiUnit.GetParent <Unit>().UnitType + " " + oldid + " => " + aoiUnit.gridId); }
public static void UpdateAddRemove(this AoiPlayerComponent self) { AoiUnitComponent aoiUnit = self.GetParent <Unit>().GetComponent <AoiUnitComponent>(); if (aoiUnit.playerIds.Enters.Count > 0) { //ToTo 通知self客户端(1个) 加入这些玩家(多个) self.AddPlayers(aoiUnit.playerIds.Enters.ToArray(), new long[1] { self.GetParent <Unit>().Id }); Console.WriteLine(" AoiPlayer-20-玩家:" + aoiUnit.playerIds.Enters.Count + " 个," + " 进入 PlayerId:" + self.GetParent <Unit>().Id + "(" + aoiUnit.gridId + ") 的视野。"); Console.WriteLine(" AoiPlayer-21-玩家:" + self.GetParent <Unit>().Id + "(" + aoiUnit.gridId + ") 看到玩家:" + aoiUnit.enemyIds.MovesSet.Count); } if (aoiUnit.enemyIds.Enters.Count > 0) { //ToTo 通知self客户端(1个) 加入这些小怪(多个) self.AddMonsters(aoiUnit.enemyIds.Enters.ToArray(), new long[1] { self.GetParent <Unit>().Id }); Console.WriteLine(" AoiPlayer-28-小怪:" + aoiUnit.enemyIds.Enters.Count + " 个," + " 进入 PlayerId:" + self.GetParent <Unit>().Id + "(" + aoiUnit.gridId + ") 的视野。"); Console.WriteLine(" AoiPlayer-29-玩家:" + self.GetParent <Unit>().Id + "(" + aoiUnit.gridId + ") 看到小怪:" + aoiUnit.enemyIds.MovesSet.Count); } if (aoiUnit.playerIds.Leaves.Count > 0) { //ToTo 通知self客户端(1个) 删除这些玩家(多个) self.RemovePlayers(aoiUnit.playerIds.Leaves.ToArray(), new long[1] { self.GetParent <Unit>().Id }); Console.WriteLine(" AoiPlayer-36-玩家:" + aoiUnit.playerIds.Leaves.Count + " 个," + " 离开 PlayerId:" + self.GetParent <Unit>().Id + "(" + aoiUnit.gridId + ") 的视野。"); Console.WriteLine(" AoiPlayer-37-玩家:" + self.GetParent <Unit>().Id + "(" + aoiUnit.gridId + ") 看到玩家:" + aoiUnit.playerIds.MovesSet.Count); } if (aoiUnit.enemyIds.Leaves.Count > 0) { //ToTo 通知self客户端(1个) 删除这些小怪(多个) self.RemoveMonsters(aoiUnit.enemyIds.Leaves.ToArray(), new long[1] { self.GetParent <Unit>().Id }); Console.WriteLine(" AoiPlayer-44-小怪:" + aoiUnit.enemyIds.Leaves.Count + " 个," + "离开 PlayerId:" + self.GetParent <Unit>().Id + "(" + aoiUnit.gridId + ") 视野。"); Console.WriteLine(" AoiPlayer-45-玩家:" + self.GetParent <Unit>().Id + "(" + aoiUnit.gridId + ") 看到小怪:" + aoiUnit.enemyIds.MovesSet.Count); } }
/// <summary> /// 将离开自己视野的人 删除掉;同时将自己从别人的视野里删除 /// </summary> public static void UpdateLeaves(this AoiUnitComponent self) { foreach (long tem in self.playerIds.Leaves) { ///通知 刚进入小怪视野的玩家(多个) 加入这个小怪的Id(1个) AoiPlayerComponent temAoiPlayer1 = Game.Scene.GetComponent <UnitComponent>().Get(tem).GetComponent <AoiPlayerComponent>(); AoiUnitComponent temAoiUnit1 = Game.Scene.GetComponent <UnitComponent>().Get(tem).GetComponent <AoiUnitComponent>(); UnitType unitType = self.GetParent <Unit>().UnitType; switch (unitType) { case UnitType.Player: temAoiUnit1.playerIds.MovesSet.Remove(self.GetParent <Unit>().Id); temAoiUnit1.playerIds.Leaves.Add(self.GetParent <Unit>().Id); ///ToTo 通知tem客户端(多个) 删除此玩家(1个) temAoiPlayer1.RemovePlayers(new long[1] { self.GetParent <Unit>().Id }, new long[1] { tem }); Console.WriteLine(" AoiUnit-86-玩家-Id:" + self.GetParent <Unit>().Id + " 离开 PlayerId:" + tem + "(" + temAoiUnit1.gridId + ") 的视野。"); Console.WriteLine(" AoiUnit-87-玩家-Id:" + tem + "(" + temAoiUnit1.gridId + ") 看到玩家:" + temAoiUnit1.playerIds.MovesSet.Count); break; case UnitType.Monster: temAoiUnit1.enemyIds.MovesSet.Remove(self.GetParent <Unit>().Id); temAoiUnit1.enemyIds.Leaves.Add(self.GetParent <Unit>().Id); ///ToTo 通知tem客户端(多个) 删除此小怪(1个) temAoiPlayer1.RemoveMonsters(new long[1] { self.GetParent <Unit>().Id }, new long[1] { tem }); Console.WriteLine(" AoiUnit-96-小怪-Id:" + self.GetParent <Unit>().Id + " 离开 PlayerId:" + tem + "(" + temAoiUnit1.gridId + ") 的视野。"); Console.WriteLine(" AoiUnit-97-玩家-Id:" + tem + "(" + temAoiUnit1.gridId + ") 看到小怪:" + temAoiUnit1.enemyIds.MovesSet.Count); break; } } foreach (long tem in self.enemyIds.Enters) { ///通知 刚进入本人视野的小怪(多个) 加入本人的Id(1个) AoiUnitComponent temAoiUnit0 = Game.Scene.GetComponent <MonsterUnitComponent>().Get(tem).GetComponent <AoiUnitComponent>(); UnitType unitType = self.GetParent <Unit>().UnitType; switch (unitType) { case UnitType.Player: temAoiUnit0.playerIds.MovesSet.Remove(self.GetParent <Unit>().Id); temAoiUnit0.playerIds.Leaves.Add(self.GetParent <Unit>().Id); break; case UnitType.Monster: temAoiUnit0.enemyIds.MovesSet.Remove(self.GetParent <Unit>().Id); temAoiUnit0.enemyIds.Leaves.Add(self.GetParent <Unit>().Id); break; } } }
/// <summary> /// 自己死亡后 将自己从别人的视野里删除 /// </summary> public static void DeathRemove(this AoiUnitComponent self) { long unitId = self.GetParent <Unit>().Id; UnitType unitType = self.GetParent <Unit>().UnitType; switch (unitType) { case UnitType.Player: foreach (long tem in self.playerIds.MovesSet) { AoiPlayerComponent temAoiPlayer1 = Game.Scene.GetComponent <UnitComponent>().Get(tem).GetComponent <AoiPlayerComponent>(); AoiUnitComponent temAoiUnit1 = Game.Scene.GetComponent <UnitComponent>().Get(tem).GetComponent <AoiUnitComponent>(); ///将自己从别人玩家的视野里删除 if (tem != unitId) { temAoiUnit1.playerIds.MovesSet.Remove(unitId); } ///通知tem客户端(多个) 删除此玩家(1个)实例,如果是本客户端则重置此玩家自己实例位置 temAoiPlayer1.RemovePlayers(new long[1] { unitId }, new long[1] { tem }); Console.WriteLine(" AoiUnit-145-玩家死亡-Id:" + unitId + " 离开 PlayerId:" + tem + "(" + temAoiUnit1.gridId + ") 的视野。"); } foreach (long tem in self.enemyIds.MovesSet) { ///将自己 从小怪(多个)的视野里删除 AoiUnitComponent temAoiUnit2 = Game.Scene.GetComponent <MonsterUnitComponent>().Get(tem).GetComponent <AoiUnitComponent>(); temAoiUnit2.playerIds.MovesSet.Remove(unitId); Console.WriteLine(" AoiUnit-151-玩家死亡-Id:" + unitId + " 离开 MonsterId:" + tem + "(" + temAoiUnit2.gridId + ") 的视野。"); } break; case UnitType.Monster: foreach (long tem in self.playerIds.MovesSet) { AoiPlayerComponent temAoiPlayer1 = Game.Scene.GetComponent <UnitComponent>().Get(tem).GetComponent <AoiPlayerComponent>(); AoiUnitComponent temAoiUnit1 = Game.Scene.GetComponent <UnitComponent>().Get(tem).GetComponent <AoiUnitComponent>(); ///将死亡的小怪 从玩家的视野里删除 temAoiUnit1.enemyIds.MovesSet.Remove(unitId); ///ToTo 通知tem客户端(多个) 删除此小怪(1个)实例 temAoiPlayer1.RemoveMonsters(new long[1] { unitId }, new long[1] { tem }); Console.WriteLine(" AoiUnit-165-小怪死亡-Id:" + unitId + " 离开 PlayerId:" + tem + "(" + temAoiUnit1.gridId + ") 的视野。"); } //foreach (long tem in self.enemyIds.MovesSet) //{ // ///将死亡小怪 从其他小怪(多个)的视野里删除 // AoiUnitComponent temAoiUnit2 = Game.Scene.GetComponent<MonsterUnitComponent>().Get(tem).GetComponent<AoiUnitComponent>(); // temAoiUnit2.enemyIds.MovesSet.Remove(unitId); //} break; } }