Пример #1
0
    public override NODE_STATE TreeExecute()
    {
        btPlayerTile player = btTileManager.instance.playerTile;

        btTile.DIRECTION playerForward = player.direction;
        btBoardSpace     space1        = btTileManager.instance.GetSpaceInDirectionFromTile(player, playerForward);

        if (space1 == null || space1.IsPassable() == true)
        {
            this.currentState = btFunctionNode.NODE_STATE.FAILED;
            return(this.currentState);
        }
        btBoardSpace space2 = btTileManager.instance.GetSpaceInDirectionFromTile(player, playerForward, 2);

        if (space2 == null || space2.IsPassable() == false)
        {
            this.currentState = btFunctionNode.NODE_STATE.FAILED;
            return(this.currentState);
        }

        player.startSpace = player.currentSpace;        //btTileManager.instance.GetSpace( player.xIndex, player.yIndex );
        player.endSpace   = space2;
        player.tileState  = btTile.TILESTATE.JUMPING;

        this.currentState = btFunctionNode.NODE_STATE.RUNNING;
        return(this.currentState);
    }
Пример #2
0
    /// <summary>
    /// Moves "tile" to "space" in the input direction
    /// </summary>
    /// <param name="_tile">The tile to be moved</param>
    /// <param name="_space">The space to move the tile to</param>
    /// <param name="_runCallbacks">Should callbacks be run on the tiles (false when resetting tiles, true during normal gameplay)</param>
    /// <param name="_movementDirection">The direction that the tile is moving in (used for callbacks)</param>
    private void MoveTileToSpace(btTile _tile, btBoardSpace _space, bool _runCallbacks, btTile.DIRECTION _movementDirection)
    {
        btBoardSpace oldSpace = _tile.currentSpace;        //this.GetSpace( _tile.xIndex, _tile.yIndex );

        _tile.currentSpace.tiles.Remove(_tile);
        //_tile.xIndex = _space.x;
        //_tile.yIndex = _space.y;
        _tile.currentSpace = _space;
        _space.tiles.Add(_tile);

        if (_runCallbacks == true)
        {
            if (oldSpace != null)
            {
                foreach (btTile oldTile in oldSpace.tiles)
                {
                    if (oldTile != _tile)
                    {
                        oldTile.OnMoveableTileExit(_tile, _movementDirection);
                    }
                }
            }

            foreach (btTile newTile in _space.tiles)
            {
                if (newTile != _tile)
                {
                    newTile.OnMoveableTileEnter(_tile, _movementDirection);
                }
            }
        }

        this.ResetTilePosition(_tile);
    }
Пример #3
0
    /// <summary>
    /// Tile update loop, managed by the game manager
    /// Returns true once all tiles have finished movements and such
    /// </summary>
    /// <returns>When all tiles have finished their update</returns>
    public bool UpdateTiles()
    {
        bool allComplete = true;

        for (int y = 0; y < this.tilesVertical; ++y)
        {
            for (int x = 0; x < this.tilesHorizontal; ++x)
            {
                btBoardSpace space = this.GetSpace(x, y);
                if (space == null)
                {
                    continue;
                }

                foreach (btTile tile in space.tiles)
                {
                    bool tileUpdate = tile.TileUpdate();
                    if (tileUpdate == false)
                    {
                        allComplete = false;
                    }
                }
            }
        }
        return(allComplete);
    }
Пример #4
0
    /// <summary>
    /// Resets all tiles back to their original positions and states
    /// </summary>
    public void ResetTiles()
    {
        for (int y = 0; y < instance.tilesVertical; ++y)
        {
            for (int x = 0; x < instance.tilesHorizontal; ++x)
            {
                btBoardSpace space = instance.tileGrid[y][x];

                for (int i = 0; i < space.tiles.Count; ++i)
                {
                    btTile tile = space.tiles[i];

                    if (tile.initialSpace != space)
                    //if ( tile.xInitial != x || tile.yInitial != y )
                    {
                        this.MoveTileToSpace(tile, tile.initialSpace, false, btTile.DIRECTION.NORTH);
                    }

                    tile.direction = tile.initialDirection;
                    tile.tileState = btTile.TILESTATE.NONE;
                    tile.ResetUpdateValues();
                    this.ResetTilePosition(tile);
                }
            }
        }
    }
Пример #5
0
    /// <summary>
    /// Returns whether the given space can be passed
    /// </summary>
    /// <param name="_xIndex">The x index of the space</param>
    /// <param name="_yIndex">The y index of the space</param>
    /// <returns></returns>
    public bool GetIsSpacePassable(int _xIndex, int _yIndex)
    {
        btBoardSpace space = this.GetSpace(_xIndex, _yIndex);

        if (space == null)
        {
            return(false);
        }

        return(space.IsPassable());
    }
Пример #6
0
    /// <summary>
    /// Moves "tile" to the space in the given
    /// </summary>
    /// <param name="_tile">The tile to be moved</param>
    /// <param name="_x">The x index of the space to move to</param>
    /// <param name="_y">The y index of the space to move to</param>
    /// <param name="_runCallbacks">Should callbacks be run on the tiles (false when resetting tiles, true during normal gameplay)</param>
    /// <param name="_movementDirection">The direction that the tile is moving in (used for callbacks)</param>
    private void MoveTileToSpaceIndex(btTile _tile, int _x, int _y, bool _runCallbacks, btTile.DIRECTION _movementDirection)
    {
        btBoardSpace space = this.GetSpace(_x, _y);

        if (space == null)
        {
            Debug.LogError("Cannot move tile \"" + _tile.name + "\" to (" + _x + ", " + _y + "): Invalid space index", _tile);
            return;
        }
        this.MoveTileToSpace(_tile, space, _runCallbacks, _movementDirection);
    }
Пример #7
0
    public override void OnMoveableTileEnter(btTile _tile, DIRECTION _enterDirection)
    {
        btBoardSpace moveToSpace = btTileManager.instance.GetSpaceInDirectionFromTile(this, this.direction, 1);

        if (moveToSpace == null || moveToSpace.IsPassable() == false)
        {
            return;
        }
        _tile.tileState       = btTile.TILESTATE.FORCE_MOVE;
        _tile.forceDirection  = this.direction;
        _tile.currentMovement = 0.0f;
        _tile.startSpace      = this.currentSpace;
        _tile.endSpace        = moveToSpace;
    }
Пример #8
0
    public override void OnMoveableTileEnter(btTile _tile, DIRECTION _enterDirection)
    {
        DIRECTION    slideDirection = _enterDirection;
        btBoardSpace adjacentSpace  = btTileManager.instance.GetSpaceInDirectionFromTile(this, slideDirection, 1);

        if (adjacentSpace == null || adjacentSpace.IsPassable() == false)
        {
            return;
        }
        _tile.tileState       = btTile.TILESTATE.SLIDING;
        _tile.slideDirection  = slideDirection;
        _tile.startSpace      = this.currentSpace;
        _tile.endSpace        = adjacentSpace;
        _tile.currentMovement = 0.0f;
    }
Пример #9
0
    public override NODE_STATE TreeExecute()
    {
        btPlayerTile player = btTileManager.instance.playerTile;

        btTile.DIRECTION playerLeft = btTileManager.DirectionTurnedLeft(player.direction);
        btBoardSpace     leftSpace  = btTileManager.instance.GetSpaceInDirectionFromTile(player, playerLeft, 1);

        if (leftSpace == null || leftSpace.IsPassable() == false)
        {
            this.currentState = btFunctionNode.NODE_STATE.FAILED;
            return(this.currentState);
        }

        return(NODE_STATE.SUCCEEDED);
    }
Пример #10
0
    /// <summary>
    /// Returns the tile at the given space indices with the given tile index
    /// </summary>
    /// <param name="_x">The x index</param>
    /// <param name="_y">The y index</param>
    /// <param name="_index">The tile index</param>
    /// <returns></returns>
    public btTile GetTileAtSpaceWithIndex(int _x, int _y, int _index)
    {
        btBoardSpace space = this.GetSpace(_x, _y);

        if (space == null)
        {
            return(null);
        }

        if (_index < 0 || _index >= space.tiles.Count)
        {
            return(null);
        }
        return(space.tiles[_index]);
    }
Пример #11
0
    /// <summary>
    /// Prints out the current state of the board
    /// </summary>
    public void Print()
    {
        string str = "";

        for (int y = 0; y < instance.tilesVertical; ++y)
        {
            for (int x = 0; x < instance.tilesHorizontal; ++x)
            {
                btBoardSpace space = instance.tileGrid[y][x];
                foreach (btTile tile in space.tiles)
                {
                    str += "(" + x + "/" + y + "): " + tile.ToString() + "\n";
                }
            }
        }
        Debug.Log(str);
    }
Пример #12
0
    /// <summary>
    /// Finds all of the tiles in the scene and places them in the correct locations
    /// </summary>
    public int FindAndAddTiles()
    {
        this.bottomLeft = this.gridTransform.localPosition - this.gridTransform.localScale * 0.5f;

        btTile[] tiles = Object.FindObjectsOfType(typeof(btTile)) as btTile[];
        foreach (btTile tile in tiles)
        {
            /// Convert the tile position into a usable index
            Vector3 tilePos = tile.transform.position;
            int     xIndex  = (int)(tilePos.x + (0.5f * (float)this.tilesHorizontal));
            int     yIndex  = (int)(tilePos.y + (0.5f * (float)this.tilesVertical));

            if (this.IsValidTileIndex(xIndex, yIndex) == false)
            {
                Debug.LogError("Tile position out of board range: xIndex=" + xIndex + " yIndex=" + yIndex + " position=" + tilePos);
                GameObject.Destroy(tile.gameObject);
                continue;
            }

            /// Set up the tile information
            //tile.xIndex = tile.xInitial = xIndex;
            //tile.yIndex = tile.yInitial = yIndex;
            tile.initialDirection = tile.direction = TileRotationToDirection(tile);

            btBoardSpace space = this.tileGrid[yIndex][xIndex];
            space.tiles.Add(tile);
            tile.currentSpace = tile.initialSpace = space;
            /// Reset the tile position using its new indices
            tile.transform.parent     = this.transform;
            tile.transform.localScale = this.tileSize;
            this.ResetTilePosition(tile);

                        #if UNITY_EDITOR
            foreach (btTile spaceTile in space.tiles)
            {
                if (spaceTile != tile && spaceTile.GetType() == tile.GetType())
                {
                    Debug.LogError("Duplicate tile \"" + spaceTile.GetType().ToString() + "\" at (" + xIndex + ", " + yIndex + ")", tile);
                }
            }
                        #endif
        }
        return(tiles.Length);
    }
Пример #13
0
    public override NODE_STATE TreeExecute()
    {
        btPlayerTile player = btTileManager.instance.playerTile;

        btTile.DIRECTION playerForward = player.direction;
        btBoardSpace     space         = btTileManager.instance.GetSpaceInDirectionFromTile(player, playerForward);

        if (space == null || space.IsPassable() == false)
        {
            this.currentState = btFunctionNode.NODE_STATE.FAILED;
            return(this.currentState);
        }
        player.tileState       = btTile.TILESTATE.MOVING_FORWARD;
        player.startSpace      = player.currentSpace;
        player.endSpace        = space;
        player.currentMovement = 0.0f;

        this.currentState = btFunctionNode.NODE_STATE.RUNNING;
        return(this.currentState);
    }
Пример #14
0
    public override NODE_STATE TreeExecute()
    {
        btPlayerTile player = btTileManager.instance.playerTile;

        btTile.DIRECTION forward = player.direction;

        for (int distance = 1; distance <= this.variable; ++distance)
        {
            btBoardSpace space = btTileManager.instance.GetSpaceInDirectionFromTile(player, forward, distance);
            if (space == null ||
                space.IsPassable() == false)
            {
                this.currentState = btFunctionNode.NODE_STATE.FAILED;
                return(this.currentState);
            }
        }

        this.currentState = btFunctionNode.NODE_STATE.SUCCEEDED;
        return(this.currentState);
    }
Пример #15
0
    /// <summary>
    /// Finds all tiles of the given type on the board
    /// </summary>
    /// <typeparam name="T">The tile type to find</typeparam>
    /// <returns>ANy tiles found</returns>
    public List <T> GetTilesOfType <T>() where T : btTile
    {
        List <T> tiles = new List <T>();

        for (int y = 0; y < instance.tilesVertical; ++y)
        {
            for (int x = 0; x < instance.tilesHorizontal; ++x)
            {
                btBoardSpace space = instance.tileGrid[y][x];
                foreach (btTile node in space.tiles)
                {
                    if (node as T != null)
                    {
                        tiles.Add(node as T);
                    }
                }
            }
        }
        return(tiles);
    }
Пример #16
0
    public override NODE_STATE TreeExecute()
    {
        btPlayerTile player = btTileManager.instance.playerTile;

        btTile.DIRECTION playerForward = player.direction;
        btBoardSpace     space1        = btTileManager.instance.GetSpaceInDirectionFromTile(player, playerForward, 1);

        if (space1 == null)           // Fail if there is no tile in front of the player
        {
            this.currentState = btFunctionNode.NODE_STATE.FAILED;
            return(this.currentState);
        }

        List <btBoulderTile> boulders = space1.GetTilesOfType <btBoulderTile>();

        if (boulders.Count == 0) // Fail if there is no boulder in front of the player
        {
            this.currentState = btFunctionNode.NODE_STATE.FAILED;
            return(this.currentState);
        }

        btBoardSpace space2 = btTileManager.instance.GetSpaceInDirectionFromTile(player, playerForward, 2);

        if (space2 == null || space2.IsPassable() == false)           // Fail if there is no tile in front of the boulder or it is impassable
        {
            this.currentState = btFunctionNode.NODE_STATE.FAILED;
            return(this.currentState);
        }

        btBoulderTile boulder = boulders[0];

        boulder.tileState       = btTile.TILESTATE.FORCE_MOVE;
        boulder.forceDirection  = player.direction;
        boulder.currentMovement = 0.0f;
        boulder.startSpace      = boulder.currentSpace;
        boulder.endSpace        = space2;

        this.currentState = btFunctionNode.NODE_STATE.RUNNING;
        return(this.currentState);
    }
Пример #17
0
    public override NODE_STATE TreeExecute()
    {
        btPlayerTile player = btTileManager.instance.playerTile;

        btTile.DIRECTION playerLeft = btTileManager.DirectionTurnedLeft(player.direction);
        btBoardSpace     space      = btTileManager.instance.GetSpaceInDirectionFromTile(player, playerLeft, 1);

        if (space == null || space.IsPassable() == false)
        {
            this.currentState = btFunctionNode.NODE_STATE.FAILED;
            return(this.currentState);
        }

        player.isTurning        = true;
        player.tileState        = btTile.TILESTATE.TURNING_LEFT;
        player.startingRotation = player.GetRotation();
        player.startSpace       = player.currentSpace;
        player.endSpace         = space;
        player.currentMovement  = 0.0f;

        this.currentState = btFunctionNode.NODE_STATE.RUNNING;
        return(this.currentState);
    }
Пример #18
0
    public override NODE_STATE TreeExecute()
    {
        btPlayerTile player = btTileManager.instance.playerTile;

        btTile.DIRECTION playerForward = player.direction;
        btBoardSpace     space1        = btTileManager.instance.GetSpaceInDirectionFromTile(player, playerForward, 1);

        if (space1 == null)           // Fail if there is no tile in front of the player
        {
            this.currentState = btFunctionNode.NODE_STATE.FAILED;
            return(this.currentState);
        }

        List <btBoulderTile> boulders = space1.GetTilesOfType <btBoulderTile>();

        if (boulders.Count == 0) // Fail if there is no boulder in front of the player
        {
            this.currentState = btFunctionNode.NODE_STATE.FAILED;
            return(this.currentState);
        }

        this.currentState = btFunctionNode.NODE_STATE.SUCCEEDED;
        return(this.currentState);
    }
Пример #19
0
    /// <summary>
    /// Finds the direction difference between two spaces
    /// </summary>
    /// <param name="_space1">The start space</param>
    /// <param name="_space2">The end space</param>
    /// <returns>The direction from the start space to the end space</returns>
    public btTile.DIRECTION GetDirectionBetweenSpaces(btBoardSpace _space1, btBoardSpace _space2)
    {
        int x = _space1.x - _space2.x;
        int y = _space1.y - _space2.y;

        if (x != 0)
        {
            x /= x;
        }
        if (y != 0)
        {
            y /= y;
        }

        if (x == 0 && y == 1)
        {
            return(btTile.DIRECTION.NORTH);
        }
        else if (x == 1 && y == 0)
        {
            return(btTile.DIRECTION.EAST);
        }
        else if (x == 0 && y == -1)
        {
            return(btTile.DIRECTION.SOUTH);
        }
        else if (x == -1 && y == 0)
        {
            return(btTile.DIRECTION.WEST);
        }
        else
        {
            Debug.Log("Uncaught movement between tiles (" + _space1.x + "," + _space1.y + ") -> (" + _space2.x + "," + _space2.y + "): (" + x + "," + y + ")");
            return(btTile.DIRECTION.NORTH);
        }
    }
Пример #20
0
    public bool TileUpdate()
    {
        switch (this.tileState)
        {
        case TILESTATE.NONE:
        {
            return(true);
        }

        case TILESTATE.MOVING_FORWARD:
        {
            bool finishedMoving = this.MoveForward(btTile.WALK_MOVE_SPEED);
            if (finishedMoving == true)
            {
                this.tileState = TILESTATE.NONE;
            }
            return(finishedMoving);
        }

        case TILESTATE.JUMPING:
        {
            if (btTile.JUMP_DURATION == 0.0f)
            {
                return(true);
            }

            this.jumpTimer += Time.deltaTime;
            float jumpDelta = jumpTimer / btTile.JUMP_DURATION;
            this.SetPosition(btTileManager.instance.GetPositionBetweenSpaces(this.startSpace, this.endSpace, jumpDelta));
            if (jumpTimer >= btTile.JUMP_DURATION)
            {
                btTileManager.instance.AddTileMovementInDirection(this, this.direction, 2);
                this.jumpTimer  = 0.0f;
                this.tileState  = TILESTATE.NONE;
                this.startSpace = this.endSpace = null;
                return(true);
            }
            return(false);
        }

        case TILESTATE.TURNING_LEFT:
        {
            if (this.isTurning == true)
            {
                if (this.SpinLeft() == true)
                {
                    this.isTurning = false;
                }
            }
            else
            {
                bool doneMoving = this.MoveForward(btTile.WALK_MOVE_SPEED);
                if (doneMoving == true)
                {
                    this.tileState = TILESTATE.NONE;
                    return(true);
                }
            }
            return(false);
        }

        case TILESTATE.TURNING_RIGHT:
        {
            if (this.isTurning == true)
            {
                if (this.SpinRight() == true)
                {
                    this.isTurning = false;
                }
            }
            else
            {
                bool doneMoving = this.MoveForward(btTile.WALK_MOVE_SPEED);
                if (doneMoving == true)
                {
                    this.tileState = TILESTATE.NONE;
                    return(true);
                }
            }
            return(false);
        }

        case TILESTATE.FORCE_MOVE:
        {
            bool doneMoving = this.MoveInDirection(btTile.FORCE_MOVE_SPEED, this.forceDirection);
            if (doneMoving == true)
            {
                this.tileState = TILESTATE.NONE;
            }
            return(doneMoving);
        }

        case TILESTATE.SLIDING:
        {
            bool doneMoving = this.MoveInDirection(btTile.SLIDE_MOVE_SPEED, this.slideDirection);
            if (doneMoving == true)
            {
                this.tileState = TILESTATE.NONE;
            }
            return(doneMoving);
        }

        default:
        {
            Debug.LogError("Uncaught tile state \"" + this.tileState + "\"");
            return(true);
        }
        }
    }
Пример #21
0
 public Vector2 GetSpacePosition(btBoardSpace _space)
 {
     return(this.BoardIndexToPosition(_space.x, _space.y));
 }
Пример #22
0
 public Vector2 GetPositionBetweenSpaces(btBoardSpace _space1, btBoardSpace _space2, float _d)
 {
     return(this.GetPositionBetweenSpaces(_space1.x, _space1.y, _space2.x, _space2.y, _d));
 }
Пример #23
0
 /// <summary>
 /// Adds a movement for the input tile to the input space, using the movement direction
 /// </summary>
 /// <param name="_tile">The tile to be moved</param>
 /// <param name="_space">The space to move the tile to</param>
 /// <param name="_movementDirection">The direction in which the tile is being moved</param>
 public void AddTileMovementToSpace(btTile _tile, btBoardSpace _space, btTile.DIRECTION _movementDirection)
 {
     this.AddTileMovement(_tile, _space.x, _space.y, _movementDirection);
 }