Exemplo n.º 1
0
    private void detectplayer()
    {
        if (playerDetected == false)
        {
            currentPosition = this.transform.position;
        }
        //---------------------------------------------------
        if ((playerTransform.position.x - this.transform.position.x <= xRangeView && playerTransform.position.y - this.transform.position.y <= yRangeView && playerTransform.position.x - this.transform.position.x >= -xRangeView && playerTransform.position.y - this.transform.position.y >= -yRangeView) && (!gameController.getrockState() && rockHideWorks == true || !gameController.gethideState() && handHideWorkd == true || playerDetected == true)) //Detects in inside imaginary box
        {
            float distance = playerTransform.position.x - this.transform.position.x;                                                                                                                                                                                                                                                                                                                                                                            //sees the player distance
            float dir      = 0;                                                                                                                                                                                                                                                                                                                                                                                                                                 //for the direction of the enemy
            if (distance > 0)
            {
                dir = 1;

                //transform.rotation = Quaternion.Euler(0, 180, 0);
            }
            if (distance < 0)
            {
                dir = -1;

                //transform.rotation = Quaternion.Euler(0, 0, 0);
            }
            transform.position = new Vector3(transform.position.x + enemySpeed * dir, transform.position.y, 0);//moves in the direction in speed parameter
            playerDetected     = true;
        }
        else
        {
            if ((currentPosition.x != this.transform.position.x) && playerDetected)
            {
                if (playerTransform.position.x > currentPosition.x + maxRangeFromStart || playerTransform.position.x < currentPosition.x - maxRangeFromStart)
                {
                    float distance = currentPosition.x - this.transform.position.x; //sees the objective to move
                    float dir      = 0;                                             //for the direction of the enemy
                    if (distance > 0)
                    {
                        dir = 1;
                    }
                    if (distance < 0)
                    {
                        dir = -1;
                    }
                    transform.position = new Vector3(transform.position.x + enemySpeed * dir, transform.position.y, 0);//moves in the direction in speed parameter
                }
            }
            else
            {
                if (playerTransform.position.x < currentPosition.x + xRangeMovement_Right || playerTransform.position.x > currentPosition.x - xRangeMovement_Left)
                {
                    if (this.transform.position.x >= startPosition.x + xRangeMovement_Right)
                    {
                        dir_routine          = -1;
                        transform.localScale = new Vector2(1, transform.localScale.y);
                    }
                    if (this.transform.position.x <= startPosition.x - xRangeMovement_Left)
                    {
                        dir_routine          = +1;
                        transform.localScale = new Vector2(-1, transform.localScale.y);
                    }
                    transform.position = new Vector3(transform.position.x + enemySpeed * dir_routine, transform.position.y, 0);//moves in the direction in speed parameter
                }
            }
            if (this.transform.position.x >= currentPosition.x - 1 || this.transform.position.x <= currentPosition.x + 1)
            {
                playerDetected = false;
            }
        }
    }
Exemplo n.º 2
0
    private void Update()
    {
        float x = Input.GetAxis("Horizontal"); //Input movement

        //Swap sprite and can't move when enter on ice or hide ability
        if (x > 0 && !blockDirectionIce && !gameController.getfreezCam())
        {
            transform.localScale = new Vector3(1, transform.localScale.y, transform.localScale.z);
            directionIce         = false;
            stopped = false;
        }
        else if (x < 0 && !blockDirectionIce && !gameController.getfreezCam())
        {
            transform.localScale = new Vector3(-1, transform.localScale.y, transform.localScale.z);
            directionIce         = true;
            stopped = false;
        }
        //Hide condition
        else if (speed == 0)
        {
            stopped = true;
        }

        //RockForm activation
        try
        {
            if (Input.GetKey(KeyCode.Space) && stopped && !ice)
            {
                //Change state
                gameController.setfreezeCamera(true);
                gameController.setrockState(true);
                eyesClosed = true;
            }
            else
            {
                gameController.setfreezeCamera(false);
                gameController.setrockState(false);
                eyesClosed = false;
            }
        }
        catch (Exception e)
        {
            print(e);
        }
        finally
        {
        }

        //Close eyes form activation
        try
        {
            if (Input.GetKey(KeyCode.Q) && stopped && !ice)
            {
                gameController.setfreezeCamera(true);
                gameController.sethideState(true);
                eyesClosed = true;
            }
            else
            {
                gameController.sethideState(false);
            }
        }
        catch (Exception e)
        {
            print(e);
        }
        finally
        {
        }

        //Jump activation
        jump = (Input.GetAxis("Jump") > 0); //devuelve true cuando se está saltando, sino, no se devuelve nada
        if (!jump)
        {
            jumpPressed = false;
        }

        try //Movement
        {
            if (grounded)
            {
                speed   = (x * maxSpeed) * (moveReduction);
                running = (x * sprintSpeed + speed) * (moveReduction);
            }
        }
        catch (Exception e)
        {
            print("SpeedPlayer");
            print(e);
        }

        try //Try to invoke damage functions (InvokeRepeatings)
        {
            if (insideFire)
            {
                if (!invokeIsCalled)
                {
                    InvokeRepeating("fireDamageTiming", 0f, damageTime);
                }
            }
            if (insideBrambles)
            {
                if (!invokeIsCalled)
                {
                    InvokeRepeating("bramblesDamageTiming", 0f, damageTime);
                    invokeIsCalled = true;
                }
            }
        }
        catch (Exception e)
        {
            print("Damage Functions");
            print(e);
        }
        finally
        {
            if (insideFire || insideBrambles)
            {
                invokeIsCalled = true;
            }
        }

        try //haunted condition
        {
            if (gameController.getbeingHunted() && !huntedState)
            {
                moveReduction += beingHuntedSpeed;
                print(moveReduction);
                huntedState = true;
            }
            else if (!gameController.getbeingHunted() && huntedState)
            {
                moveReduction -= beingHuntedSpeed;
                print(moveReduction);
                huntedState = false;
            }
        }
        catch (Exception e)
        {
            print("BeingHunted");
            print(e);
        }
        finally
        {
        }

        anim.SetFloat("speed", Mathf.Abs(x));                     //Run animation
        anim.SetBool("grounded", grounded);                       //Jump animation
        anim.SetBool("hidestate", gameController.getrockState()); //Hide animation
        animator.SetBool("eyes", eyesClosed);                     //Closed eyes animation
        anim.SetBool("eyestate", gameController.gethideState());
    }