168 lines
5.4 KiB
C#
168 lines
5.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
/// <summary>
|
|
/// 拖动对象脚本
|
|
/// </summary>
|
|
public class DragObject_Mon : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
|
|
{
|
|
/// <summary>
|
|
/// 拖动根对象
|
|
/// </summary>
|
|
private ForgingMaterial_Mon forgingMaterial_Mon;
|
|
/// <summary>
|
|
/// 锻造栏组
|
|
/// </summary>
|
|
private List<GameObject> forgingbar_game;
|
|
/// <summary>
|
|
/// 顾客游戏对象
|
|
/// </summary>
|
|
private GameObject customerObject;
|
|
/// <summary>
|
|
/// 拖动放置状态
|
|
/// </summary>
|
|
private bool dragstate=false;
|
|
/// <summary>
|
|
/// 设置拖动根对象
|
|
/// </summary>
|
|
/// <param name="forgingMaterial_Mon_"></param>
|
|
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<Image>().sprite = GameObject.FindGameObjectWithTag("铁匠玩家").GetComponent<BlacksmithPlayer_Mon>().FindArticleBoxsSprite(this.forgingMaterial_Mon.getId());
|
|
|
|
}
|
|
private void Awake()
|
|
{
|
|
this.LoadGame();
|
|
}
|
|
/// <summary>
|
|
/// 更新位置
|
|
/// </summary>
|
|
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<BlacksmithPlayer_Mon>().dragmaterial(this.forgingMaterial_Mon.getId(),1);
|
|
this.transform.SetParent(this.FindClosestTransform());
|
|
this.FindClosestTransform().GetComponent<ForgingBar_Mon>().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<BlacksmithPlayer_Mon>().dragmaterial(this.forgingMaterial_Mon.getId(), 1);
|
|
GameObject.FindGameObjectWithTag("铁匠玩家").GetComponent<BlacksmithPlayer_Mon>().SubmitWeapons(this.forgingMaterial_Mon.getId());
|
|
Destroy(this.gameObject);
|
|
}
|
|
else
|
|
{
|
|
Destroy(this.gameObject);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
|
|
Destroy(this.gameObject);
|
|
}
|
|
}
|
|
}
|
|
private void LateUpdate()
|
|
{
|
|
this.Updateposition();
|
|
}
|
|
/// <summary>
|
|
/// 返回距离最近的锻造栏
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
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<BlacksmithPlayer_Mon>().add_article(this.forgingMaterial_Mon.getId(),1);
|
|
Destroy(this.gameObject);
|
|
|
|
|
|
}
|
|
|
|
public void OnDrag(PointerEventData eventData)
|
|
{
|
|
}
|
|
|
|
public void OnEndDrag(PointerEventData eventData)
|
|
{
|
|
}
|
|
}
|