Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Audio;
- using UnityEngine.UIElements;
- public class PlayerController : MonoBehaviour
- {
- public float rotationSpeed;
- public float flightSpeed;
- private Camera mainCamera;
- private bool flying = false;
- public GameObject bullet;
- public GameObject bomb;
- private float bulletFireDelay = .2f;
- private float bulletCooldownRemaining;
- private bool bulletCooldown = false;
- private float bombFireDelay = 3f;
- private float bombCooldownRemaining;
- private bool bombCooldown = false;
- private GameObject bulletSpawner;
- private GameObject bombSpawner;
- public GameObject explosion;
- public ParticleSystem smoke;
- public ParticleSystem fire;
- public bool dead = false;
- bool crashed = false;
- private Rigidbody rb;
- private DebrisSpawner debrisSpawner;
- private float bombGrace = .3f;
- private bool bombTouch = false;
- private bool stalled = false;
- public float debugXAngle;
- public float debugYAngle;
- // Start is called before the first frame update
- void Start()
- {
- mainCamera = GameObject.Find("Main Camera").GetComponent<Camera>();
- bulletSpawner = GameObject.Find("BulletSpawner");
- bombSpawner = GameObject.Find("BombSpawner");
- bulletCooldownRemaining = bulletFireDelay;
- rb = GetComponent<Rigidbody>();
- debrisSpawner = GameObject.FindAnyObjectByType<DebrisSpawner>();
- }
- // Update is called once per frame
- void Update()
- {
- debugXAngle = transform.eulerAngles.x;
- debugYAngle = transform.eulerAngles.y;
- if (!dead)
- {
- if (Input.GetKey(KeyCode.W) && flying && !stalled)
- {
- transform.Rotate(-rotationSpeed * Time.deltaTime, 0, 0, Space.World);
- }
- else if (Input.GetKey(KeyCode.S) && flying && !stalled)
- {
- transform.Rotate(rotationSpeed * Time.deltaTime, 0, 0, Space.World);
- }
- if (Input.GetKey(KeyCode.D) && flightSpeed <= 20)
- {
- flightSpeed += 10 * Time.deltaTime;
- if (flightSpeed >= 15)
- flying = true;
- }
- else if (Input.GetKey(KeyCode.A) && flightSpeed >= 0)
- {
- flightSpeed -= 10 * Time.deltaTime;
- }
- if (Input.GetKeyDown(KeyCode.Q) && flying && !stalled)
- {
- StartCoroutine("Flip");
- }
- if (Input.GetKey(KeyCode.Space))
- {
- Shoot();
- }
- if (Input.GetKeyDown(KeyCode.E))
- {
- BombsAway();
- }
- }
- if (dead && !crashed) //keep plane tangent to the balistic arc while dead and crashing
- {
- // Create a rotation that looks along the velocity vector
- Quaternion lookRotation = Quaternion.LookRotation(rb.velocity);
- // Convert to Euler angles, modify to lock the y-axis rotation
- Vector3 euler = lookRotation.eulerAngles;
- euler.z = transform.rotation.eulerAngles.z; // Lock y-axis rotation
- // Convert back to Quaternion and apply to the transform
- transform.rotation = Quaternion.Euler(euler);
- }
- else if (transform.position.y >= 55 || (flightSpeed <= 10 && flying) && !stalled)
- {
- Stall();
- }
- else if (stalled)
- {
- transform.Rotate(0f, 180f * Time.deltaTime, 0f, Space.World);
- if(flightSpeed >= 15)
- {
- rb.velocity = Vector3.zero;
- rb.angularVelocity = Vector3.zero;
- stalled = false;
- StartCoroutine("EndStall");
- }
- }
- else //normal powered movement
- {
- if((System.Math.Abs(0 - transform.eulerAngles.y) < .001f) || (System.Math.Abs(180 - transform.eulerAngles.y) < .001f))
- transform.Translate(0, 0, flightSpeed * Time.deltaTime, Space.Self);
- }
- if (bulletCooldown) //controls rate of gunfire
- {
- bulletCooldownRemaining -= Time.deltaTime;
- if (bulletCooldownRemaining < 0)
- {
- bulletCooldown = false;
- bulletCooldownRemaining = bulletFireDelay;
- }
- }
- if (bombCooldown) //controls rate of bomb use
- {
- bombCooldownRemaining -= Time.deltaTime;
- if (bombCooldownRemaining < 0)
- {
- bombCooldown = false;
- bombCooldownRemaining = bombFireDelay;
- }
- }
- if(bombTouch) //controls grace period between touching a bomb and actually impacting it
- {
- bombGrace -= Time.deltaTime;
- if (bombGrace < -1)
- {
- bombGrace = .3f;
- bombTouch = false;
- }
- }
- }
- private void Stall()
- {
- flightSpeed = 0;
- stalled = true;
- rb.useGravity = true;
- StartCoroutine("StartStall");
- }
- IEnumerator StartStall()
- {
- float angle;
- if (transform.eulerAngles.x >= 0 && transform.eulerAngles.x <= 90)
- angle = 30 - transform.eulerAngles.x;
- else
- angle = 390 - transform.eulerAngles.x;
- if (transform.eulerAngles.z > 0)
- angle *= -1f;
- iTween.RotateAdd(gameObject, iTween.Hash("x", angle, "time", 1f, "islocal", true));
- yield return null;
- }
- IEnumerator EndStall()
- {
- float angleYDelta;
- float angleYTarget;
- float angleZDelta;
- float angleZTarget;
- if (transform.eulerAngles.y > 0 && transform.eulerAngles.y <= 180)
- {
- angleYDelta = 180 - transform.eulerAngles.y;
- angleYTarget = 180;
- }
- else
- {
- angleYDelta = 360 - transform.eulerAngles.y;
- angleYTarget = 0;
- }
- if (transform.eulerAngles.z > 0 && transform.eulerAngles.z <= 180)
- {
- angleZDelta = 180 - transform.eulerAngles.z;
- angleZTarget = 180;
- }
- else
- {
- angleZDelta = 360 - transform.eulerAngles.z;
- angleZTarget = 0;
- }
- //rb.velocity = Vector3.zero;
- rb.angularVelocity = Vector3.zero;
- iTween.RotateAdd(gameObject, iTween.Hash("y", angleYDelta, "z", angleZDelta, "time", 1f, "islocal", true));
- Vector3 stableAngle = new Vector3(transform.eulerAngles.x, angleYTarget, angleZTarget);
- transform.eulerAngles = stableAngle;
- rb.velocity = Vector3.zero;
- rb.angularVelocity = Vector3.zero;
- rb.useGravity = false;
- yield return null;
- }
- private void LateUpdate()
- {
- //track camera with player along z axis
- if (transform.position.z > -215 && transform.position.z < 215)
- {
- mainCamera.transform.position = new Vector3(mainCamera.transform.position.x, mainCamera.transform.position.y, transform.position.z);
- }
- }
- private void OnTriggerEnter(Collider other)
- {
- if (other.CompareTag("Ground"))
- {
- Crash();
- }
- if(other.CompareTag("Bomb"))
- {
- bombTouch = true;
- }
- else if (other.CompareTag("Building"))
- {
- Explode();
- BuildingHealth bh = other.GetComponentInParent<BuildingHealth>();
- bh.Explode();
- }
- else if (other.CompareTag("StorageTank"))
- {
- Explode();
- StorageTankHealth sth = other.GetComponentInParent<StorageTankHealth>();
- sth.Explode();
- }
- else if (!dead)
- {
- Explode();
- }
- }
- private void OnTriggerStay(Collider other)
- {
- if (!dead)
- {
- if (bombTouch && bombGrace <= 0)
- {
- Destroy(other.gameObject);
- Explode();
- }
- }
- }
- private void Explode()
- {
- stalled = false;
- Instantiate(explosion, (transform.forward * 2.5f) + transform.position, explosion.transform.rotation);
- smoke.Play();
- fire.Play();
- dead = true;
- rb.AddForce(transform.forward * flightSpeed, ForceMode.Impulse);
- flightSpeed = 0;
- rb.useGravity = true;
- debrisSpawner.SpawnDebris(transform.position, 5, 3, 0);
- }
- private void Crash()
- {
- stalled = false;
- Instantiate(explosion, (transform.forward * 2.5f) + transform.position, explosion.transform.rotation);
- smoke.Play();
- fire.Play();
- dead = true;
- crashed = true;
- rb.useGravity = false;
- flightSpeed = 0;
- rb.velocity = Vector3.zero;
- debrisSpawner.SpawnDebris((transform.forward * 2.5f) + transform.position, 5, 3, 0);
- }
- IEnumerator Flip()
- {
- iTween.RotateAdd(gameObject, iTween.Hash("z", 180, "islocal", true, "time", 1.0f, "easetype", iTween.EaseType.spring));
- yield return null;
- }
- private void Shoot()
- {
- if (!bulletCooldown)
- {
- Instantiate(bullet, bulletSpawner.transform.position, transform.rotation);
- bulletCooldown = true;
- }
- }
- private void BombsAway()
- {
- if (!bombCooldown)
- {
- Instantiate(bomb, bombSpawner.transform.position, transform.rotation);
- bombCooldown = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement