PlayerControl.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class PlayerControl : MonoBehaviour
  6. {
  7. /// <summary>
  8. /// 最小速度
  9. /// </summary>
  10. public float minSpeed = 0.7f;
  11. /// <summary>
  12. /// 最大速度
  13. /// </summary>
  14. public float maxSpeed = 1.4f;
  15. /// <summary>
  16. /// 爆炸特效
  17. /// </summary>
  18. public GameObject explose;
  19. public float currentValue;
  20. public float maxValue = 100;
  21. public Slider armorSlider;
  22. public Text currentText;
  23. public Text maxText;
  24. private CameraFollow cf;
  25. /// <summary>
  26. /// 鼠标到飞船的距离
  27. /// </summary>
  28. private float dis = 0;
  29. /// <summary>
  30. /// 飞船的速度
  31. /// </summary>
  32. private float player_speed = 0;
  33. // Use this for initialization
  34. void Start()
  35. {
  36. currentValue = maxValue;
  37. UpdateContent();
  38. cf = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<CameraFollow>();
  39. }
  40. // Update is called once per frame
  41. void Update()
  42. {
  43. Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  44. mousePos.z = 0;
  45. dis = Vector2.Distance(new Vector2(transform.position.x, transform.position.y), new Vector2(mousePos.x, mousePos.y));
  46. if (dis <= 0.1f)
  47. {
  48. }
  49. else
  50. {
  51. TurnTo(mousePos);
  52. player_speed = dis * 0.5f;
  53. player_speed = Mathf.Clamp(player_speed, minSpeed, maxSpeed);
  54. transform.Translate(Vector3.up * Time.deltaTime * player_speed);
  55. }
  56. }
  57. void TurnTo(Vector3 pos)
  58. {
  59. Vector3 dir = pos - transform.position;
  60. float theta = Mathf.Atan2(dir.y, dir.x);
  61. float angle = theta * Mathf.Rad2Deg;
  62. transform.localEulerAngles = new Vector3(0, 0, angle - 90);
  63. }
  64. void OnTriggerEnter2D(Collider2D other)
  65. {
  66. if (other.tag == "enemy")
  67. {
  68. Instantiate(explose, other.gameObject.transform.position, Quaternion.identity);
  69. cf.Shake();
  70. GetHurt(50);
  71. Destroy(other.gameObject);
  72. }
  73. }
  74. void GetHurt(int damage)
  75. {
  76. if (currentValue > 0)
  77. {
  78. currentValue -= damage;
  79. UpdateContent();
  80. if (currentValue <= 0)
  81. {
  82. Instantiate(explose, transform.position, Quaternion.identity);
  83. GameManage.Instance.GameOver();
  84. }
  85. }
  86. }
  87. void UpdateContent()
  88. {
  89. currentText.text = currentValue.ToString();
  90. maxText.text = "/" + maxValue.ToString();
  91. armorSlider.value = (currentValue / maxValue);
  92. }
  93. }