Boss.cs 3.95 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 18 19
    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 int health = 100;
    public GameObject diety;
20 21
    public Color damageColor;
    public float damageColorSpeed = 8;
22
    public Color dangerColor;
23 24 25
    private SpriteRenderer sr;
    private Animator animator;

26 27 28
    public AudioClip bossMusic;
    private AudioSource bgm;

29 30 31 32
    GameObject player;

    private bool triggered = false;
    public float triggerDist = 10f;
Andrew Van Buren's avatar
Andrew Van Buren committed
33 34 35 36 37

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

63 64 65
        // Set color
        sr.color = Color.Lerp(sr.color, (health < dangerTheshold ? dangerColor : Color.white), Time.deltaTime * damageColorSpeed);

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

Andrew Van Buren's avatar
Andrew Van Buren committed
77 78
    void PickWaypoint() {
        currentWaypoint = waypoints[Random.Range(0, waypoints.Length)].transform;
79 80
    }

Andrew Van Buren's avatar
Andrew Van Buren committed
81 82 83 84 85 86 87
    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);
88 89
    }

Andrew Van Buren's avatar
Andrew Van Buren committed
90
    void GetHit(int amount) {
91
        if (triggered) {
92
            health -= amount;
93 94 95 96
            sr.color = damageColor;
            if (health <= 0) {
                Die();
            }
Andrew Van Buren's avatar
Andrew Van Buren committed
97
        }
98 99
    }

Andrew Van Buren's avatar
Andrew Van Buren committed
100
    void Die() {
101
        bgm.Stop();
Andrew Van Buren's avatar
Andrew Van Buren committed
102 103
        Instantiate(diety, transform.position, Quaternion.identity);
        Destroy(gameObject);
104
    }
105 106 107 108 109 110 111 112 113 114 115 116 117

    IEnumerator Attack() {
        if (triggered) {
            animator.SetBool("Attacking", true);
            yield return new WaitForSeconds(0.3f);
            Shoot(0);
            Shoot(90);
            Shoot(180);
            Shoot(270);
            yield return new WaitForSeconds(0.3f);
            animator.SetBool("Attacking", false);
        }
    }
118
}