using System.Collections;
using UnityEngine;
using System.Collections.Generic;
public class BoardManager : MonoBehaviour
{
private const float TILE_SIZE = 1.0f;
private const float TILE_OFFSET = 0.5F;
private int selectionX = -1;
private int selectionY = -1;
public List<GameObject> chessmanPrefabs;
private List<GameObject> activeChessman = new List<GameObject>();
private void Start()
{
SpawnAllChessmans();
}
private void Update()
{
UpdateSelection();
DrawChessboard();
}
private void UpdateSelection()
{
if (!Camera.main)
return;
RaycastHit hit;
if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 25.0f,LayerMask.GetMask("ChessPlane")))
{
selectionX = (int)hit.point.x;
selectionY = (int)hit.point.z;
}
else
{
selectionX = -1;
selectionY = -1;
}
}
private void SpawnChessman(int index, Vector3 position)
{
GameObject go = Instantiate(chessmanPrefabs [index], position, Quaternion.identity) as GameObject;
go.transform.SetParent(transform);
activeChessman.Add(go);
}
private SpawnAllChessmans()
{
activeChessman = new List<GameObject>();
//Spawn White Team!
//King
SpawnChessman(0, GetTileCenter(3, 0));
//Queen
SpawnChessman(1, GetTileCenter(4, 0));
//Rooks
SpawnChessman(2, GetTileCenter(4, 0));
SpawnChessman(2, GetTileCenter(7, 0));
//Bishops
SpawnChessman(3, GetTileCenter(2, 0));
SpawnChessman(3, GetTileCenter(5, 0));
//Kinghts
SpawnChessman(4, GetTileCenter(1, 0));
SpawnChessman(4, GetTileCenter(6, 0));
//Pawns
for (int i = 0; i < 8; i++)
SpawnChessman(5, GetTileCenter(i, 1));
}
private Vector3 GetTileCenter(int x, int y)
{
Vector3 origin = Vector3.zero;
origin.x += (TILE_SIZE * x) + TILE_OFFSET;
origin.y += (TILE_SIZE * y) + TILE_OFFSET;
return origin;
}
private void DrawChessboard()
{
Vector3 widthLine = Vector3.right * 8;
Vector3 heightLine = Vector3.forward * 8;
for (int i=0; i <= 8; i++)
{
Vector3 start = Vector3.forward * i;
Debug.DrawLine(start, start + widthLine);
for (int j=0; j <= 8; j++)
{
start = Vector3.right * j;
Debug.DrawLine(start, start + heightLine);
}
}
// Draw the selection
if(selectionX >= 0 && selectionY >=0)
{
Debug.DrawLine(Vector3.forward * selectionY + Vector3.right * selectionX, Vector3.forward * (selectionY + 1) + Vector3.right * (selectionX + 1));
Debug.DrawLine(Vector3.forward * (selectionY + 1) + Vector3.right * selectionX, Vector3.forward * selectionY + Vector3.right * (selectionX + 1));
}
}
}