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.

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