Boss.cs 4.28 KB
Newer Older
1 2 3 4 5 6
using UnityEngine;
using System.Collections;

public class Boss : MonoBehaviour {

    private Rigidbody2D rb;
7 8 9
    public float speed = 75f;
    public float dangerSpeedMultiplier = 2f;
    public int dangerTheshold = 10;
10 11 12 13 14 15 16 17
    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;
18
    public AudioClip attackSound;
19 20
    public int health = 100;
    public GameObject diety;
21 22
    public Color damageColor;
    public float damageColorSpeed = 8;
23
    public AudioClip damageSound;
24
    public Color dangerColor;
25 26 27
    private SpriteRenderer sr;
    private Animator animator;

28 29 30
    public AudioClip bossMusic;
    private AudioSource bgm;

31 32
    private AudioSource audioSource;

33 34 35 36
    GameObject player;

    private bool triggered = false;
    public float triggerDist = 10f;
Andrew Van Buren's avatar
Andrew Van Buren committed
37 38 39 40 41

    // Use this for initialization
    void Start () {
        waypoints = GameObject.FindGameObjectsWithTag("Waypoint");
        rb = GetComponent<Rigidbody2D>();
42 43 44
        sr = GetComponent<SpriteRenderer>();
        animator = GetComponent<Animator>();
        player = GameObject.FindGameObjectWithTag("Player");
Andrew Van Buren's avatar
Andrew Van Buren committed
45
        PickWaypoint();
46
        rb.isKinematic = true;
47
        bgm = Camera.main.GetComponentInChildren<AudioSource>();
48
        audioSource = GetComponent<AudioSource>();
49 50 51 52
    }
	
	// Update is called once per frame
	void Update () {
53
        if (currentWaypoint && triggered) {
Andrew Van Buren's avatar
Andrew Van Buren committed
54 55 56 57
            if (Vector2.Distance(transform.position, currentWaypoint.position) < distanceThreshold) {
                PickWaypoint();
            } else {
                Vector2 dir = currentWaypoint.position - transform.position;
58 59
                Vector2 force = new Vector2(dir.x, dir.y) * speed * (health < dangerTheshold ? dangerSpeedMultiplier : 1);
                rb.AddForce(Vector2.ClampMagnitude(force, speed * (health < dangerTheshold ? dangerSpeedMultiplier : 1)));
60 61 62
            }
        }
        
Andrew Van Buren's avatar
Andrew Van Buren committed
63
        if(Time.time > lastAttack) {
64
            lastAttack = Time.time + (attackCooldown + Random.Range(0, attackRandom)) / (health < dangerTheshold ? dangerSpeedMultiplier : 1);
65 66
            StartCoroutine(Attack());
        }
Andrew Van Buren's avatar
Andrew Van Buren committed
67

68 69 70
        // Set color
        sr.color = Color.Lerp(sr.color, (health < dangerTheshold ? dangerColor : Color.white), Time.deltaTime * damageColorSpeed);

71
        if(Vector2.Distance(player.transform.position, transform.position) < triggerDist) {
72 73 74 75
            if (!triggered) {
                bgm.clip = bossMusic;
                bgm.Play();
            }
76 77 78
            triggered = true;
            animator.SetBool("Moving", true);
            rb.isKinematic = false;
Andrew Van Buren's avatar
Andrew Van Buren committed
79
        }
80 81
    }

Andrew Van Buren's avatar
Andrew Van Buren committed
82 83
    void PickWaypoint() {
        currentWaypoint = waypoints[Random.Range(0, waypoints.Length)].transform;
84 85
    }

Andrew Van Buren's avatar
Andrew Van Buren committed
86 87 88 89 90 91 92
    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<Rigidbody2D>().velocity = vel;
        projectile_clone.transform.eulerAngles = new Vector3(0, 0, angle);
93 94
    }

Andrew Van Buren's avatar
Andrew Van Buren committed
95
    void GetHit(int amount) {
96
        if (triggered) {
97 98
            if (damageSound)
                audioSource.PlayOneShot(damageSound);
99
            health -= amount;
100 101 102 103
            sr.color = damageColor;
            if (health <= 0) {
                Die();
            }
Andrew Van Buren's avatar
Andrew Van Buren committed
104
        }
105 106
    }

Andrew Van Buren's avatar
Andrew Van Buren committed
107
    void Die() {
108
        bgm.Stop();
Andrew Van Buren's avatar
Andrew Van Buren committed
109 110
        Instantiate(diety, transform.position, Quaternion.identity);
        Destroy(gameObject);
111
    }
112 113 114 115

    IEnumerator Attack() {
        if (triggered) {
            animator.SetBool("Attacking", true);
116 117
            if(attackSound)
                audioSource.PlayOneShot(attackSound);
118 119 120 121 122 123 124 125 126
            yield return new WaitForSeconds(0.3f);
            Shoot(0);
            Shoot(90);
            Shoot(180);
            Shoot(270);
            yield return new WaitForSeconds(0.3f);
            animator.SetBool("Attacking", false);
        }
    }
127
}