You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.3 KiB

  1. using UnityEngine;
  2. using System.Collections;
  3. [RequireComponent(typeof(Rigidbody2D))]
  4. public class PlayerController : InteractableObject {
  5. public GameObject bullet;
  6. public float speed;
  7. public float spinSpeed = 5f;
  8. public int health;
  9. public Renderer healthMat;
  10. public Color[] healthColor;
  11. public GameObject Explosion;
  12. public ParticleSystem boostEffect;
  13. private int bulletDirection = -1;
  14. private Rigidbody2D rigid;
  15. private float lastHit = 0.0f;
  16. private bool isImmobilized = false;
  17. // Use this for initialization
  18. void Start() {
  19. rigid = GetComponent<Rigidbody2D>();
  20. rigid.AddForce(transform.right * 200);
  21. }
  22. // Update is called once per frame
  23. void Update() {
  24. }
  25. public void rotatePlayer() {
  26. if (isImmobilized)
  27. return;
  28. rigid.velocity = Vector3.zero;
  29. transform.Rotate(new Vector3(0, 0, spinSpeed) * 50 * Time.deltaTime);
  30. }
  31. public void movePlayer() {
  32. if (isImmobilized)
  33. return;
  34. //Debug.Log(transform.rotation.eulerAngles.z * Mathf.Deg2Rad);
  35. boostEffect.startRotation = -transform.rotation.eulerAngles.z * Mathf.Deg2Rad;
  36. boostEffect.Play();
  37. rigid.AddForce(transform.right * speed,ForceMode2D.Impulse);
  38. }
  39. GameObject findClosestTarget() {
  40. GameObject[] targets = GameObject.FindGameObjectsWithTag("Enemy");
  41. GameObject tMin = null;
  42. float minDist = Mathf.Infinity;
  43. Vector3 currentPos = transform.position;
  44. foreach (GameObject t in targets) {
  45. float dist = Vector3.Distance(t.transform.position, currentPos);
  46. if (dist < minDist) {
  47. tMin = t;
  48. minDist = dist;
  49. }
  50. }
  51. return tMin;
  52. }
  53. public void shoot() {
  54. if (isImmobilized)
  55. return;
  56. GameObject bulletClone = (GameObject)Instantiate(bullet, transform.position + (transform.up * bulletDirection * 0.1f), transform.rotation);
  57. MissileController bulletScript = bulletClone.GetComponent<MissileController>();
  58. bulletScript.startVelocity = transform.up * bulletDirection;
  59. bulletDirection = -bulletDirection;
  60. bulletScript.ignoreList = GetComponents<Collider2D>();
  61. bulletScript.target = findClosestTarget();
  62. bulletScript.enabled = true;
  63. }
  64. private void ImmobilizePlayer() {
  65. isImmobilized = true;
  66. rigid.angularDrag = 0;
  67. rigid.drag = 1;
  68. rigid.constraints = RigidbodyConstraints2D.None;
  69. }
  70. public override void shot() {
  71. if (Time.time - lastHit < 1) {
  72. return;
  73. }
  74. health--;
  75. if (isImmobilized) {
  76. killPlayer();
  77. }
  78. if (health <= 0) {
  79. ImmobilizePlayer();
  80. health = 0;
  81. }
  82. healthMat.material.color = healthColor[3-health];
  83. lastHit = Time.time;
  84. }
  85. private void killPlayer() {
  86. if (Explosion != null)
  87. DontDestroyOnLoad(Instantiate(Explosion, transform.position, transform.rotation));
  88. GameMemory.control.playerDeath();
  89. Destroy(this.gameObject, 0.1f);
  90. }
  91. public void setHealth(int input) {
  92. health = input;
  93. healthMat.material.color = healthColor[3 - health];
  94. }
  95. }