using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player: MonoBehaviour {
? ? public Animator ani;
? ? public Transform pos;
? ? public LineRenderer lineRenderer;
? ? public UIButton shootBtn;//射擊按鈕
? ? public UIButton skillBtn_1, skillBtn_2, skillBtn_3;//1技能按鈕
? ? public Camera ca;
? ? public int enemyCount = 0;//當(dāng)前擊殺的怪物數(shù)量
? ? public float survivalTime = 0;//存活時(shí)間
? ? public double hp = 20;//人物血量
? ? public double maxHp = 20;//人物最大血量
? ? public GameManager gameManager;
? ? public bool isDie = false;
? ? public static Player instance;
? ? //public UISprite skillMaskSprite_1;//1技能遮罩圖片
? ? //public UILabel skillTimeText_1;//1技能冷卻時(shí)間文本
? ? //public float collingTime = 5;//冷卻時(shí)間
? ? private void Awake()
? ? {
? ? ? ? instance = this;
? ? }
? ? // Use this for initialization
? ? void Start () {
? ? ? ? maxHp = hp = SaveDate.instance.currentAcc.hp;
? ? ? ? ani = GetComponent<Animator>();
? ? ? ? pos = transform.Find("GunBarrelEnd");
? ? ? // lineRenderer = GameObject.Find("LineRenderer").GetComponent<LineRenderer>();
? ? ? ? //lineRenderer.gameObject.SetActive(false);
? ? ? ? //shootBtn = GameObject.Find("UI Root").transform.Find("ShootButton").GetComponent<UIButton>();
? ? ? // shootBtn.onClick.Add(new EventDelegate(ShootBtnEvent));
? ? ? ? //skillBtn_1 = GameObject.Find("UI Root").transform.Find("skill_1").GetComponent<UIButton>();
? ? ? //? skillBtn_2 = GameObject.Find("UI Root").transform.Find("skill_2").GetComponent<UIButton>();
? ? ? // skillBtn_3 = GameObject.Find("UI Root").transform.Find("skill_3").GetComponent<UIButton>();
? ? ? // skillBtn_1.onClick.Add(new EventDelegate(Skill_1));
? ? ? //? skillBtn_2.onClick.Add(new EventDelegate(Skill_1));
? ? ? ? //skillBtn_3.onClick.Add(new EventDelegate(Skill_5));
? ? ? ? gameManager = GameObject.Find("Main Camera").GetComponent<GameManager>();
? ? ? ? ca = GameObject.Find("Main Camera").GetComponent<Camera>();
? ? }
// Update is called once per frame
void Update () {
? ? ? ? if (!isDie)
? ? ? ? {
? ? ? ? ? ? if (Rocker.ins.isPress)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? PlayerMouseMove();
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? PlayerKeyMove();
? ? ? ? ? ? }
? ? ? ? ? ? //Skill_2();
? ? ? ? ? ? //Skill_4();
? ? ? ? ? ? //PlayerShoot();
? ? ? ? ? ? //gameManager.taskManager.taskCtrl.Timer();
? ? ? ? }
? ? ? ?
? ? }
? ? //鍵盤移動(dòng)人物事件
? ? void PlayerKeyMove() {
? ? ? ? float h = Input.GetAxis("Horizontal");//獲取水平軸
? ? ? ? float v = Input.GetAxis("Vertical");//獲取垂直軸
? ? ? ? transform.Translate(new Vector3(h, 0, v) * Time.deltaTime * 3);
? ? ? ? //print(h + "--v--" + v);
? ? ? ? //當(dāng)h或v不等于0處于運(yùn)動(dòng)狀態(tài)
? ? ? ? if (h != 0 || v != 0)
? ? ? ? {
? ? ? ? ? // ani.SetBool("IsMove", true);
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? //ani.SetBool("IsMove", false);
? ? ? ? }
? ? ? ? if (Input.GetKey(KeyCode.Q))
? ? ? ? {
? ? ? ? ? ? transform.Rotate(Vector3.up, -3.0f);
? ? ? ? }
? ? ? ? if (Input.GetKey(KeyCode.E))
? ? ? ? {
? ? ? ? ? ? transform.Rotate(Vector3.up, 3.0f);
? ? ? ? }
? ? ? ?
? ? ? ? //transform.Rotate(Vector3.left, Input.GetAxis("Horizontal"));
? ? ? ? //transform.Rotate(Vector3.right, Input.GetAxis("Vertical"));
? ? }
//鼠標(biāo)搖桿移動(dòng)人物事件
? ? public void PlayerMouseMove() {
? ? ? ? if (Rocker.ins.dir != Vector3.zero)
? ? ? ? {
? ? ? ? ? ? ani.SetBool("IsMove", true);
? ? ? ? ? ? //四元數(shù)? 控制物體旋轉(zhuǎn),可以避免萬向鎖
? ? ? ? ? ? Quaternion que = Quaternion.LookRotation(Rocker.ins.dir);
? ? ? ? ? ? //人物旋轉(zhuǎn)
? ? ? ? ? ? transform.rotation = Quaternion.Lerp(transform.rotation, que,0.3f);
? ? ? ? ? ? //人物移動(dòng)
? ? ? ? ? ? transform.Translate(Rocker.ins.dir * Time.deltaTime * 15, Space.World);
? ? ? ? ? ? //相機(jī)旋轉(zhuǎn)
? ? ? ? ? ? //ca.transform.rotation = transform.rotation;
? ? ? ? ? ? //ca.transform.LookAt(transform.position);
? ? ? ? }
? ? ? ? else {
? ? ? ? //? ? ani.SetBool("IsMove", false);
? ? ? ? }
? ? }
? ? //射擊的方法
? ? void PlayerShoot() {
? ? ? ? if (Input.GetMouseButton(0))
? ? ? ? {
? ? ? ? ? ? //lineRenderer.gameObject.SetActive(true);
? ? ? ? ? ? //StartCoroutine(Des());
? ? ? ? ? ? //實(shí)例化射線
? ? ? ? ? ? Ray ray = new Ray(pos.position, pos.forward);
? ? ? ? //? lineRenderer.SetPosition(0, pos.position);//設(shè)置射線渲染器第一個(gè)點(diǎn)坐標(biāo)
? ? ? ? ? ? RaycastHit hit;//接受射線碰撞的物體信息
? ? ? ? ? ? ? ? ? ? ? ? ? //表示如果碰撞到物體
? ? ? ? ? ? if (Physics.Raycast(ray, out hit))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //hit.point射線撞擊點(diǎn)
? ? ? ? ? ? ? //? lineRenderer.SetPosition(1, hit.point);
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? // lineRenderer.SetPosition(1, pos.position + pos.forward * 10);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? if (Input.GetMouseButtonUp(0)) {
? ? ? ? ? ? //lineRenderer.gameObject.SetActive(false);
? ? ? ? }
? ? }
? ? IEnumerator Des() {
? ? ? ? yield return new WaitForSeconds(0.2f);
? ? ? ? //lineRenderer.gameObject.SetActive(false);
? ? }
? ? public void ShootBtnEvent() { //激光射線特效
? ? ? ? //lineRenderer.gameObject.SetActive(true);
? ? ? ? StartCoroutine(Des());
? ? ? ? Ray ray = new Ray(pos.position, pos.forward);
? ? //? lineRenderer.SetPosition(0, pos.position);
? ? ? ? RaycastHit hit;
? ? ? ? if (Physics.Raycast(ray, out hit))
? ? ? ? {
? ? ? ? ? // lineRenderer.SetPosition(1, hit.point);
? ? ? ? ? ? if (hit.collider.tag == "enemy")
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //hit.collider.GetComponent<EnemyInit>().hit = 1;
? ? ? ? ? ? ? ? //hit.collider.GetComponent<EnemyInit>().Behit();
? ? ? ? ? ? ? ? //hit.collider.GetComponent<EnemyInit>().hit = 0;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? else {
? ? ? ? ? // lineRenderer.SetPosition(1, pos.position + pos.forward * 50);
? ? ? ? }
? ? }
? ? public GameObject effectObj;
? ? public GameObject effect1Obj;
? ? public void Skill_1()
? ? { //自恢復(fù)特效
? ? ? ? //實(shí)例化出來一個(gè)特效
? ? ? ? //GameObject effect = Instantiate(effectObj);
? ? ? ? //給定特效位置
? ? ? ? //effect.transform.position = transform.position;
? ? ? ? //Destroy(effect,1);
? ? }
? ? //定義一個(gè)層級
? ? public LayerMask lm, lm2;
? ? public void Skill_2() { //閃現(xiàn)技能
? ? ? ? //射線檢測位置
? ? ? ? Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
? ? ? ? RaycastHit hit;
? ? ? ? if (Physics.Raycast(ray,out hit,50,lm))
? ? ? ? {
? ? ? ? ? ? if (Input.GetMouseButtonDown(0))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //實(shí)例化出來一個(gè)特效
? ? ? ? ? ? ? ? //GameObject effect = Instantiate(effectObj);
? ? ? ? ? ? ? ? //給定特效位置
? ? ? ? ? ? ? // effect.transform.position = hit.point;
? ? ? ? ? ? ? ? //transform.position = hit.point;
? ? ? ? ? ? ? // Destroy(effect, 1);
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? IEnumerator Wait(Vector3 pos)
? ? {
? ? ? ? yield return new WaitForSeconds(3);
? ? ? ? transform.position = pos;
? ? }
? ? public void Skill_4() { //傳送技能的實(shí)現(xiàn)
? ? ? ? Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
? ? ? ? RaycastHit hit;
? ? ? ? if (Physics.Raycast(ray, out hit, 100, lm)) {
? ? ? ? ? ? if (Input.GetMouseButtonDown(1))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //生成特效
? ? ? ? ? ? ? ? //特效位置,1生成到人物自身位置,2生成到傳送點(diǎn)特效
? ? ? ? ? ? ? ? //等待5s把鼠標(biāo)點(diǎn)擊位置賦值給人物
? ? ? ? ? ? ? ? //實(shí)例化出來一個(gè)特效
? ? ? ? ? ? ? ? //GameObject effect = Instantiate(effect1Obj);
? ? ? ? ? ? ? ? //給定特效位置
? ? ? ? ? ? ? // effect.transform.position = transform.position;
? ? ? ? ? ? ? ? //實(shí)例化出來一個(gè)特效
? ? ? ? ? ? ? ? //GameObject effect1 = Instantiate(effect1Obj);
? ? ? ? ? ? ? ? //給定特效位置
? ? ? ? ? ? ? // effect1.transform.position = hit.point;
? ? ? ? ? ? ? // StartCoroutine(Wait(hit.point));
? ? ? ? ? ? ? //? Destroy(effect,3);
? ? ? ? ? ? ? ? //Destroy(effect1,3);
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? //技能范圍顯示及移動(dòng)
? ? public GameObject big;
? ? public GameObject small;
? ? public void Skill_5() {
? ? ? ? big.SetActive(true);
? ? ? ? small.SetActive(true);
? ? ? ? StartCoroutine(SmallMove());
? ? }
? ? //技能顯示層級
? ? public LayerMask lm3;
? ? //技能范圍小圓移動(dòng)事件
? ? IEnumerator SmallMove() {
? ? ? ? while (true)
? ? ? ? {
? ? ? ? ? //射線檢測
? ? ? ? ? ? Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
? ? ? ? ? ? RaycastHit hit;
? ? ? ? ? ? if (Physics.Raycast(ray,out hit,20,lm3))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //獲取人物和鼠標(biāo)點(diǎn)之間的差值
? ? ? ? ? ? ? ? Vector3 offset = hit.point-transform.position;
? ? ? ? ? ? ? ? if (offset.magnitude>2)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? offset = offset.normalized * 2;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? small.transform.position = transform.position+ offset;
? ? ? ? ? ? ? ? if (Input.GetMouseButtonDown(0))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? //開啟技能冷卻協(xié)程
? ? ? ? ? ? ? ? ? ? //skillBtn_3.GetComponent<CollingSkill1>().Skill();
? ? ? ? ? ? ? ? ? ? //隱藏圖片
? ? ? ? ? ? ? ? ? ? //big.SetActive(false);
? ? ? ? ? ? ? ? ? // small.SetActive(false);
? ? ? ? ? ? ? ? ? ? //實(shí)例化特效
? ? ? ? ? ? ? ? ? // GameObject effect = Instantiate(effect1Obj);
? ? ? ? ? ? ? ? ? ? //effect.transform.position = small.transform.position;
? ? ? ? ? ? ? ? ? ? KillSuper();
? ? ? ? ? ? ? ? ? // Destroy(effect,3);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? yield return null;
? ? ? ? }
? ? }
? ? //技能釋放---范圍攻擊
? ? public LayerMask lm5;
? ? void KillSuper()
? ? {
? ? ? //范圍檢測
? ? ? ? Collider[] co = Physics.OverlapSphere(small.transform.position, 2, lm5);
? ? ? ?
? ? ? ? for (int i = 0; i < co.Length; i++)
? ? ? ? {
? ? ? ? ? ? if (co[i].tag == "enemy")
? ? ? ? ? ? {
? ? ? ? ? ? ? //? co[i].GetComponent<EnemyInit>().hit = Random.Range(1,4);
? ? ? ? ? ? ? // co[i].GetComponent<EnemyInit>().Behit();
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? //人物死亡
? ? public void PlayerDieEvent() {
? ? ? ? isDie = true;
? ? ? ? //播放死亡畫面
? ? ? ? //ani.SetTrigger("die");
? ? ? ? //彈出結(jié)束畫面
? ? }
}