Boss.cs 3.35 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using UnityEngine;
using System.Collections;

public class Boss : MonoBehaviour {

    private Rigidbody2D rb;
    public float speed = 1f;
    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;
18
19
20
21
22
23
24
25
26
    public Color damageColor;
    public float damageColorSpeed = 8;
    private SpriteRenderer sr;
    private Animator animator;

    GameObject player;

    private bool triggered = false;
    public float triggerDist = 10f;
Andrew Van Buren's avatar
Andrew Van Buren committed
27
28
29
30
31

    // Use this for initialization
    void Start () {
        waypoints = GameObject.FindGameObjectsWithTag("Waypoint");
        rb = GetComponent<Rigidbody2D>();
32
33
34
        sr = GetComponent<SpriteRenderer>();
        animator = GetComponent<Animator>();
        player = GameObject.FindGameObjectWithTag("Player");
Andrew Van Buren's avatar
Andrew Van Buren committed
35
        PickWaypoint();
36
        rb.isKinematic = true;
37
38
39
40
    }
	
	// Update is called once per frame
	void Update () {
41
        if (currentWaypoint && triggered) {
Andrew Van Buren's avatar
Andrew Van Buren committed
42
43
44
45
46
47
            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;
                rb.AddForce(Vector2.ClampMagnitude(force, speed));
48
49
50
            }
        }
        
Andrew Van Buren's avatar
Andrew Van Buren committed
51
52
        if(Time.time > lastAttack) {
            lastAttack = Time.time + attackCooldown + Random.Range(0, attackRandom);
53
54
            StartCoroutine(Attack());
        }
Andrew Van Buren's avatar
Andrew Van Buren committed
55

56
57
58
59
60
        sr.color = Color.Lerp(sr.color, Color.white, Time.deltaTime * damageColorSpeed);
        if(Vector2.Distance(player.transform.position, transform.position) < triggerDist) {
            triggered = true;
            animator.SetBool("Moving", true);
            rb.isKinematic = false;
Andrew Van Buren's avatar
Andrew Van Buren committed
61
        }
62
63
    }

Andrew Van Buren's avatar
Andrew Van Buren committed
64
65
    void PickWaypoint() {
        currentWaypoint = waypoints[Random.Range(0, waypoints.Length)].transform;
66
67
    }

Andrew Van Buren's avatar
Andrew Van Buren committed
68
69
70
71
72
73
74
    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);
75
76
    }

Andrew Van Buren's avatar
Andrew Van Buren committed
77
    void GetHit(int amount) {
78
79
80
81
82
83
        if (triggered) {
            health -= health;
            sr.color = damageColor;
            if (health <= 0) {
                Die();
            }
Andrew Van Buren's avatar
Andrew Van Buren committed
84
        }
85
86
    }

Andrew Van Buren's avatar
Andrew Van Buren committed
87
88
89
    void Die() {
        Instantiate(diety, transform.position, Quaternion.identity);
        Destroy(gameObject);
90
    }
91
92
93
94
95
96
97
98
99
100
101
102
103

    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);
        }
    }
104
}