Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Shark : Photon.PunBehaviour, IPunObservable
- {
- [SerializeField] float defaultSpeed = 1.9f;
- [SerializeField] GameObject woodParticle;
- [SerializeField] Behaviour componentDisable;
- private float currentSpeed = 0;
- private float attackTimer = 0;
- private float attackTimer2 = 0;
- private bool isPlayerAttack = false;
- private bool isObstacle = false;
- private bool sharkDie = false;
- public static bool isBuildAttack = false;
- public static bool canAttack = false;
- private Vector3 newWaypointlocation;
- private Vector3 lastPlayerPosition = Vector3.zero;
- private Animator animator;
- private AudioSource audioSource;
- public static float health = 150;
- public static int dmgCount = 0;
- private Transform target;
- public Transform Target
- {
- get { return target; }
- set
- {
- target = value;
- if (target == null)
- {
- attackTimer2 = 0;
- canAttack = false;
- attackTimer = 0;
- isPlayerAttack = false;
- }
- else
- lastPlayerPosition = target.position;
- }
- }
- private void Awake()
- {
- animator = GetComponent<Animator>();
- audioSource = GetComponent<AudioSource>();
- }
- private void Start()
- {
- if (PhotonNetwork.isMasterClient)
- {
- ResetShark();
- }
- else
- {
- componentDisable.enabled = false;
- }
- }
- private void Update()
- {
- if(PhotonNetwork.isMasterClient)
- {
- if(isObstacle)
- transform.position = Vector3.MoveTowards(transform.position, new Vector3(newWaypointlocation.x, -1f, newWaypointlocation.z), currentSpeed * Time.deltaTime);
- else
- transform.position = Vector3.MoveTowards(transform.position, newWaypointlocation, currentSpeed * Time.deltaTime);
- Vector3 relativePos = newWaypointlocation - transform.position;
- if(relativePos != Vector3.zero)
- {
- Quaternion targetRotation;
- if (isPlayerAttack)
- {
- targetRotation = Quaternion.LookRotation((new Vector3(target.position.x, 0.3f, target.position.z) - transform.position).normalized);
- }
- else
- {
- targetRotation = Quaternion.LookRotation(relativePos.normalized, Vector3.up);
- }
- transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, currentSpeed * Time.deltaTime);
- }
- if(!sharkDie)
- {
- if (Vector3.Distance(transform.position, newWaypointlocation) < 2)
- {
- if (!isBuildAttack && !isPlayerAttack)
- SharkIdle();
- }
- if(!isBuildAttack && target != null)
- {
- attackTimer2 += Time.deltaTime;
- if (attackTimer2 >= 5)
- {
- SharkAttack();
- }
- }
- if (health < 0)
- {
- health = 0;
- sharkDie = true;
- StartCoroutine(SharkDie());
- }
- if (dmgCount >= 5)
- {
- dmgCount = 0;
- canAttack = false;
- attackTimer = 0;
- isPlayerAttack = false;
- isBuildAttack = false;
- SharkIdle();
- }
- if (!canAttack)
- {
- attackTimer += Time.deltaTime;
- if (attackTimer >= 50)
- { // 250 time before next shark attack
- canAttack = true;
- attackTimer = 0;
- }
- }
- }
- }
- }
- private void SharkIdle()
- {
- if (woodParticle.activeSelf)
- {
- woodParticle.SetActive(false);
- audioSource.enabled = false;
- }
- isObstacle = false;
- currentSpeed = defaultSpeed;
- animator.SetFloat("SharkState", 0);
- newWaypointlocation = lastPlayerPosition + Random.insideUnitSphere * Random.Range(20, 50);
- Vector3 TP = transform.position;
- TP.x = newWaypointlocation.x;
- newWaypointlocation.y = -0.2f;
- TP.z = newWaypointlocation.z;
- }
- public void SharkAttack()
- {
- if (target == null)
- return;
- if(!isPlayerAttack)
- {
- animator.SetFloat("SharkState", 1);
- currentSpeed += defaultSpeed;
- isPlayerAttack = true;
- }
- newWaypointlocation = new Vector3(target.position.x, 0.05f, target.position.z+2f);
- }
- public void AttackRaft()
- {
- if (!isPlayerAttack && canAttack && !isBuildAttack)
- {
- currentSpeed = defaultSpeed;
- isBuildAttack = true;
- newWaypointlocation = new Vector3(transform.position.x, transform.position.y + 0.15f, transform.position.z);
- if (!woodParticle.activeSelf)
- {
- woodParticle.SetActive(true);
- audioSource.enabled = true;
- animator.SetFloat("SharkState", 1);
- }
- }
- }
- private IEnumerator SharkDie()
- {
- newWaypointlocation = new Vector3(transform.position.x, -2f, transform.position.z);
- animator.SetFloat("SharkState", 2);
- photonView.RPC("RpcSharkLoot",
- PhotonTargets.MasterClient,
- new Vector3(transform.position.x, transform.position.y, transform.position.z)
- );
- yield return new WaitForSeconds(5f);
- ResetShark();
- }
- private void ResetShark()
- {
- isPlayerAttack = false;
- isBuildAttack = false;
- canAttack = false;
- sharkDie = false;
- attackTimer = 0f;
- transform.position = new Vector3(Random.Range(-10, 10), -2f, Random.Range(-10, 10));
- dmgCount = 0;
- health = 150;
- SharkIdle();
- }
- [PunRPC]
- void RpcSharkLoot(Vector3 pos)
- {
- PhotonNetwork.Instantiate
- (
- "Prefabs/Items/SharkMeat",
- pos,
- Quaternion.identity,
- 0,
- null
- );
- }
- public void CheckObstracle()
- {
- if (!isBuildAttack)
- isObstacle = true;
- }
- public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
- {
- if (animator == null)
- return;
- if (stream.isWriting)
- {
- stream.SendNext(animator.GetFloat("SharkState"));
- stream.SendNext(isPlayerAttack);
- stream.SendNext(isBuildAttack);
- stream.SendNext(health);
- stream.SendNext(dmgCount);
- stream.SendNext(sharkDie);
- stream.SendNext(woodParticle.activeSelf);
- stream.SendNext(audioSource.enabled);
- }
- else
- {
- this.animator.SetFloat("SharkState", (float)stream.ReceiveNext());
- this.isPlayerAttack = (bool)stream.ReceiveNext();
- isBuildAttack = (bool)stream.ReceiveNext();
- health = (float)stream.ReceiveNext();
- dmgCount = (int)stream.ReceiveNext();
- this.sharkDie = (bool)stream.ReceiveNext();
- this.woodParticle.SetActive((bool)stream.ReceiveNext());
- this.audioSource.enabled = (bool)stream.ReceiveNext();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement