123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class PlayerControl : MonoBehaviour
- {
- /// <summary>
- /// 最小速度
- /// </summary>
- public float minSpeed = 0.7f;
- /// <summary>
- /// 最大速度
- /// </summary>
- public float maxSpeed = 1.4f;
- /// <summary>
- /// 爆炸特效
- /// </summary>
- public GameObject explose;
- public float currentValue;
- public float maxValue = 100;
- public Slider armorSlider;
- public Text currentText;
- public Text maxText;
- private CameraFollow cf;
- /// <summary>
- /// 鼠标到飞船的距离
- /// </summary>
- private float dis = 0;
- /// <summary>
- /// 飞船的速度
- /// </summary>
- private float player_speed = 0;
- // Use this for initialization
- void Start()
- {
- currentValue = maxValue;
- UpdateContent();
-
- cf = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<CameraFollow>();
- }
- // Update is called once per frame
- void Update()
- {
- Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
- mousePos.z = 0;
- dis = Vector2.Distance(new Vector2(transform.position.x, transform.position.y), new Vector2(mousePos.x, mousePos.y));
- if (dis <= 0.1f)
- {
- }
- else
- {
- TurnTo(mousePos);
- player_speed = dis * 0.5f;
- player_speed = Mathf.Clamp(player_speed, minSpeed, maxSpeed);
- transform.Translate(Vector3.up * Time.deltaTime * player_speed);
- }
- }
- void TurnTo(Vector3 pos)
- {
- Vector3 dir = pos - transform.position;
- float theta = Mathf.Atan2(dir.y, dir.x);
- float angle = theta * Mathf.Rad2Deg;
- transform.localEulerAngles = new Vector3(0, 0, angle - 90);
- }
- void OnTriggerEnter2D(Collider2D other)
- {
- if (other.tag == "enemy")
- {
-
- Instantiate(explose, other.gameObject.transform.position, Quaternion.identity);
- cf.Shake();
- GetHurt(50);
- Destroy(other.gameObject);
- }
- }
- void GetHurt(int damage)
- {
- if (currentValue > 0)
- {
- currentValue -= damage;
- UpdateContent();
- if (currentValue <= 0)
- {
- Instantiate(explose, transform.position, Quaternion.identity);
- GameManage.Instance.GameOver();
- }
- }
- }
- void UpdateContent()
- {
- currentText.text = currentValue.ToString();
- maxText.text = "/" + maxValue.ToString();
- armorSlider.value = (currentValue / maxValue);
- }
- }
|