Ejemplo n.º 1
0
    public List <Vector2Int> GetAreaOfEffect(Unit source, Direction direction, int pushbackDist)
    {
        List <Vector2Int> result = new List <Vector2Int>();
        Vector2Int        origin = source.GetMapPosition();

        origin += MapMath.DirToRelativeLoc(direction);

        if (!MapMath.InMapBounds(origin))
        {
            return(result);
        }

        /*
         * Debug.Log( "Unit at (" + source.GetMapPosition().x + ", " + source.GetMapPosition().y + ") "
         + source.GetDirection() + " is Cleaving at Origin Point (" + origin.x + ", " + origin.y + ")" );
         */
        //one tile forward
        result.Add(origin);

        for (int i = 0; i < pushbackDist - 1; i++)
        {
            origin += MapMath.DirToRelativeLoc(direction);
            result.Add(origin);
        }
        return(result);
    }
Ejemplo n.º 2
0
    /// <summary>
    /// Knocks target unit in a line with a given direction.
    /// </summary>
    /// <param name="target"></param>
    /// <param name="knockback"></param>
    /// <param name="kbDirection"></param>
    public static void DisplaceUnit(Unit target, Unit source, Attack ability, int knockback, Direction kbDirection)
    {
        //look for collisions
        Vector2Int        pushbackLoc  = target.GetMapPosition() + (MapMath.DirToRelativeLoc(kbDirection) * knockback);
        List <Vector2Int> pushbackArea = GetLineAOE(target.GetMapPosition(), kbDirection, knockback);
        Vector2Int?       searchResult = null;

        foreach (Vector2Int tile in pushbackArea)
        {
            if (MapController.instance.weightedMap[tile] == (int)TileWeight.OBSTRUCTED)
            {
                searchResult = tile;
                break;
            }
        }
        if (searchResult != null)
        {
            //deal damage when we cant go full knockback dist
            target.ChangeHealth(-1, source, ability);
            Vector2Int diff    = (Vector2Int)searchResult - target.GetMapPosition();
            Vector2Int absDiff = new Vector2Int(Mathf.Abs(diff.x), Mathf.Abs(diff.y));
            Debug.Assert(source.GetDirection() != Direction.NO_DIR);
            //Debug.Log(pushbackLoc);
            switch (source.GetDirection())
            {
            case Direction.N:
                pushbackLoc = new Vector2Int(target.GetMapPosition().x, target.GetMapPosition().y + (absDiff.y - 1));
                break;

            case Direction.S:
                pushbackLoc = new Vector2Int(target.GetMapPosition().x, target.GetMapPosition().y - (absDiff.y - 1));
                break;

            case Direction.W:
                pushbackLoc = new Vector2Int(target.GetMapPosition().x - (absDiff.x - 1), target.GetMapPosition().y);
                break;

            case Direction.E:
                pushbackLoc = new Vector2Int(target.GetMapPosition().x + (absDiff.x - 1), target.GetMapPosition().y);
                break;
            }
            //Debug.Log(newLocation);
        }
        if (MapMath.InMapBounds(pushbackLoc))
        {
            // ******************************** target.Move(pushbackLoc.x, pushbackLoc.y, MovementType.KNOCKBACK);
            target.hasMoved = false;
        }
        //else if()
        else
        {
            DeathData data = new DeathData(source, ability, ability.GetDamage(), target.GetMapPosition());
            target.KillMe(data);
        }
    }
Ejemplo n.º 3
0
    private void InitMap()
    {
        mostWest  = 0;
        mostEast  = 0;
        mostNorth = 0;
        mostSouth = 0;
        HashSet <Vector2Int> visited  = new HashSet <Vector2Int>();
        Queue <Vector2Int>   frontier = new Queue <Vector2Int>();
        Vector2Int           origin   = new Vector2Int(0, 0);

        frontier.Enqueue(origin); // Should only contain tiles in range
        weightedMap.Add(origin, (int)(walkableTiles.GetTile(new Vector3Int(origin.x, origin.y, 0)) as WeightedTile).weight);

        while (frontier.Count != 0)
        {
            Vector2Int visiting = frontier.Dequeue();
            if (visited.Contains(visiting))
            {
                continue;
            }                                             // TODO: Implement changing priority in the PQ, and remove this.
            mostEast  = Mathf.Max(mostEast, visiting.y);
            mostWest  = Mathf.Min(mostWest, visiting.y);
            mostNorth = Mathf.Max(mostNorth, visiting.x);
            mostSouth = Mathf.Min(mostSouth, visiting.x);

            Dictionary <Vector2Int, Direction> neighbors = MapMath.GetNeighbors(visiting);
            foreach (Vector2Int neighbor in neighbors.Keys)
            {
                //check if there are tiles in that location, check if its not in weightedMap dict
                if (walkableTiles.GetTile(new Vector3Int(neighbor.x, neighbor.y, 0)))
                {
                    if (!visited.Contains(neighbor) && !MapMath.InMapBounds(MapMath.GridToMap(neighbor)))
                    {
                        frontier.Enqueue(neighbor);
                        if (walkableTiles.GetTile(new Vector3Int(neighbor.x, neighbor.y, 0)) is WeightedTile)
                        {
                            //Debug.Log(neighbor);
                            weightedMap.Add(MapMath.GridToMap(neighbor), (int)(walkableTiles.GetTile(new Vector3Int(neighbor.x, neighbor.y, 0)) as WeightedTile).weight);
                        }
                    }
                }
            }

            visited.Add(visiting);
        }

        /*
         * Debug.Log("most east: " + mostEast);
         * Debug.Log("most west: " + mostWest);
         * Debug.Log("most south: " + mostSouth);
         * Debug.Log("most north: " + mostNorth);
         */
    }
Ejemplo n.º 4
0
    /*
     * public static Dictionary<Vector2Int, Direction> FindMoveableTilesStraight(int[,] map, Vector2Int mapPosition, int moveSpeed)
     * {
     *  Dictionary<Vector2Int, Direction> shortestFrom = new Dictionary<Vector2Int, Direction>();
     *  Dictionary<Vector2Int, int> movementCost = new Dictionary<Vector2Int, int>();
     *
     *  Stack<Vector2Int> frontier = new Stack<Vector2Int>();
     *  frontier.Push(mapPosition); // Should only contain tiles in range
     *  movementCost[mapPosition] = 0; // Contains frontier and visited
     *  shortestFrom[mapPosition] = Direction.NO_DIR;
     *
     *  while (frontier.Count != 0)
     *  {
     *      Vector2Int visiting = frontier.Pop();
     *
     *      Dictionary<Vector2Int, Direction> neighbors = GetNeighbors(visiting);
     *      foreach (Vector2Int neighbor in neighbors.Keys)
     *      {
     *          if (shortestFrom[visiting] != Direction.NO_DIR && neighbors[neighbor] != shortestFrom[visiting]) { continue; }
     *          if (!MapMath.InMapBounds(neighbor)) { continue; }
     *          int nextDist = MapController.instance.map[neighbor.x, neighbor.y] + movementCost[visiting];
     *          if (nextDist > moveSpeed) { continue; }
     *          frontier.Push(neighbor);
     *          movementCost[neighbor] = nextDist;
     *          shortestFrom[neighbor] = neighbors[neighbor];
     *      }
     *  }
     *  return shortestFrom;
     *
     *
     * }*/

    public static Dictionary <Vector2Int, Direction> FindMoveableTiles(Dictionary <Vector2Int, int> map, Vector2Int mapPosition, int moveSpeed)
    {
        Dictionary <Vector2Int, Direction> shortestFrom = new Dictionary <Vector2Int, Direction>();
        Dictionary <Vector2Int, int>       movementCost = new Dictionary <Vector2Int, int>();

        HashSet <Vector2Int>       visited  = new HashSet <Vector2Int>();
        PriorityQueue <Vector2Int> frontier = new PriorityQueue <Vector2Int>();

        frontier.Enqueue(mapPosition, 0); // Should only contain tiles in range
        movementCost[mapPosition] = 0;    // Contains frontier and visited
        shortestFrom[mapPosition] = Direction.NO_DIR;

        while (frontier.Count != 0)
        {
            Vector2Int visiting = frontier.Dequeue();
            if (visited.Contains(visiting))
            {
                continue;
            }                                           // TODO: Implement changing priority in the PQ, and remove this.

            Dictionary <Vector2Int, Direction> neighbors = MapMath.GetNeighbors(visiting);
            foreach (Vector2Int neighbor in neighbors.Keys)
            {
                if (visited.Contains(neighbor) || !MapMath.InMapBounds(neighbor))
                {
                    continue;
                }
                int nextDist = MapController.instance.weightedMap[neighbor] + movementCost[visiting];
                if (nextDist > moveSpeed)
                {
                    continue;
                }
                if (!movementCost.ContainsKey(neighbor) || nextDist < movementCost[neighbor])
                {
                    frontier.Enqueue(neighbor, nextDist);
                    movementCost[neighbor] = nextDist;
                    shortestFrom[neighbor] = neighbors[neighbor];
                }
            }

            visited.Add(visiting);
        }

        return(shortestFrom);
    }
Ejemplo n.º 5
0
    //variation of the findmovetiles alg to save time in devising an algorithm.
    public static List <Vector2Int> GetCircleAOE(Vector2Int source, Direction direction, int abilityRadius)
    {
        List <Vector2Int>            circleList   = new List <Vector2Int>();
        Dictionary <Vector2Int, int> movementCost = new Dictionary <Vector2Int, int>();

        HashSet <Vector2Int>       visited  = new HashSet <Vector2Int>();
        PriorityQueue <Vector2Int> frontier = new PriorityQueue <Vector2Int>();

        frontier.Enqueue(source, 0); // Should only contain tiles in range
        movementCost[source] = 0;    // Contains frontier and visited
        circleList.Add(source);

        while (frontier.Count != 0)
        {
            Vector2Int visiting = frontier.Dequeue();
            if (visited.Contains(visiting))
            {
                continue;
            }

            Dictionary <Vector2Int, Direction> neighbors = MapMath.GetNeighbors(visiting);
            foreach (Vector2Int neighbor in neighbors.Keys)
            {
                if (visited.Contains(neighbor) || !MapMath.InMapBounds(neighbor))
                {
                    continue;
                }
                int nextDist = 1 + movementCost[visiting];
                if (nextDist > abilityRadius)
                {
                    continue;
                }
                if (!movementCost.ContainsKey(neighbor) || nextDist < movementCost[neighbor])
                {
                    frontier.Enqueue(neighbor, nextDist);
                    movementCost[neighbor] = nextDist;
                    circleList.Add(neighbor);
                }
            }

            visited.Add(visiting);
        }

        return(circleList);
    }
Ejemplo n.º 6
0
    public static List <Vector2Int> GetTShapedAOE(Vector2Int source, Direction direction, int abilityRange)
    {
        List <Vector2Int> result = new List <Vector2Int>();
        Vector2Int        origin = source;

        for (int i = 0; i < abilityRange; i++)
        {
            origin += MapMath.DirToRelativeLoc(direction);
        }

        if (!MapMath.InMapBounds(origin))
        {
            return(result);
        }

        result.Add(origin);

        if (direction == Direction.N || direction == Direction.S)
        {
            if (MapMath.InMapBounds(new Vector2Int(origin.x + 1, origin.y)))
            {
                result.Add(new Vector2Int(origin.x + 1, origin.y));
            }
            if (MapMath.InMapBounds(new Vector2Int(origin.x - 1, origin.y)))
            {
                result.Add(new Vector2Int(origin.x - 1, origin.y));
            }
        }
        else if (direction == Direction.E || direction == Direction.W)
        {
            if (MapMath.InMapBounds(new Vector2Int(origin.x, origin.y + 1)))
            {
                result.Add(new Vector2Int(origin.x, origin.y + 1));
            }
            if (MapMath.InMapBounds(new Vector2Int(origin.x, origin.y - 1)))
            {
                result.Add(new Vector2Int(origin.x, origin.y - 1));
            }
        }
        return(result);
    }
Ejemplo n.º 7
0
    public override List <Vector2Int> GetAreaOfEffect(Vector2Int source, Direction direction)
    {
        //get tile in front
        List <Vector2Int> result = new List <Vector2Int>();
        Vector2Int        origin = source;

        origin += MapMath.DirToRelativeLoc(direction);

        if (!MapMath.InMapBounds(origin))
        {
            return(result);
        }

        /*
         * Debug.Log( "Unit at (" + source.GetMapPosition().x + ", " + source.GetMapPosition().y + ") "
         + source.GetDirection() + " is Cleaving at Origin Point (" + origin.x + ", " + origin.y + ")" );
         */

        result.Add(origin);
        return(result);
    }
Ejemplo n.º 8
0
    public static List <Vector2Int> GetLineAOE(Vector2Int source, Direction direction, int abilityRange)
    {
        List <Vector2Int> result = new List <Vector2Int>();
        Vector2Int        origin = source;

        origin += MapMath.DirToRelativeLoc(direction);

        if (!MapMath.InMapBounds(origin))
        {
            return(result);
        }

        //one tile forward
        result.Add(origin);

        for (int i = 0; i < abilityRange - 1; i++)
        {
            origin += MapMath.DirToRelativeLoc(direction);
            result.Add(origin);
        }
        return(result);
    }