123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730 |
- 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 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, 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()
- {
- battleObjectDict = new Dictionary<int, BattleObject>();
- crystalDict = new Dictionary<int, CrystalBase>();
- crystalList = new List<CrystalBase>();
- doorDict = new Dictionary<int, Door>();
- doorList = new List<Door>();
- 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 CreateMap()
- {
- CreateBlock();
- CreateBuilding();
- }
- private void CreateBlock()
- {
- Block[] blockArr = GameObject.FindObjectsOfType<Block> ();
- for(int i=0; i<blockArr.Length; i++)
- {
- this.blockList.Add(blockArr[i]);
- }
- }
- private void CreateBuilding()
- {
- CrystalBase[] crystalBaseArr = GameObject.FindObjectsOfType<CrystalBase> ();
- for(int i=0; i<crystalBaseArr.Length; i++)
- {
- CrystalBase crystalBase = crystalBaseArr[i];
- crystalDict.Add(crystalBase.id, crystalBase);
- crystalList.Add(crystalBase);
- }
- FlagBase[] flagBaseArr = GameObject.FindObjectsOfType<FlagBase> ();
- for(int i=0; i<flagBaseArr.Length; i++)
- {
- FlagBase flagBase = flagBaseArr [i];
- flagBase.Init(this);
- this.flagBaseDict.Add(flagBase.team, flagBase);
- }
- Door[] doorArr = GameObject.FindObjectsOfType<Door> ();
- for(int i=0; i<doorArr.Length; i++)
- {
- Door door = doorArr[i];
- door.Init (this);
- door.typeId = MapObjectUtil.TypeId.Door.GetHashCode();
- this.doorDict.Add(door.id, door);
- this.doorList.Add(door);
- }
- MapItemPos[] mapItemPosArr = GameObject.FindObjectsOfType<MapItemPos> ();
- for(int i=0; i<mapItemPosArr.Length; i++)
- {
- MapItemPos mapItemPos = mapItemPosArr [i];
- mapItemPos.typeId = MapObjectUtil.TypeId.MapItemPos.GetHashCode ();
- mapItemPos.Init(this);
- this.itemPosDict.Add(mapItemPos.id, mapItemPos);
- this.itemPosList.Add(mapItemPos);
- }
- StartPos[] startPosArr = GameObject.FindObjectsOfType<StartPos> ();
- for(int i=0; i<startPosArr.Length; i++)
- {
- StartPos startPos = startPosArr[i];
- startPos.Init(this);
- this.startPosDict.Add(startPos.team, startPos);
- }
- }
- 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(true || 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);
- itemDict.Add(mapItem.id, mapItem);
- itemList.Add(mapItem);
- return mapItem;
- }
- 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 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);
- }
- }
|