/* protected IEnumerator WaitForAttack() * { * yield return new WaitForSeconds (.1f); * var validTargets = FindTargets(attackRange); * HighlightMoveArea(validMoves, Color.red); * * while (!moving) { * if (Input.GetMouseButtonDown (0)) * { * var target = Camera.main.ScreenToWorldPoint(Input.mousePosition); * //moving = Move(validMoves,new IntegerLocation (target)); * } * yield return null; * } * * HighlightMoveArea (validMoves, Color.white); * } */ /* --may not be needed anymore * calculates the area of the circle that corresponds to * the unit's movement radius */ /* private int CalcMoveArea() * { * //straight line distance all around plus origin * int area = 4 * moves + 1; * int temp = 0; * //the num of possible locs for each quadrant if you turn is the sum of numbers up to move radius * for (int i = 0; i < moves; i++) { * temp += i; * } * area += 4 * temp; * * return area; * } */ /* Finds path with A* algorithm * Heuristic Function: Manhattan Distance * H(a,b) = |a.x - b.x| + |a.y - b.y| * */ private Dictionary <IntegerLocation, IntegerLocation> FindPath(IntegerLocation start, int limit) { Queue <IntegerLocation> frontier = new Queue <IntegerLocation> (); ArrayList discovered = new ArrayList(); frontier.Enqueue(start); discovered.Add(start); Dictionary <IntegerLocation, IntegerLocation> cameFrom = new Dictionary <IntegerLocation, IntegerLocation> (); //invalid loc used as sentinel value to prevent loop overflow cameFrom[start] = new IntegerLocation(-1, -1); while (frontier.Count > 0) { var current = frontier.Dequeue(); if (Mathf.CeilToInt(Vector2.Distance(start.toVector2(), current.toVector2())) >= limit) { return(cameFrom); } foreach (IntegerLocation next in GetNeighbors(current)) { if (!discovered.Contains(next)) { frontier.Enqueue(next); discovered.Add(next); cameFrom[next] = current; } } } return(cameFrom); }
/* // updated movement of unit over network (not used but possibly useful) * [PunRPC] void updateMovement(Vector3 newPosition) * { * transform.position = newPosition; * } */ protected bool Move(Dictionary <IntegerLocation, IntegerLocation> locations, IntegerLocation end) { IntegerLocation start = new IntegerLocation(transform.position); int dist = IntegerLocation.Distance(start, end); if (dist > moves) { //TODO: tell user target is too far for the unit return(false); } // Dictionary<IntegerLocation,IntegerLocation> result = FindPath (start, end); LinkedList <Vector2> path = new LinkedList <Vector2>(); path.AddLast(end.toVector2()); if (locations.ContainsKey(end)) { var current = locations [end]; while (current != (new IntegerLocation(-1, -1))) { path.AddFirst(current.toVector2()); current = locations [current]; } } else { //couldn't find a path to end return(false); } /* * foreach (Vector3 loc in path) * transform.position = loc; */ StartCoroutine(SmoothMovement(path)); //photonView.RPC("makeSmoothMovement", PhotonTargets.MasterClient, path); return(true); }
/* protected IEnumerator WaitForAttack() { yield return new WaitForSeconds (.1f); var validTargets = FindTargets(attackRange); HighlightMoveArea(validMoves, Color.red); while (!moving) { if (Input.GetMouseButtonDown (0)) { var target = Camera.main.ScreenToWorldPoint(Input.mousePosition); //moving = Move(validMoves,new IntegerLocation (target)); } yield return null; } HighlightMoveArea (validMoves, Color.white); } */ /* --may not be needed anymore * calculates the area of the circle that corresponds to the unit's movement radius */ /* private int CalcMoveArea() { //straight line distance all around plus origin int area = 4 * moves + 1; int temp = 0; //the num of possible locs for each quadrant if you turn is the sum of numbers up to move radius for (int i = 0; i < moves; i++) { temp += i; } area += 4 * temp; return area; } */ /* Finds path with A* algorithm * Heuristic Function: Manhattan Distance * H(a,b) = |a.x - b.x| + |a.y - b.y| * */ private Dictionary<IntegerLocation, IntegerLocation> FindPath (IntegerLocation start, int limit) { Queue<IntegerLocation> frontier = new Queue<IntegerLocation> (); ArrayList discovered = new ArrayList(); frontier.Enqueue (start); discovered.Add(start); Dictionary<IntegerLocation,IntegerLocation> cameFrom = new Dictionary<IntegerLocation, IntegerLocation> (); //invalid loc used as sentinel value to prevent loop overflow cameFrom[start] = new IntegerLocation(-1,-1); while (frontier.Count > 0) { var current = frontier.Dequeue(); if(Mathf.CeilToInt(Vector2.Distance(start.toVector2(),current.toVector2())) >= limit){ return cameFrom; } foreach (IntegerLocation next in GetNeighbors(current)){ if(!discovered.Contains(next)){ frontier.Enqueue(next); discovered.Add(next); cameFrom[next] = current; } } } return cameFrom; }
/* // updated movement of unit over network (not used but possibly useful) [PunRPC] void updateMovement(Vector3 newPosition) { transform.position = newPosition; } */ protected bool Move(Dictionary<IntegerLocation,IntegerLocation> locations, IntegerLocation end) { IntegerLocation start = new IntegerLocation(transform.position); int dist = IntegerLocation.Distance (start, end); if (dist > moves) { //TODO: tell user target is too far for the unit return false; } // Dictionary<IntegerLocation,IntegerLocation> result = FindPath (start, end); LinkedList<Vector2> path = new LinkedList<Vector2>(); path.AddLast (end.toVector2()); if (locations.ContainsKey (end)) { var current = locations [end]; while (current != (new IntegerLocation(-1,-1))) { path.AddFirst (current.toVector2()); current = locations [current]; } } else { //couldn't find a path to end return false; } /* foreach (Vector3 loc in path) transform.position = loc; */ StartCoroutine (SmoothMovement (path)); //photonView.RPC("makeSmoothMovement", PhotonTargets.MasterClient, path); return true; }