Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.35 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class EagleController : MonoBehaviour
  7. {
  8.     public GameObject bunny;
  9.     public int EAGLE_ROTATION = 3;
  10.     private int RANGE = 6;
  11.     private Animator animator;
  12.     public float fly_speed = 1.0f;
  13.     private bool can_catch = true;
  14.     private Vector3 original_pos;
  15.     private BunnyInteractionHandler bunny_script_;
  16.     private Animator bunny_animator_;
  17.     LifeHandler lfh_;
  18.  
  19.     private int rotation_status = 0;
  20.     public GameObject step1_;
  21.  
  22.     void Start()
  23.     {
  24.         animator = this.GetComponent<Animator>();
  25.         original_pos = this.transform.position;
  26.         bunny_script_ = bunny.GetComponent<BunnyInteractionHandler>();
  27.         bunny_animator_ = bunny.GetComponent<Animator>();
  28.         lfh_ = Component.FindObjectOfType<LifeHandler>();
  29.         step1_.GetComponent<Text>().text = EAGLE_ROTATION.ToString();
  30.     }
  31.  
  32.     void Update()
  33.     {
  34.         bool go_catch = IsBunnyAhead();
  35.         int bunny_distance = GetDistanceFromBunny();
  36.  
  37.         if(go_catch)
  38.         {  
  39.             bunny_script_.CallMovementInterrupt();
  40.  
  41.             // Neet to catch but out of the catch range
  42.             if(bunny_distance > 1)
  43.             {
  44.                 //Debug.Log("FLY");
  45.                 this.transform.position = Vector3.MoveTowards(transform.position, bunny.transform.position, fly_speed * Time.deltaTime);
  46.                 animator.SetFloat("Speed", fly_speed);
  47.             }
  48.             if(bunny_distance < 2)
  49.             {
  50.                 //Debug.Log("CATCH NOW");
  51.                 animator.SetFloat("Speed", 0);
  52.                 animator.SetTrigger("Attack");
  53.                 bunny_animator_.ResetTrigger("Revive");
  54.                 bunny_animator_.SetTrigger("Die");
  55.             }
  56.         }
  57.  
  58.         // Bunny is dead
  59.         if(IsAnimationEnded(animator, "Attack") && IsAnimationEnded(bunny_animator_, "Dead"))
  60.         {
  61.             if(can_catch){
  62.                 Debug.Log("Catch finished");
  63.                 ActionController action_controller = bunny.GetComponent<BunnyInteractionHandler>().action_controller;
  64.                 action_controller.Interrupt();
  65.                 animator.SetFloat("Speed", 0);
  66.                 animator.ResetTrigger("Attack");
  67.                 bunny_animator_.SetTrigger("Revive");
  68.                 lfh_.TakeLife();
  69.                 lfh_.ShowendScreen();
  70.  
  71.                 Reset();
  72.             }
  73.             can_catch = false;
  74.        
  75.            
  76.         }
  77.        
  78.     }
  79.  
  80.     public void Rotate()
  81.     {
  82.         rotation_status++;
  83.         if(rotation_status > EAGLE_ROTATION)
  84.         {
  85.             rotation_status = 0;
  86.         }
  87.  
  88.         if(rotation_status == EAGLE_ROTATION)
  89.         {
  90.             transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y + 90, transform.localEulerAngles.z);
  91.         }
  92.         else
  93.         {
  94.             Debug.Log("Rotation Status = " + rotation_status);
  95.         }
  96.     }
  97.  
  98.     private bool IsAnimationEnded(Animator anim, string animation_name)
  99.     {
  100.         if(anim.GetCurrentAnimatorStateInfo(0).IsName(animation_name) && anim.GetCurrentAnimatorStateInfo(0).normalizedTime >= 0.8f)
  101.         {
  102.             return true;
  103.         }
  104.         can_catch = true;
  105.         return false;
  106.     }
  107.  
  108.     int GetDistanceFromBunny()
  109.     {
  110.         return (int)Mathf.Floor(Vector3.Distance(this.transform.position, bunny.transform.position));
  111.     }
  112.  
  113.     bool IsBunnyAhead()
  114.     {
  115.         Vector3 RAY_START = transform.position + new Vector3(0, 0.5f, 0);
  116.         Ray myRay = new Ray(RAY_START, transform.forward * RANGE);
  117.         RaycastHit hit;
  118.  
  119.         if(Physics.Raycast(myRay, out hit, RANGE))
  120.         {
  121.             if(hit.collider.tag == "Bunny")
  122.             {
  123.                 // Debug.Log("I See the bunny");
  124.                 return true;
  125.             }
  126.             else
  127.             {
  128.                 // Debug.Log("Bunny Lost");
  129.                 animator.SetFloat("Speed", 0);
  130.                 return false;
  131.             }
  132.         }
  133.         return false;
  134.     }
  135.  
  136.  
  137.     private void Reset()
  138.     {
  139.         bunny_animator_.ResetTrigger("Die");
  140.         animator.ResetTrigger("Attack");
  141.         animator.SetFloat("Speed", 0);
  142.         bunny_script_.ResetBunny();
  143.         this.transform.position = original_pos;
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement