using UnityEngine; using System.Collections; public class Boss : MonoBehaviour { private Rigidbody2D rb; public float speed = 75f; public float dangerSpeedMultiplier = 2f; public int dangerTheshold = 10; public GameObject[] waypoints; private Transform currentWaypoint; public float distanceThreshold = 0.5f; public GameObject projectile; public float attackCooldown = 2.0f; public float attackRandom = 1.0f; private float lastAttack = 0; public float projectileSpeed = 5; public AudioClip attackSound; public int health = 100; public GameObject diety; public Color damageColor; public float damageColorSpeed = 8; public AudioClip damageSound; public Color dangerColor; private SpriteRenderer sr; private Animator animator; public AudioClip bossMusic; private AudioSource bgm; private AudioSource audioSource; GameObject player; private bool triggered = false; public float triggerDist = 10f; // Use this for initialization void Start () { waypoints = GameObject.FindGameObjectsWithTag("Waypoint"); rb = GetComponent(); sr = GetComponent(); animator = GetComponent(); player = GameObject.FindGameObjectWithTag("Player"); PickWaypoint(); rb.isKinematic = true; bgm = Camera.main.GetComponentInChildren(); audioSource = GetComponent(); } // Update is called once per frame void Update () { if (currentWaypoint && triggered) { if (Vector2.Distance(transform.position, currentWaypoint.position) < distanceThreshold) { PickWaypoint(); } else { Vector2 dir = currentWaypoint.position - transform.position; Vector2 force = new Vector2(dir.x, dir.y) * speed * (health < dangerTheshold ? dangerSpeedMultiplier : 1); rb.AddForce(Vector2.ClampMagnitude(force, speed * (health < dangerTheshold ? dangerSpeedMultiplier : 1))); } } if(Time.time > lastAttack) { lastAttack = Time.time + (attackCooldown + Random.Range(0, attackRandom)) / (health < dangerTheshold ? dangerSpeedMultiplier : 1); StartCoroutine(Attack()); } // Set color sr.color = Color.Lerp(sr.color, (health < dangerTheshold ? dangerColor : Color.white), Time.deltaTime * damageColorSpeed); if(Vector2.Distance(player.transform.position, transform.position) < triggerDist) { if (!triggered) { bgm.clip = bossMusic; bgm.Play(); } triggered = true; animator.SetBool("Moving", true); rb.isKinematic = false; } } void PickWaypoint() { currentWaypoint = waypoints[Random.Range(0, waypoints.Length)].transform; } void Shoot(int angle) { float rad = (angle + 90) * Mathf.Deg2Rad; Vector2 vel = new Vector2(Mathf.Cos(rad), Mathf.Sin(rad)) * projectileSpeed; GameObject projectile_clone = Instantiate(projectile, transform.position, Quaternion.identity) as GameObject; projectile_clone.GetComponent().velocity = vel; projectile_clone.transform.eulerAngles = new Vector3(0, 0, angle); } void GetHit(int amount) { if (triggered) { if (damageSound) audioSource.PlayOneShot(damageSound); health -= amount; sr.color = damageColor; if (health <= 0) { Die(); } } } void Die() { bgm.Stop(); Instantiate(diety, transform.position, Quaternion.identity); Destroy(gameObject); } IEnumerator Attack() { if (triggered) { animator.SetBool("Attacking", true); if(attackSound) audioSource.PlayOneShot(attackSound); yield return new WaitForSeconds(0.3f); Shoot(0); Shoot(90); Shoot(180); Shoot(270); yield return new WaitForSeconds(0.3f); animator.SetBool("Attacking", false); } } }