using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerControl : MonoBehaviour
{
///
/// 最小速度
///
public float minSpeed = 0.7f;
///
/// 最大速度
///
public float maxSpeed = 1.4f;
///
/// 爆炸特效
///
public GameObject explose;
public float currentValue;
public float maxValue = 100;
public Slider armorSlider;
public Text currentText;
public Text maxText;
private CameraFollow cf;
///
/// 鼠标到飞船的距离
///
private float dis = 0;
///
/// 飞船的速度
///
private float player_speed = 0;
// Use this for initialization
void Start()
{
currentValue = maxValue;
UpdateContent();
cf = GameObject.FindGameObjectWithTag("MainCamera").GetComponent();
}
// 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);
}
}