public bool CheckForCapture(Faction faction)
 {
     if (GetValue() % 10 == 0)
     {
         int         radius = (GetValue() / 10) - 1;
         Map.Coord[] coords = map.GetCellsInCircle(this.pos, radius, true).ToArray();
         foreach (Map.Coord c in coords)
         {
             ClickableTile tempTile = map.tiles[c.x, c.y];
             tempTile.SetOwner(faction);
             tempTile.SetValue(1);
             map.audioSource.Play();
         }
         return(true);
     }
     else
     {
         return(false);
     }
 }
Example #2
0
    /*void Update() {
     *  currentMap.graph.UpdateGraph();
     * }*/

    public Map GenerateMap(Faction[] factions, int seed)
    {
        currentMap = maps[mapIndex];
        System.Random prng = new System.Random(seed);

        currentMap.tiles = new ClickableTile[currentMap.size.x, currentMap.size.y];

        allTileCoords = new List <Map.Coord>();
        for (int x = 0; x < currentMap.size.x; x++)
        {
            for (int y = 0; y < currentMap.size.y; y++)
            {
                allTileCoords.Add(new Map.Coord(x, y));
            }
        }
        shuffledTileCoords = new Queue <Map.Coord>(Utility.ShuffleArray(allTileCoords.ToArray(), seed));

        //Get corners (will be where we spawn units)
        List <Map.Coord> corners = new List <Map.Coord>();

        corners.Add(new Map.Coord(0, 0));
        corners.Add(new Map.Coord(currentMap.size.x - 1, currentMap.size.y - 1));
        corners.Add(new Map.Coord(currentMap.size.x - 1, 0));
        corners.Add(new Map.Coord(0, currentMap.size.y - 1));

        // Create GenreratedMap GO to hold tiles
        string holderName = "GeneratedMap";

        if (transform.FindChild(holderName))
        {
            DestroyImmediate(transform.FindChild(holderName).gameObject);
        }

        Transform mapHolder = new GameObject(holderName).transform;

        mapHolder.parent = transform;

        List <Transform> tileTransforms = new List <Transform>(currentMap.size.x * currentMap.size.y);

        // Create tiles
        for (int x = 0; x < currentMap.size.x; x++)
        {
            for (int y = 0; y < currentMap.size.y; y++)
            {
                // Step through loop to create new positions
                Vector3 tilePosition = CoordToPosition(new Map.Coord(x, y), 0f);
                // Create new tile instance from prefab, rotate 90 degrees, and cast as Tranform type
                Transform newTile = Instantiate(tilePrefab, tilePosition, Quaternion.identity) as Transform;            // Quaternion.Euler(Vector3.right * 90))
                //Scale newTile based on outlinePercent
                newTile.localScale = new Vector3((1 - outlinePercent) * tileSize, 1f, (1 - outlinePercent) * tileSize); //Vector3.one * (1 - outlinePercent) * tileSize;
                // Set tile's parent to GenratedMap GO
                newTile.parent = mapHolder;
                tileTransforms.Add(newTile);

                ClickableTile ct = newTile.GetComponent <ClickableTile>();
                currentMap.tiles[x, y] = ct;
                ct.pos = new Map.Coord(x, y);

                ct.SetValue((int)prng.Next(1, currentMap.maxTileValue));
                ct.map = currentMap;
            }
        }
        currentMap.graph = new Graph(tileTransforms.ToArray(), currentMap.size.x, GraphFunctionName.Ripple);

        #region Obstacle Generation
        bool[,] obstacleMap = new bool[(int)currentMap.size.x, (int)currentMap.size.y];

        // Determine number of obstacles
        int obstacleCount        = (int)(currentMap.size.x * currentMap.size.y * currentMap.obstaclePercent);
        int currentObstacleCount = 0;

        // Create obstacles using PerlinNoise
        PerlinNoise perlin         = new PerlinNoise(currentMap.size.x, currentMap.size.y, seed);
        Map.Coord[] obstacleCoords = GetPerlinCoords(perlin, currentMap.obstaclePercent);

        foreach (Map.Coord c in obstacleCoords)
        {
            obstacleMap[c.x, c.y] = true;
            currentObstacleCount++;
            // Check that this coord is valid
            if (c != currentMap.Center && MapIsFloodable(obstacleMap, currentObstacleCount) && !corners.Contains(c))
            {
                //Create obstacle
                Transform newObstacle = CreateObstacle(c, perlin.GetValueAt(c.x, c.y));
                // Parent obstacle to tile sharing coord position
                newObstacle.parent = currentMap.tiles[c.x, c.y].transform;
            }
            else
            {
                obstacleMap[c.x, c.y] = false;
                currentObstacleCount--;
            }
        }
        #endregion

        // Create units for each active faction, initialize them, and set its parent to the mapHolder
        Map.Coord origin; //this will tell us which quadrant to spawn each faction's units
        // step trough our list of factions TODO change back to length of factions
        for (int i = 0; i < 2 /*factions.Length*/; i++)
        {
            // determine quadrant
            origin = corners[i];

            //for (int n = 0; n <= factions[i].GetStartingNumUnits(); n++) { TODO multiple units. for now just stick to ine for simplicity's sake

            Unit newUnit = Instantiate(unitPrefab, CoordToPosition(origin, 1f), Quaternion.identity).GetComponent <Unit>() as Unit;
            if (newUnit != null)
            {
                newUnit.Init(currentMap, origin, factions[i]);
                newUnit.transform.parent = mapHolder;
            }
        }

        GeneratePathfindingGraph(currentMap);


        return(currentMap);
    }