using UnityEngine; using System.Collections; [RequireComponent(typeof(Rigidbody2D))] public class PlayerController : InteractableObject { public GameObject bullet; public float speed; public float spinSpeed = 5f; public int health; public Renderer healthMat; public Color[] healthColor; public GameObject Explosion; public ParticleSystem boostEffect; private int bulletDirection = -1; private Rigidbody2D rigid; private float lastHit = 0.0f; private bool isImmobilized = false; // Use this for initialization void Start() { rigid = GetComponent(); rigid.AddForce(transform.right * 200); } // Update is called once per frame void Update() { } public void rotatePlayer() { if (isImmobilized) return; rigid.velocity = Vector3.zero; transform.Rotate(new Vector3(0, 0, spinSpeed) * 50 * Time.deltaTime); } public void movePlayer() { if (isImmobilized) return; //Debug.Log(transform.rotation.eulerAngles.z * Mathf.Deg2Rad); boostEffect.startRotation = -transform.rotation.eulerAngles.z * Mathf.Deg2Rad; boostEffect.Play(); rigid.AddForce(transform.right * speed,ForceMode2D.Impulse); } GameObject findClosestTarget() { GameObject[] targets = GameObject.FindGameObjectsWithTag("Enemy"); GameObject tMin = null; float minDist = Mathf.Infinity; Vector3 currentPos = transform.position; foreach (GameObject t in targets) { float dist = Vector3.Distance(t.transform.position, currentPos); if (dist < minDist) { tMin = t; minDist = dist; } } return tMin; } public void shoot() { if (isImmobilized) return; GameObject bulletClone = (GameObject)Instantiate(bullet, transform.position + (transform.up * bulletDirection * 0.1f), transform.rotation); MissileController bulletScript = bulletClone.GetComponent(); bulletScript.startVelocity = transform.up * bulletDirection; bulletDirection = -bulletDirection; bulletScript.ignoreList = GetComponents(); bulletScript.target = findClosestTarget(); bulletScript.enabled = true; } private void ImmobilizePlayer() { isImmobilized = true; rigid.angularDrag = 0; rigid.drag = 1; rigid.constraints = RigidbodyConstraints2D.None; } public override void shot() { if (Time.time - lastHit < 1) { return; } health--; if (isImmobilized) { killPlayer(); } if (health <= 0) { ImmobilizePlayer(); health = 0; } healthMat.material.color = healthColor[3-health]; lastHit = Time.time; } private void killPlayer() { if (Explosion != null) DontDestroyOnLoad(Instantiate(Explosion, transform.position, transform.rotation)); GameMemory.control.playerDeath(); Destroy(this.gameObject, 0.1f); } public void setHealth(int input) { health = input; healthMat.material.color = healthColor[3 - health]; } }