private void UpdateTileBelow()
    {
        floorRay = new Ray(this.transform.position, Vector3.down);
        if (Physics.Raycast(floorRay, out floorHit, floorRayLength, floorMask))
        {
            tileBelowObject = floorHit.collider.gameObject;
            tileBelow = tileBelowObject.GetComponent<LevelTile>();

            Debug.DrawRay(this.transform.position, floorRay.direction, Color.cyan);
        }
        else
        {
            tileBelowObject = null;
            tileBelow = null;
        }
    }
Example #2
0
    public LevelModel(int width, int length)
    {
        Width  = width;
        Length = length;

        Rooms = new List <Vector3>();

        Tiles = new LevelTile[width, length];

        HellContiguousTiles = new LevelTile[width, length];

        HeavenContiguousTiles = new LevelTile[width, length];

        // Populate tiles.
        for (int z = 0; z < length; ++z)
        {
            for (int x = 0; x < width; ++x)
            {
                LevelTile tile = new LevelTile();

                tile.X = x;
                tile.Z = z;

                Tiles[x, z] = tile;
            }
        }

        // Punch random holes.
        for (int z = 0; z < length; ++z)
        {
            for (int x = 0; x < width; ++x)
            {
                if (Random.value < HoleProbability)
                {
                    int holeSize = Random.Range(MinHoleSize, MaxHoleSize);

                    Vector2 roomCenter = new Vector3(x, z, holeSize);

                    PunchHole(roomCenter, holeSize);

                    Rooms.Add(roomCenter);
                }
            }
        }

        // Punch hole for hell base / player spawn.
        HellSpawn.Set(width / 2, length / 2);

        PunchHole(HellSpawn, SpawnHoleSize);

        // Punch hole for heaven base / hero spawn.
        var angle = Random.value * Mathf.PI * 2;

        HeavenSpawn.x = (int)(width / 2 + Mathf.Cos(angle) * (width - SpawnHoleSize) / 2);

        HeavenSpawn.y = (int)(length / 2 + Mathf.Sin(angle) * (length - SpawnHoleSize) / 2);

        PunchHole(HeavenSpawn, SpawnHoleSize);

        // Evaluate tiles for wall types
        for (int z = 0; z < length; ++z)
        {
            for (int x = 0; x < width; ++x)
            {
                EvaluateTile(x, z);
            }
        }

        FloodFill((int)HellSpawn.x, (int)HellSpawn.y, HellContiguousTiles);
        FloodFill((int)HeavenSpawn.x, (int)HeavenSpawn.y, HeavenContiguousTiles);
    }
Example #3
0
 public int ManhattanDistanceTo(LevelTile gridItem)
 {
     return(GridUtility.ManhattanDistance(position, gridItem.position));
 }
 public void OnShipEmerging(LevelTile tile)
 {
 }
 public void OnTorpedoHitGroundOrWater(LevelTile tile, Torpedo torpedo, float posX, float posY)
 {
 }
 public void OnTorpedoSunk(LevelTile tile, Torpedo ammunition, TorpedoFailure?torpedoFailure)
 {
 }
 public void OnGunHit(LevelTile tile, float posX, float posY)
 {
     levelView.OnGunHit(tile, posX, posY);
 }
 private static bool CanGenerateMonster(LevelTile tile, MonsterPosition position) =>
 position switch
 {
    // Update is called once per frame
    void Update()
    {
        //		Vector3 forward = transform.TransformDirection(Vector3.forward) * 10;
        //		Debug.DrawRay(transform.position, forward, Color.red);

        // Get mouse's screen position.
        mousePosition = Input.mousePosition;
        //		mousePosition.z = gameCamera.nearClipPlane;
        mouseWorldPosition = gameCamera.ScreenToWorldPoint(mousePosition);

        // Get ray from screen point.
        cameraRay = gameCamera.ScreenPointToRay(mousePosition); // world space

        cameraOrigin = cameraRay.origin;
        cameraDirection = cameraRay.direction;

        // Check if ray can hit the floor
        RaycastHit floorHit;

        LevelTile currentlySelectedTile = null;

        if (Physics.Raycast(cameraRay, out floorHit, cameraRayLength, floorMask))
        {
            currentlySelectedTile = floorHit.transform.gameObject.GetComponent<LevelTile>();
            dir = cameraRay.direction * cameraRayLength;
        //			Vector3 pos = gameCamera.transform.position;
        //			Vector3 target = pos + dir;
            dir1 = currentlySelectedTile.transform.position - gameCamera.transform.position;

            Debug.DrawRay(currentlySelectedTile.transform.position, -dir1, Color.green);
        //			Debug.DrawRay(gameCamera.transform.position, dir, Color.blue);
            Debug.DrawRay(cameraRay.origin, dir,  Color.white);
        //			Debug.DrawRay(currentlySelectedTile.transform.position, Vector3.up*cameraRayLength, Color.yellow);
        //			Debug.DrawLine(currentlySelectedTile.transform.position,
        //			               currentlySelectedTile.transform.position + Vector3.up*cameraRayLength,
        //			               Color.magenta);

            if (selectedTile != null)
            {
                if (!GameObject.Equals(currentlySelectedTile, selectedTile) || GameObject.Equals (lastClickedTile, currentlySelectedTile))
                {
                    // If the last selected tile is not the same tile as now,
                    // let's reset to old selected tile's position first.
                    Vector3 position = selectedTile.transform.position;
                    position.y = selectedTile.y + selectedTile.size.y/2;
                    selectedTile.transform.position = position;
                }

            }

            if (!GameObject.Equals (lastClickedTile, currentlySelectedTile))
            {
                // Let's raise the selected tile now.
                Vector3 newPosition = currentlySelectedTile.transform.position;
                newPosition.y = currentlySelectedTile.y + currentlySelectedTile.size.y/2 + 0.25f;
                currentlySelectedTile.transform.position = newPosition;

                selectedTile = currentlySelectedTile;
            }

        }
        else
        {
            if (selectedTile != null)
            {
                // Let's reset to old selected tile's position first.
                Vector3 position = selectedTile.transform.position;
                position.y = selectedTile.y + selectedTile.size.y/2;
                selectedTile.transform.position = position;
                selectedTile = null;
            }

        }

        if (Input.GetMouseButtonUp(0))
        {
            Debug.Log ("OnMouseButtonUp()");
            if (selectedTile != null)
            {
                if (OnTileSelectionCallback != null)
                {
                    OnTileSelectionCallback(selectedTile.gameObject);
                }

                lastClickedTile = selectedTile;
            }
        }
    }
Example #10
0
 public void highlightTile(LevelTile tile)
 {
     highlightEffect(tile, tileHighlighterPrefab, tileHighlighterPool);
 }
Example #11
0
 public void highlightChange(LevelTile tile)
 {
     highlightEffect(tile, changeHighlighterPrefab, changeHighlighterPool);
 }
Example #12
0
 public override IEnumerator Execute(Unit attackerUnit, Unit targetUnit, Vector3Int targetPos, LevelTile targetTile)
 {
     if (targetTile && targetTile.GetType() == typeof(BushTile))
     {
         attackerUnit.PlaySound(tileIgniteSound);
         targetTile.Activate(targetPos);
     }
     yield return(new WaitForEndOfFrame());
 }
Example #13
0
    public void IsTraversable_NoLevelElements_True()
    {
        levelTile = new LevelTile(tilePosition);

        NUnit.Framework.Assert.IsTrue(levelTile.IsTraversable());
    }
 public void revealTilesAround(LevelTile lt)
 {
     tilesToReveal.Add(lt);
     enabled = true;
 }
Example #15
0
 private bool TileCanOccupyPosition(LevelTile candidateTile)
 {
     return(candidateTile.ToStringCoords().All(stringCoord => !_levelReference.CoordOccupied(stringCoord)));
 }
Example #16
0
 public AircraftCarrierTileView(LevelTile levelTile, IFrameWork framework)
     : base(levelTile, framework)
 {
 }
Example #17
0
 public MiddleIslandTileView(LevelTile levelTile, IFrameWork framework)
     : base(levelTile, framework)
 {
     animableElements = new List <AnimationState>();
     // flagNode = null;
 }
    public void processTapGesture(Vector2 tapPos)
    {
        if (foundItem && Managers.Player.Alive)
        {
            recalculateNumbers();
            LevelTile foundLT = foundItem.levelTile;
            if (foundLT.tileType != LevelTile.TileType.MAP)
            {
                //Reveal the found LT
                revealTile(foundLT, true);
                //Reveal the tiles around the found LT
                foreach (LevelTile levelTile in getSurroundingTiles(foundLT))
                {
                    if (levelTile.Revealed)
                    {
                        revealTile(levelTile, true);
                    }
                }
                //Check if goals have been achieved
                if (Managers.Player.GoalAchieved)
                {
                    //Check if map has been completed
                    if (Managers.Player.completedMap())
                    {
                        //Go to latest revealed location
                        Managers.Camera.moveTo(
                            FindObjectOfType <MapLineUpdater>().LastRevealedSpot
                            );
                    }
                    else
                    {
                        //Go to start
                        Managers.Camera.moveTo(Managers.Start);
                    }
                }
            }
            else
            {
                //Check if map has been completed
                if (Managers.Player.completedMap())
                {
                    //Go to latest revealed location
                    Managers.Camera.moveTo(
                        FindObjectOfType <MapLineUpdater>().LastRevealedSpot
                        );
                }
            }
            foundItem.retire();
            foundItem = null;
            return;
        }
        if (checkReset(tapPos))
        {
            return;
        }
        LevelTile lt = getTile(tapPos);

        if (lt != null)
        {
            //If it's revealed
            if (lt.Revealed)
            {
                //Auto-Reveal
                //If the count of surrounding flags equals
                //the count of surrounding trap tiles,
                int itemCount = getAdjacentCount(lt, LevelTile.TileType.TRAP);
                itemCount += getAdjacentCount(lt, LevelTile.TileType.TREASURE);
                if (lt.Empty && lt.tileType != LevelTile.TileType.MAP &&
                    getAdjacentFlagCount(lt) == itemCount)
                {
                    //Reveal the surrounding non-flagged tiles
                    foreach (LevelTile neighbor in getSurroundingTiles(lt))
                    {
                        if (!neighbor.Flagged && !neighbor.Revealed)
                        {
                            if (neighbor.tileType == LevelTile.TileType.TRAP)
                            {
                                Managers.Player.takeHit();
                            }
                            if (neighbor.tileType == LevelTile.TileType.TREASURE)
                            {
                                Managers.Player.findTrophy();
                            }
                            revealTile(neighbor);
                        }
                    }
                    if (!Managers.Player.Alive)
                    {
                        revealBoard();
                    }
                }
                //Auto-Flag
                //If the count of surrounding unrevealed tiles equals
                //the count of surrounding trap tiles,
                if (lt.Empty && lt.tileType != LevelTile.TileType.MAP &&
                    getAdjacentRevealedCount(lt, true) == itemCount)
                {
                    //Flag the surrounding non-revealed tiles
                    foreach (LevelTile neighbor in getSurroundingTiles(lt))
                    {
                        if (!neighbor.Flagged && !neighbor.Revealed)
                        {
                            //Flag it
                            processFlagGesture(neighbor.transform.position);
                        }
                    }
                }
            }
            //If it's not flagged
            if (!lt.Flagged)
            {
                if (!anyRevealed)
                {
                    generateLevelPostTap(tapPos);
                    anyRevealed = true;
                }
                if ((!lt.Revealed) || lt.DetectedAny)
                {
                    Managers.Effect.highlightChange(lt);
                }
                LevelTile.TileType revealedItem = LevelTile.TileType.EMPTY;
                bool shouldRevealBoard          = false;
                bool prevRevealed = lt.Revealed;
                if (lt.tileType == LevelTile.TileType.TRAP)
                {
                    revealedItem = LevelTile.TileType.TRAP;
                    if (!Managers.Player.takeHit())
                    {
                        shouldRevealBoard = true;
                    }
                }
                if (lt.tileType == LevelTile.TileType.TREASURE)
                {
                    revealedItem = LevelTile.TileType.TREASURE;
                    Managers.Player.findTrophy();
                }
                if (!LevelTile.empty(revealedItem))
                {
                    lt.Revealed = true;
                    Managers.Effect.highlightChange(lt);
                    if (shouldRevealBoard)
                    {
                        revealBoard();
                    }
                    generatePostItemReveal(revealedItem);
                }
                else
                {
                    revealTile(lt);
                }
                if (lt.tileType == LevelTile.TileType.MAP)
                {
                    //if it's already been revealed
                    //but not activated yet
                    if (prevRevealed && !lt.Activated)
                    {
                        lt.Activated = true;
                        Managers.Effect.highlightChange(lt);
                        generatePostItemReveal(LevelTile.TileType.MAP);
                    }
                }
            }
        }
    }
Example #19
0
 public OceanTileView(LevelTile levelTile, IFrameWork framework)
     : base(levelTile, framework)
 {
     floatingNodes = new SceneNode[0];
 }
Example #20
0
 public override IEnumerator Execute(Unit attackerUnit, Unit targetUnit, Vector3Int targetPos, LevelTile targetTile)
 {
     if (targetUnit != null)
     {
         targetUnit.resistancesManager.AddResistance(resistance);
     }
     yield return(new WaitForEndOfFrame());
 }
Example #21
0
 public void OnTileBombed(LevelTile tile, Ammunition ammunition)
 {
 }
Example #22
0
 public ShipBunkerTileView(LevelTile levelTile, IFrameWork framework)
     : base(levelTile, framework)
 {
     animationState = null;
 }
Example #23
0
 public void OnTileDestroyed(LevelTile tile, Ammunition ammunition)
 {
 }
Example #24
0
    /*
     * Returns an array of positions between startPosition and endPosition if exists a path between them
     * If the path is blocked or out of bounds it returns null
     */
    public Vector3[] FindPath(Vector3 startPosition, Vector3 endPosition)
    {
        int startX, startY, endX, endY;

        GetXY(startPosition, out startX, out startY);
        GetXY(endPosition, out endX, out endY);

        if (startX < 0 || startX >= Width)
        {
            return(null);
        }
        if (startY < 0 || startY >= Height)
        {
            return(null);
        }
        if (endX < 0 || endX >= Width)
        {
            return(null);
        }
        if (endY < 0 || endY >= Height)
        {
            return(null);
        }

        LevelTile endTile   = GetGridTile(endX, endY);
        LevelTile startTile = GetGridTile(startX, startY);

        if (startTile == endTile)
        {
            return(null);
        }
        List <int[]> searchTiles = new List <int[]>();

        TileInPath[,] tilesInRange   = new TileInPath[Width, Height];
        tilesInRange[startX, startY] = new TileInPath(startX, startY, null);

        int x, y;

        for (int i = 0; i < directions.Length; i++)
        {
            x = startX + directions[i][0];
            y = startY + directions[i][1];
            if (x < 0 || x >= Width)
            {
                continue;
            }
            if (y < 0 || y >= Height)
            {
                continue;
            }
            tilesInRange[x, y] = new TileInPath(x, y, tilesInRange[startX, startY]);
            searchTiles.Add(new int[] { x, y });
        }
        LevelTile currentTile;

        int[] currentTileInfo;

        while (searchTiles.Count > 0)
        {
            currentTileInfo = searchTiles[0];
            searchTiles.RemoveAt(0);
            x           = currentTileInfo[0];
            y           = currentTileInfo[1];
            currentTile = GetGridTile(x, y);
            if (currentTile == endTile)
            {
                TileInPath     currentTileInPath = tilesInRange[x, y].parent;
                List <Vector3> path = new List <Vector3>();
                //END
                while (currentTileInPath != tilesInRange[startX, startY])
                {
                    path.Add(GetWorldPosition(currentTileInPath.x, currentTileInPath.y));
                    currentTileInPath = currentTileInPath.parent;
                }

                path.Reverse();
                return(path.ToArray());
            }
            if (currentTile == null || currentTile.UnitInTile != null)
            {
                continue;
            }

            int newX, newY;
            for (int i = 0; i < directions.Length; i++)
            {
                newX = x + directions[i][0];
                newY = y + directions[i][1];
                if (newX < 0 || newX >= Width)
                {
                    continue;
                }
                if (newY < 0 || newY >= Height)
                {
                    continue;
                }
                if (tilesInRange[newX, newY] != null)
                {
                    continue;
                }
                tilesInRange[newX, newY] = new TileInPath(newX, newY, tilesInRange[x, y]);
                searchTiles.Add(new int[] { newX, newY });
            }
        }

        return(null);
    }
Example #25
0
 public void OnShipBeginSubmerging(LevelTile tile)
 {
 }
Example #26
0
    /*
     * This function takes care of setting up the grid on Editor.
     * Every time its values are modified, it iterates through the grid to delete extra rows and columns or to add more.
     * TODO: Instantiating generates SendMessages. SendMessages cannot used OnValidate so it triggers a warning.
     */
    private void OnValidate()
    {
#if UNITY_EDITOR
        if (EditorApplication.isPlaying)
        {
            return;
        }
#endif

        int      i        = 0;
        Column[] _newGrid = new Column[Width];
        //Iterate through columns
        for (; i < _grid.Length && i < Width; i++)
        {
            _newGrid[i] = _grid[i];
            if (_newGrid[i].column.Length != Height)
            {
                int         j         = 0;
                LevelTile[] newColumn = new LevelTile[Height];
                for (; j < _newGrid[i].column.Length && j < Height; j++)
                {
                    newColumn[j] = _newGrid[i].column[j];
                }

                //Delete extra  row tiles
                for (; j < _newGrid[i].column.Length; j++)
                {
                    StartCoroutine(DestroyTileObject(_newGrid[i].column[j].TileObject));
                }

                //Add extra rows
                for (; j < Height; j++)
                {
                    newColumn[j] = GenerateNewTile(i, j);
                }

                _newGrid[i].column = newColumn;
            }
        }

        //Delete extra column tiles
        for (; i < _grid.Length; i++)
        {
            for (int j = 0; j < _grid[i].column.Length; j++)
            {
                StartCoroutine(DestroyTileObject(_grid[i].column[j].TileObject));
            }
        }

        //Add extra columns
        for (; i < Width; i++)
        {
            _newGrid[i]        = new Column();
            _newGrid[i].column = new LevelTile[Height];
            for (int j = 0; j < Height; j++)
            {
                _newGrid[i].column[j] = GenerateNewTile(i, j);
            }
        }

        _grid = _newGrid;
        if (_levelCollider == null)
        {
            Debug.LogError("No Level Collider found, be sure to add the level collider to the LevelGrid");
        }
        _levelCollider.localScale = new Vector3(Width, 1, Height);

        //Set Camera Initial Position
        Camera camera = Camera.main;
        if (camera == null)
        {
            Debug.LogError("No Camera found, be sure to add a Main Camera to the Scene");
        }
        camera.transform.parent.position = new Vector3(Width * .5f - .5f, 0, 0);
    }
Example #27
0
 public void OnShipSubmerged(LevelTile tile)
 {
 }
Example #28
0
 public void displayNumber(LevelTile parent)
 {
     levelTile = parent;
     displayNumber();
 }
Example #29
0
 public RouteInformation(LevelTile startLocation, LevelTile endLocation)
 {
     StartLocationIndex = startLocation.TileIndecies;
     EndLocationIndex   = endLocation.TileIndecies;
 }
Example #30
0
    // private int[] WallT = new int[9] {
    //     2, 1, 2,
    //     2, 0, 2,
    //     2, 2, 2
    // };

    // private int[] WallR = new int[9] {
    //     2, 2, 2,
    //     2, 0, 1,
    //     2, 2, 2
    // };

    // private int[] WallB = new int[9] {
    //     2, 2, 2,
    //     2, 0, 2,
    //     2, 1, 2
    // };

    // private int[] WallL = new int[9] {
    //     2, 2, 2,
    //     1, 0, 2,
    //     2, 2, 2
    // };

    // private int[] CorTL = new int[9] {
    //     1, 0, 2,
    //     0, 0, 2,
    //     2, 2, 2
    // };

    public void EvaluateTile(int x, int z)
    {
        LevelTile tile = Tiles[x, z];

        if (tile.Opened)
        {
            tile.WallTop = tile.WallBottom = tile.WallLeft = tile.WallRight = false;
            return;
        }

        // If the tile position is out of bounds, value is 2 (ignore tile).
        // If tile is inside bounds, opened = 1, closed = 0
        //int tileTL = (x > 0 && z > 0)                  ? (Tiles[x - 1, z - 1].Opened ? 1 : 0) : 2;
        int tileTT = z > 0 ? (Tiles[x, z - 1].Opened ? 1 : 0) : 2;
        // int tileTR = (x < Width - 1 && z > 0)          ? (Tiles[x + 1, z - 1].Opened ? 1 : 0) : 2;
        int tileRR = x < Width - 1 ? (Tiles[x + 1, z].Opened ? 1 : 0) : 2;
        // int tileBR = (x < Width - 1 && z < Length - 1) ? (Tiles[x + 1, z + 1].Opened ? 1 : 0) : 2;
        int tileBB = z < Length - 1 ? (Tiles[x, z + 1].Opened ? 1 : 0) : 2;
        // int tileBL = (x > 0 && z < Length - 1)         ? (Tiles[x - 1, z + 1].Opened ? 1 : 0) : 2;
        int tileLL = x > 0 ? (Tiles[x - 1, z].Opened ? 1 : 0) : 2;

        if (tileTT == 1)
        {
            tile.WallTop = true;
        }
        if (tileRR == 1)
        {
            tile.WallRight = true;
        }
        if (tileBB == 1)
        {
            tile.WallBottom = true;
        }
        if (tileLL == 1)
        {
            tile.WallLeft = true;
        }

        // int[] openedTable = new int[9] {
        //     tileTL, tileTT, tileTR,
        //     tileLL, 0, tileRR,
        //     tileBL, tileBB, tileBR
        // };

        // if (CheckTables(openedTable, WallT))
        // {
        //     tile.WallTop = true;
        // }
        // if (CheckTables(openedTable, WallR))
        // {
        //     tile.WallRight = true;
        // }
        // if (CheckTables(openedTable, WallB))
        // {
        //     tile.WallBottom = true;
        // }
        // if (CheckTables(openedTable, WallL))
        // {
        //     tile.WallLeft = true;
        // }
    }