Esempio n. 1
0
    // Logic of attacked received. Deactivates temporary all the colliders.
    public void Attacked()
    {
        if (!attacked)
        {
            rigid.isKinematic = true;
            attacked          = true;
            speed             = attackSpeed;
            states            = enemy_states.HITTED;
            --lives;
            collider.enabled      = false;
            swordCollider.enabled = false;
            headCollider.enabled  = false;
            SoundManager.instance.PlaySingle(hitSound);

            if (lives <= 0)
            {
                states = enemy_states.DEATH;
                fieldOfView.enabled = false;
                isInAttack          = false;
                InvokeRepeating("TiltModel", 0f, 0.1f);
                ScoreManager.instance.EnemyDefeated();
            }
            else
            {
                StartCoroutine(ActivateColliders(0.2f));
            }
        }

        // To impulse player from enemy
        player.ReactToAttack(transform.position.x);
    }
Esempio n. 2
0
    // Trigger of hit animation
    public void HittedTrigger()
    {
        if (states != enemy_states.DEATH)
        {
            rigid.isKinematic    = false;
            collider.enabled     = true;
            fieldOfView.enabled  = true;
            headCollider.enabled = true;

            if ((transform.rotation.eulerAngles.y < 270.0f + 1) && (transform.rotation.eulerAngles.y > 270.0f - 1))
            {
                if (playerPos.position.x > transform.position.x)
                {
                    transform.Rotate(0.0f, -(transform.eulerAngles.y - 90), 0.0f);
                }
            }
            else
            {
                if (playerPos.position.x < transform.position.x)
                {
                    transform.Rotate(0.0f, 270 - transform.eulerAngles.y, 0.0f);
                }
            }

            speed  = chaseSpeed;
            states = enemy_states.CHASE;
        }
    }
Esempio n. 3
0
 // Logic in kick
 public void Kick()
 {
     player.DecrementLives(meleeDamage);
     sight  = false;
     speed  = waitSpeed;
     states = enemy_states.WAIT;
 }
Esempio n. 4
0
    // Trigger that detect player and change the state to Shoot
    void OnTriggerStay(Collider coll)
    {
        // If is the player and between player and archer isn't anything then shoot him
        if (coll.CompareTag("Player"))
        {
            //Ray from origin to player
            origin    = transform.position;
            origin.y += transform.localScale.y * 0.75f;
            ray       = new Ray(origin, player.transform.position - origin);

            if ((Vector3.Distance(player.transform.position, transform.position) <= maxSightMeleeAttack) && (states != enemy_states.MELEE_ATTACK))
            {
                // If distance is low changes to melee attack
                speed       = meleeAttackSpeed;
                timePerKick = 0.0f;
                states      = enemy_states.MELEE_ATTACK;
            }
            else if (Physics.Raycast(ray, out hit, maxSightShoot, layerMask) && (states != enemy_states.HITTED) && (hit.collider.gameObject.CompareTag("Player")))
            {
                // Else changes to Shoot
                sight  = true;
                speed  = shootSpeed;
                states = enemy_states.SHOOT;
            }
            else if (sight == true)
            {
                sight = false;
                animator.SetBool("Shoot", false);
            }
        }
    }
Esempio n. 5
0
 // Trigger of hit animation
 public void HittedTrigger()
 {
     if (states != enemy_states.DEATH)
     {
         speed  = chaseSpeed;
         states = enemy_states.CHASE;
     }
 }
Esempio n. 6
0
    // Trigger of end attack, disables colliders of axes
    public void AttackTrigger()
    {
        axeCollider1.enabled = false;
        axeCollider2.enabled = false;
        isInAttack           = false;

        if (attackSucces)
        {
            speed  = walkSpeed;
            states = enemy_states.RETURNING;
        }
    }
Esempio n. 7
0
 void OnCollisionEnter(Collision coll)
 {
     // If collides with glitch, enemy turns kinematic to avoid push
     if ((states != enemy_states.DEATH) && (sight == true) && (coll.contacts[0].otherCollider.CompareTag("Player")))
     {
         rigid.isKinematic = true;
     }
     else if ((sight == false) && (coll.contacts[0].otherCollider.CompareTag("Player")))
     {
         rigid.isKinematic = true;
         states            = enemy_states.TURN;
     }
 }
Esempio n. 8
0
 // Trigger of turn animation
 public void TurnTrigger()
 {
     if ((player.transform.position.x > transform.position.x) && (transform.eulerAngles.y == 270.0f))
     {
         transform.eulerAngles = new Vector3(0.0f, 90.0f, 0.0f);
         rotationTime          = 0.5f;
     }
     else if ((player.transform.position.x < transform.position.x) && (transform.eulerAngles.y == 90.0f))
     {
         transform.eulerAngles = new Vector3(0.0f, 270.0f, 0.0f);
         rotationTime          = 0.5f;
     }
     speed  = waitSpeed;
     states = enemy_states.WAIT;
 }
Esempio n. 9
0
    // Trigger in recover animation
    public void RecoverTrigger()
    {
        rigid.isKinematic = false;
        if ((transform.rotation.eulerAngles.y < 270.0f + 1) && (transform.rotation.eulerAngles.y > 270.0f - 1))
        {
            transform.Rotate(0.0f, -(transform.eulerAngles.y - 90), 0.0f);
        }
        else
        {
            transform.Rotate(0.0f, 270 - transform.eulerAngles.y, 0.0f);
        }

        sight  = false;
        speed  = walkSpeed;
        states = enemy_states.RETURNING;
    }
Esempio n. 10
0
 // Logic of attack
 public void Attack()
 {
     isInAttack = false;
     player.DecrementLives(damageAttack);
     sight = false;
     if (player.lives > 0)
     {
         speed  = searchSpeed;
         time   = searchingTime;
         states = enemy_states.SEARCH;
     }
     else
     {
         speed  = patrolSpeed;
         time   = waitTime;
         states = enemy_states.WAIT;
     }
 }
Esempio n. 11
0
 // Trigger for when she loses of sight Glitch
 void OnTriggerExit(Collider coll)
 {
     if (coll.CompareTag("Player"))
     {
         sight = false;
         if (Vector3.Distance(player.transform.position, transform.position) < maxSightShoot)
         {
             // If the distance is lower than maxSightShoot turns
             states = enemy_states.TURN;
         }
         else
         {
             // Else Glitch escape and she wait
             speed  = waitSpeed;
             states = enemy_states.WAIT;
         }
     }
 }
Esempio n. 12
0
 // Logic in death of the enemy. Deactivates all the colliders.
 public void Defeated()
 {
     sight  = false;
     speed  = waitSpeed;
     states = enemy_states.DEATH;
     animator.SetBool("Attack", false);
     animator.SetBool("Shoot", false);
     rigid.isKinematic    = true;
     collider.enabled     = false;
     kickCollider.enabled = false;
     fieldOfView.enabled  = false;
     headCollider.enabled = false;
     SoundManager.instance.PlaySingle(hitSound);
     InvokeRepeating("TiltModel", 0f, 0.1f);
     // To impulse player from enemy
     player.ReactToAttack(transform.position.x);
     ScoreManager.instance.EnemyDefeated();
 }
Esempio n. 13
0
 // Trigger that detect collisions with patrol points and limit points
 void OnTriggerEnter(Collider coll)
 {
     // Only detect collisions with patrol points when enemy is patrolling
     if ((coll.gameObject.CompareTag("PatrolPoint")) && (states == enemy_states.PATROL))
     {
         speed  = patrolSpeed;
         states = enemy_states.WAIT;
         time   = waitTime;
     }
     // If is in a limit he stops and search glitch
     else if ((coll.gameObject.CompareTag("LimitPoint")) && (states != enemy_states.SEARCH))
     {
         speed          = searchSpeed;
         states         = enemy_states.SEARCH;
         time           = searchingTime;
         sight          = false;
         isInLimitPoint = true;
     }
 }
Esempio n. 14
0
    void OnTriggerStay(Collider coll)
    {
        if (coll.gameObject.CompareTag("LimitPoint") && (states == enemy_states.HITTED) && (!coll.GetComponent <LimitPoint>().fall))
        {
            isInLimit = true;
        }

        if ((sight == false) && coll.gameObject.CompareTag("Player") && ((states == enemy_states.WAIT) || (states == enemy_states.RETURNING) || (states == enemy_states.CHASE)))
        {
            origin   = transform.position;
            origin.y = collider.bounds.extents.y;
            ray      = new Ray(origin, player.transform.position - origin);

            if ((Physics.Raycast(ray, out hit, maxSightChase, layerMask)) && hit.collider.gameObject.CompareTag("Player"))
            {
                sight  = true;
                speed  = chaseSpeed;
                states = enemy_states.CHASE;
            }
        }
    }
Esempio n. 15
0
    // Logic of attacked received. Deactivates temporary all the colliders.
    public void Attacked()
    {
        speed                = attackSpeed;
        states               = enemy_states.HITTED;
        rigid.isKinematic    = true;
        collider.enabled     = false;
        axeCollider1.enabled = false;
        axeCollider2.enabled = false;
        fieldOfView.enabled  = false;
        headCollider.enabled = false;
        --lives;
        SoundManager.instance.PlaySingle(hitSound);

        if (lives <= 0)
        {
            dropItems();
            states     = enemy_states.DEATH;
            isInAttack = false;
        }

        // To impulse player from enemy
        player.ReactToAttack(transform.position.x);
    }
Esempio n. 16
0
    void OnTriggerStay(Collider coll)
    {
        // if the enemy hasn't seen Glitch he patrols and if he detects him with the raycast
        // then changes his state to Chase and changes his speed too.
        if ((sight == false) && coll.gameObject.CompareTag("Player") && (states != enemy_states.HITTED))
        {
            origin    = transform.position;
            origin.y += transform.localScale.y * 0.75f;
            ray       = new Ray(origin, player.transform.position - origin);

            if ((Physics.Raycast(ray, out hit, maxSightChase, layerMask)) && hit.collider.gameObject.CompareTag("Player"))
            {
                sight = true;
                speed = chaseSpeed;

                if (states == enemy_states.WAIT || states == enemy_states.PATROL)
                {
                    lastPosition = transform.position;      // We save the point in the patrol to return later
                }
                states = enemy_states.CHASE;
            }
        }
    }
Esempio n. 17
0
 // Controls slip and death when exits a collider
 void OnTriggerExit(Collider coll)
 {
     if (coll.gameObject.CompareTag("Player"))
     {
         sight = false;
     }
     else if (coll.gameObject.CompareTag("LimitPoint"))
     {
         if (isInLimit)
         {
             isInLimit = false;
         }
         else if ((coll.GetComponent <LimitPoint>().fall) && (states == enemy_states.CHASE))
         {
             states = enemy_states.SLIP;
             sight  = false;
             speed  = fallSpeed;
         }
     }
     else if (coll.gameObject.CompareTag("Death"))
     {
         gameObject.SetActive(false);
     }
 }
Esempio n. 18
0
    void Update()
    {
        // To control fps
        if (world.doUpdate)
        {
            // Update state and speed in animator
            animator.SetFloat("Speed", speed * speedConstant);
            animator.SetInteger("State", (int)states);
            animator.SetBool("Attack", isInAttack);
            switch (states)
            {
                case enemy_states.WAIT:
                    // IDLE
                    break;

                // Enemy chases Glitch until reach him, reach a limit point
                case enemy_states.CHASE:
                   
                    // Chasing logic

                    speed = chaseSpeed;
                    transform.Translate(Vector3.forward * speed * world.lag);

                    if ((sight == true) && (Vector3.Distance(playerPos.position, transform.position) <= maxSightAttack))
                    {
                        speed = attackSpeed;
                        isInAttack = true;
                        states = enemy_states.ATTACK;
                    }

                    break;

                // Enemy attacks Glitch
                case enemy_states.ATTACK:

                    // Attacking logic

                    // If Glitch isn't in attack scope then enemy chases him
                    if ((Vector3.Distance(playerPos.position, transform.position) > maxSightAttack) && !isInAttack)
                    {
                        speed = chaseSpeed;
                        states = enemy_states.CHASE;
                    }
                    break;

                case enemy_states.HITTED:
                    // State to put particles or something
                    break;

                case enemy_states.RETURNING:
                    speed = walkSpeed;
                    if (!isInAttack)
                    {
                        if ((transform.position.x < initialPosition.x + 1) && (transform.position.x > initialPosition.x - 1))
                        {
                            transform.Rotate(0.0f, 270 - transform.eulerAngles.y, 0.0f);
                            speed = waitSpeed;
                            states = enemy_states.WAIT;
                        }
                        else
                        {
                            transform.Translate(Vector3.forward * speed * world.lag);
                        }

                        if (attackSucces)
                        {
                            if ((transform.rotation.eulerAngles.y < 270.0f + 1) && (transform.rotation.eulerAngles.y > 270.0f - 1))
                            {
                                if (initialPosition.x > transform.position.x)
                                {
                                    transform.Rotate(0.0f, -(transform.eulerAngles.y - 90), 0.0f);
                                }
                            }
                            else
                            {
                                if (initialPosition.x < transform.position.x)
                                {
                                    transform.Rotate(0.0f, 270 - transform.eulerAngles.y, 0.0f);
                                }
                            }
                            attackSucces = false;
                        }
                    }
                    break;

                case enemy_states.IMPACT:
                    speed = fallSpeed;
                    break;

                case enemy_states.SLIP:
                    transform.Translate(Vector3.forward * speed * world.lag);
                    break;

                case enemy_states.FALL:
                    transform.Translate(Vector3.down * speed * world.lag);
                    break;
            }
        }
        else
        {
            animator.SetFloat("Speed", 0.0f);
        }
    }
Esempio n. 19
0
    void Update()
    {
        // To control fps
        if (world.doUpdate)
        {
            // Update state and speed in animator
            animator.SetFloat("Speed", speed * speedConstant);
            animator.SetInteger("State", (int)states);
            animator.SetBool("Attack", isInAttack);
            switch (states)
            {
            case enemy_states.WAIT:
                // IDLE
                break;

            // Enemy chases Glitch until reach him, reach a limit point
            case enemy_states.CHASE:

                // Chasing logic

                speed = chaseSpeed;
                transform.Translate(Vector3.forward * speed * world.lag);

                if ((sight == true) && (Vector3.Distance(playerPos.position, transform.position) <= maxSightAttack))
                {
                    speed      = attackSpeed;
                    isInAttack = true;
                    states     = enemy_states.ATTACK;
                }

                break;

            // Enemy attacks Glitch
            case enemy_states.ATTACK:

                // Attacking logic

                // If Glitch isn't in attack scope then enemy chases him
                if ((Vector3.Distance(playerPos.position, transform.position) > maxSightAttack) && !isInAttack)
                {
                    speed  = chaseSpeed;
                    states = enemy_states.CHASE;
                }
                break;

            case enemy_states.HITTED:
                // State to put particles or something
                break;

            case enemy_states.RETURNING:
                speed = walkSpeed;
                if (!isInAttack)
                {
                    if ((transform.position.x < initialPosition.x + 1) && (transform.position.x > initialPosition.x - 1))
                    {
                        transform.Rotate(0.0f, 270 - transform.eulerAngles.y, 0.0f);
                        speed  = waitSpeed;
                        states = enemy_states.WAIT;
                    }
                    else
                    {
                        transform.Translate(Vector3.forward * speed * world.lag);
                    }

                    if (attackSucces)
                    {
                        if ((transform.rotation.eulerAngles.y < 270.0f + 1) && (transform.rotation.eulerAngles.y > 270.0f - 1))
                        {
                            if (initialPosition.x > transform.position.x)
                            {
                                transform.Rotate(0.0f, -(transform.eulerAngles.y - 90), 0.0f);
                            }
                        }
                        else
                        {
                            if (initialPosition.x < transform.position.x)
                            {
                                transform.Rotate(0.0f, 270 - transform.eulerAngles.y, 0.0f);
                            }
                        }
                        attackSucces = false;
                    }
                }
                break;

            case enemy_states.IMPACT:
                speed = fallSpeed;
                break;

            case enemy_states.SLIP:
                transform.Translate(Vector3.forward * speed * world.lag);
                break;

            case enemy_states.FALL:
                transform.Translate(Vector3.down * speed * world.lag);
                break;
            }
        }
        else
        {
            animator.SetFloat("Speed", 0.0f);
        }
    }
Esempio n. 20
0
 // Trigger that detect collisions with patrol points and limit points
 void OnTriggerEnter(Collider coll)
 {
     // Only detect collisions with patrol points when enemy is patrolling
     if ((coll.gameObject.CompareTag("PatrolPoint")) && (states == enemy_states.PATROL))
     {
         speed = patrolSpeed;
         states = enemy_states.WAIT;
         time = waitTime;
     }
     // If is in a limit he stops and search glitch
     else if ((coll.gameObject.CompareTag("LimitPoint")) && (states != enemy_states.SEARCH))
     {
         speed = searchSpeed;
         states = enemy_states.SEARCH;
         time = searchingTime;
         sight = false;
         isInLimitPoint = true;
     }
 }
Esempio n. 21
0
    // Logic of attacked received. Deactivates temporary all the colliders.
    public void Attacked()
    {
        speed = attackSpeed;
        states = enemy_states.HITTED;
        rigid.isKinematic = true;
        collider.enabled = false;
        axeCollider1.enabled = false;
        axeCollider2.enabled = false;
        fieldOfView.enabled = false;
        headCollider.enabled = false;
        --lives;
        SoundManager.instance.PlaySingle(hitSound);

        if (lives <= 0)
        {
            dropItems();
            states = enemy_states.DEATH;
            isInAttack = false;
        }

        // To impulse player from enemy
        player.ReactToAttack(transform.position.x);
    }
Esempio n. 22
0
    // Trigger of end attack, disables colliders of axes
    public void AttackTrigger()
    {
        axeCollider1.enabled = false;
        axeCollider2.enabled = false;
        isInAttack = false;

        if (attackSucces)
        {
            speed = walkSpeed;
            states = enemy_states.RETURNING;
        }
    }
Esempio n. 23
0
    void Update()
    {
        // To control fps
        if (world.doUpdate)
        {
            // Update state and speed in animator
            animator.SetInteger("State", (int)states);
            animator.SetFloat("Speed", speed * speedConstant);

            switch (states)
            {
                // Enemy shoot arrows to Glitch
                case enemy_states.SHOOT:

                    // Shooting logic

                    // If player deaths archer waits, else if is possible shoots
                    if ((player.playerController.state == PlayerController.player_state.DEATH) || (!render.isVisible))
                    {
                        animator.SetBool("Shoot", false);
                        states = enemy_states.WAIT;
                        sight = false;
                    }
                    else if ((sight) && ((arrow == null) || (!arrow.activeInHierarchy)))
                    {
                        animator.SetBool("Shoot", true);
                        shooted = false;
                    }

                    // If arrow is not initialized or reset
                    if (shooted == false)
                    {
                        origin = transform.position;
                        origin.y += collider.bounds.extents.y * 2 * 0.75f;

                        // We calculate the angle
                        float x = origin.x - hit.point.x;
                        float y = origin.y - hit.point.y;
                        float alfa = Mathf.Atan(y / x);

                        // We pass it from radians to degrees
                        alfa = (180.0f * alfa) / Mathf.PI;

                        // ShootLevel 1->Up, 2->Down, 0->Middle 
                        if (player.transform.position.x > origin.x)
                        {
                            if (alfa > 15.0f)
                            {
                                animator.SetInteger("ShootLevel", 1);
                            }
                            else if (alfa < -15.0f)
                            {
                                animator.SetInteger("ShootLevel", 2);
                            }
                            else
                            {
                                animator.SetInteger("ShootLevel", 0);
                            }
                        }
                        else
                        {
                            if (-alfa > 15.0f)
                            {
                                animator.SetInteger("ShootLevel", 1);
                            }
                            else if (-alfa < -15.0f)
                            {
                                animator.SetInteger("ShootLevel", 2);
                            }
                            else
                            {
                                animator.SetInteger("ShootLevel", 0);
                            }
                        }
                    }

                    // If distance to Glitch is lower than chase field of view then changes to Chase state.
                    // If Glitch is in melee attack scope then enemy attacks to him with a kick, changing her state to Melee attack
                    // If archer is motionless then she can't move
                    if ((motionless == false) && (Vector3.Distance(player.transform.position, transform.position) < maxSightChase))
                    {
                        speed = chaseSpeed;
                        states = enemy_states.CHASE;
                    }

                    if (Vector3.Distance(player.transform.position, transform.position) <= maxSightMeleeAttack)
                    {
                        speed = meleeAttackSpeed;
                        timePerKick = 0.0f;
                        states = enemy_states.MELEE_ATTACK;
                    }
                    break;

                // Enemy chases Glitch until reach him or lose sight of Glitch
                case enemy_states.CHASE:

                    // Chasing logic
                    rotationTime -= world.lag;
                    if (rotationTime <= 0.0f)
                    {
                        transform.Translate(Vector3.forward * speed * world.lag);
                    }

                    if (Vector3.Distance(player.transform.position, transform.position) > maxSightPersecution)
                    {
                        speed = shootSpeed;
                        states = enemy_states.SHOOT;
                    }
                    else if (Vector3.Distance(player.transform.position, transform.position) <= maxSightMeleeAttack)
                    {
                        speed = meleeAttackSpeed;
                        timePerKick = 0.0f;
                        states = enemy_states.MELEE_ATTACK;
                    }

                    break;

                case enemy_states.TURN:

                    // Turn logic
                    if ((player.transform.position.x > transform.position.x) && (transform.eulerAngles.y > 269.0f))
                    {
                        transform.eulerAngles = new Vector3(0.0f, 90.0f, 0.0f);
                        rotationTime = 0.5f;
                    }
                    else if ((player.transform.position.x < transform.position.x) && (transform.eulerAngles.y < 91.0f))
                    {
                        transform.eulerAngles = new Vector3(0.0f, 270.0f, 0.0f);
                        rotationTime = 0.5f;
                    }
                    speed = waitSpeed;
                    states = enemy_states.WAIT;
                    break;

                // Enemy attacks to Glitch with a kick
                case enemy_states.MELEE_ATTACK:

                    // Melee attack logic

                    // A kick for timePerKick time
                    timePerKick -= world.lag;
                    if (timePerKick <= 0.0f)
                    {
                        animator.SetBool("Attack", true);
                        kickCollider.enabled = true;
                    }

                    if (Vector3.Distance(player.transform.position, transform.position) > maxSightMeleeAttack)
                    {
                        if (motionless == false)
                        {
                            speed = chaseSpeed;
                            states = enemy_states.CHASE;
                        }
                        else
                        {
                            speed = shootSpeed;
                            states = enemy_states.SHOOT;
                        }
                    }

                    break;

                case enemy_states.HITTED:
                    // State to put particles or something
                    break;
            }
        }
        else
        {
            // If slowfps is active then speed in animations is 0
            animator.SetFloat("Speed", 0.0f);
        }
    }
Esempio n. 24
0
    void Update()
    {
        // To control fps
        if (world.doUpdate)
        {
            // Update state and speed in animator
            animator.SetFloat("Speed", speed*speedConstant);
            animator.SetInteger("State", (int)states);
            animator.SetBool("Attack", isInAttack);

            if (states != enemy_states.PATROL && walkingSoundSource != null)
            {
                walkingSoundSource.Stop();
                walkingSoundSource = null;
            }

            switch (states)
            {
                case enemy_states.WAIT:
                    // ANIMATION OF ROTATION HERE

                    // In patrol point knight turns and continue with the patrol
                    time -= world.lag;
                    if (time <= 0.0f)
                    {
                        if ((transform.rotation.eulerAngles.y < 270.0f + 1) && (transform.rotation.eulerAngles.y > 270.0f - 1))
                        {
                            transform.Rotate(0.0f, -(transform.eulerAngles.y - 90), 0.0f);
                        }
                        else
                        {
                            transform.Rotate(0.0f, 270 - transform.eulerAngles.y, 0.0f);
                        }
                        speed = patrolSpeed;
                        states = enemy_states.PATROL;
                    }
                    break;

                case enemy_states.PATROL:
                    // Patrolling logic

                    transform.Translate(Vector3.forward * speed * world.lag);

                    if(Camera.current != null)
                    { 
                        Vector3 positionOnScreen = Camera.current.WorldToViewportPoint(transform.position);

                        if (positionOnScreen.x > 0 &&  positionOnScreen.x < 1 &&
                            positionOnScreen.y > 0 && positionOnScreen.y < 1 &&
                            (walkingSoundSource == null || !walkingSoundSource.isPlaying))
                        {
                            walkingSoundSource = SoundManager.instance.PlaySingle(walkingSound);
                        }
                    }

                    break;

                // Enemy chases Glitch until reach him, reach a limit point or lose sight of Glitch
                case enemy_states.CHASE:
                    // Chasing logic

                    speed = chaseSpeed;
                    if ((transform.rotation.eulerAngles.y < 270.0f + 1) && (transform.rotation.eulerAngles.y > 270.0f - 1))
                    {
                        if (playerPos.position.x > transform.position.x)
                        {
                            transform.Rotate(0.0f, -(transform.eulerAngles.y - 90), 0.0f);
                            rotationTime = 0.5f;
                        }
                    }
                    else
                    {
                        if (playerPos.position.x < transform.position.x)
                        {
                            transform.Rotate(0.0f, 270 - transform.eulerAngles.y, 0.0f);
                            rotationTime = 0.5f;
                        }
                    }

                    rotationTime -= world.lag;
                    if (rotationTime <= 0.0f)
                    {
                        transform.Translate(Vector3.forward * speed * world.lag);
                    }

                    origin = transform.position;
                    origin.y += transform.localScale.y * 0.75f;
                    ray = new Ray(origin, player.transform.position - origin);

                    if (!Physics.Raycast(ray, out hit, maxSightChase, layerMask) || !hit.collider.gameObject.CompareTag("Player"))
                    {
                        speed = searchSpeed;
                        sight = false;
                        states = enemy_states.SEARCH;
                        time = searchingTime;
                    }
                    else if (Vector3.Distance(playerPos.position, transform.position) <= maxSightAttack)
                    {
                        speed = attackSpeed;
                        states = enemy_states.ATTACK;
                        isInAttack = true;
                    }

                    break;

                // Enemy attacks Glitch
                case enemy_states.ATTACK:

                    // Attacking logic

                    // If distance to Glitch is more than chase field of view then changes to Search state
                    // else if Glitch isn't in attack scope then enemy chases him
                    if ((Vector3.Distance(playerPos.position, transform.position) > maxSightChase) && !isInAttack)
                    {
                        speed = searchSpeed;
                        sight = false;
                        states = enemy_states.SEARCH;
                        time = searchingTime;
                    }
                    else if (Vector3.Distance(playerPos.position, transform.position) > maxSightAttack && !isInAttack)
                    {
                        speed = chaseSpeed;
                        states = enemy_states.CHASE;
                    }
                    break;

                // Enemy looks at one side and the other looking for Glitch. After 5 seconds he returns to last patrol position 
                // and continues patrolling.
                case enemy_states.SEARCH:

                    // Searching logic
                    time -= world.lag;
                    if (time <= 0.0f)
                    {
                        if ((transform.rotation.eulerAngles.y < 270.0f + 1) && (transform.rotation.eulerAngles.y > 270.0f - 1))
                        {
                            if (lastPosition.x > transform.position.x)
                            {
                                transform.Rotate(0.0f, -(transform.eulerAngles.y - 90), 0.0f);
                            }
                        }
                        else
                        {
                            if (lastPosition.x < transform.position.x)
                            {
                                transform.Rotate(0.0f, 270 - transform.eulerAngles.y, 0.0f);
                            }
                        }
                        speed = patrolSpeed;
                        states = enemy_states.RETURNING;
                        searchRotationTime = 1.0f;
                        isInLimitPoint = false;
                    }

                    origin = transform.position;
                    origin.y += transform.localScale.y * 0.75f;
                    ray = new Ray(origin, player.transform.position - origin);

                    if ((Physics.Raycast(ray, out hit, maxSightSearch, layerMask)) && (hit.collider.gameObject.CompareTag("Player")) && (sight == true))
                    {
                        speed = chaseSpeed;
                        states = enemy_states.CHASE;
                        isInLimitPoint = false;
                    }
                    break;

                case enemy_states.RETURNING:
                    speed = patrolSpeed;
                    transform.Translate(Vector3.forward * speed * world.lag);
                    if ((transform.position.x < lastPosition.x + 1) && (transform.position.x > lastPosition.x - 1))
                    {
                        sight = false;
                        speed = patrolSpeed;
                        states = enemy_states.PATROL;
                    }
                    break;

                case enemy_states.HITTED:
                    // State to put particles or something
                    break;

/*				case enemy_states.DEATH:
					animator.speed = animator.speed + 0.01f;
					break;
*/
            }
        }
        else
        {
            animator.SetFloat("Speed", 0.0f);
        }
    }
Esempio n. 25
0
 // Trigger for when she loses of sight Glitch 
 void OnTriggerExit(Collider coll)
 {
     if (coll.CompareTag("Player"))
     {
         sight = false;
         if (Vector3.Distance(player.transform.position, transform.position) < maxSightShoot)
         {
             // If the distance is lower than maxSightShoot turns
             states = enemy_states.TURN;
         }
         else
         {
             // Else Glitch escape and she wait
             speed = waitSpeed;
             states = enemy_states.WAIT;
         }
     }
 }
Esempio n. 26
0
 // Trigger of hit animation
 public void HittedTrigger()
 {
     if (states != enemy_states.DEATH)
     {
         speed = chaseSpeed;
         states = enemy_states.CHASE;
     }
 }
Esempio n. 27
0
    // Logic of attacked received. Deactivates temporary all the colliders.
    public void Attacked()
    {
        if (!attacked)
        {
            rigid.isKinematic = true;
            attacked = true;
            speed = attackSpeed;
            states = enemy_states.HITTED;
            --lives;
            collider.enabled = false;
            swordCollider.enabled = false;
            headCollider.enabled = false;
            SoundManager.instance.PlaySingle(hitSound);

            if (lives <= 0)
            {
                states = enemy_states.DEATH;
                fieldOfView.enabled = false;
                isInAttack = false;
                InvokeRepeating("TiltModel", 0f, 0.1f);
                ScoreManager.instance.EnemyDefeated();
            }
            else
            {
                StartCoroutine(ActivateColliders(0.2f));
            }
        }

        // To impulse player from enemy
        player.ReactToAttack(transform.position.x);
    }
Esempio n. 28
0
    void Update()
    {
        // To control fps
        if (world.doUpdate)
        {
            // Update state and speed in animator
            animator.SetFloat("Speed", speed * speedConstant);
            animator.SetInteger("State", (int)states);
            animator.SetBool("Attack", isInAttack);

            if (states != enemy_states.PATROL && walkingSoundSource != null)
            {
                walkingSoundSource.Stop();
                walkingSoundSource = null;
            }

            switch (states)
            {
            case enemy_states.WAIT:
                // ANIMATION OF ROTATION HERE

                // In patrol point knight turns and continue with the patrol
                time -= world.lag;
                if (time <= 0.0f)
                {
                    if ((transform.rotation.eulerAngles.y < 270.0f + 1) && (transform.rotation.eulerAngles.y > 270.0f - 1))
                    {
                        transform.Rotate(0.0f, -(transform.eulerAngles.y - 90), 0.0f);
                    }
                    else
                    {
                        transform.Rotate(0.0f, 270 - transform.eulerAngles.y, 0.0f);
                    }
                    speed  = patrolSpeed;
                    states = enemy_states.PATROL;
                }
                break;

            case enemy_states.PATROL:
                // Patrolling logic

                transform.Translate(Vector3.forward * speed * world.lag);

                if (Camera.current != null)
                {
                    Vector3 positionOnScreen = Camera.current.WorldToViewportPoint(transform.position);

                    if (positionOnScreen.x > 0 && positionOnScreen.x < 1 &&
                        positionOnScreen.y > 0 && positionOnScreen.y < 1 &&
                        (walkingSoundSource == null || !walkingSoundSource.isPlaying))
                    {
                        walkingSoundSource = SoundManager.instance.PlaySingle(walkingSound);
                    }
                }

                break;

            // Enemy chases Glitch until reach him, reach a limit point or lose sight of Glitch
            case enemy_states.CHASE:
                // Chasing logic

                speed = chaseSpeed;
                if ((transform.rotation.eulerAngles.y < 270.0f + 1) && (transform.rotation.eulerAngles.y > 270.0f - 1))
                {
                    if (playerPos.position.x > transform.position.x)
                    {
                        transform.Rotate(0.0f, -(transform.eulerAngles.y - 90), 0.0f);
                        rotationTime = 0.5f;
                    }
                }
                else
                {
                    if (playerPos.position.x < transform.position.x)
                    {
                        transform.Rotate(0.0f, 270 - transform.eulerAngles.y, 0.0f);
                        rotationTime = 0.5f;
                    }
                }

                rotationTime -= world.lag;
                if (rotationTime <= 0.0f)
                {
                    transform.Translate(Vector3.forward * speed * world.lag);
                }

                origin    = transform.position;
                origin.y += transform.localScale.y * 0.75f;
                ray       = new Ray(origin, player.transform.position - origin);

                if (!Physics.Raycast(ray, out hit, maxSightChase, layerMask) || !hit.collider.gameObject.CompareTag("Player"))
                {
                    speed  = searchSpeed;
                    sight  = false;
                    states = enemy_states.SEARCH;
                    time   = searchingTime;
                }
                else if (Vector3.Distance(playerPos.position, transform.position) <= maxSightAttack)
                {
                    speed      = attackSpeed;
                    states     = enemy_states.ATTACK;
                    isInAttack = true;
                }

                break;

            // Enemy attacks Glitch
            case enemy_states.ATTACK:

                // Attacking logic

                // If distance to Glitch is more than chase field of view then changes to Search state
                // else if Glitch isn't in attack scope then enemy chases him
                if ((Vector3.Distance(playerPos.position, transform.position) > maxSightChase) && !isInAttack)
                {
                    speed  = searchSpeed;
                    sight  = false;
                    states = enemy_states.SEARCH;
                    time   = searchingTime;
                }
                else if (Vector3.Distance(playerPos.position, transform.position) > maxSightAttack && !isInAttack)
                {
                    speed  = chaseSpeed;
                    states = enemy_states.CHASE;
                }
                break;

            // Enemy looks at one side and the other looking for Glitch. After 5 seconds he returns to last patrol position
            // and continues patrolling.
            case enemy_states.SEARCH:

                // Searching logic
                time -= world.lag;
                if (time <= 0.0f)
                {
                    if ((transform.rotation.eulerAngles.y < 270.0f + 1) && (transform.rotation.eulerAngles.y > 270.0f - 1))
                    {
                        if (lastPosition.x > transform.position.x)
                        {
                            transform.Rotate(0.0f, -(transform.eulerAngles.y - 90), 0.0f);
                        }
                    }
                    else
                    {
                        if (lastPosition.x < transform.position.x)
                        {
                            transform.Rotate(0.0f, 270 - transform.eulerAngles.y, 0.0f);
                        }
                    }
                    speed              = patrolSpeed;
                    states             = enemy_states.RETURNING;
                    searchRotationTime = 1.0f;
                    isInLimitPoint     = false;
                }

                origin    = transform.position;
                origin.y += transform.localScale.y * 0.75f;
                ray       = new Ray(origin, player.transform.position - origin);

                if ((Physics.Raycast(ray, out hit, maxSightSearch, layerMask)) && (hit.collider.gameObject.CompareTag("Player")) && (sight == true))
                {
                    speed          = chaseSpeed;
                    states         = enemy_states.CHASE;
                    isInLimitPoint = false;
                }
                break;

            case enemy_states.RETURNING:
                speed = patrolSpeed;
                transform.Translate(Vector3.forward * speed * world.lag);
                if ((transform.position.x < lastPosition.x + 1) && (transform.position.x > lastPosition.x - 1))
                {
                    sight  = false;
                    speed  = patrolSpeed;
                    states = enemy_states.PATROL;
                }
                break;

            case enemy_states.HITTED:
                // State to put particles or something
                break;

/*				case enemy_states.DEATH:
 *                                      animator.speed = animator.speed + 0.01f;
 *                                      break;
 */
            }
        }
        else
        {
            animator.SetFloat("Speed", 0.0f);
        }
    }
Esempio n. 29
0
    // Trigger in recover animation
    public void RecoverTrigger()
    {
        rigid.isKinematic = false;
        if ((transform.rotation.eulerAngles.y < 270.0f + 1) && (transform.rotation.eulerAngles.y > 270.0f - 1))
        {
            transform.Rotate(0.0f, -(transform.eulerAngles.y - 90), 0.0f);
        }
        else
        {
            transform.Rotate(0.0f, 270 - transform.eulerAngles.y, 0.0f);
        }

        sight = false;
        speed = walkSpeed;
        states = enemy_states.RETURNING;
    }
Esempio n. 30
0
    // Trigger of turn animation
    public void TurnTrigger()
    {

        if ((player.transform.position.x > transform.position.x) && (transform.eulerAngles.y == 270.0f))
        {
            transform.eulerAngles = new Vector3(0.0f, 90.0f, 0.0f);
            rotationTime = 0.5f;
        }
        else if ((player.transform.position.x < transform.position.x) && (transform.eulerAngles.y == 90.0f))
        {
            transform.eulerAngles = new Vector3(0.0f, 270.0f, 0.0f);
            rotationTime = 0.5f;
        }
        speed = waitSpeed;
        states = enemy_states.WAIT;
    }
Esempio n. 31
0
    // Trigger of hit animation
    public void HittedTrigger()
    {
        if (states != enemy_states.DEATH)
        {
            rigid.isKinematic = false;
            collider.enabled = true;
            fieldOfView.enabled = true;
            headCollider.enabled = true;

            if ((transform.rotation.eulerAngles.y < 270.0f + 1) && (transform.rotation.eulerAngles.y > 270.0f - 1))
            {
                if (playerPos.position.x > transform.position.x)
                {
                    transform.Rotate(0.0f, -(transform.eulerAngles.y - 90), 0.0f);
                }
            }
            else
            {
                if (playerPos.position.x < transform.position.x)
                {
                    transform.Rotate(0.0f, 270 - transform.eulerAngles.y, 0.0f);
                }
            }

            speed = chaseSpeed;
            states = enemy_states.CHASE;
        }
    }
Esempio n. 32
0
 // Logic in kick
 public void Kick()
 {
     player.DecrementLives(meleeDamage);
     sight = false;
     speed = waitSpeed;
     states = enemy_states.WAIT;
 }
Esempio n. 33
0
 // Trigger of slip animation
 public void SlipTrigger()
 {
     speed = fallSpeed;
     states = enemy_states.FALL;
 }
Esempio n. 34
0
 // Logic in death of the enemy. Deactivates all the colliders.
 public void Defeated()
 {
     sight = false;
     speed = waitSpeed;
     states = enemy_states.DEATH;
     animator.SetBool("Attack", false);
     animator.SetBool("Shoot", false);
     rigid.isKinematic = true;
     collider.enabled = false;
     kickCollider.enabled = false;
     fieldOfView.enabled = false;
     headCollider.enabled = false;
     SoundManager.instance.PlaySingle(hitSound);
     InvokeRepeating("TiltModel", 0f, 0.1f);
     // To impulse player from enemy
     player.ReactToAttack(transform.position.x);
     ScoreManager.instance.EnemyDefeated();
 }
Esempio n. 35
0
    void OnTriggerStay(Collider coll)
    {
        // if the enemy hasn't seen Glitch he patrols and if he detects him with the raycast
        // then changes his state to Chase and changes his speed too.
        if ((sight == false) && coll.gameObject.CompareTag("Player") && (states != enemy_states.HITTED))
        {
            origin = transform.position;
            origin.y += transform.localScale.y * 0.75f;
            ray = new Ray(origin, player.transform.position - origin);

            if ((Physics.Raycast(ray, out hit, maxSightChase, layerMask)) && hit.collider.gameObject.CompareTag("Player"))
            {
                sight = true;
                speed = chaseSpeed;

                if (states == enemy_states.WAIT || states == enemy_states.PATROL)
                {
                    lastPosition = transform.position;      // We save the point in the patrol to return later
                }
                states = enemy_states.CHASE;
            }
        }
    }
Esempio n. 36
0
    // Trigger that detect player and change the state to Shoot
    void OnTriggerStay(Collider coll)
    {
        // If is the player and between player and archer isn't anything then shoot him
        if (coll.CompareTag("Player"))
        {
            //Ray from origin to player
            origin = transform.position;
            origin.y += transform.localScale.y*0.75f;
            ray = new Ray(origin, player.transform.position - origin);

            if ((Vector3.Distance(player.transform.position, transform.position) <= maxSightMeleeAttack) && (states != enemy_states.MELEE_ATTACK))
            {
                // If distance is low changes to melee attack
                speed = meleeAttackSpeed;
                timePerKick = 0.0f;
                states = enemy_states.MELEE_ATTACK;
            }
            else if (Physics.Raycast(ray, out hit, maxSightShoot, layerMask) && (states != enemy_states.HITTED) && (hit.collider.gameObject.CompareTag("Player")))
            {
                // Else changes to Shoot
                sight = true;
                speed = shootSpeed;
                states = enemy_states.SHOOT;
            }
            else if (sight == true) 
            {
                sight = false;
                animator.SetBool("Shoot", false);
            }
        }
    }
Esempio n. 37
0
    void Update()
    {
        // To control fps
        if (world.doUpdate)
        {
            // Update state and speed in animator
            animator.SetInteger("State", (int)states);
            animator.SetFloat("Speed", speed * speedConstant);

            switch (states)
            {
            // Enemy shoot arrows to Glitch
            case enemy_states.SHOOT:

                // Shooting logic

                // If player deaths archer waits, else if is possible shoots
                if ((player.playerController.state == PlayerController.player_state.DEATH) || (!render.isVisible))
                {
                    animator.SetBool("Shoot", false);
                    states = enemy_states.WAIT;
                    sight  = false;
                }
                else if ((sight) && ((arrow == null) || (!arrow.activeInHierarchy)))
                {
                    animator.SetBool("Shoot", true);
                    shooted = false;
                }

                // If arrow is not initialized or reset
                if (shooted == false)
                {
                    origin    = transform.position;
                    origin.y += collider.bounds.extents.y * 2 * 0.75f;

                    // We calculate the angle
                    float x    = origin.x - hit.point.x;
                    float y    = origin.y - hit.point.y;
                    float alfa = Mathf.Atan(y / x);

                    // We pass it from radians to degrees
                    alfa = (180.0f * alfa) / Mathf.PI;

                    // ShootLevel 1->Up, 2->Down, 0->Middle
                    if (player.transform.position.x > origin.x)
                    {
                        if (alfa > 15.0f)
                        {
                            animator.SetInteger("ShootLevel", 1);
                        }
                        else if (alfa < -15.0f)
                        {
                            animator.SetInteger("ShootLevel", 2);
                        }
                        else
                        {
                            animator.SetInteger("ShootLevel", 0);
                        }
                    }
                    else
                    {
                        if (-alfa > 15.0f)
                        {
                            animator.SetInteger("ShootLevel", 1);
                        }
                        else if (-alfa < -15.0f)
                        {
                            animator.SetInteger("ShootLevel", 2);
                        }
                        else
                        {
                            animator.SetInteger("ShootLevel", 0);
                        }
                    }
                }

                // If distance to Glitch is lower than chase field of view then changes to Chase state.
                // If Glitch is in melee attack scope then enemy attacks to him with a kick, changing her state to Melee attack
                // If archer is motionless then she can't move
                if ((motionless == false) && (Vector3.Distance(player.transform.position, transform.position) < maxSightChase))
                {
                    speed  = chaseSpeed;
                    states = enemy_states.CHASE;
                }

                if (Vector3.Distance(player.transform.position, transform.position) <= maxSightMeleeAttack)
                {
                    speed       = meleeAttackSpeed;
                    timePerKick = 0.0f;
                    states      = enemy_states.MELEE_ATTACK;
                }
                break;

            // Enemy chases Glitch until reach him or lose sight of Glitch
            case enemy_states.CHASE:

                // Chasing logic
                rotationTime -= world.lag;
                if (rotationTime <= 0.0f)
                {
                    transform.Translate(Vector3.forward * speed * world.lag);
                }

                if (Vector3.Distance(player.transform.position, transform.position) > maxSightPersecution)
                {
                    speed  = shootSpeed;
                    states = enemy_states.SHOOT;
                }
                else if (Vector3.Distance(player.transform.position, transform.position) <= maxSightMeleeAttack)
                {
                    speed       = meleeAttackSpeed;
                    timePerKick = 0.0f;
                    states      = enemy_states.MELEE_ATTACK;
                }

                break;

            case enemy_states.TURN:

                // Turn logic
                if ((player.transform.position.x > transform.position.x) && (transform.eulerAngles.y > 269.0f))
                {
                    transform.eulerAngles = new Vector3(0.0f, 90.0f, 0.0f);
                    rotationTime          = 0.5f;
                }
                else if ((player.transform.position.x < transform.position.x) && (transform.eulerAngles.y < 91.0f))
                {
                    transform.eulerAngles = new Vector3(0.0f, 270.0f, 0.0f);
                    rotationTime          = 0.5f;
                }
                speed  = waitSpeed;
                states = enemy_states.WAIT;
                break;

            // Enemy attacks to Glitch with a kick
            case enemy_states.MELEE_ATTACK:

                // Melee attack logic

                // A kick for timePerKick time
                timePerKick -= world.lag;
                if (timePerKick <= 0.0f)
                {
                    animator.SetBool("Attack", true);
                    kickCollider.enabled = true;
                }

                if (Vector3.Distance(player.transform.position, transform.position) > maxSightMeleeAttack)
                {
                    if (motionless == false)
                    {
                        speed  = chaseSpeed;
                        states = enemy_states.CHASE;
                    }
                    else
                    {
                        speed  = shootSpeed;
                        states = enemy_states.SHOOT;
                    }
                }

                break;

            case enemy_states.HITTED:
                // State to put particles or something
                break;
            }
        }
        else
        {
            // If slowfps is active then speed in animations is 0
            animator.SetFloat("Speed", 0.0f);
        }
    }
Esempio n. 38
0
    void OnTriggerStay(Collider coll)
    {
        if (coll.gameObject.CompareTag("LimitPoint") && (states == enemy_states.HITTED) && (!coll.GetComponent<LimitPoint>().fall))
        {
            isInLimit = true;
        }

        if ((sight == false) && coll.gameObject.CompareTag("Player") && ((states == enemy_states.WAIT) || (states == enemy_states.RETURNING) || (states == enemy_states.CHASE)))
        {
            origin = transform.position;
            origin.y = collider.bounds.extents.y;
            ray = new Ray(origin, player.transform.position - origin);

            if ((Physics.Raycast(ray, out hit, maxSightChase, layerMask)) && hit.collider.gameObject.CompareTag("Player"))
            {
                sight = true;
                speed = chaseSpeed;
                states = enemy_states.CHASE;
            }
        }
    }
Esempio n. 39
0
 // Trigger of slip animation
 public void SlipTrigger()
 {
     speed  = fallSpeed;
     states = enemy_states.FALL;
 }
Esempio n. 40
0
 // Controls slip and death when exits a collider 
 void OnTriggerExit(Collider coll)
 {
     if (coll.gameObject.CompareTag("Player"))
     {
         sight = false;
     }
     else if (coll.gameObject.CompareTag("LimitPoint"))
     {
         if (isInLimit)
         {
             isInLimit = false;
         }
         else if ((coll.GetComponent<LimitPoint>().fall) && (states == enemy_states.CHASE))
         {
             states = enemy_states.SLIP;
             sight = false;
             speed = fallSpeed;
         }
     }
     else if (coll.gameObject.CompareTag("Death"))
     {
         gameObject.SetActive(false);
     }
 }
Esempio n. 41
0
 void OnCollisionEnter(Collision coll)
 {
     // If collides with glitch, enemy turns kinematic to avoid push
     if ((states != enemy_states.DEATH) && (sight == true) && (coll.contacts[0].otherCollider.CompareTag("Player")))
     {
         rigid.isKinematic = true;
     }
     else if ((sight == false) && (coll.contacts[0].otherCollider.CompareTag("Player")))
     {
         rigid.isKinematic = true;
         states = enemy_states.TURN;
     }
 }
Esempio n. 42
0
 // Logic of attack
 public void Attack()
 {
     isInAttack = false;
     player.DecrementLives(damageAttack);
     sight = false;
     if (player.lives > 0)
     {
         speed = searchSpeed;
         time = searchingTime;
         states = enemy_states.SEARCH;
     }
     else
     {
         speed = patrolSpeed;
         time = waitTime;
         states = enemy_states.WAIT;
     }
 }