Ejemplo n.º 1
0
    /// <summary>
    /// Finds the most optimal spot to move to. If it can move a spot where the player
    /// unit is at the top of its shoot radius, then it will. If not it will just move as far as it can
    /// </summary>
    /// <returns>The tile that will be moved to</returns>
    Node MoveCloser()
    {
        TileScript tile = null;

        if (!thisUnit.HasShot())
        {
            // FIND A TILE WITH LINE OF SIGHT TO AN ENEMY
            List <TileScript> tilesUnitCanSee = GridManagerScript.instance.GetTilesUnitCanSee(closestUnit, thisUnit.GetShootRadius() + (thisUnit.GetShootRadius() / 2));

            //if there are tiles the enemy can see (should literally always be true)
            if (tilesUnitCanSee != null)
            {
                // Determine how many of the tiles the enemy can see are within walking range of our movement
                List <TileScript> overlappingTiles = GridManagerScript.instance.GetWalkableTilesInRange(tilesUnitCanSee);

                //foreach (TileScript thisTile in overlappingTiles)
                //{
                //    thisTile.sr.color = Color.blue;
                //}

                if (overlappingTiles != null)
                {
                    tile = GridManagerScript.instance.FindClosestTile(thisUnit.transform.position, overlappingTiles);
                }
            }
        }

        // NORMAL MOVE CLOSER

        if (tile == null)
        {
            //Get the vector between the two units
            Vector3 diff = closestUnitPos - thisUnit.transform.position;
            //Make it a unit vector
            diff.Normalize();
            //Mutiply it by the length of your shoot range to get the vector to the end of your shoot range in the direction you want
            diff *= thisUnit.GetShootRadius();
            //Get the vector from this unit  to the tile that will make the player unit at the tip of your range
            Vector3 rangeToUnit = closestUnitPos - thisUnit.transform.position - diff;
            //RangetoUnit is currently using the unit as 0,0 so we have to make the grid's 0,0 the actual starting point
            rangeToUnit += thisUnit.transform.position;

            //Find the closest tile to that point within your walkable zone
            tile = GridManagerScript.instance.FindClosestTile(rangeToUnit);
        }

        return(tile.GetNode());
    }
Ejemplo n.º 2
0
    /// <summary>
    /// Moves the unit as far as it can away from the closest player unit
    /// </summary>
    /// <returns>The tile that will be moved to</returns>
    Node MoveFarther()
    {
        //Get the vector between the two units
        Vector3 diff = closestUnitPos - thisUnit.transform.position;

        //Negate it to get the farthest direction from the player unit
        diff *= -1f;
        //Normalize and multiply by the amount of movement you have left to go as far as possible
        diff.Normalize();
        diff *= thisUnit.GetMovesLeft();

        diff += thisUnit.transform.position;

        TileScript tile = GridManagerScript.instance.FindClosestTile(diff);

        return(tile.GetNode());
    }