Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.85 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Shark : Photon.PunBehaviour, IPunObservable
  5. {
  6.     [SerializeField] float defaultSpeed = 1.9f;
  7.     [SerializeField] GameObject woodParticle;
  8.     [SerializeField] Behaviour componentDisable;
  9.  
  10.     private float currentSpeed = 0;
  11.     private float attackTimer = 0;
  12.     private float attackTimer2 = 0;
  13.     private bool isPlayerAttack = false;
  14.     private bool isObstacle = false;
  15.     private bool sharkDie = false;
  16.  
  17.     public static bool isBuildAttack = false;
  18.     public static bool canAttack = false;
  19.  
  20.     private Vector3 newWaypointlocation;
  21.     private Vector3 lastPlayerPosition = Vector3.zero;
  22.  
  23.  
  24.     private Animator animator;
  25.     private AudioSource audioSource;
  26.  
  27.     public static float health = 150;
  28.     public static int dmgCount = 0;
  29.  
  30.  
  31.     private Transform target;
  32.     public Transform Target
  33.     {
  34.         get { return target; }
  35.         set
  36.         {
  37.             target = value;
  38.             if (target == null)
  39.             {
  40.                 attackTimer2 = 0;
  41.                 canAttack = false;
  42.                 attackTimer = 0;
  43.                 isPlayerAttack = false;
  44.             }
  45.             else
  46.                 lastPlayerPosition = target.position;
  47.         }
  48.     }
  49.  
  50.     private void Awake()
  51.     {
  52.         animator = GetComponent<Animator>();
  53.         audioSource = GetComponent<AudioSource>();
  54.     }
  55.  
  56.     private void Start()
  57.     {
  58.         if (PhotonNetwork.isMasterClient)
  59.         {
  60.             ResetShark();
  61.         }
  62.         else
  63.         {
  64.             componentDisable.enabled = false;
  65.  
  66.         }
  67.  
  68.     }
  69.  
  70.     private void Update()
  71.     {
  72.         if(PhotonNetwork.isMasterClient)
  73.         {
  74.             if(isObstacle)
  75.                 transform.position = Vector3.MoveTowards(transform.position, new Vector3(newWaypointlocation.x, -1f, newWaypointlocation.z), currentSpeed * Time.deltaTime);
  76.             else
  77.                 transform.position = Vector3.MoveTowards(transform.position, newWaypointlocation, currentSpeed * Time.deltaTime);
  78.  
  79.             Vector3 relativePos = newWaypointlocation - transform.position;
  80.             if(relativePos != Vector3.zero)
  81.             {
  82.                 Quaternion targetRotation;
  83.  
  84.                 if (isPlayerAttack)
  85.                 {
  86.                     targetRotation = Quaternion.LookRotation((new Vector3(target.position.x, 0.3f, target.position.z) - transform.position).normalized);
  87.                 }
  88.                 else
  89.                 {
  90.                     targetRotation = Quaternion.LookRotation(relativePos.normalized, Vector3.up);
  91.                 }
  92.  
  93.                 transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, currentSpeed * Time.deltaTime);
  94.             }
  95.  
  96.             if(!sharkDie)
  97.             {
  98.                 if (Vector3.Distance(transform.position, newWaypointlocation) < 2)
  99.                 {
  100.                     if (!isBuildAttack && !isPlayerAttack)
  101.                         SharkIdle();
  102.                 }
  103.  
  104.                 if(!isBuildAttack && target != null)
  105.                 {
  106.                     attackTimer2 += Time.deltaTime;
  107.                     if (attackTimer2 >= 5)
  108.                     {
  109.                         SharkAttack();
  110.                     }
  111.                 }
  112.  
  113.                 if (health < 0)
  114.                 {
  115.                     health = 0;
  116.                     sharkDie = true;
  117.                     StartCoroutine(SharkDie());
  118.                 }
  119.  
  120.                 if (dmgCount >= 5)
  121.                 {
  122.                     dmgCount = 0;
  123.                     canAttack = false;
  124.                     attackTimer = 0;
  125.                     isPlayerAttack = false;
  126.                     isBuildAttack = false;
  127.                     SharkIdle();
  128.                 }
  129.  
  130.                 if (!canAttack)
  131.                 {
  132.                     attackTimer += Time.deltaTime;
  133.                     if (attackTimer >= 50)
  134.                     { // 250 time before next shark attack
  135.                         canAttack = true;
  136.                         attackTimer = 0;
  137.                     }
  138.                 }
  139.             }
  140.         }
  141.     }
  142.  
  143.  
  144.     private void SharkIdle()
  145.     {
  146.         if (woodParticle.activeSelf)
  147.         {
  148.             woodParticle.SetActive(false);
  149.             audioSource.enabled = false;
  150.         }
  151.  
  152.         isObstacle = false;
  153.         currentSpeed = defaultSpeed;
  154.         animator.SetFloat("SharkState", 0);
  155.  
  156.         newWaypointlocation = lastPlayerPosition + Random.insideUnitSphere * Random.Range(20, 50);
  157.         Vector3 TP = transform.position;
  158.         TP.x = newWaypointlocation.x;
  159.         newWaypointlocation.y = -0.2f;
  160.         TP.z = newWaypointlocation.z;
  161.     }
  162.  
  163.     public void SharkAttack()
  164.     {
  165.         if (target == null)
  166.             return;
  167.  
  168.         if(!isPlayerAttack)
  169.         {
  170.             animator.SetFloat("SharkState", 1);
  171.             currentSpeed += defaultSpeed;
  172.             isPlayerAttack = true;
  173.         }
  174.  
  175.         newWaypointlocation = new Vector3(target.position.x, 0.05f, target.position.z+2f);
  176.     }
  177.  
  178.     public void AttackRaft()
  179.     {
  180.         if (!isPlayerAttack && canAttack && !isBuildAttack)
  181.         {
  182.             currentSpeed = defaultSpeed;
  183.             isBuildAttack = true;
  184.  
  185.             newWaypointlocation = new Vector3(transform.position.x, transform.position.y + 0.15f, transform.position.z);
  186.  
  187.             if (!woodParticle.activeSelf)
  188.             {
  189.                 woodParticle.SetActive(true);
  190.                 audioSource.enabled = true;
  191.                 animator.SetFloat("SharkState", 1);
  192.             }
  193.  
  194.         }
  195.     }
  196.  
  197.     private IEnumerator SharkDie()
  198.     {
  199.         newWaypointlocation = new Vector3(transform.position.x, -2f, transform.position.z);
  200.  
  201.         animator.SetFloat("SharkState", 2);
  202.  
  203.         photonView.RPC("RpcSharkLoot",
  204.         PhotonTargets.MasterClient,
  205.         new Vector3(transform.position.x, transform.position.y, transform.position.z)
  206.         );
  207.  
  208.         yield return new WaitForSeconds(5f);
  209.  
  210.         ResetShark();
  211.     }
  212.  
  213.  
  214.     private void ResetShark()
  215.     {
  216.         isPlayerAttack = false;
  217.         isBuildAttack = false;
  218.         canAttack = false;
  219.         sharkDie = false;
  220.         attackTimer = 0f;
  221.         transform.position = new Vector3(Random.Range(-10, 10), -2f, Random.Range(-10, 10));
  222.  
  223.         dmgCount = 0;
  224.         health = 150;
  225.         SharkIdle();
  226.     }
  227.  
  228.     [PunRPC]
  229.     void RpcSharkLoot(Vector3 pos)
  230.     {
  231.         PhotonNetwork.Instantiate
  232.             (
  233.                 "Prefabs/Items/SharkMeat",
  234.                 pos,
  235.                 Quaternion.identity,
  236.                 0,
  237.                 null
  238.             );
  239.  
  240.     }
  241.  
  242.     public void CheckObstracle()
  243.     {
  244.         if (!isBuildAttack)
  245.             isObstacle = true;
  246.     }
  247.  
  248.  
  249.  
  250.     public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
  251.     {
  252.         if (animator == null)
  253.             return;
  254.  
  255.         if (stream.isWriting)
  256.         {
  257.             stream.SendNext(animator.GetFloat("SharkState"));
  258.             stream.SendNext(isPlayerAttack);
  259.             stream.SendNext(isBuildAttack);
  260.             stream.SendNext(health);
  261.             stream.SendNext(dmgCount);
  262.             stream.SendNext(sharkDie);
  263.             stream.SendNext(woodParticle.activeSelf);
  264.             stream.SendNext(audioSource.enabled);
  265.         }
  266.         else
  267.         {
  268.             this.animator.SetFloat("SharkState", (float)stream.ReceiveNext());
  269.             this.isPlayerAttack = (bool)stream.ReceiveNext();
  270.             isBuildAttack = (bool)stream.ReceiveNext();
  271.             health = (float)stream.ReceiveNext();
  272.             dmgCount = (int)stream.ReceiveNext();
  273.             this.sharkDie = (bool)stream.ReceiveNext();
  274.             this.woodParticle.SetActive((bool)stream.ReceiveNext());
  275.             this.audioSource.enabled = (bool)stream.ReceiveNext();
  276.         }
  277.     }
  278.  
  279.  
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement