diff options
| author | Tom Harley | 2018-09-03 23:34:21 +0100 |
|---|---|---|
| committer | Tom Harley | 2018-09-03 23:36:01 +0100 |
| commit | 5440734d820606b29a795c46172ade5d7eba8efb (patch) | |
| tree | 85867f40afb02cfa2a5028b515ed0a195866b7e9 /Assets/Scripts/Bullet.cs | |
| download | powerless-5440734d820606b29a795c46172ade5d7eba8efb.tar.gz powerless-5440734d820606b29a795c46172ade5d7eba8efb.zip | |
Initial commit
Diffstat (limited to 'Assets/Scripts/Bullet.cs')
| -rw-r--r-- | Assets/Scripts/Bullet.cs | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/Assets/Scripts/Bullet.cs b/Assets/Scripts/Bullet.cs new file mode 100644 index 0000000..5e8b1fc --- /dev/null +++ b/Assets/Scripts/Bullet.cs @@ -0,0 +1,100 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Bullet : MonoBehaviour { + public string colour = "yellow"; + public float damage = 1f; + + public AudioClip bounceNoise; + public float bounceNoiseVolume = 1.0f; + public AudioClip hitWallNoise; + public float hitWallNoiseVolume = 1.0f; + public AudioClip hitEnemyNoise; + public float hitEnemyNoiseVolume = 1.0f; + public AudioClip genericHitNoise; + public float genericHitNoiseVolume = 1.0f; + + public bool bounceOnWrongDoors = true; + + private bool leftPlayer = false; + + private Rigidbody2D body; + private AudioSource audio; + + void Start() { + body = GetComponent<Rigidbody2D>(); + audio = GetComponent<AudioSource>(); + } + + void Update() { + if (body.velocity.x < 0) { + transform.localScale = new Vector2(-1, 1); + } else if (body.velocity.x > 0) { + transform.localScale = new Vector2(1, 1); + } else { + transform.rotation = Quaternion.Euler(new Vector3(0, 0, 90)); + } + } + + void OnTriggerEnter2D(Collider2D collider) { + if (!leftPlayer) { + leftPlayer = true; + return; + } + + if (genericHitNoise) audio.PlayOneShot(genericHitNoise, genericHitNoiseVolume); + + GameObject other = collider.gameObject; + + switch (other.tag) { + case "Door": + DoorLogic dl = other.GetComponent<DoorLogic>(); + if (dl.colour == this.colour) { + dl.Open(); + Die(); + } else { + if (bounceOnWrongDoors) { this.Bounce(); } else { Die(); } + } + break; + + case "Player": + CharacterControl cc = other.GetComponent<CharacterControl>(); + cc.Damaged(); + Die(); + break; + + case "Enemy": + if (hitEnemyNoise) audio.PlayOneShot(hitEnemyNoise, hitEnemyNoiseVolume); + ICanDie icd = other.GetComponent<ICanDie>(); + if (icd) icd.Hit(damage); + Die(); + break; + + case "Projectile": + // do nothing + break; + + default: + if (hitWallNoise) audio.PlayOneShot(hitWallNoise, hitWallNoiseVolume); + Die(); + break; + } + } + + void Die() { + GetComponent<SpriteRenderer>().enabled = false; + GetComponent<BoxCollider2D>().enabled = false; + + Invoke("ReallyDie", 3f); + } + + void ReallyDie() { + Destroy(this.gameObject); + } + + void Bounce() { + if (bounceNoise) audio.PlayOneShot(bounceNoise, bounceNoiseVolume); + body.velocity = new Vector2(body.velocity.x * -1, body.velocity.y); + } +} |
