using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; /// /// 拖动对象脚本 /// public class DragObject_Mon : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler { /// /// 拖动根对象 /// private ForgingMaterial_Mon forgingMaterial_Mon; /// /// 锻造栏组 /// private List forgingbar_game; /// /// 顾客游戏对象 /// private GameObject customerObject; /// /// 拖动放置状态 /// private bool dragstate=false; /// /// 设置拖动根对象 /// /// public void setforgingMaterial_Mon(ForgingMaterial_Mon forgingMaterial_Mon_) { this.forgingMaterial_Mon = forgingMaterial_Mon_; } public void LoadGame() { this.forgingbar_game = GameObject.FindGameObjectsWithTag("锻造栏").ToList(); this.customerObject = GameObject.Find("顾客"); } private void Start() { this.GetComponent().sprite = GameObject.FindGameObjectWithTag("铁匠玩家").GetComponent().FindArticleBoxsSprite(this.forgingMaterial_Mon.getId()); } private void Awake() { this.LoadGame(); } /// /// 更新位置 /// private void Updateposition() { //判断是否放置 if (this.dragstate) { return; } //拖动状态 if (this.forgingMaterial_Mon.getdrastate()) { this.transform.position=Input.mousePosition; } else { //判断是否有可放置的格子判断是否是材料id if (this.FindClosestTransform() != null&& this.forgingMaterial_Mon.getId() < 10) { //Debug.Log("X" + Mathf.Abs(this.transform.position.x - this.FindClosestTransform().position.x)+ "Y" + Mathf.Abs(this.transform.position.y - this.FindClosestTransform().position.y)); if (Mathf.Abs(this.transform.position.x - this.FindClosestTransform().position.x) <= 50f && Mathf.Abs(this.transform.position.y - this.FindClosestTransform().position.y) <= 50f) { this.gameObject.transform.position=this.FindClosestTransform().position; this.dragstate = true; //拖拽放在锻造栏中则减少材料数量 GameObject.FindGameObjectWithTag("铁匠玩家").GetComponent().dragmaterial(this.forgingMaterial_Mon.getId(),1); this.transform.SetParent(this.FindClosestTransform()); this.FindClosestTransform().GetComponent().setarticleid(this.forgingMaterial_Mon.getId()); } else { Destroy(this.gameObject); } } else if (this.customerObject!=null) { float x_ = Mathf.Abs(this.transform.position.x - this.customerObject.transform.position.x); float y_ = Mathf.Abs(this.transform.position.y - this.customerObject.transform.position.y); // Debug.Log("检测顾客位置"); //Debug.Log("判断顾客距离x:"+x_+",y:"+y_); if (x_ <= 300f &&y_<= 300f) { //拖拽放在锻造栏中则减少材料数量 GameObject.FindGameObjectWithTag("铁匠玩家").GetComponent().dragmaterial(this.forgingMaterial_Mon.getId(), 1); GameObject.FindGameObjectWithTag("铁匠玩家").GetComponent().SubmitWeapons(this.forgingMaterial_Mon.getId()); Destroy(this.gameObject); } else { Destroy(this.gameObject); } } else { Destroy(this.gameObject); } } } private void LateUpdate() { this.Updateposition(); } /// /// 返回距离最近的锻造栏 /// /// private Transform FindClosestTransform() { if (this.forgingbar_game == null || this.forgingbar_game.Count == 0) { return null; // 列表为空或未初始化 } Transform closestObject = null; float minDistance = Mathf.Infinity; foreach (GameObject obj in this.forgingbar_game) { float distance = Vector3.Distance(obj.transform.position, this.transform.position); if (distance < minDistance) { minDistance = distance; closestObject = obj.transform; } } return closestObject; } public void OnBeginDrag(PointerEventData eventData) { Debug.Log("开始拖拽"); GameObject.FindGameObjectWithTag("铁匠玩家").GetComponent().add_article(this.forgingMaterial_Mon.getId(),1); Destroy(this.gameObject); } public void OnDrag(PointerEventData eventData) { } public void OnEndDrag(PointerEventData eventData) { } }