public List <MapOne> GetPath(MapOne startPo, MapOne endPo) { int count = 0; if (mapCtrl == null) { mapCtrl = GameObject.FindGameObjectWithTag("Eniroment").GetComponent <MapCtrl>(); } if (endPo.mPosition == startPo.mPosition || endPo.IsObstacle == true) { return(null); } List <MapOne> openList = new List <MapOne>(); List <MapOne> closeList = new List <MapOne>(); openList.Add(startPo); while (openList.Count > 0) { MapOne minPo = FindPointWithMinF(openList); openList.Remove(minPo); closeList.Add(minPo); List <MapOne> surroundPoints = FindSurroundPoint(minPo); SurroundPointsFilter(surroundPoints, closeList); foreach (var surroundPo in surroundPoints) { if (openList.Contains(surroundPo)) { continue; } else { surroundPo.ParentCude = minPo; CalcF(surroundPo, endPo); openList.Add(surroundPo); OpenListAll.Add(surroundPo); } } bool b = true; foreach (MapOne one in openList) { b = b && OpenListAll.Contains(one); } if (b == true) { count++; } if (count > 10) { OpenListAll.Clear(); return(null); } if (openList.IndexOf(endPo) > -1) { break; } } return(ShowPath(startPo, endPo)); }
private List <MapOne> ShowPath(MapOne start, MapOne end) { mPathPosList.Clear(); MapOne temp = end; if (end.ParentCude == null) { return(null); } while (true) { mPathPosList.Add(temp); if (temp.ParentCude == start) { mPathPosList.Add(temp.ParentCude); break; } temp = temp.ParentCude; if (temp == null) { break; } if (mPathPosList.Count > 144) { mPathPosList.Clear(); break; } } foreach (MapOne one in mPathPosList) { one.ParentCude = null; } return(mPathPosList); }
public void AddPathActive(MapOne one) { if (!mPathActives.Contains(one)) { mPathActives.Add(one); } }
private void LoadMaps() { var wm = new WorldMap(); MapMaps.Add("worldmap", wm); var lvl1 = new MapOne(); MapMaps.Add("mapone", lvl1); var lvl2 = new MapTwo(); MapMaps.Add("maptwo", lvl2); var lvl3 = new MapThree(); MapMaps.Add("mapthree", lvl3); var lvl4 = new MapFour(); MapMaps.Add("mapfour", lvl4); var lvl5 = new MapFive(); MapMaps.Add("mapfive", lvl5); var lvl6 = new MapSix(); MapMaps.Add("mapsix", lvl6); var lvl7 = new MapSeven(); MapMaps.Add("mapseven", lvl7); var lvl8 = new MapEight(); MapMaps.Add("mapeight", lvl8); var lvl9 = new MapNine(); MapMaps.Add("mapnine", lvl9); }
public void AddHelpPath(MapOne mapOne) { if (!HelpPos.Contains(mapOne)) { HelpPos.Add(mapOne); } }
public void AddMapPath(MapOne mapOne) { if (!mmaps.Contains(mapOne)) { mmaps.Add(mapOne); } }
public void AddBtnPath(MapOne mapOne) { if (!mBtnPaths.Contains(mapOne)) { mBtnPaths.Add(mapOne); } }
public void AddMaps(MapOne one) { if (mMaps.Contains(one)) { return; } mMaps.Add(one); }
private void SetBtnPathToMapone(Vector2 Dir, MapOne go, BtnColor btn) { GameObject mgo = go.GameOne.transform.Find("Trail").gameObject; if (Dir.x == 1) { mgo.GetComponent <SpriteRenderer>().sprite = levelSys.GetTraillSprite(btn.name); mgo.transform.localPosition = new Vector3(0.5f, 0f, 0); mgo.transform.localEulerAngles = new Vector3(0, 0, 0); mgo.transform.localScale = new Vector3(4.45f, 0.7f, 1); mgo.SetActive(true); if (btn.PathActives.Count == 0) { btn.AddPathActive(btn.BtnPath[0]); } btn.AddPathActive(go); } else if (Dir.x == -1) { mgo.GetComponent <SpriteRenderer>().sprite = levelSys.GetTraillSprite(btn.name); mgo.transform.localPosition = new Vector3(-0.5f, 0, 0); mgo.transform.localScale = new Vector3(4.45f, 0.7f, 1); mgo.transform.localEulerAngles = new Vector3(0, 0, 0); mgo.SetActive(true); if (btn.PathActives.Count == 0) { btn.AddPathActive(btn.BtnPath[0]); } btn.AddPathActive(go); } else if (Dir.y == 1) { mgo.GetComponent <SpriteRenderer>().sprite = levelSys.GetTraillSprite(btn.name); mgo.transform.localPosition = new Vector3(0, 0.5f, 0); mgo.transform.localScale = new Vector3(4.45f, 0.7f, 1); mgo.transform.localEulerAngles = new Vector3(0, 0, 90); mgo.SetActive(true); if (btn.PathActives.Count == 0) { btn.AddPathActive(btn.BtnPath[0]); } btn.AddPathActive(go); } else if (Dir.y == -1) { mgo.GetComponent <SpriteRenderer>().sprite = levelSys.GetTraillSprite(btn.name); mgo.transform.localPosition = new Vector3(0, -0.5f, 0); mgo.transform.localScale = new Vector3(4.45f, 0.7f, 1); mgo.transform.localEulerAngles = new Vector3(0, 0, 90); mgo.SetActive(true); if (btn.PathActives.Count == 0) { btn.AddPathActive(btn.BtnPath[0]); } btn.AddPathActive(go); } }
public BtnColor GetBtnForPath(MapOne mapOne, BtnColor Btn) { foreach (BtnColor btn in BtnColors) { if (btn.BtnPath.Contains(mapOne) && Btn != btn && Btn.mTargetBtnColor != btn) { return(btn); } } return(null); }
public BtnColor GetBtnForpos(MapOne one) { foreach (BtnColor btn in BtnColors) { if (btn.mPosition == one.mPosition) { return(btn); } } return(null); }
public MapManager() { myMapOne = new MapOne(); myMapTwo = new MapTwo(); Pause = new Pause(); Powerup = new PowerupManager(); Player = new Player(); Enemy = new EnemyManager(); UI = new UserInterface(); UI.Init(); }
//寻找 Openlist中 F最小的值 public MapOne FindPointWithMinF(List <MapOne> OpenList) { float f = float.MaxValue; MapOne temp = null; foreach (var p in OpenList) { if (p.F < f) { temp = p; f = p.F; } } return(temp); }
//初始化按钮信息 public void InitLoadBtn() { if (BtnColors == null || BtnColors.Count == 0) { return; } foreach (BtnColor btn in BtnColors) { if (btn == null) { return; } btn.mColorGame.transform.parent.parent.GetComponent <SpriteRenderer>().DOFade(1, 0.01f); MapOne one = GetMapOne((int)btn.mPosition.x, (int)btn.mPosition.y); one.transform.Find("BgMapOne").gameObject.SetActive(false); Destroy(btn.mColorGame.transform.parent.gameObject); } }
//计算该点到终点的F值 private void CalcF(MapOne now, MapOne end) { float h = Mathf.Abs(end.XPosition - now.XPosition) + Mathf.Abs(end.YPosition - now.YPosition); float g = 0; if (now.ParentCude == null) { g = 0; } else { g = Vector2.Distance(new Vector2(now.XPosition, now.YPosition), new Vector2(now.ParentCude.XPosition, now.ParentCude.YPosition)) + now.ParentCude.G; } float f = g + h; now.F = f; now.G = g; now.H = h; }
//寻找当前点的周围全部点 public List <MapOne> FindSurroundPoint(MapOne CurPo) { List <MapOne> list = new List <MapOne>(); MapOne up = null, down = null, left = null, right = null; if (CurPo.YPosition < LevelCreateSys.Col - 1) { up = mapCtrl.GetMapOne((int)CurPo.XPosition, (int)CurPo.YPosition + 1); } if (CurPo.YPosition > 0) { down = mapCtrl.GetMapOne((int)CurPo.XPosition, (int)CurPo.YPosition - 1); } if (CurPo.XPosition < LevelCreateSys.Row - 1) { right = mapCtrl.GetMapOne((int)CurPo.XPosition + 1, (int)CurPo.YPosition); } if (CurPo.XPosition > 0) { left = mapCtrl.GetMapOne((int)CurPo.XPosition - 1, (int)CurPo.YPosition); } if (down != null && down.IsObstacle == false) { list.Add(down); } if (up != null && up.IsObstacle == false) { list.Add(up); } if (left != null && left.IsObstacle == false) { list.Add(left); } if (right != null && right.IsObstacle == false) { list.Add(right); } return(list); }
//创建地图资源 public void CreateMap() { mapCtrl.Maps.Clear(); GameObject One = Resources.Load <GameObject>(@"Prefabs\MapOne\MapOne"); for (int i = 0; i < Row; i++) { GameObject Map = new GameObject("Map" + i.ToString()); Map.transform.SetParent(transform); Map.transform.localPosition = new Vector3(0, 0, 0); for (int j = 0; j < Col; j++) { GameObject go = Instantiate(One); go.transform.position = new Vector3(j, 0, 0); go.transform.name = i.ToString() + "_" + j.ToString(); go.transform.SetParent(Map.transform); go.transform.localPosition = new Vector3(i, j, 0); MapOne mapOne = go.GetComponent <MapOne>(); mapCtrl.AddMaps(mapOne); } } }
public void SetTipPaht() { if (GoldSystem.Instance.GetGoldMount(0) < 5) { return; } GoldSystem.Instance.GetGoldMount(-5); View.Instance.GameUI.SetGoldCount(); View.Instance.PlayUI.SetGoldCount(); int rodom = UnityEngine.Random.Range(0, ks.Count); if (ks.Count == 0) { return; } BtnColor btn = BtnColors[ks[rodom]]; ks.Remove(ks[rodom]); MapOne mapOne2 = btn.HelpPos[1]; btn.HelpPos.RemoveAt(1); btn.AddHelpPath(mapOne2); if (btn.IsFinished) { bool b = true; List <MapOne> isTrue = new List <MapOne>(); isTrue.Add(GetMapOne((int)btn.mPosition.x, (int)btn.mPosition.y)); for (int i = 0; i < btn.HelpPos.Count - 1; i++) { Vector2 Path = btn.HelpPos[i + 1].mPosition - btn.HelpPos[i].mPosition; MapOne one = new MapOne(); one.XPosition = -1; one.YPosition = -1; if (Path.x != 0) { int d = (int)btn.HelpPos[i].XPosition; while (one.XPosition != btn.HelpPos[i + 1].XPosition) { d += (int)(Path.x / Mathf.Abs(Path.x)); one = GetMapOne(d, (int)btn.HelpPos[i].YPosition); isTrue.Add(one); } } else { int c = (int)btn.HelpPos[i].YPosition; while (one.YPosition != btn.HelpPos[i + 1].YPosition) { c += (int)(Path.y / Mathf.Abs(Path.y)); one = GetMapOne((int)btn.HelpPos[i].XPosition, c); isTrue.Add(one); } } } if (isTrue.Count == btn.BtnPath.Count || isTrue.Count == btn.mTargetBtnColor.BtnPath.Count) { if (isTrue.Count == btn.BtnPath.Count) { for (int i = 0; i < isTrue.Count; i++) { b = b && btn.BtnPath.Contains(isTrue[i]); } } else { for (int i = 0; i < isTrue.Count; i++) { b = b && btn.mTargetBtnColor.BtnPath.Contains(isTrue[i]); } } } else { b = false; } if (b) { isTrue.Clear(); SetTipPaht(); return; } b = true; } btn.PathActives.Clear(); SetAllNOActives(btn); if (btn.BtnPath.Count != 1) { btn.BtnPath.RemoveRange(1, btn.BtnPath.Count - 1); } if (btn.mTargetBtnColor.BtnPath.Count != 1) { btn.mTargetBtnColor.BtnPath.RemoveRange(1, btn.mTargetBtnColor.BtnPath.Count - 1); } for (int i = 0; i < btn.HelpPos.Count - 1; i++) { Vector2 Path = btn.HelpPos[i + 1].mPosition - btn.HelpPos[i].mPosition; float XPosition = -1; float YPosition = -1; if (Path.x != 0) { int d = (int)btn.HelpPos[i].XPosition; while (XPosition != btn.HelpPos[i + 1].XPosition) { d += (int)(Path.x / Mathf.Abs(Path.x)); XPosition = GetMapOne(d, (int)btn.HelpPos[i].YPosition).XPosition; btn.AddBtnPath(GetMapOne(d, (int)btn.HelpPos[i].YPosition)); } } else { int c = (int)btn.HelpPos[i].YPosition; while (YPosition != btn.HelpPos[i + 1].YPosition) { c += (int)(Path.y / Mathf.Abs(Path.y)); YPosition = GetMapOne((int)btn.HelpPos[i].XPosition, c).YPosition; btn.AddBtnPath(GetMapOne((int)btn.HelpPos[i].XPosition, c)); } } } btn.GetComponent <BoxCollider2D>().enabled = false; btn.mTargetBtnColor.GetComponent <BoxCollider2D>().enabled = false; GetHasPathToOther(btn); btn.IsFinished = true; btn.transform.Find("HelpUI").gameObject.SetActive(true); btn.mTargetBtnColor.IsFinished = true; btn.mTargetBtnColor.transform.Find("HelpUI").gameObject.SetActive(true); btn.maps = btn.BtnPath; foreach (MapOne one in btn.BtnPath) { one.GameOne.transform.Find("TrailDrag").GetComponent <BoxCollider2D>().enabled = false; one.IsObstacle = true; if (btn.BtnPath[btn.BtnPath.Count - 1] != one) { btn.AddPathActive(one); } } source.clip = AutoLine; source.Play(); View.Instance.GameUI.SetFootCountTxt(); }
private void Start() { mapCtrl = GameObject.Find("Eniroment").GetComponent <MapCtrl>(); Own = transform.parent.GetComponent <MapOne>(); }
private void Update() { int x = (int)Math.Round(this.transform.position.x); int y = (int)Math.Round(this.transform.position.y); int x1 = (int)Math.Round(this.transform.position.x) - (int)transform.parent.parent.position.x; int y1 = (int)Math.Round(this.transform.position.y) - (int)transform.parent.parent.position.y; if (Mathf.Abs(x1) >= 0 || Mathf.Abs(y1) >= 0) { MapOne TargetOne = mapCtrl.GetMapOne(x, y); BtnColor currentBtn = this.transform.parent.GetChild(0).GetComponent <BtnColor>(); if (x >= LevelCreateSys.Col || y >= LevelCreateSys.Row || x < 0 || y < 0) { return; } if (TargetOne.IsObstacle == false || currentBtn.mTargetBtnColor.mPosition == new Vector2(x, y) || TargetOne.mPosition == currentBtn.mPosition) { if (TargetOne == currentBtn.BtnPath[currentBtn.BtnPath.Count - 1]) { return; } else { if (!currentBtn.IsFinished && !(kk == x1 && xx == y1)) { mapCtrl.source.clip = mapCtrl.mButton; mapCtrl.source.Play(); kk = x1; xx = y1; } } BtnColor TragetBtn = mapCtrl.GetBtnForPath(TargetOne, currentBtn); if (TragetBtn != null) { TragetBtn.IsFinished = false; TragetBtn.mTargetBtnColor.IsFinished = false; int remove = TragetBtn.BtnPath.IndexOf(TargetOne); TragetBtn.BtnPath.RemoveRange(remove, TragetBtn.BtnPath.Count - remove); } else { mapCtrl.GetHasPathToOther(currentBtn); } //判断是否可以继续运动,如果按钮的状态没有完成则无法运动 if (currentBtn.IsFinished) { if (!currentBtn.BtnPath.Contains(TargetOne)) { return; } else { currentBtn.IsFinished = false; } currentBtn.mTargetBtnColor.IsFinished = false; } //判断 当前路线是否重合,重合整理路线 if (currentBtn.BtnPath.Contains(TargetOne)) { if (currentBtn.BtnPath.Count != 1) { int remove = currentBtn.BtnPath.IndexOf(TargetOne); currentBtn.BtnPath.RemoveRange(remove + 1, currentBtn.BtnPath.Count - remove - 1); } } else { currentBtn.AddBtnPath(TargetOne); int Count = currentBtn.BtnPath.Count; float xoff = Math.Abs(currentBtn.BtnPath[Count - 1].XPosition - currentBtn.BtnPath[Count - 2].XPosition); float yoff = Math.Abs(currentBtn.BtnPath[Count - 1].YPosition - currentBtn.BtnPath[Count - 2].YPosition); //判断当前运动的位置是否发生大规模运动 if (xoff + yoff != 1) { //ToDo 高速度的响应解决方案 ---> currentBtn.BtnPath[currentBtn.BtnPath.Count - 2].GameOne.transform.GetChild(2).gameObject.SetActive(false); List <MapOne> a1 = new List <MapOne>(); if (currentBtn.mTargetBtnColor.mPosition == TargetOne.mPosition) { TargetOne.IsObstacle = false; mapCtrl.GetMapOne((int)currentBtn.mPosition.x, (int)currentBtn.mPosition.y).IsObstacle = false; a1 = AStart.GetInstance.GetPath (mapCtrl.GetMapOne((int)currentBtn.BtnPath[Count - 2].XPosition, (int)currentBtn.BtnPath[Count - 2].YPosition), TargetOne); TargetOne.IsObstacle = true; } else { mapCtrl.GetMapOne((int)currentBtn.mPosition.x, (int)currentBtn.mPosition.y).IsObstacle = false; a1 = AStart.GetInstance.GetPath (mapCtrl.GetMapOne((int)currentBtn.BtnPath[Count - 2].XPosition, (int)currentBtn.BtnPath[Count - 2].YPosition), TargetOne); } mapCtrl.GetMapOne((int)currentBtn.mPosition.x, (int)currentBtn.mPosition.y).IsObstacle = true; if (a1 == null || a1.Count == 0) { currentBtn.BtnPath.Remove(TargetOne); return; } else { if (isHas(a1, currentBtn)) { int index = IndexMin(a1, currentBtn); currentBtn.BtnPath.RemoveRange(index + 1, currentBtn.BtnPath.Count - index - 1); foreach (MapOne cc in a1) { int cx = a1.IndexOf(cc); int insert = a1.IndexOf(currentBtn.BtnPath[index]); if (cx == 0 || cx == a1.Count - 1) { continue; } if (a1.IndexOf(cc) <= insert) { int Inde = insert - cx; currentBtn.BtnPath.Add(a1[Inde]); } } } else { foreach (MapOne c in a1) { if (c == a1[a1.Count - 1] || c == a1[0]) { continue; } if (c == a1[a1.Count - 1] && c == a1[0]) { continue; } int index = a1.Count - a1.IndexOf(c) - 1; currentBtn.BtnPath.Insert(currentBtn.BtnPath.Count - 1, a1[index]); } } } mapCtrl.GetHasPathToOther(currentBtn); } } if (currentBtn.mTargetBtnColor.mPosition == new Vector2(x, y)) { View.Instance.GameUI.SetFootCountTxt(); currentBtn.IsFinished = true; currentBtn.mTargetBtnColor.IsFinished = true; if (currentBtn.mColorGame.transform.Find("HelpUI").gameObject.activeSelf == false) { mapCtrl.source.clip = mapCtrl.Winline; mapCtrl.source.Play(); } } if (currentBtn.BtnPath.Count >= 2) { MapOne one; for (int i = 0; i < currentBtn.BtnPath.Count - 1; i++) { Vector2 ks = currentBtn.BtnPath[i + 1].mPosition - currentBtn.BtnPath[i].mPosition; if (ks.x + ks.y > 1) { one = currentBtn.BtnPath[i + 1]; currentBtn.BtnPath.RemoveRange(i + 1, currentBtn.BtnPath.Count - i - 1); } } } } } }
//计算最小预算值点G值 private float CalcG(MapOne surround, MapOne minPo) { return(Vector2.Distance(surround.mPosition, minPo.mPosition) + minPo.G); }