Esempio n. 1
0
    ///Make the player move one space in the direction they are facing
    private IEnumerator moveForward()
    {
        Vector3 movement = getForwardVector();

        bool ableToMove = false;

        //without any movement, able to move should stay false
        if (movement != Vector3.zero)
        {
            //check destination for objects/transparents
            Collider   objectCollider      = null;
            Collider   transparentCollider = null;
            Collider[] hitColliders        = Physics.OverlapSphere(transform.position + movement + new Vector3(0, 0.5f, 0),
                                                                   0.4f);
            if (hitColliders.Length > 0)
            {
                for (int i = 0; i < hitColliders.Length; i++)
                {
                    if (hitColliders[i].name.ToLowerInvariant().Contains("_object"))
                    {
                        objectCollider = hitColliders[i];
                    }
                    else if (hitColliders[i].name.ToLowerInvariant().Contains("_transparent"))
                    {
                        transparentCollider = hitColliders[i];
                    }
                }
            }

            if (objectCollider != null)
            {
                //send bump message to the object's parent object
                objectCollider.transform.parent.gameObject.SendMessage("bump", SendMessageOptions.DontRequireReceiver);
            }
            else
            {
                //if no objects are in the way
                int destinationTileTag = destinationMap.getTileTag(transform.position + movement);

                RaycastHit bridgeHit =
                    MapCollider.getBridgeHitOfPosition(transform.position + movement + new Vector3(0, 0.1f, 0));
                if (bridgeHit.collider != null || destinationTileTag != 1)
                {
                    //wall tile tag

                    if (bridgeHit.collider == null && !surfing && destinationTileTag == 2)
                    {
                        //(water tile tag)
                    }
                    else
                    {
                        if (surfing && destinationTileTag != 2f)
                        {
                            //disable surfing if not headed to water tile
                            updateAnimation("walk", walkFPS);
                            speed   = walkSpeed;
                            surfing = false;
                            StartCoroutine("dismount");
                            BgmHandler.main.PlayMain(accessedAudio, accessedAudioLoopStartSamples);
                        }

                        if (destinationMap != currentMap)
                        {
                            //if moving onto a new map
                            currentMap          = destinationMap;
                            accessedMapSettings = destinationMap.gameObject.GetComponent <MapSettings>();
                            if (accessedAudio != accessedMapSettings.getBGM())
                            {
                                //if audio is not already playing
                                accessedAudio = accessedMapSettings.getBGM();
                                accessedAudioLoopStartSamples = accessedMapSettings.getBGMLoopStartSamples();
                                BgmHandler.main.PlayMain(accessedAudio, accessedAudioLoopStartSamples);
                            }
                            destinationMap.BroadcastMessage("repair", SendMessageOptions.DontRequireReceiver);
                            if (accessedMapSettings.mapNameBoxTexture != null)
                            {
                                MapName.display(accessedMapSettings.mapNameBoxTexture, accessedMapSettings.mapName,
                                                accessedMapSettings.mapNameColor);
                            }
                            Debug.Log(destinationMap.name + "   " + accessedAudio.name);
                        }

                        if (transparentCollider != null)
                        {
                            //send bump message to the transparents's parent object
                            transparentCollider.transform.parent.gameObject.SendMessage("bump",
                                                                                        SendMessageOptions.DontRequireReceiver);
                        }

                        ableToMove = true;
                        yield return(StartCoroutine(move(movement)));
                    }
                }
            }
        }

        //if unable to move anywhere, then set moving to false so that the player stops animating.
        if (!ableToMove)
        {
            Invoke("playBump", 0.05f);
            moving    = false;
            animPause = true;
        }
    }
Esempio n. 2
0
    /*/
     * /// Checks the position for player and returns the next position to check
     * private Vector3 checkPosition(Vector3 position){
     *      playerAtPosition = false;
     *      positionIsBlocked = false;
     *
     *      Vector3 nextPosition = position;
     *
     *
     *      Vector3 movement = new Vector3(0,0,0);
     *      if(trainer.direction == 0){
     *              movement = new Vector3(0,0,1f);}
     *      else if(trainer.direction == 1){
     *              movement = new Vector3(1f,0,0);}
     *      else if(trainer.direction == 2){
     *              movement = new Vector3(0,0,-1f);}
     *      else if(trainer.direction == 3){
     *              movement = new Vector3(-1f,0,0);}
     *
     *      float currentTileTag = trainer.currentMap.getTileTag(position);
     *      //check current tileTag for stairs(3)
     *      if(Mathf.Floor(currentTileTag) == 3f){
     *              //check if stair direction is same as trainer direction
     *              if(Mathf.Round(((currentTileTag) - 3f)*10) == trainer.direction){
     *                      movement += new Vector3(0,0.5f,0);}
     *              else{
     *                      float flippedDirection = trainer.direction + 2;
     *                      if(flippedDirection > 3){
     *                              flippedDirection -= 4;}
     *                      if(Mathf.Round(((currentTileTag) - 3f)*10) == flippedDirection){
     *                              movement += new Vector3(0,-0.5f,0);}
     *              }
     *      }
     *      float destinationTileTag = trainer.currentMap.getTileTag(position+movement);
     *      //check destination tileTag for stairs(3)
     *      if(Mathf.Floor(destinationTileTag) == 3f){
     *              if(Mathf.Round(((destinationTileTag) - 3f)*10) == trainer.direction){
     *                      movement += new Vector3(0,0.5f,0);}
     *      } //else if its stair-top(4)
     *      else if(Mathf.Floor(destinationTileTag) == 4f){
     *              if(Mathf.Round(((destinationTileTag) - 4f)*10) == trainer.direction){
     *                      movement += new Vector3(0,-0.5f,0);}
     *      }
     *
     *      destinationTileTag = trainer.currentMap.getTileTag(position+movement);
     *      //check destination tileTag for impassibles
     *      if(Mathf.Floor(destinationTileTag) == 0f || Mathf.Floor(destinationTileTag) == 2f){
     *              positionIsBlocked = true;}
     *      else if(trainer.trainerSurfing){ //if a surf trainer, normal tiles are impassible
     *              if(Mathf.Floor(destinationTileTag) == 1f || Mathf.Floor(destinationTileTag) == 5f){
     *                      positionIsBlocked = true;}
     *      }
     *      else if(Mathf.Floor(destinationTileTag) == 6f){
     *              positionIsBlocked = true;}
     *
     *      nextPosition = position + movement;
     *      //check nextPosition for objects/followers and check for the player
     *      Collider[] hitColliders = Physics.OverlapSphere (nextPosition, 0.2f);
     *      if (hitColliders.Length > 0){
     *              for (int i = 0; i < hitColliders.Length; i++){
     *                      if(hitColliders[i].name == "Player_Transparent"){
     *                              playerAtPosition = true;
     *                              i = hitColliders.Length;}
     *                      else if(hitColliders[i].name == "Follower_Transparent" ||
     *                              hitColliders[i].name.Contains("_Object")){
     *                              positionIsBlocked = true;
     *                              i = hitColliders.Length;}
     *              }
     *      }
     *
     *      return nextPosition;
     * }
     * //*/

    /// Checks the position for player and returns the next position to check
    private Vector3 checkPosition(Vector3 position)
    {
        playerAtPosition  = false;
        positionIsBlocked = false;

        Vector3 nextPosition = position;

        Vector3 forwardsVector = new Vector3(0, 0, 0);

        if (trainer.direction == 0)
        {
            forwardsVector = new Vector3(0, 0, 1f);
        }
        else if (trainer.direction == 1)
        {
            forwardsVector = new Vector3(1f, 0, 0);
        }
        else if (trainer.direction == 2)
        {
            forwardsVector = new Vector3(0, 0, -1f);
        }
        else if (trainer.direction == 3)
        {
            forwardsVector = new Vector3(-1f, 0, 0);
        }

        Vector3 movement = forwardsVector;


        //Check destination map																	//0.5f to adjust for stair height
        MapCollider destinationMap = trainer.currentMap;

        //cast a ray directly downwards from the position directly in front of the npc			//1f to check in line with player's head
        RaycastHit[] mapHitColliders = Physics.RaycastAll(position + movement + new Vector3(0, 1.5f, 0), Vector3.down);
        RaycastHit   mapHit          = new RaycastHit();

        //cycle through each of the collisions
        if (mapHitColliders.Length > 0)
        {
            for (int i = 0; i < mapHitColliders.Length; i++)
            {
                //if a collision's gameObject has a mapCollider, it is a map. set it to be the destination map.
                if (mapHitColliders[i].collider.gameObject.GetComponent <MapCollider>() != null)
                {
                    mapHit         = mapHitColliders[i];
                    destinationMap = mapHit.collider.gameObject.GetComponent <MapCollider>();
                    i = mapHitColliders.Length;
                }
            }
        }

        //check for a bridge at the destination
        RaycastHit bridgeHit = MapCollider.getBridgeHitOfPosition(position + movement + new Vector3(0, 1.5f, 0));

        if (bridgeHit.collider != null)
        {
            //modify the forwards vector to align to the bridge.
            movement -= new Vector3(0, (position.y - bridgeHit.point.y), 0);
        }
        //if no bridge at destination
        else if (mapHit.collider != null)
        {
            //modify the forwards vector to align to the mapHit.
            movement -= new Vector3(0, (position.y - mapHit.point.y), 0);
        }


        float currentSlope     = Mathf.Abs(MapCollider.getSlopeOfPosition(position, trainer.direction));
        float destinationSlope = Mathf.Abs(MapCollider.getSlopeOfPosition(position + forwardsVector, trainer.direction));
        float yDistance        = Mathf.Abs((position.y + movement.y) - position.y);

        yDistance = Mathf.Round(yDistance * 100f) / 100f;

        //if either slope is greater than 1 it is too steep.
        if (currentSlope <= 1 && destinationSlope <= 1)
        {
            //if yDistance is greater than both slopes there is a vertical wall between them
            if (yDistance <= currentSlope || yDistance <= destinationSlope)
            {
                //check destination tileTag for impassibles
                int destinationTileTag = destinationMap.getTileTag(position + movement);
                if (destinationTileTag == 1)
                {
                    positionIsBlocked = true;
                }
                else
                {
                    if (trainer.trainerSurfing)                     //if a surf trainer, normal tiles are impassible
                    {
                        if (destinationTileTag != 2)
                        {
                            positionIsBlocked = true;
                        }
                    }
                    else                      //if not a surf trainer, surf tiles are impassible
                    {
                        if (destinationTileTag == 2)
                        {
                            positionIsBlocked = true;
                        }
                    }
                }

                //check destination for objects/player/follower
                Collider[] hitColliders = Physics.OverlapSphere(position + movement, 0.4f);
                if (hitColliders.Length > 0)
                {
                    for (int i = 0; i < hitColliders.Length; i++)
                    {
                        if (hitColliders[i].name == "Player_Transparent")
                        {
                            playerAtPosition = true;
                        }
                        else if (hitColliders[i].name == "Follower_Transparent" ||
                                 hitColliders[i].name.ToLowerInvariant().Contains("_object"))
                        {
                            positionIsBlocked = true;
                        }
                    }
                }
            }
        }
        return(position + movement);
    }