Beispiel #1
0
    // TODO make this work wtih all the different tilemap layers.
    private void PopulateContainingCells()
    {
        // TODO throw error when not spawned?
        if (!IsSpawned)
        {
            return;
        }

        Tilemap tm = LandMap;

        NorthernBounds = -10000000;
        SouthernBounds = 10000000;
        EasternBounds  = -10000000;
        WesternBounds  = 10000000;

        // Get an enumerator for all the cells within this area.
        BoundsInt.PositionEnumerator pe = tm.cellBounds.allPositionsWithin;
        do
        {
            if (tm.GetSprite(pe.Current) != null)
            {
                Vector2Int coords = WorldGridHolder.GetCell(pe.Current).Coordinates;
                localContainingCellCoordinates.Add(coords);
                if (coords.x > NorthernBounds)
                {
                    NorthernBounds = coords.x;
                }
                if (coords.x < SouthernBounds)
                {
                    SouthernBounds = coords.x;
                }
                if (coords.y > EasternBounds)
                {
                    EasternBounds = coords.y;
                }
                if (coords.y < WesternBounds)
                {
                    WesternBounds = coords.y;
                }

                #region Debug
                if (DebugSettings.DebugZone)
                {
                    tm.SetTile(new Vector3Int(coords.x, coords.y, 0), DebugSettings.DebugZoneTile);
                }
                #endregion
            }
        } while (pe.MoveNext());

        // Now that we have the localized values, let's generalize them for global usage.
        foreach (Vector2Int localCellCorrdinate in localContainingCellCoordinates)
        {
            containingCellCoordinates.Add(new Vector2Int(
                                              localCellCorrdinate.x + spawnLocation.x,
                                              localCellCorrdinate.y + spawnLocation.y));
        }
    }
 public void Update()
 {
     if (moving && moveList.Count > 0)
     {
         if (grid.GetCell(this.transform.position).Equals(moveList[0]))
         {
             moveList.RemoveAt(0);
         }
         else
         {
             this.GetComponent <Rigidbody>().velocity = (moveList[0].worldPosition - this.transform.position).normalized * speed;
         }
     }
 }
Beispiel #3
0
 public void OnWorldCreated(WorldGrid world)
 {
     //Debug.Log("World created");
     world.ForAllValidCoords(coord =>
     {
         var cell       = world.GetCell(coord);
         var gameObject = GenerateCell(cell.Status == CellStatus.OPEN, coord);
         _objects.Add(coord, gameObject);
     });
 }
Beispiel #4
0
    // Update is called once per frame
    void Update()
    {
        if (Time.time > _nextTime)
        {
            _nextTime = Time.time + IntervalTime;

            var coord = CoordsToToggle[_index];

            var cell = WorldGrid.GetCell(coord);
            var next = new Cell(cell.Status == CellStatus.OPEN ? CellStatus.FILLED : CellStatus.OPEN, cell.Type);
            WorldGrid.SetCell(coord, next);

            _index = (_index + 1) % CoordsToToggle.Length; // increment index and wrap it around array size.
        }
    }
    // Use this for initialization
    void Start()
    {
        grid.Init();

        //Get all rooms, pick random one
        GameObject[] rooms  = GameObject.FindGameObjectsWithTag("Room");
        int          chosen = Random.Range(0, rooms.Length);

        //Spawn character in random location
        Character char_spawn = Instantiate(characterPrefab, grid.GetCell(rooms [chosen].transform.position).worldPosition, Quaternion.identity);

        char_spawn.grid = this.grid;
        //Start character's AI
        char_spawn.Init();
        //Start enemy spawner
        enemySpawner = StartCoroutine(SpawnEnemies());
    }
Beispiel #6
0
    void PathFromZoneToZone(Zone startZone, Zone endZone, TileBase groundTile)
    {
        System.Random randy = new System.Random();

        // Determine direction from start to end, which also determines which edge/corner to use.
        // Consequencely, this determines the endzone edge/corner to use.
        HexDirection pathDirection = Direction.GetDirection(startZone.spawnLocation, endZone.spawnLocation);

        // Get all the cells along the edge closest to our target.
        List <Vector2Int> startEdge = SelectDirectionalEdge(startZone, pathDirection);
        List <Vector2Int> endEdge   = SelectDirectionalEdge(endZone, Direction.Opposite(pathDirection));

        // Choose a random cell along the appropriate edge, these will be the end points of the path.
        Vector2Int startCoordinates = startEdge[randy.Next(startEdge.Count)];
        Vector2Int endCoordinates   = endEdge[randy.Next(endEdge.Count)];

        world.GetCell(startCoordinates).special = SpecialType.Path;
        world.GetCell(endCoordinates).special   = SpecialType.Path;

        HexDirection nextDirection = pathDirection;
        WorldCell    currentCell   = world.GetCell(startCoordinates).GetNeighbor(pathDirection);

        currentCell.special = SpecialType.Path;

        // cycle through the cells, starting from startCell, and heading towards pathDirection.
        bool NeighborIsEndCoordinates = false;

        while (!NeighborIsEndCoordinates)
        {
            foreach (WorldCell neighbor in currentCell.GetListOfNeighbors())
            {
                if (endCoordinates == neighbor.Coordinates)
                {
                    NeighborIsEndCoordinates = true;
                }
            }

            if (!NeighborIsEndCoordinates)
            {
                // TODO handle out of bounds properly.
                // TODO don't select a cell we've already done in this loop.
                bool      validNextCell = false;
                WorldCell nextCell      = null;
                while (!validNextCell)
                {
                    // TODO randomly determine which direction we will be going, offset from the nextDirection (or exactly equal to nextDirection)
                    nextDirection = GetNextDirection(Direction.GetDirection(currentCell.Coordinates, endCoordinates));
                    nextCell      = currentCell.GetNeighbor(nextDirection);
                    validNextCell = true;
                }
                currentCell = nextCell;

                // Build the path with what we found.
                currentCell.special = SpecialType.Path;
                #region Debug
                if (DebugSettings.DebugPath)
                {
                    tilemap.SetTile(new Vector3Int(currentCell.Coordinates.x, currentCell.Coordinates.y, 0), DebugSettings.DebugPathTile);
                    // tm.SetTile(new Vector3Int(coords.x, coords.y, 0), DebugSettings.DebugPathTile);
                }
                else
                {
                    tilemap.SetTile(new Vector3Int(currentCell.Coordinates.x, currentCell.Coordinates.y, 0), groundTile);
                }
                #endregion
                // todo chance for "extra" things to appear on the side.
            }
        }
    }