using Com.LuisPedroFonseca.ProCamera2D; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class PegLegIoController : MonoBehaviour { [SerializeField] float xSpeed = 100f; [SerializeField] float ySpeed = 100f; [SerializeField] AudioClip spawn; [SerializeField] AudioClip turn; [SerializeField] AudioClip pew; [SerializeField] AudioClip ouch; [SerializeField] Sprite center; [SerializeField] Sprite left; [SerializeField] Sprite right; [SerializeField] GameObject bullet1; [SerializeField] GameObject bullet2; [SerializeField] List bullets; [SerializeField] GameObject deathObject; [SerializeField] List fireSpeeds; // per second [SerializeField] float hurtTime = 1.5f; // seconds [SerializeField] int bulletOffset = 8; // pixels [SerializeField] float smoothness = 0.2f; // seconds to converge to mouse position [SerializeField] float invincibleTime = 2f; // seconds [SerializeField] int health = 1; [SerializeField] Vector2 spawnPoint; [SerializeField] float spawnTime = 1f; // seconds Material material; AudioSource audioSource; Rigidbody2D body; PLIGameController game; SpriteRenderer sprite; SpriteRenderer poomSprite; bool isFiring = false; bool isHurting = false; bool wantFire = false; bool invincible = false; bool isFacingDown = false; bool isSpawning = true; int border = 32; int w = 256; int h = 256; // Start is called before the first frame update void Start() { game = GameObject.FindGameObjectWithTag("PegLegIoGame").GetComponent(); material = GetComponent().material; audioSource = game.GetComponent(); body = GetComponent(); sprite = GetComponent(); poomSprite = transform.GetChild(0).GetComponent(); invincible = true; Invoke("InvincibleOff", invincibleTime); Invoke("SpawnOff", spawnTime); audioSource.PlayOneShot(spawn); poomSprite.color = Color.clear; } void SpawnOff() { isSpawning = false; } void InvincibleOff() { invincible = false; } Vector2 Clamp(Vector2 v) { return new Vector2( Mathf.Clamp(v.x, -1 * w / 2 + border, w / 2 - border), Mathf.Clamp(v.y, -1 * h / 2 + border, h / 2 - border)); } private void SpawnBullet(GameObject proto, Vector2 delta, float angle = 0) { GameObject bullet = Instantiate(proto); bullet.transform.position = new Vector2( transform.position.x + delta.x, transform.position.y + delta.y); bullet.GetComponent().InitializeBullet(isFacingDown, angle); } IEnumerator Poom() { poomSprite.color = new Color(1f, 1f, 1f, 0.5f); yield return new WaitForSeconds(0.05f); poomSprite.color = Color.clear; } IEnumerator FirePattern1() { const float yDeltaFar = 12f; const float xDeltaFar = 4f; const float yDeltaPoom = 4f; const float xDeltaFPoom = 10f; audioSource.PlayOneShot(pew); StartCoroutine(Poom()); SpawnBullet(bullet2, new Vector2(xDeltaFar, yDeltaFar)); SpawnBullet(bullet2, new Vector2(-1f*xDeltaFar, yDeltaFar)); SpawnBullet(bullet1, new Vector2(xDeltaFPoom, yDeltaPoom), Mathf.PI / 128f); SpawnBullet(bullet1, new Vector2(-1f*xDeltaFPoom, yDeltaPoom), -1f * Mathf.PI / 128f); yield return new WaitForSeconds(0.1f); StartCoroutine(Poom()); SpawnBullet(bullet1, new Vector2(xDeltaFPoom, yDeltaPoom), Mathf.PI / 64f); SpawnBullet(bullet1, new Vector2(-1f*xDeltaFPoom, yDeltaPoom), - 1f * Mathf.PI / 64f); yield return new WaitForSeconds(0.1f); StartCoroutine(Poom()); SpawnBullet(bullet1, new Vector2(xDeltaFPoom, yDeltaPoom), Mathf.PI / 32f); SpawnBullet(bullet1, new Vector2(-1f*xDeltaFPoom, yDeltaPoom), -1f * Mathf.PI / 32f); yield return new WaitForSeconds(0.1f); isFiring = false; } IEnumerator Hurt() { if (!isHurting) { if (!invincible) { health -= 1; } Debug.Log("OUCH!"); if (health < 1) { game.StartCoroutine(game.PlayerDie()); Die(); } else { isHurting = true; material.EnableKeyword("GLITCH_ON"); material.EnableKeyword("FLICKER_ON"); audioSource.PlayOneShot(ouch); yield return new WaitForSeconds(hurtTime); isHurting = false; material.DisableKeyword("GLITCH_ON"); material.DisableKeyword("FLICKER_ON"); } } } void Die() { Debug.Log("What a world...!"); GameObject e = Instantiate(deathObject); e.transform.position = transform.position; e.GetComponent().velocity = body.velocity; game.ShakeCamera(); Destroy(gameObject); } private void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.CompareTag("PegLegIoEnemy")) { collision.gameObject.GetComponent().Hurt(2, true); StartCoroutine(Hurt()); } if (collision.gameObject.CompareTag("PegLegIoBullet")) { Destroy(collision.gameObject); StartCoroutine(Hurt()); } } // Update is called once per frame void Update() { if (!isSpawning) { if (!wantFire && Input.GetKeyDown(KeyCode.Z)) { wantFire = true; } if (!isFiring && wantFire) { isFiring = true; wantFire = false; StartCoroutine(FirePattern1()); } float vX = 0, vY = 0; if (Input.GetKey(KeyCode.LeftArrow)) { vX -= xSpeed; } if (Input.GetKey(KeyCode.RightArrow)) { vX += ySpeed; } if (Input.GetKey(KeyCode.DownArrow)) { vY -= ySpeed; } if (Input.GetKey(KeyCode.UpArrow)) { vY += ySpeed; } body.velocity = new Vector2(vX, vY); if (vX < -0.1f) sprite.sprite = left; else if (vX > 0.1f) sprite.sprite = right; else sprite.sprite = center; // DEBUG if (Input.GetMouseButtonDown(2)) { StartCoroutine(Hurt()); } if (Input.GetMouseButtonDown(1)) { isFacingDown = !isFacingDown; audioSource.PlayOneShot(turn); transform.localScale = new Vector2(1.0f, -1 * transform.localScale.y); } } } private void FixedUpdate() { if (isSpawning) { Vector2 vel = body.velocity; Vector2.SmoothDamp(transform.position, Clamp(spawnPoint), ref vel, smoothness); body.velocity = vel; } else { transform.position = Clamp(transform.position); } if (health > 1 || invincible) { material.EnableKeyword("HOLOGRAM_ON"); } else { material.DisableKeyword("HOLOGRAM_ON"); } } }