using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CheckerBoard : MonoBehaviour {
public Piece[,] pieces = new Piece[8, 8];
public GameObject whitePiece;
public GameObject blackPiece;
private Vector3 boardoffset = new Vector3(-4.0f, 0, -4.0f);
private Vector3 pieceoffset = new Vector3(0.5f, 0, 0.5f);
private Piece SelectedPiece;
private List<piece> forcedPieces;
public bool isWhite;
private bool isWhiteTurn;
private bool hasKilled;
private Vector2 mouseover;
private Vector2 startDrag;
private Vector2 endDrag;
private void Start()
{
isWhiteTurn = true;
isWhite = true;
forcedPieces = new List<piece>();
GenerateBoard();
}
private void Update()
{
UpdateMouseOver();
if((isWhite)?isWhiteTurn:!isWhiteTurn)
{
int x = (int)mouseover.x;
int y = (int)mouseover.y;
if (SelectedPiece != null)
{
UpdatePieceDrag(SelectedPiece);
}
if (Input.GetMouseButtonDown(0))
{
SelectPiece(x, y);
}
if (Input.GetMouseButtonUp(0))
TryMove((int)startDrag.x, (int)startDrag.y, x, y);
}
}
private void UpdateMouseOver()
{
if(!Camera.main)
{
Debug.Log("Unable to find main camera");
return;
}
RaycastHit hit;
if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),out hit,25.0f,LayerMask.GetMask("Board")))
{
mouseover.x = (int)(hit.point.x - boardoffset.x);
mouseover.y = (int)(hit.point.z - boardoffset.z);
}
else
{
mouseover.x = -1;
mouseover.y = -1;
}
}
private void GenerateBoard()
{
for(int y=0; y<3; y++)
{
bool oddRow = (y % 2 == 0);
for(int x=0; x<8; x+=2)
{
GeneratePiece((oddRow) ? x : x +1, y);
}
}
for (int y = 7; y > 4; y--)
{
bool oddRow = (y % 2 == 0);
for (int x = 0; x < 8; x += 2)
{
GeneratePiece((oddRow) ? x : x + 1, y);
}
}
}
private void GeneratePiece(int x, int y)
{
bool isPieceWhite = (y > 3) ? false : true;
GameObject go = Instantiate(isPieceWhite?whitePiece:blackPiece) as GameObject;
go.transform.SetParent(transform);
Piece p = go.GetComponent<piece>();
pieces[x, y] = p;
MovePiece(p, x, y);
}
private void MovePiece(Piece p, int x, int y)
{
p.transform.position = (Vector3.right * x) + (Vector3.forward * y) + boardoffset + pieceoffset;
}
private void SelectPiece(int x, int y)
{
if (x < 0 || x >= 8 || y < 0 || y >= 8)
return;
Piece p = pieces[x, y];
if(p != null && p.isWhite == isWhite)
{
if (forcedPieces.Count == 0)
{
SelectedPiece = p;
startDrag = mouseover;
}
else
{
if (forcedPieces.Find(fp => fp == p) == null)
return;
SelectedPiece = p;
startDrag = mouseover;
}
}
}
private void TryMove(int x1,int y1,int x2,int y2)
{
forcedPieces = ScanForPossibleMove();
startDrag = new Vector2(x1, y1);
endDrag = new Vector2(x2, y2);
SelectedPiece = pieces[x1, y1];
if(x2 < 0 || x2 >= 8 || y2 < 0 || y2 >= 8)
{
if (SelectedPiece != null)
{
MovePiece(SelectedPiece, x1,y1);
}
startDrag = Vector2.zero;
SelectedPiece = null;
return;
}
if(SelectedPiece != null)
{
if( endDrag == startDrag)
{
MovePiece(SelectedPiece, x1, y1);
startDrag = Vector2.zero;
SelectedPiece = null;
return;
}
if(SelectedPiece.validMove(pieces, x1, y1, x2, y2))
{
if(Mathf.Abs(x2 - x1) == 2)
{
Piece p = pieces[(x1 + x2) / 2, (y1 + y2) / 2];
if(p != null)
{
pieces[(x1 + x2) / 2, (y1 + y2) / 2] = null;
Destroy(p.gameObject);
hasKilled = true;
}
}
if(forcedPieces.Count != 0 && !hasKilled)
{
MovePiece(SelectedPiece, x1, y1);
startDrag = Vector2.zero;
SelectedPiece = null;
return;
}
pieces[x2, y2] = SelectedPiece;
pieces[x1, y1] = null;
MovePiece(SelectedPiece,x2, y2);
EndTurn();
}
else
{
MovePiece(SelectedPiece, x1, y1);
startDrag = Vector2.zero;
SelectedPiece = null;
return;
}
}
}
private void EndTurn()
{
int x = (int)endDrag.x;
int y = (int)endDrag.y;
if(SelectedPiece != null)
{
if(SelectedPiece.isWhite && !SelectedPiece.isKing && y == 7)
{
SelectedPiece.isKing = true;
SelectedPiece.transform.Rotate(Vector3.right * 180);
}
else if(!SelectedPiece.isWhite && !SelectedPiece.isKing && y == 0)
{
SelectedPiece.isKing = true;
SelectedPiece.transform.Rotate(Vector3.right * 180);
}
}
SelectedPiece = null;
startDrag = Vector2.zero;
if (ScanForPossibleMove(SelectedPiece, x, y).Count != 0 && hasKilled)
return;
isWhiteTurn = !isWhiteTurn;
isWhite = !isWhite;
hasKilled = false;
CheckVictory();
}
private void CheckVictory()
{
var ps = FindObjectsOfType<piece>();
bool hasWhite = false, hasBlack = false;
for(int i = 0; i < ps.Length; i++)
{
if (ps[i].isWhite)
hasWhite = true;
else
hasBlack = true;
}
if (!hasWhite)
Victory(false);
if (!hasBlack)
Victory(true);
}
public void Victory(bool isWhite)
{
if (isWhite)
Debug.Log("White player has won");
else
Debug.Log("Black player has won");
}
private List<piece> ScanForPossibleMove()
{
forcedPieces = new List<piece>();
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
if (pieces[i, j] != null && pieces[i, j].isWhite == isWhiteTurn)
if (pieces[i, j].IsForceToMove(pieces, i, j))
forcedPieces.Add(pieces[i, j]);
return forcedPieces;
}
private List<piece> ScanForPossibleMove(Piece p, int x, int y)
{
forcedPieces = new List<piece>();
if (pieces[x, y].IsForceToMove(pieces, x, y))
forcedPieces.Add(pieces[x, y]);
return forcedPieces;
}
private void UpdatePieceDrag(Piece p)
{
if (!Camera.main)
{
Debug.Log("Unable to find main camera");
return;
}
RaycastHit hit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 25.0f, LayerMask.GetMask("Board")))
{
p.transform.position = hit.point + Vector3.up;
}
}
}
[edit]Code block added - OriginalGriff[/edit]
What I have tried:
i had tried to start from the mouse over but it didnt work