Exemple #1
0
    private Vector3 getTarget(GameObject tileGroup, MoveType move, TileType movedTile)
    {
        //For down we need highest X / For up lowest X
        //For Left we need lowest Z / For right highest Z
        Vector3 target = new Vector3(0.0f, -1.1f, 0.0f); //hopefully this is ok

        foreach (Transform tile in tileGroup.transform)
        {
            tile.GetComponent <Human>().puzzle = puzzle;
            tile.GetComponent <Human>().tile   = movedTile;
            switch (move)
            {
            case MoveType.Down:
                if (tile.position.x > target.x || target.y == -1.1f)
                {
                    target    = tile.position;
                    target.x += 0.5f;
                }
                break;

            case MoveType.Up:
                if (tile.position.x < target.x || target.y == -1.1f)
                {
                    target    = tile.position;
                    target.x -= 0.5f;
                }
                break;

            case MoveType.Left:
                if (tile.position.z < target.z || target.y == -1.1f)
                {
                    target    = tile.position;
                    target.z -= 0.5f;
                }
                break;

            case MoveType.Right:
                if (tile.position.z > target.z || target.y == -1.1f)
                {
                    target    = tile.position;
                    target.z += 0.5f;
                }
                break;
            }
        }
        return(target);
    }
Exemple #2
0
    //make this a coroutine with actual movement, will collide with tiles and make them visible (maybe?!)
    public IEnumerator MoveToTile(TileType tile, bool nomessage = false)
    {
        //UpdateThreats();
        //Am I going to a tile that has a piece?
        if ((tile.CurrentPiece != null) && (!nomessage))
        {
            Debug.Log("Found a piece at the tile, going there instead");
            tile = tile.CurrentPiece.GetComponent <TileType>();
        }

        //saving original position (for message)
        Transform OriginPosition = CurrentActivePiece.CurrentTile;
        Vector3   Origin         = OriginPosition.position;

        //Debug.Log("destination tile type: "+tile.GetComponent<TileType>().Type);
        WaitingForMove = true;
        CurrentActivePiece.CurrentTile.GetComponent <Collider>().enabled = true;
        CurrentActivePiece.turn++;
        Vector3 destination = new Vector3(tile.transform.position.x, tile.transform.position.y, CurrentActivePiece.transform.position.z);

        CurrentActivePiece.HideDestinations();
        Vector3 movestep = (destination - CurrentActivePiece.transform.position) / (Vector3.Distance(destination, CurrentActivePiece.transform.position) * 3);

        if (Vector3.Distance(destination, CurrentActivePiece.transform.position) > 4)
        {
            GetComponent <AudioSource>().clip = sliding;
            GetComponent <AudioSource>().Play();
        }
        // CurrentActivePiece.SetActive(true);
        Debug.Log("About to get moving");
        while ((Vector3.Distance(destination, CurrentActivePiece.transform.position) > 0.2f))
        {
            //Debug.Log("distance: "+Vector3.Distance(destination, CurrentActivePiece.transform.position));
            CurrentActivePiece.transform.position = CurrentActivePiece.transform.position + movestep;
            yield return(new WaitForSeconds(0.01f));
            //UpdateVisibility();
        }
        Debug.Log("finished moving");
        //yield return new WaitForSeconds(0.01f);
        GetComponent <AudioSource>().clip = putdown;
        GetComponent <AudioSource>().Play();
        CurrentActivePiece.transform.position = destination;
        //did we land on stairs?



        //Start preparing the displayed message
        //Describe the move in chess notation


        string PieceType = CurrentActivePiece.PieceType;

        //string OriginCoordinate = OriginPosition.position.x.ToString() + OriginPosition.position.y.ToString();

        string PieceSymbol      = CurrentActivePiece.PieceSymbol;
        string OriginCoordinate = OriginPosition.position.x.ToString() + OriginPosition.position.y.ToString();

        if (tile == null)
        {
            tile = TileList[0];
        }

        string DestinationCoordinate = tile.transform.position.x.ToString() + tile.transform.position.y.ToString();
        string ActionSymbol          = ""; // will be set to x or * by EatPiece()
        string DestinationPiece      = "";
        string InCheck   = "";
        string Promotion = "";

        //check whether piece has enemy king in check



        TempMessage = ""; //reset message
        // Calculate notation for the squares

        string startRank      = (OriginPosition.position.y).ToString();
        int    startFileIndex = Mathf.FloorToInt(OriginPosition.position.x);
        string startFile      = files[startFileIndex];

        string destRank      = (tile.transform.position.y).ToString();
        int    destFileIndex = Mathf.FloorToInt(tile.transform.position.x);
        string destFile      = files[destFileIndex];

        //Debug.Log("Moved piece's NewQueen is " + CurrentActivePiece.NewQueen + ", type is " + CurrentActivePiece.PieceType);



        // UpdateThreats();
        //was this a piece? then eat it!

        if (tile.transform.tag == "piece")
        {
            if (tile.GetComponent <Piece>().PieceColor != "red")
            {
                tile.GetComponent <Piece>().eaten = true;
            }

            ActionSymbol = EatPiece(tile.GetComponent <Piece>()); // SETS TempMessage with Attack Flavour

            Debug.Log("eat piece at destination");
            DestinationPiece = tile.GetComponent <Piece>().PieceType;
        }
        WaitingForMove = false;
        foreach (Piece piece in PieceList)
        {
            piece.SetActive(true);
        }
        yield return(new WaitForSeconds(0.05f));

        // MOVED Pawn promotion above check calculation between a P promoting to Q can put the K in check, so should
        // calculate its possible attacks based on that.
        //will not do the queening if the pawn was eaten
        Debug.Log("is this new queen?");
        if ((CurrentActivePiece.NewQueen) && (!CurrentActivePiece.eaten) && (ActionSymbol != ".") && (ActionSymbol != "*"))
        {
            Debug.Log("yes");
            CurrentActivePiece.Queen();
            CurrentActivePiece = PieceList[0];
            //CurrentActivePiece.SetActive(true);
            CurrentActivePiece.FindAvailableDestinations();
            OriginPosition = CurrentActivePiece.CurrentTile;
            if (CurrentActivePiece.human)
            {
                LastSelectedPiece = CurrentActivePiece = PieceList[0];
            }


            //OriginPosition.position = Origin;
            //ChangeTurn();


            Promotion = "=Q";
        }

        //waiting for all collisions to register
        if ((tile != null) && (tile.GetComponent <TileType>().Type == 3))
        {
            Debug.Log("landed on stairs");

            if (CurrentActivePiece.PieceColor == "white")
            {
                LastSelectedPiece = null;
                changinglevel     = true;
                ChangeLevel();
            }
            else
            {
                //remove king
                PieceList.Remove(CurrentActivePiece);
                CurrentActivePiece.transform.position = new Vector3(10000, 10000, 10000);
                //CurrentActivePiece = PieceList[0];
            }
        }
        yield return(new WaitForSeconds(0.1f));



        UpdateThreats();
        //yield return new WaitForSeconds(2.2f);
        //UpdateThreats();
        UpdateVisibility();
        foreach (Piece piece in PieceList)
        {
            piece.SetActive(false);
        }



        if (CurrentActivePiece.check)
        {
            InCheck = "+";
        }
        // The eating/attacking message is defined in TempMessage by the EatPiece() function
        //Now building the full notation
        //string FullNotation = "(" + PieceType + OriginCoordinate + ActionSymbol + DestinationPiece + DestinationCoordinate + InCheck + ") ";



        if (ActionSymbol != "" && PieceType == "pawn")
        {
            // This is a pawn attack/capture, its symbol should be its current square
            PieceSymbol = startFile + startRank;
        }

        string MoveNotation = PieceSymbol + ActionSymbol + destFile + destRank + Promotion + InCheck;

        //Now assembling complete message
        string FullMoveMessage;

        if (!nomessage)
        {
            if (CurrentActivePiece.human)
            {
                Turn++;
                FullMoveMessage = Turn.ToString() + ". " + MoveNotation;
                string AttackFlavour = TempMessage;
                if (AttackFlavour != "")
                {
                    FullMoveMessage += " (" + AttackFlavour + ")";
                }

                WhiteMoveMessage = FullMoveMessage; //keeping it for black's turn
            }
            else
            {
                FullMoveMessage = WhiteMoveMessage + " " + MoveNotation;
                string AttackFlavour = TempMessage;
                if (AttackFlavour != "")
                {
                    string TempFullMoveMessage = FullMoveMessage + " (" + AttackFlavour + ")";
                    while (TempFullMoveMessage.Length > statusLineLength)
                    {
                        Debug.Log("Too long: " + TempFullMoveMessage + " (" + TempFullMoveMessage.Length + " > " + statusLineLength);
                        if (ActionSymbol == "*")
                        {
                            AttackFlavour = GenerateAttackMessage(tile.GetComponent <Piece>(), hits);
                        }
                        else if (ActionSymbol == ".")
                        {
                            AttackFlavour = GenerateAttackMessage(tile.GetComponent <Piece>(), misses);
                        }
                        TempFullMoveMessage = WhiteMoveMessage + " " + MoveNotation + " (" + AttackFlavour + ")";
                    }
                    FullMoveMessage = TempFullMoveMessage;
                }
            }

            //display message
            DisplayMsg(FullMoveMessage);
        }



        //if attack but not kill, send piece back to where it came from!
        if (WaitingForMoveBack)
        {
            WaitingForMove = true;
            Debug.Log("going back to where I came from");

            WaitingForMoveBack = false;
            TileType temptile = FindTileByPosition(Origin);
            if (temptile == null)
            {
                temptile = OriginPosition.GetComponent <TileType>();
            }
            StartCoroutine(MoveToTile(temptile, true));
            Debug.Log("waiting to get back");
            yield break;

            /* while (WaitingForMove==true)
             * {
             *   //Debug.Log("WaitingForMove:" + WaitingForMove);
             *   yield return new WaitForSeconds(0.001f);
             *   //Debug.Log("WaitingForMove2:" + WaitingForMove);
             *
             * }*/

            Debug.Log("got back, finish move function");
        }
        WaitingForMove = false;
        UpdateVisibility();
        //UpdateThreats();

        if ((!changinglevel) && (!gameover))
        {
            ChangeTurn();
        }
    }