예제 #1
0
/*	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);
    }
예제 #2
0
    /* // 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);
    }
예제 #3
0
    /*
     * Get the list of <location>'s non-diagonal neighbors
     *
     * Occupied neighbors will not be added to the list
     */
    protected ArrayList GetNeighbors(IntegerLocation location)
    {
        ArrayList neighbors = new ArrayList();

        for (int x = -1; x <= 1; x++)
        {
            for (int y = -1; y <= 1; y++)
            {
                //Get only non-diagonal neighbors
                if (((x == -1 || x == 1) && y == 0) || ((y == -1 || y == 1) && x == 0))
                {
                    Vector2 target = new Vector2(location.x + x, location.y + y);
                    circleCollider.enabled = false;
                    RaycastHit2D hit = Physics2D.Linecast(new Vector2(location.x, location.y), target, blockingLayer);
                    circleCollider.enabled = true;
                    if (hit.transform == null)
                    {
                        neighbors.Add(new IntegerLocation(target));
                    }
                }
            }
        }
        return(neighbors);
    }
예제 #4
0
 public static int Distance(IntegerLocation a, IntegerLocation b)
 {
     return((int)Math.Round(Math.Sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y))));
 }
예제 #5
0
    public override bool Equals(object obj)
    {
        IntegerLocation other = obj as IntegerLocation;

        return(this == other);
    }
	public static int Distance(IntegerLocation a, IntegerLocation b)
	{
		return (int)Math.Round(Math.Sqrt ((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)));
	}
예제 #7
0
	/*
	 * Get the list of <location>'s non-diagonal neighbors
	 * 
	 * Occupied neighbors will not be added to the list
	*/
	protected ArrayList GetNeighbors(IntegerLocation location)
	{
		ArrayList neighbors = new ArrayList ();
		for (int x = -1; x <= 1; x++) {
			for(int y = -1; y <= 1; y++){
				//Get only non-diagonal neighbors
				if(((x == -1 || x == 1) && y == 0) || ((y == -1 || y == 1) && x == 0)){
					Vector2 target = new Vector2(location.x+x,location.y+y);
					circleCollider.enabled = false;
					RaycastHit2D hit = Physics2D.Linecast(new Vector2(location.x,location.y),target,blockingLayer);
					circleCollider.enabled = true;
					if(hit.transform == null){
						neighbors.Add(new IntegerLocation(target));
					}
				}
			}
		}
		return neighbors;
	}
예제 #8
0
/*	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;
	}
예제 #9
0
	/* // 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;
	}