using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public enum Firestate { ExtinguishFire,//灭火 NotExtinguishFire//未灭火 } public class Fire : MonoBehaviour { public bool isPlayerInRange = false; // 检查玩家是否在范围内 private bool isExtinguishing = false; // 是否正在灭火 public float extinguishTime = 3f; // 灭火所需的时间 private float holdTime = 0f; // 按住按钮的计时 public float fireHurt = 10f; public float dis; public float maxfiredis = 2f; public float firedis = 20f; public PlayerState ps; float timer = 0; //private Firestate firestate = Firestate.NotExtinguishFire; public UseSkill UseSkill; public PlayerMovement_Jpystick characterControl;//玩家控制器 public Text TimeText; void Start() { ps = GameObject.Find("player").GetComponent(); TimeText.gameObject.SetActive(false); } void Update() { HurtPeople(); if (UseSkill == null) { return; } if (UseSkill.currentItem == null) { return; } else if(UseSkill.currentItem.transform.name != "NoFire" && UseSkill.currentItem.transform.name!="gun") { return; } MieFire(); } public void HurtPeople() { dis = Vector3.Distance(GameObject.Find("player").transform.position, this.transform.position); if(dis 1f) { //Debug.Log("================================================"); ps.beHurt(fireHurt); timer = 0; } } } public void MieFire() { //// 检查玩家是否在范围内且按住灭火键 //if (isPlayerInRange && Input.GetMouseButton(0)) //{ // // 禁用玩家控制器,防止玩家移动 // if (!isExtinguishing) // DisablePlayerControl(); // holdTime += Time.deltaTime; // Debug.Log("灭火计时:" + holdTime); // if (holdTime >= extinguishTime && !isExtinguishing) // { // // 如果玩家按住键达到指定时间,停止火焰 // StopFire(); // isExtinguishing = true; // 防止重复触发灭火 // } //} //else //{ // //Debug.Log("松开按键"); // // 如果玩家松开按键或者离开范围,重置计时 // holdTime = 0f; // isExtinguishing = false; // // 恢复玩家控制器 // if (!Input.GetMouseButton(0)) EnablePlayerControl(); //} Debug.Log(11111111); if (isPlayerInRange&& UseSkill.IsWater) { // 计算玩家与火源的距离 dis = Vector3.Distance(GameObject.Find("player").transform.position, this.transform.position); // 如果玩家距离火源足够近且按下鼠标左键才触发灭火 if (dis <= firedis) { TimeText.gameObject.SetActive(true); // 禁用玩家控制器,防止玩家移动 if (!isExtinguishing) DisablePlayerControl(); holdTime += Time.deltaTime; Debug.Log("灭火计时:" + holdTime); TimeText.GetComponent().text = "灭火计时:" + holdTime; if (holdTime >= extinguishTime && !isExtinguishing) { // 如果玩家按住键达到指定时间,停止火焰 StopFire(); isExtinguishing = true; // 防止重复触发灭火 } } } else { // 如果松开按键或者离开范围,重置计时 holdTime = 0f; isExtinguishing = false; //TimeText.gameObject.SetActive(false); // 恢复玩家控制器 } } // 开始火焰 public void StartFire() { this.gameObject.SetActive(true); isExtinguishing = false; // 重置灭火状态 } // 停止火焰 public void StopFire() { this.characterControl.DragTheScreen.enabled=true; this.gameObject.SetActive(false); Debug.Log("火焰已被熄灭"); isPlayerInRange = false; // 玩家离开范围 UseSkill.FireOUt(); TimeText.gameObject.SetActive(false); } // 当玩家进入火焰范围时触发 private void OnTriggerEnter(Collider other) { //if (other.CompareTag("Player")) //{ // isPlayerInRange = true; // 玩家进入范围 // characterControl = other.GetComponent(); //} if (other.CompareTag("Player")) { Debug.Log(111111111); isPlayerInRange = true; // 玩家进入范围 characterControl = other.GetComponent(); UseSkill= other.GetComponent(); //// 可以调整此处的触发范围 //float playerDistance = Vector3.Distance(other.transform.position, this.transform.position); //if (playerDistance <= maxfiredis) //{ // isPlayerInRange = true; //} //else //{ // isPlayerInRange = false; //} } } // 当玩家离开火焰范围时触发 private void OnTriggerExit(Collider other) { //if (other.CompareTag("Player")) //{ // isPlayerInRange = false; // 玩家离开范围 // characterControl = null; //} if (other.CompareTag("Player")) { isPlayerInRange = false; // 玩家离开范围 characterControl.DragTheScreen.enabled = true; UseSkill.FireOUt(); // TimeText.gameObject.SetActive(false); } } // 禁用玩家控制器 private void DisablePlayerControl() { if (characterControl != null) { characterControl.DragTheScreen.enabled = false; } } // 启用玩家控制器 private void EnablePlayerControl() { if (characterControl != null) { characterControl.DragTheScreen.enabled = true; } } }