aboutsummaryrefslogtreecommitdiff
path: root/Assets/Scripts/Brain.cs
blob: 36c2493597d66bedbb9e4a9b395ae372bb17d580 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Brain : MonoBehaviour {
  public float chargeInterval = 2f;
  public float chargeDuration = 1f;

  public GameObject wavePrefab;
  public Vector2 waveSpawnOffset = new Vector2(0, 0);
  public Vector2 waveVelocity = new Vector2(1, 0);

  private bool charging = false;
  private bool facingRight;

  private Animator anim;

  public bool Charging {
    get {
      return charging;
    }

    set {
      charging = value;
      if (anim) anim.SetBool("charging", value);
    }
  }

  void Start() {
    anim = GetComponent<Animator>();

    facingRight = !GetComponent<SpriteRenderer>().flipX;
  }

  void Awake() {
    Idle();
  }

  void Idle() {
    Charging = false;
    Invoke("Charge", chargeInterval);
  }

  void Charge() {
    Charging = true;
    Invoke("Fire", chargeDuration);
  }

  void Fire() {
    Vector2 spawnPosition = this.transform.position + (Vector3)((facingRight ? 1 : -1) * waveSpawnOffset);
    GameObject wave = Instantiate(wavePrefab, spawnPosition, Quaternion.identity);
    Rigidbody2D wrb = wave.GetComponent<Rigidbody2D>();
    wrb.velocity = waveVelocity * (facingRight ? 1 : -1);

    Idle();
  }
}