Esempio n. 1
0
    private void CheckForClients()
    {
        TileCoordinates currentTile = GridCoords.FromWorldToTilePosition(transform.position);
        GridTile        tile        = GridCoords.CurrentGridInfo.gameGridTiles[currentTile.tileX, currentTile.tileY];
        GridTile_Planet planet      = tile.GetComponent <GridTile_Planet>();

        if (planet != null)
        {
            CheckForClients(planet);
        }
    }
Esempio n. 2
0
    private GridTile_Planet GetClosestStartPlanet(List <GridTile_Planet> completeList)
    {
        if (ShipManager.instance == null)
        {
            return(null);
        }

        List <Ship> allShips    = ShipManager.instance.AllShips;
        int         count       = completeList.Count;
        int         targetIndex = int.MaxValue;
        int         minRating   = int.MaxValue;

        foreach (var ship in allShips)
        {
            TileCoordinates shipPos = GridCoords.FromWorldToTilePosition(ship.transform.position);

            for (int i = 0; i < count; i++)
            {
                TileCoordinates planetPos = completeList[i].TileCoordinates;
                if (shipPos != planetPos)
                {
                    int distanceRating = PathFinder.instance.GetDistanceRating(shipPos.tileX, shipPos.tileY, planetPos.tileX, planetPos.tileY, true);
                    if (distanceRating < minRating)
                    {
                        minRating   = distanceRating;
                        targetIndex = i;
                    }
                    //Debug.Log("Distance from " + ship.shipName + " to " + completeList[i].PlanetName + " : " + distanceRating);
                }
            }
        }

        if (targetIndex < count)
        {
            return(completeList[targetIndex]);
        }

        return(null);
    }
Esempio n. 3
0
    private IEnumerator MoveToDestination(TileCoordinates destCoords, string destName)
    {
        if (PathFinder.instance == null)
        {
            yield break;
        }

        TileCoordinates currentTileCoords = GridCoords.FromWorldToTilePosition(transform.position);
        float           nodeCost          = 0f;
        List <PathNode> pathNodes         = PathFinder.instance.FindLinearPath(currentTileCoords.tileX, currentTileCoords.tileY, destCoords.tileX, destCoords.tileY, out nodeCost);

        if (pathNodes == null)
        {
            CancelRoute("Invalid path to destination - " + destName + ".");
            yield break;
        }

        List <Vector2> pathPositions = new List <Vector2>();

        foreach (var node in pathNodes)
        {
            pathPositions.Add(node.tile.TileCenter);
        }

        PathNode previousNode = null;

        for (int i = 0; i < pathNodes.Count; i++)
        {
            // IF HIT A ROCK
            if (!pathNodes[i].isTraversable)
            {
                CancelRoute("Obstacle in the way.");
                break;
            }

            //if (previousNode != null)
            //    if (!PathFinder.instance.GetValidNeighbourList(previousNode, false).Contains(pathNodes[i]))
            //    {
            //        CancelRoute("DIAGONAL OBSTACLE IN THE WAY!");
            //        break;
            //    }

            previousNode = pathNodes[i];

            if (i == 0)
            {
                continue;
            }

            TileCoordinates currentShipCoords = GridCoords.FromWorldToTilePosition(transform.position);
            MoveDirection   moveDirection     = default;

            if (pathNodes[i].x < currentShipCoords.tileX)
            {
                moveDirection = MoveDirection.Left;
            }
            else if (pathNodes[i].x > currentShipCoords.tileX)
            {
                moveDirection = MoveDirection.Right;
            }
            else if (pathNodes[i].y > currentShipCoords.tileY)
            {
                moveDirection = MoveDirection.Down;
            }
            else if (pathNodes[i].y < currentShipCoords.tileY)
            {
                moveDirection = MoveDirection.Up;
            }
            else
            {
                moveDirection = MoveDirection.None;
            }

            if (shipDirectionChange != null)
            {
                shipDirectionChange(this, moveDirection);
            }

            yield return(new WaitForSeconds(tileTravelSpeed * nodeCost));

            transform.position = pathPositions[i];
        }
        _currentMove = null;
        yield return(null);
    }
Esempio n. 4
0
    private List <GridTile_Planet> GetStartPlanetByDistance(List <GridTile_Planet> completeList)
    {
        if (ShipManager.instance == null)
        {
            return(null);
        }

        List <int>  planetDistances = new List <int>();
        List <Ship> allShips        = ShipManager.instance.AllShips;
        int         count           = 0;

        foreach (var planet in completeList)
        {
            TileCoordinates planetPos   = planet.TileCoordinates;
            int             minDistance = int.MaxValue;

            foreach (var ship in allShips)
            {
                TileCoordinates shipPos = GridCoords.FromWorldToTilePosition(ship.transform.position);
                if (shipPos != planetPos)
                {
                    int distanceRating = PathFinder.instance.GetDistanceRating(shipPos.tileX, shipPos.tileY, planetPos.tileX, planetPos.tileY, true);
                    if (distanceRating < minDistance)
                    {
                        minDistance = distanceRating;
                    }
                }
            }
            planet.CurrentDistanceRating = minDistance;
            planetDistances.Add(minDistance);
            count++;
        }
        planetDistances.Sort();

        int targetDistanceRating = _clientRules[_currentRuleIndex].startDistanceRating;

        List <GridTile_Planet> shortDistancePlanets  = new List <GridTile_Planet>();
        List <GridTile_Planet> mediumDistancePlanets = new List <GridTile_Planet>();
        List <GridTile_Planet> longDistancePlanets   = new List <GridTile_Planet>();
        List <GridTile_Planet> candidates            = new List <GridTile_Planet>();
        int shortCount  = 0;
        int mediumCount = 0;
        int longCount   = 0;

        int third     = planetDistances[(count / 3) - 1];
        int twoThirds = planetDistances[(count / 3 * 2) - 1];

        foreach (var planet in completeList)
        {
            if (planet.CurrentDistanceRating <= third)
            {
                shortDistancePlanets.Add(planet);
                shortCount++;
            }
            else if (planet.CurrentDistanceRating > twoThirds)
            {
                longDistancePlanets.Add(planet);
                longCount++;
            }
            else
            {
                mediumDistancePlanets.Add(planet);
                mediumCount++;
            }
        }

        if (targetDistanceRating == 1)
        {
            if (shortCount > 0)
            {
                candidates = shortDistancePlanets;
            }

            else if (mediumCount > 0)
            {
                candidates = mediumDistancePlanets;
            }

            else if (longCount > 0)
            {
                candidates = longDistancePlanets;
            }

            else
            {
                candidates = new List <GridTile_Planet>(completeList);
            }
        }

        else if (targetDistanceRating == 2)
        {
            if (mediumCount > 0)
            {
                candidates = mediumDistancePlanets;
            }
            else
            {
                List <GridTile_Planet> shortLong = new List <GridTile_Planet>();
                int shortLongCount = 0;
                foreach (var planet in shortDistancePlanets)
                {
                    shortLong.Add(planet);
                    shortLongCount++;
                }
                foreach (var planet in longDistancePlanets)
                {
                    shortLong.Add(planet);
                    shortLongCount++;
                }

                if (shortLongCount > 0)
                {
                    candidates = shortLong;
                }
                else
                {
                    candidates = new List <GridTile_Planet>(completeList);
                }
            }
        }
        else if (targetDistanceRating == 3)
        {
            if (longCount > 0)
            {
                candidates = longDistancePlanets;
            }

            else if (mediumCount > 0)
            {
                candidates = mediumDistancePlanets;
            }

            else if (shortCount > 0)
            {
                candidates = shortDistancePlanets;
            }

            else
            {
                candidates = new List <GridTile_Planet>(completeList);
            }
        }
        return(candidates);
    }
Esempio n. 5
0
    private void PlaceSingleShipOnMap(Ship ship)
    {
        List <GridTile> allCandidateTiles = new List <GridTile>();
        List <GridTile> finalCandidates   = new List <GridTile>();
        int             minShipHeat       = int.MaxValue;

        foreach (var tile in GridCoords.CurrentGridInfo.gameGridTiles)
        {
            if (tile.tileType == 0)
            {
                allCandidateTiles.Add(tile);
                if (tile.ShipStartHeat < minShipHeat)
                {
                    minShipHeat = tile.ShipStartHeat;
                }
            }
        }

        foreach (var sh in shipInventory)
        {
            if (sh == ship)
            {
                continue;
            }

            TileCoordinates shipPos = GridCoords.FromWorldToTilePosition(sh.transform.position);
            if (!GridCoords.IsInGrid(shipPos.tileX, shipPos.tileY))
            {
                continue;
            }

            GridTile tileToRemove = GridCoords.CurrentGridInfo.gameGridTiles[shipPos.tileX, shipPos.tileY];
            allCandidateTiles.Remove(tileToRemove);
        }

        int count = 0;

        foreach (var tile in allCandidateTiles)
        {
            if (tile.ShipStartHeat <= minShipHeat + 2)
            {
                finalCandidates.Add(tile);
                count++;
            }
        }
        int             randomIndex  = UnityEngine.Random.Range(0, count);
        Vector2         shipPosition = finalCandidates[randomIndex].TileCenter;
        TileCoordinates shipCoords   = finalCandidates[randomIndex].TileCoordinates;

        ship.transform.position = shipPosition;

        // Add heat, one circle cast per heat distance
        for (int j = 1; j <= shipHeatDistance; j++)
        {
            float heatCastRadius = GridCoords.CurrentGridInfo.TileWidth * j;

            Collider2D[] allHits = Physics2D.OverlapCircleAll(shipPosition, heatCastRadius, tileLayerMask);
            foreach (var hit in allHits)
            {
                GridTile candidate = hit.GetComponent <GridTile>();
                if (hit != null)
                {
                    candidate.AddShipHeat(1);
                }
            }
        }

        foreach (var tile in allCandidateTiles)
        {
            // If same X
            if (tile.tileX == shipCoords.tileX)
            {
                // If not exact same tile
                if (tile.tileY != shipCoords.tileY)
                {
                    // If neighbour
                    if (tile.tileY == shipCoords.tileY - 1 || tile.tileY == shipCoords.tileY + 1)
                    {
                        tile.AddShipHeat(2);
                    }
                    // If other
                    else
                    {
                        tile.AddShipHeat(2);
                    }
                }
            }

            if (tile.tileY == shipCoords.tileY)
            {
                // If not exact same tile
                if (tile.tileX != shipCoords.tileX)
                {
                    // If neighbour
                    if (tile.tileX == shipCoords.tileX - 1 || tile.tileX == shipCoords.tileX + 1)
                    {
                        tile.AddShipHeat(2);
                    }
                    // If other
                    else
                    {
                        tile.AddShipHeat(2);
                    }
                }
            }
        }
    }