From 5440734d820606b29a795c46172ade5d7eba8efb Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Mon, 3 Sep 2018 23:34:21 +0100 Subject: Initial commit --- Assets/Scripts/Brain.cs | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Assets/Scripts/Brain.cs (limited to 'Assets/Scripts/Brain.cs') diff --git a/Assets/Scripts/Brain.cs b/Assets/Scripts/Brain.cs new file mode 100644 index 0000000..36c2493 --- /dev/null +++ b/Assets/Scripts/Brain.cs @@ -0,0 +1,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(); + + facingRight = !GetComponent().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(); + wrb.velocity = waveVelocity * (facingRight ? 1 : -1); + + Idle(); + } +} -- cgit v1.2.1