Killed Characters are put to the side

This commit is contained in:
firefighter198
2021-10-05 18:55:56 +02:00
parent cb3e7563a6
commit cc11f92f0a
7 changed files with 1937 additions and 107 deletions
+124 -71
View File
@@ -6,13 +6,19 @@ using UnityEngine;
public class Game : MonoBehaviour
{
public Camera cam;
private Cell[,] cells = new Cell[8, 8];
private Cell[] killedCharacCellsWhite = new Cell[16];
private int indexKilledCharacCellWhite = 0, indexKilledCharacCellBlack = 0;
private Cell[] killedCharacCellsBlack = new Cell[16];
public Teams currentTeam = Teams.White;
private bool areKilledCellsActive = false;
private Cell clickedCell = null;
private List<Cell> validMovesList = new List<Cell>();
public bool isPaused = false;
private void Start()
{
//Get all the Cells from the hierachy
@@ -23,82 +29,109 @@ public class Game : MonoBehaviour
cells[x, y] = GameObject.Find("Field" + y + "," + x).gameObject.GetComponent<Cell>();
}
}
for (int i = 0; i < 16; i++)
{
killedCharacCellsWhite[i] = GameObject.Find("AddCell" + i).gameObject.GetComponent<Cell>();
killedCharacCellsBlack[i] = GameObject.Find("AddCell" + i + " (1)").gameObject.GetComponent<Cell>();
}
}
private void Update()
{
//Check if left clicked
if (Input.GetMouseButtonDown(0))
if (!isPaused)
{
//Get the clicked cell
Cell tempCell = GetCellOnPositionMouse(Input.mousePosition);
//Check if left clicked
if (Input.GetMouseButtonDown(0))
{
//Get the clicked cell
Cell tempCell = GetCellOnPositionMouse(Input.mousePosition);
if(validMovesList.Contains(tempCell))
{
if (tempCell.connected != null)
if (tempCell != null)
{
Destroy(tempCell.connected.gameObject);
}
tempCell.connected = clickedCell.connected;
tempCell.connected.gameObject.transform.position = (tempCell.gameObject.transform.position + Vector3.back);
clickedCell.connected = null;
DeColorAllCells();
validMovesList.Clear();
if (currentTeam == Teams.White)
{
currentTeam = Teams.Black;
}
else
{
currentTeam = Teams.White;
}
}
else
{
//if the clickedCell == tempCell, deselect the cell
if (clickedCell == tempCell)
{
clickedCell = null;
DeColorAllCells();
validMovesList.Clear();
}
//else select the cell
else
{
clickedCell = tempCell;
ColorCellExclusive(clickedCell, new Color(.5f, .1f, .1f));
}
}
//check if there is a clicked cell
if(clickedCell != null)
{
//check if any character is on the cell
if (clickedCell.connected != null)
{
//check if the character belongs to the curren team
if (clickedCell.connected.team == currentTeam)
if (validMovesList.Contains(tempCell))
{
//get all the valid moves from the character based on the current grid
bool[,] validMoves = clickedCell.connected.GetValidMoves(cells, this);
for (int x = 0; x < 8; x++)
if (tempCell.connected != null)
{
for (int y = 0; y < 8; y++)
if (tempCell.connected.team == Teams.White)
{
if (validMoves[y, x])
tempCell.connected.gameObject.transform.position = killedCharacCellsWhite[indexKilledCharacCellWhite].gameObject.transform.position + Vector3.back;
killedCharacCellsWhite[indexKilledCharacCellWhite].connected = tempCell.connected;
indexKilledCharacCellWhite++;
}
else
{
tempCell.connected.gameObject.transform.position = killedCharacCellsBlack[indexKilledCharacCellBlack].gameObject.transform.position + Vector3.back;
killedCharacCellsBlack[indexKilledCharacCellBlack].connected = tempCell.connected;
indexKilledCharacCellBlack++;
}
}
tempCell.connected = clickedCell.connected;
tempCell.connected.gameObject.transform.position =
(tempCell.gameObject.transform.position + Vector3.back);
clickedCell.connected = null;
DeColorAllCells();
validMovesList.Clear();
if (currentTeam == Teams.White)
{
currentTeam = Teams.Black;
}
else
{
currentTeam = Teams.White;
}
}
else
{
//if the clickedCell == tempCell, deselect the cell
if (clickedCell == tempCell)
{
clickedCell = null;
DeColorAllCells();
validMovesList.Clear();
}
//else select the cell
else
{
clickedCell = tempCell;
ColorCellExclusive(clickedCell, new Color(.5f, .1f, .1f));
}
}
}
//check if there is a clicked cell
if (clickedCell != null)
{
//check if any character is on the cell
if (clickedCell.connected != null)
{
//check if the character belongs to the curren team
if (clickedCell.connected.team == currentTeam)
{
//get all the valid moves from the character based on the current grid
bool[,] validMoves = clickedCell.connected.GetValidMoves(cells, this);
for (int x = 0; x < 8; x++)
{
for (int y = 0; y < 8; y++)
{
if (cells[x, y].connected != null)
if (validMoves[y, x])
{
ColorCell(x, y, new Color(.6f, .4f, .2f));
if (cells[x, y].connected != null)
{
ColorCell(x, y, new Color(.6f, .4f, .2f));
}
else
{
ColorCell(x, y, new Color(.1f, .5f, .1f));
}
validMovesList.Add(cells[x, y]);
}
else
{
ColorCell(x, y, new Color(.1f, .5f, .1f));
}
validMovesList.Add(cells[x, y]);
}
}
}
@@ -119,12 +152,12 @@ public class Game : MonoBehaviour
}
}
}
//Color the selected cell and deColor all others
private void ColorCellExclusive(Cell cell, Color color)
{
int x = 0, y = 0;
for (int ix = 0; ix < 8; ix++)
{
for (int iy = 0; iy < 8; iy++)
@@ -136,7 +169,7 @@ public class Game : MonoBehaviour
}
}
}
ColorCellExclusive(x, y, color);
}
@@ -170,18 +203,38 @@ public class Game : MonoBehaviour
{
return GetCellOnPosition(cam.ScreenToWorldPoint(position));
}
public Cell GetCellOnPosition(Vector3 position)
{
Cell result = null;
//Shoot a raycast from mousePointer / touch position
RaycastHit2D hit = Physics2D.Raycast(position, Vector3.forward);
if (hit.collider != null)
{
result = hit.collider.gameObject.GetComponent<Cell>();
if (ArrayContainsCell2D(hit.collider.gameObject.GetComponent<Cell>(), cells) || areKilledCellsActive)
{
result = hit.collider.gameObject.GetComponent<Cell>();
}
}
return result;
}
}
private bool ArrayContainsCell2D(Cell cell, Cell[,] cells)
{
for (int x = 0; x < cells.GetLength(0); x++)
{
for (int y = 0; y < cells.GetLength(1); y++)
{
if (cells[x, y] == cell)
{
return true;
}
}
}
return false;
}
}