123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065 |
- 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 Dictionary<int, UAVehicle> uavDict;
- 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>();
- uavDict = new Dictionary<int, UAVehicle> ();
- 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;
- }
- public UAVehicle GetUAVehicle(int userId)
- {
- UAVehicle uav = null;
- uavDict.TryGetValue (userId, out uav);
- return uav;
- }
- public void AddUAVehicle(UAVehicle uav)
- {
- if (!uavDict.ContainsKey (uav.userId))
- uavDict.Add (uav.userId, uav);
- else
- Debuger.LogError ("Already exist uav "+uav.userId);
- }
- public void RemoveUAVehicle(UAVehicle uav)
- {
- uavDict.Remove (uav.userId);
- }
- }
|