Boss.cs 2.22 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;
Andrew Van Buren's avatar
Andrew Van Buren committed
18
19
20
21
22
23

    // Use this for initialization
    void Start () {
        waypoints = GameObject.FindGameObjectsWithTag("Waypoint");
        rb = GetComponent<Rigidbody2D>();
        PickWaypoint();
24
25
26
27
    }
	
	// Update is called once per frame
	void Update () {
Andrew Van Buren's avatar
Andrew Van Buren committed
28
29
30
31
32
33
34
        if (currentWaypoint) {
            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));
35
36
37
            }
        }
        
Andrew Van Buren's avatar
Andrew Van Buren committed
38
39
40
41
42
43
44
45
        if(Time.time > lastAttack) {
            lastAttack = Time.time + attackCooldown + Random.Range(0, attackRandom);

            Shoot(0);
            Shoot(90);
            Shoot(180);
            Shoot(270);
        }
46
47
    }

Andrew Van Buren's avatar
Andrew Van Buren committed
48
49
    void PickWaypoint() {
        currentWaypoint = waypoints[Random.Range(0, waypoints.Length)].transform;
50
51
    }

Andrew Van Buren's avatar
Andrew Van Buren committed
52
53
54
55
56
57
58
    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);
59
60
    }

Andrew Van Buren's avatar
Andrew Van Buren committed
61
62
63
64
65
    void GetHit(int amount) {
        health -= health;
        if (health <= 0) {
            Die();
        }
66
67
    }

Andrew Van Buren's avatar
Andrew Van Buren committed
68
69
70
    void Die() {
        Instantiate(diety, transform.position, Quaternion.identity);
        Destroy(gameObject);
71
72
    }
}