在新弄一个选框的Sprite当作一个新的预制体,新建GameManager脚本,控制象棋的走棋
Update函数主要逻辑:
当鼠标点击时触发
1、若没有被选中的棋子,则判断此次点击的是否是棋子,若是,则选中它;
2、若有被选中的棋子,则判断此次点击的是否是棋子,若是,扔掉当前位置上的棋子,并将选中的棋子移动到当前位置;若不是,则移动选中的棋子到当前位置。取消选中;
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class GameManager : MonoBehaviour {
//被选中的棋子的ID
public int selectedId;
//实例化被选中时的Sprite(一个选框)
public GameObject fab_selected;
public GameObject Selected;
//音乐文件
public AudioSource clickMusic;
// Use this for initialization
void Start ()
{
}
void Awake()
{
//初始化selected物体并设置为disabled
Selected = Instantiate(fab_selected, transform.localPosition, Quaternion.identity) as GameObject;
Selected.transform.position = new Vector3(0, 0, 0);
Selected.name = "Selected";
Selected.SetActive(false);
}
// Update is called once per frame
void Update ()
{
//当鼠标点击时
if (Input.GetMouseButtonDown(0))
{
//摄像机到点击位置的射线
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
//Debug.Log(selectedId);
if (Physics.Raycast(ray, out hit) && hit.point.x > -2.29 && hit.point.x < 2.29 && hit.point.y > -2.6 && hit.point.y < 2.5)
{
//若当前没有选中棋子
if (selectedId == -1)
{
// 若点击的位置上有棋子
// 1.获取选中的棋子的ID
// 2.生成一个新的Sprite(Selected),代表其选中的是该棋子
if (hit.collider.gameObject.name != "Bcakground")
{
selectedId = int.Parse(hit.collider.gameObject.name);
Selected.transform.position = new Vector3(StoneManager.s[selectedId]._x, StoneManager.s[selectedId]._y, 0);
Selected.SetActive(true);
}
}
//当前有选中的棋子
else
{
// 若点击的位置上没有棋子
// 1.将选中的棋子移至当前位置
// 2.播放音效
// 3.置selectedId为-1
// 4.将Selected隐藏
if (hit.collider.gameObject.name == "Bcakground")
{
//Debug.Log("hit.point:"+hit.point);
GameObject Stone = GameObject.Find(selectedId.ToString());
Stone.transform.position = Center(hit.point);
if (!clickMusic.isPlaying)
{
clickMusic.Play();
}
selectedId = -1;
Selected.SetActive(false);
}
// 若点击的位置上没有棋子
// 1.判断当前位置上的棋子是否为当前被选中的棋子,若不是则执行下一步
// 2.将当前位置上的棋子吃掉
// 3.将选中的棋子移至当前位置
// 4.播放音效
// 5.置selectedId为-1
// 6.将Selected隐藏
else
{
int destoryId = int.Parse(hit.collider.gameObject.name);
//Debug.Log("destoryId:" + destoryId);
//Debug.Log("selectedId:" + selectedId);
if (destoryId != selectedId)
{
StoneManager.s[destoryId]._dead = true;
hit.collider.gameObject.SetActive(false);
GameObject Stone = GameObject.Find(selectedId.ToString());
Stone.transform.position = Center(hit.point);
if (!clickMusic.isPlaying)
{
clickMusic.Play();
}
selectedId = -1;
Selected.SetActive(false);
}
}
}
}
}
}
/// <summary>
/// 通过鼠标点击的位置,获取距离当前坐标点最近的中心点的位置
/// </summary>
/// <param name="point"></param>
/// <returns></returns>
Vector3 Center(Vector3 point)
{
//x,y,z为要返回的三维坐标
//将象棋分为9列(i)和10行(j)
//计算距离鼠标所指坐标点的最近的行列的序号(tpmi、tmpj)
//通过行列的序号算出位于该行该列的中心点的坐标位置并返回
//修改棋子的位置(StoneManager.s[selectedId]._x、StoneManager.s[selectedId]._y)属性
float x, y,z=0;
int i, tmpi=1,j,tmpj=1;
double min=0.51f;
for (i = -4; i < 5;++i )
{
if (System.Math.Abs(point.x - i * 0.51) < min)
{
min =System.Math.Abs(point.x - i * 0.51);
tmpi = i;
}
}
x = (float)(tmpi * 0.51);
min = 0.51f;
if (point.y > -0.04)
{
for (j = 0; j < 5; j++)
{
if (System.Math.Abs(point.y - (j * 0.51+0.21)) < min)
{
min = System.Math.Abs(point.y - (j * 0.51 + 0.21));
tmpj = j;
}
}
y = (float)(tmpj * 0.51 + 0.21);
}
else
{
for (j = 0; j < 5; j++)
{
if (System.Math.Abs(point.y + (j * 0.51+0.31)) < min)
{
min = System.Math.Abs(point.y + (j * 0.51+0.31));
tmpj = j;
}
}
y = (float)(-tmpj * 0.51 - 0.31);
}
//Debug.Log("point.y:" + point.y);
//Debug.Log("y:"+y);
StoneManager.s[selectedId]._x = x;
StoneManager.s[selectedId]._y = y;
return new Vector3(x, y, z);
}
}