1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036 |
- using System.Xml;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Map
- {
- public const float TILE_WIDTH = 1f;
- public const float TILE_LENGTH = 1f;
- public MapData.MapID id;
- public bool isEditing;
- private int _columns;
- private int _rows;
- private string _name;
- private SpatialAStar<AStarNode, object> aStar;
- private List<AstarSearchQueue> searchQueue;
- private Dictionary<int, BattleObject> battleObjectDict;
- private Dictionary<int, CrystalBase> crystalDict;
- private List<CrystalBase> crystalList;
- private Dictionary<int, Door> doorDict;
- private List<Door> doorList;
- private Dictionary<int, List<MapItem>> itemTileDict;
- private Dictionary<int, MapItem> itemDict;
- private List<MapItem> itemList;
- private Dictionary<int, MapItemPos> itemPosDict;
- private List<MapItemPos> itemPosList;
- private Dictionary<TeamUtil.Team, FlagItem> flagDict;
- private Dictionary<TeamUtil.Team, FlagBase> flagBaseDict;
- private Dictionary<TeamUtil.Team, StartPos> startPosDict;
- private List<AStarNode> teamBlockList;
- private List<Block> blockList;
- private XmlNode mapNode;
- public Map()
- {
- searchQueue = new List<AstarSearchQueue>();
- battleObjectDict = new Dictionary<int, BattleObject>();
- crystalDict = new Dictionary<int, CrystalBase>();
- crystalList = new List<CrystalBase>();
- doorDict = new Dictionary<int, Door>();
- doorList = new List<Door>();
- itemTileDict = new Dictionary<int, List<MapItem>>();
- itemDict = new Dictionary<int, MapItem>();
- itemList = new List<MapItem>();
- itemPosDict = new Dictionary<int, MapItemPos>();
- itemPosList = new List<MapItemPos>();
- flagDict = new Dictionary<TeamUtil.Team, FlagItem>();
- flagBaseDict = new Dictionary<TeamUtil.Team, FlagBase>();
- startPosDict = new Dictionary<TeamUtil.Team, StartPos>();
- teamBlockList = new List<AStarNode>();
- blockList = new List<Block>();
- }
- public static int XToColumn(float x)
- {
- return (int)(x/TILE_WIDTH);
- }
- public static int ZToRow(float z)
- {
- return (int)(z/TILE_LENGTH);
- }
- public static float GetCenterX(int col)
- {
- return col*TILE_WIDTH+TILE_WIDTH/2f;
- }
- public static float GetCenterZ(int row)
- {
- return row*TILE_WIDTH+TILE_LENGTH/2f;
- }
- public int GetIndexByGrid(int col, int row)
- {
- if(!IsInRange(col, row))
- return -1;
- return row * columns + col;
- }
- public Vector2 GetGridByIndex(int index)
- {
- int col = index % rows;
- int row = index / columns;
- return new Vector2(col, row);
- }
- public bool IsInRange(int col, int row)
- {
- if(col < 0 || col >= columns || row < 0 || row >= rows)
- return false;
- return true;
- }
- public string name
- {
- get{return _name;}
- }
- public int columns
- {
- get{return _columns;}
- }
- public int rows
- {
- get{return _rows;}
- }
- public void CreateEmptyMap(int columns, int rows)
- {
- this._columns = columns;
- this._rows = rows;
- AStarNode[,] grid = new AStarNode[columns, rows];
- for(int y=0; y<rows; y++)
- {
- for(int x=0; x<columns; x++)
- {
- grid[x, y] = new AStarNode()
- {
- X = x,
- Y = y,
- };
- }
- }
- aStar = new SpatialAStar<AStarNode, object>(grid);
- }
- public void CreateMap(XmlDocument xml)
- {
- mapNode = xml.SelectSingleNode("map");
- XmlNode infoNode = mapNode.SelectSingleNode("info");
- this._columns = int.Parse(infoNode.Attributes["columns"].Value);
- this._rows = int.Parse(infoNode.Attributes["rows"].Value);
- this._name = infoNode.Attributes["name"].Value;
- AStarNode[,] grid = new AStarNode[_columns, rows];
- XmlNode tileNode = mapNode.SelectSingleNode("tile");
- XmlNodeList tileRowNodeList = tileNode.SelectNodes("t");
- List<AStarNode> blockNodeList = new List<AStarNode>();
- for(int y=0; y<_rows; y++)
- {
- XmlNode tileRowNode = tileRowNodeList[y];
- string tileRowStr = tileRowNode.InnerText;
- for(int x=0; x<_columns; x++)
- {
- int tileType = int.Parse(tileRowStr.Substring(x, 1));
- AStarNode.Type t = AStarNode.TypeArr[tileType];
- AStarNode node = new AStarNode()
- {
- type = t,
- X = x,
- Y = y,
- };
- if(t == AStarNode.Type.BlueWall || t == AStarNode.Type.RedWall || t == AStarNode.Type.YellowWall)
- blockNodeList.Add(node);
- grid[x, y] = node;
- }
- }
-
- aStar = new SpatialAStar<AStarNode, object>(grid);
- for(int i=0; i<blockNodeList.Count; i++)
- {
- CreateBlock(blockNodeList[i]);
- }
- CreateBuilding(mapNode);
- CreateFlag(mapNode);
- }
- private void CreateBlock(AStarNode node)
- {
- float x = GetCenterX(node.X);
- float z = GetCenterZ(node.Y);
-
- GameObject blockObj = GameObject.Instantiate(Resources.Load(Config.BLOCK_PREFAB)) as GameObject;
-
- Vector3 position = new Vector3();
- position.x = x;
- position.z = z;
- blockObj.transform.position = position;
-
- Block block = blockObj.GetComponent<Block>();
- block.Init(this, TeamUtil.GetTeamByAstarNodeType(node.type));
- this.blockList.Add(block);
- }
- private void CreateFlag(XmlNode xml)
- {
- XmlNode flagNode = xml.SelectSingleNode("flag");
- if(flagNode != null)
- {
- XmlNodeList flagList = flagNode.SelectNodes("f");
- for(int i=0; i<flagList.Count; i++)
- {
- XmlNode f = flagList.Item(i);
- int team = int.Parse(f.Attributes["team"].Value);
- int column = int.Parse(f.Attributes["c"].Value);
- int row = int.Parse(f.Attributes["r"].Value);
- Vector3 position = new Vector3();
- position.x = GetCenterX(column);
- position.z = GetCenterZ(row);
- }
- }
- }
- private void CreateBuilding(XmlNode xml)
- {
- XmlNode buildingNode = xml.SelectSingleNode("building");
- if(buildingNode != null)
- {
- XmlNodeList baseList = buildingNode.SelectNodes("crystal");
- for(int i=0; i<baseList.Count; i++)
- {
- XmlElement b = baseList.Item(i) as XmlElement;
- float x = StringUtil.ToFloat(b.GetAttribute("x"));
- float y = StringUtil.ToFloat(b.GetAttribute("y"));
- int isStart = StringUtil.ToInt(b.GetAttribute("s"));
- int occupyPriority = StringUtil.ToInt(b.GetAttribute("p"));
- int overwhelming = StringUtil.ToInt(b.GetAttribute("o"));
- int startPosDir = StringUtil.ToInt(b.GetAttribute("f"));
- string model = b.GetAttribute ("md");
- GameObject crystalBaseObj = null;
- if(StringUtil.Empty(model))
- crystalBaseObj = GameObject.Instantiate(Resources.Load(Config.CRYSTAL_BASE_PREFAB)) as GameObject;
- else
- crystalBaseObj = GameObject.Instantiate(Resources.Load(Config.MAP_OBJECT_FOLDER + model)) as GameObject;
-
- Vector3 position = new Vector3();
- position.x = x;
- position.z = y;
- crystalBaseObj.transform.position = position;
- CrystalBase crystalBase = crystalBaseObj.GetComponent<CrystalBase>();
- crystalBase.id = i;
- crystalBase.isStart = isStart;
- crystalBase.occupyPriority = occupyPriority;
- crystalBase.overwhelming = overwhelming>0;
- crystalBase.startPositionDirect = MapObject.GetStartPositionDirect(startPosDir);
- crystalDict.Add(i, crystalBase);
- crystalList.Add(crystalBase);
- }
- XmlNodeList flagbaseList = buildingNode.SelectNodes("flagbase");
- for(int i=0; i<flagbaseList.Count; i++)
- {
- XmlElement d = flagbaseList.Item(i) as XmlElement;
- float x = StringUtil.ToFloat(d.GetAttribute("x"));
- float y = StringUtil.ToFloat(d.GetAttribute("y"));
- int team = StringUtil.ToInt(d.GetAttribute("t"));
- int isStart = StringUtil.ToInt(d.GetAttribute("s"));
- int startPosDir = StringUtil.ToInt(d.GetAttribute("f"));
- string model = d.GetAttribute ("md");
- GameObject flagBaseObj = null;
- if (StringUtil.Empty (model))
- flagBaseObj = GameObject.Instantiate (Resources.Load (Config.FLAG_BASE_PREFAB)) as GameObject;
- else
- flagBaseObj = GameObject.Instantiate (Resources.Load (Config.MAP_OBJECT_FOLDER + model)) as GameObject;
-
- Vector3 position = new Vector3();
- position.x = x;
- position.z = y;
- flagBaseObj.transform.position = position;
-
- FlagBase flagBase = flagBaseObj.GetComponent<FlagBase>();
- flagBase.Init(this);
- flagBase.team = TeamUtil.GetTeam(team);
- flagBase.isStart = isStart;
- flagBase.startPositionDirect = MapObject.GetStartPositionDirect(startPosDir);
- this.flagBaseDict.Add(flagBase.team, flagBase);
- }
- XmlNodeList doorList = buildingNode.SelectNodes("door");
- for(int i=0; i<doorList.Count; i++)
- {
- XmlNode d = doorList.Item(i);
- float x = StringUtil.ToFloat(d.Attributes["x"].Value);
- float y = StringUtil.ToFloat(d.Attributes["y"].Value);
- int occupyPriority = StringUtil.ToInt(d.Attributes["p"].Value);
-
- GameObject doorObj = GameObject.Instantiate(Resources.Load(Config.DOOR_PREFAB)) as GameObject;
-
- Vector3 position = new Vector3();
- position.x = x;
- position.z = y;
- doorObj.transform.position = position;
-
- Door door = doorObj.GetComponent<Door>();
- door.index = i;
- door.occupyPriority = occupyPriority;
- door.typeId = MapObjectUtil.TypeId.Door.GetHashCode();
- doorDict.Add(i, door);
- this.doorList.Add(door);
- }
- XmlNodeList itemList = buildingNode.SelectNodes("item");
- for(int i=0; i<itemList.Count; i++)
- {
- XmlNode d = itemList.Item(i);
- int id = StringUtil.ToInt(d.Attributes["i"].Value);
- float x = StringUtil.ToFloat(d.Attributes["x"].Value);
- float y = StringUtil.ToFloat(d.Attributes["y"].Value);
-
- GameObject mapItemPosObj = GameObject.Instantiate(Resources.Load(Config.ITEM_POS_PREFAB)) as GameObject;
-
- Vector3 position = new Vector3();
- position.x = x;
- position.z = y;
- mapItemPosObj.transform.position = position;
-
- MapItemPos mapItemPos = mapItemPosObj.GetComponent<MapItemPos>();
- mapItemPos.id = i;
- mapItemPos.typeId = MapObjectUtil.TypeId.MapItemPos.GetHashCode();
- mapItemPos.Init(this);
- itemPosDict.Add(i, mapItemPos);
- itemPosList.Add(mapItemPos);
- }
- XmlNodeList startList = buildingNode.SelectNodes("start");
- for(int i=0; i<startList.Count; i++)
- {
- XmlElement d = startList.Item(i) as XmlElement;
- float x = StringUtil.ToFloat(d.GetAttribute("x"));
- float y = StringUtil.ToFloat(d.GetAttribute("y"));
- int team = StringUtil.ToInt(d.GetAttribute("t"));
- int startPosDir = StringUtil.ToInt(d.GetAttribute("f"));
- string model = d.GetAttribute ("md");
- GameObject startObj = GameObject.Instantiate (Resources.Load (Config.MAP_OBJECT_FOLDER + model)) as GameObject;
- Vector3 position = new Vector3();
- position.x = x;
- position.z = y;
- startObj.transform.position = position;
- StartPos startPos = startObj.GetComponent<StartPos>();
- startPos.Init(this);
- startPos.team = TeamUtil.GetTeam(team);
- startPos.startPositionDirect = MapObject.GetStartPositionDirect(startPosDir);
- this.startPosDict.Add(startPos.team, startPos);
- }
- }
- }
- public AStarNode GetAStarNode(int x, int y)
- {
- if(x < 0 || x >= columns || y < 0 || y >= rows)
- {
- return null;
- }
- return aStar.SearchSpace[x, y];
- }
- public AStarNode GetAStarNodeByPosition(Vector3 position)
- {
- int x = (int)(position.x/TILE_WIDTH);
- int y = (int)(position.z/TILE_LENGTH);
- return GetAStarNode(x, y);
- }
- public AStarNode[,] GetAStarGrid()
- {
- return aStar.SearchSpace;
- }
- public Vector3 GetAstarNodePosition(int x, int y)
- {
- return new Vector3((x+0.5f)*Map.TILE_WIDTH, 0, (y+0.5f)*Map.TILE_LENGTH);
- }
- public LinkedList<AStarNode> GetPath(AStarPoint start, AStarPoint end, Craft craft)
- {
- AStarNode node = GetAStarNode(end.x, end.y);
- if(!node.IsWalkable(craft))
- {
- end = AStarHelper.getGoalFromGoal(start, end, aStar.SearchSpace, craft);
- }
- return aStar.Search(start, end, craft);
- }
- public void PathSearchEnqueue(AStarPoint start, AStarPoint end, Craft craft)
- {
- for(int i=0; i<searchQueue.Count; i++)
- {
- AstarSearchQueue queue = searchQueue[i];
- if(craft == queue.craft)
- {
- queue.start = start;
- queue.end = end;
- return;
- }
- }
- searchQueue.Add(new AstarSearchQueue(start, end, craft));
- }
- public void PathSearchDequeue()
- {
- if(searchQueue.Count > 0)
- {
- AstarSearchQueue queue = searchQueue[0];
- LinkedList<AStarNode> list = GetPath(queue.start, queue.end, queue.craft);
- queue.craft.SetPath(list);
- searchQueue.RemoveAt(0);
- }
- }
- public AStarNode FindNearestEmptyAstarNode(int col, int row)
- {
- int step = 1;
- while(true)
- {
- for(int i=-step; i<=step; i++)
- {
- int c = i;
- int r = step - Mathf.Abs(i);
- AStarNode node = GetAStarNode(col+c, row+r);
- if(node != null && node.type == AStarNode.Type.Empty)
- {
- return node;
- }
- if(r != 0)
- {
- node = GetAStarNode(col+c, row-r);
- if(node != null && node.type == AStarNode.Type.Empty)
- {
- return node;
- }
- }
- }
- step++;
- if(step > 10)
- {
- break;
- }
- }
- return null;
- }
- public Vector3 GetStartPosition(Player player)
- {
- Vector3 pos = Vector3.zero;
- if(startPosDict.ContainsKey(player.team))
- {
- return startPosDict[player.team].GetStartPosition();
- }
- float distance = float.MaxValue;
- List<MapBase> list = new List<MapBase>();
- for(int i=0; i<crystalList.Count; i++)
- {
- CrystalBase crystalBase = crystalList[i];
- if(crystalBase.startPositionDirect == MapObject.StartPostionDirect.None)
- continue;
- if(crystalBase.isStart >= 0 && crystalBase.GetStation() != null && crystalBase.GetStation().team == player.team)
- {
- Player.Hero hero = player.GetHero();
- if(hero.IsFirstTimeSelectHero())
- {
- if(crystalBase.isStart > 0)
- list.Add(crystalBase);
- }
- else
- {
- float d = NumberUtil.distanceVector3(crystalBase.position, hero.deadPostion);
- if(d < distance)
- {
- distance = d;
- pos = crystalBase.position;
- list.Clear();
- list.Add(crystalBase);
- }
- else if(d == distance+5f)
- {
- list.Add(crystalBase);
- }
- }
- }
- }
- // if(player.IsFirstTimeSelectHero() && flagBaseDict.ContainsKey(player.team))
- // {
- // list.Add(flagBaseDict[player.team]);
- // }
- if(list.Count > 0)
- {
- int i = Random.Range(0, list.Count);
- return list[i].GetStartPosition();
- }
- else if(flagBaseDict.ContainsKey(player.team))
- {
- return flagBaseDict[player.team].GetStartPosition();
- }
- return Vector3.zero;
- }
- public CrystalBase GetCrystalBase(int id)
- {
- if(crystalDict.ContainsKey(id))
- return crystalDict [id];
- return null;
- }
- public List<CrystalBase> GetCrystalBaseList()
- {
- return crystalList;
- }
- public CrystalBase GetInRangeCrystalBase(Vector3 pos)
- {
- for(int i=0; i<crystalList.Count; i++)
- {
- CrystalBase c = crystalList[i];
- float d = NumberUtil.distanceVector3(pos, c.position);
- if(d < c.range)
- {
- return c;
- }
- }
- return null;
- }
- public Station CreateStation(StationData data, BattleController battleController)
- {
- GameObject stationObj = null;
- if(id == MapData.MapID.Challenge)
- stationObj = GameObject.Instantiate(Resources.Load(Config.MAP_OBJECT_FOLDER + "CenterStation")) as GameObject;
- else
- stationObj = GameObject.Instantiate(Resources.Load(Config.STATION_PREFAB)) as GameObject;
- CrystalBase crystalBase = GetCrystalBase(data.crystalId);
-
- Station station = stationObj.GetComponent<Station>();
- station.Init(this);
- station.id = data.id;
- station.userId = data.userId;
- station.team = data.team;
- station.typeId = MapObjectUtil.TypeId.Station.GetHashCode();
- station.aiType = AI.AIType.Station;
- station.position = crystalBase.position;
- crystalBase.SetStation(station);
- AddBattleObject (station);
- stationObj.AddComponent<StationAI>().init(battleController);
- return station;
- }
- public void ClearBlocks(TeamUtil.Team team)
- {
- if(blockList.Count > 0)
- {
- for(int i=blockList.Count-1; i>=0; i--)
- {
- Block block = blockList[i];
- if(block.team == team)
- {
- block.Remove();
- blockList.RemoveAt(i);
- }
- }
- }
- }
- public Door GetDoorByIndex(int index)
- {
- if(doorDict.ContainsKey(index))
- return doorDict[index];
- return null;
- }
- public List<Door> GetDoorList()
- {
- return doorList;
- }
- public void RemoveDoor(Door door)
- {
- doorDict.Remove(door.id);
- doorList.Remove(door);
- CheckDoorRemove();
- }
- public void RemoveNoIdDoor()
- {
- foreach(KeyValuePair<int, Door> kvp in doorDict)
- {
- if(kvp.Value.id == 0)
- {
- doorDict.Remove(kvp.Key);
- doorList.Remove(kvp.Value);
- GameObject.Destroy(kvp.Value.gameObject);
- }
- }
- CheckDoorRemove();
- }
- private void CheckDoorRemove()
- {
- int blueCount = 0;
- int redCount = 0;
- for(int i=0; i<doorList.Count; i++)
- {
- Door door = doorList[i];
- if(door.team == TeamUtil.Team.Blue)
- {
- blueCount++;
- }
- else if(door.team == TeamUtil.Team.Red)
- {
- redCount++;
- }
- }
- if(blueCount == 0)
- ClearBlocks(TeamUtil.Team.Blue);
- if(redCount == 0)
- ClearBlocks(TeamUtil.Team.Red);
- }
- public MapItemPos GetMapItemPos(int id)
- {
- if(itemPosDict.ContainsKey(id))
- return itemPosDict[id];
- return null;
- }
- public List<MapItemPos> GetMapItemPosList()
- {
- return itemPosList;
- }
- public MapItem CreateMapItem(MapItemData data)
- {
- if(itemDict.ContainsKey(data.id))
- {
- return itemDict [data.id];
- }
- GameObject mapItemObj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>(Config.BIGGER_ITEM_PREFAB));
- MapItem mapItem = mapItemObj.GetComponent<MapItem>();
- mapItem.Init(this);
- mapItem.id = data.id;
- mapItem.team = data.team;
- mapItem.SetPosition(data.postion);
- mapItem.TakeTiles();
- itemDict.Add(mapItem.id, mapItem);
- itemList.Add(mapItem);
- return mapItem;
- }
- public void MapItemPlace(int tileIndex, MapItem item)
- {
- if(tileIndex < 0)
- return;
- if(!itemTileDict.ContainsKey(tileIndex))
- itemTileDict.Add(tileIndex, new List<MapItem>());
- itemTileDict[tileIndex].Add(item);
- }
- public void MapItemClean(MapItem item)
- {
- List<int> tileIndexs = item.GetTileIndexs();
- for(int i=0; i<tileIndexs.Count; i++)
- {
- int index = tileIndexs[i];
- if(!itemTileDict.ContainsKey(index))
- continue;
- itemTileDict[index].Remove(item);
- }
- }
- public MapItem GetMapItem(int id)
- {
- if(itemDict.ContainsKey(id))
- return itemDict[id];
- return null;
- }
- public List<MapItem> GetMapItemByRange(Vector3 originPos, float distance)
- {
- List<MapItem> list = new List<MapItem>();
- for(int i=0; i<itemList.Count; i++)
- {
- MapItem mapItem = itemList[i];
- if(NumberUtil.distanceVector3(originPos, mapItem.position, true) <= distance)
- list.Add(mapItem);
- }
- return list;
- }
- public List<MapItem> GetMapItemList()
- {
- return itemList;
- }
- public List<MapItem> GetMapItemByGrid(int col, int row)
- {
- int tileIndex = GetIndexByGrid(col, row);
- if(!itemTileDict.ContainsKey(tileIndex))
- return null;
- return itemTileDict[tileIndex];
- }
- public void RemoveMapItem(MapItem mapItem)
- {
- if(mapItem != null)
- {
- itemDict.Remove(mapItem.id);
- itemList.Remove(mapItem);
- if(itemPosDict.ContainsKey(mapItem.id))
- itemPosDict[mapItem.id].item = null;
- }
- }
- public void ClearAllMapItem()
- {
- for(int i=itemList.Count-1; i>=0; i--)
- {
- itemList[i].Remove();
- }
- }
- public StartPos GetStartPos(TeamUtil.Team team)
- {
- if(startPosDict.ContainsKey(team))
- return startPosDict[team];
- return null;
- }
- public FlagBase GetFlagBase(TeamUtil.Team team)
- {
- return flagBaseDict[team];
- }
-
- public FlagItem CreateFlag(TeamUtil.Team team)
- {
- GameObject flagObj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>(Config.FLAG_PREFAB));
- FlagItem flagItem = flagObj.GetComponent<FlagItem>();
- flagItem.Init(this);
- flagItem.team = team;
- RemoveFlag(team);
- flagDict.Add(team, flagItem);
- return flagItem;
- }
- public FlagItem GetFlag(TeamUtil.Team team)
- {
- if(flagDict.ContainsKey(team))
- return flagDict[team];
- return null;
- }
- public bool HasFlag()
- {
- return flagDict.Count > 0;
- }
- public FlagItem RemoveFlag(TeamUtil.Team team)
- {
- FlagItem flag = GetFlag(team);
- flagDict.Remove(team);
- if(flag != null)
- {
- flag.Remove();
- }
- return flag;
- }
- public void CleanFlag()
- {
- // how to remove the Map objects
- RemoveFlag(TeamUtil.Team.Blue);
- RemoveFlag(TeamUtil.Team.Red);
- }
- public Dictionary<int, BattleObject> GetBattleObjectDict()
- {
- return battleObjectDict;
- }
- public BattleObject AddBattleObject(BattleObject battleObject)
- {
- Dictionary<int, BattleObject> dict = battleObjectDict;
- dict.Add(battleObject.id, battleObject);
- return battleObject;
- }
- public void RemoveBattleObject(BattleObject battleObject)
- {
- battleObjectDict.Remove(battleObject.id);
- if(battleObject.typeId == MapObjectUtil.TypeId.Door.GetHashCode())
- {
- Door door = battleObject as Door;
- RemoveDoor(door);
- }
- }
- public void ClearAllBattleObject()
- {
- List<BattleObject> list = new List<BattleObject>();
- Dictionary<int, BattleObject> battleObjDict = GetBattleObjectDict();
- foreach(KeyValuePair<int, BattleObject> kvp in battleObjDict)
- {
- list.Add(kvp.Value);
- }
-
- for(int i=0; i<list.Count; i++)
- {
- list[i].Dead();
- }
- }
- public BattleObject GetBattleObject(int id)
- {
- if(!battleObjectDict.ContainsKey(id))
- {
- return null;
- }
- return battleObjectDict[id];
- }
- public List<BattleObject> GetBattleObjectByRange(Vector3 origin, float range, TeamUtil.Team team, List<BattleObject> excepts = null)
- {
- List<BattleObject> list = new List<BattleObject> ();
- foreach(KeyValuePair<int, BattleObject> item in battleObjectDict)
- {
- int id = item.Key;
- BattleObject obj = item.Value;
- bool add = false;
- if(team != TeamUtil.Team.None)
- {
- if(team.Equals(item.Value.team))
- {
- add = true;
- }
- }
- else
- {
- add = true;
- }
- if(excepts != null && excepts.Contains(obj))
- continue;
- if(add && NumberUtil.distanceVector3(origin, obj.position) <= range)
- {
- list.Add(item.Value);
- }
- }
- return list;
- }
- public List<BattleObject> GetBattleObjectBySector(Vector3 origin, float direction, float range, TeamUtil.Team team, List<BattleObject> excepts = null)
- {
- List<BattleObject> list = new List<BattleObject> ();
- foreach(KeyValuePair<int, BattleObject> item in battleObjectDict)
- {
- int id = item.Key;
- BattleObject obj = item.Value;
-
- bool add = false;
- if(team != TeamUtil.Team.None)
- {
- if(team.Equals(item.Value.team))
- {
- add = true;
- }
- }
- else
- {
- add = true;
- }
-
- if(excepts != null && excepts.Contains(obj))
- continue;
-
- if(add && NumberUtil.distanceVector3(origin, obj.position) <= range)
- {
- float targetAngle = NumberUtil.radianToAngle(NumberUtil.getRadianByATan(obj.position.x, obj.position.z, origin.x, origin.z));
- float originAngle = NumberUtil.radianToAngle(direction);
- float deltaAngle = Mathf.Abs(NumberUtil.corverAngleBetween(targetAngle - originAngle, -180f, 180f));
- if(deltaAngle <= 30f)
- {
- list.Add(item.Value);
- }
- }
- }
- return list;
- }
- public List<BattleObject> GetBattleObjectByFrontRect(Vector3 origin, float direction, float range, TeamUtil.Team team, List<BattleObject> excepts = null)
- {
- List<BattleObject> list = new List<BattleObject> ();
- foreach(KeyValuePair<int, BattleObject> item in battleObjectDict)
- {
- int id = item.Key;
- BattleObject obj = item.Value;
-
- bool add = false;
- if(team != TeamUtil.Team.None)
- {
- if(team.Equals(item.Value.team))
- {
- add = true;
- }
- }
- else
- {
- add = true;
- }
- if(!add)
- continue;
-
- if(excepts != null && excepts.Contains(obj))
- continue;
- float distance = NumberUtil.distanceVector3(origin, obj.position, true);
- if(distance <= range)
- {
- float targetRadian = NumberUtil.getRadianByATan(obj.position.x, obj.position.z, origin.x, origin.z);
- float targetAngle = NumberUtil.radianToAngle(targetRadian);
- float originAngle = NumberUtil.radianToAngle(direction);
- float deltaAngle = Mathf.Abs(NumberUtil.corverAngleBetween(targetAngle - originAngle, -180f, 180f));
- float delatRadian = NumberUtil.angleToRadian(deltaAngle);
- float cos = Mathf.Cos(delatRadian) * distance;
- float sin = Mathf.Sin(delatRadian) * distance;
- Debuger.LogWarning(obj+" angle:"+deltaAngle+" cos:"+cos+" sin:"+sin+" range:"+range);
- if(deltaAngle <= 90f && cos < range && sin < 1.5f)
- {
- list.Add(item.Value);
- }
- }
- }
- return list;
- }
- public Craft GetNearestCraft(Vector3 origin, TeamUtil.Team team)
- {
- Craft craft = null;
- float minDis = float.MaxValue;
- foreach(KeyValuePair<int, BattleObject> item in battleObjectDict)
- {
- if(item.Value is Craft && item.Value.team == team)
- {
- float dis = NumberUtil.distanceVector3(origin, item.Value.position, true);
- if(dis < minDis)
- {
- minDis = dis;
- craft = item.Value as Craft;
- }
- }
- }
- return craft;
- }
- }
|