This commit is contained in:
firefighter198
2021-10-04 23:10:35 +02:00
parent 4041338420
commit 12aeb116dd
103 changed files with 9067 additions and 1750 deletions
+11
View File
@@ -0,0 +1,11 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bishop : Character
{
public override bool[,] GetValidMoves(Cell[,] cells, Game game)
{
return null;
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 843bca5b9b65c744b8f87208f3ce7aef
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+8
View File
@@ -0,0 +1,8 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cell : MonoBehaviour
{
public Character connected;
}
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 914ccdfc751746744ab0d9986aaf1420
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+25
View File
@@ -0,0 +1,25 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class Character : MonoBehaviour
{
public Teams team;
public abstract bool[,] GetValidMoves(Cell[,] cells, Game game);
protected bool IsPositionInGrid(int x, int y)
{
if (x < 0 || y < 0 || x > 7 || y > 7)
{
return false;
}
return true;
}
}
public enum Teams
{
White, Black
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 179b17dcb667118419acf51301f94a71
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+187
View File
@@ -0,0 +1,187 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Game : MonoBehaviour
{
public Camera cam;
private Cell[,] cells = new Cell[8, 8];
public Teams currentTeam = Teams.White;
private Cell clickedCell = null;
private List<Cell> validMovesList = new List<Cell>();
private void Start()
{
//Get all the Cells from the hierachy
for (int x = 0; x < 8; x++)
{
for (int y = 0; y < 8; y++)
{
cells[x, y] = GameObject.Find("Field" + y + "," + x).gameObject.GetComponent<Cell>();
}
}
}
private void Update()
{
//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)
{
Destroy(tempCell.connected.gameObject);
}
tempCell.connected = clickedCell.connected;
tempCell.connected.gameObject.transform.position = (tempCell.gameObject.transform.position);
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 (validMoves[y, x])
{
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]);
}
}
}
}
}
}
}
}
//Reset all colors of the cells
private void DeColorAllCells()
{
for (int ix = 0; ix < 8; ix++)
{
for (int iy = 0; iy < 8; iy++)
{
ColorCell(ix, iy, Color.white);
}
}
}
//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++)
{
if (cells[ix, iy] == cell)
{
x = ix;
y = iy;
}
}
}
ColorCellExclusive(x, y, color);
}
//Color the selected cell and deColor all others
private void ColorCellExclusive(int x, int y, Color color)
{
for (int ix = 0; ix < 8; ix++)
{
for (int iy = 0; iy < 8; iy++)
{
if (ix == x && iy == y)
{
ColorCell(ix, iy, color);
}
else
{
ColorCell(ix, iy, Color.white);
}
}
}
}
//Color the selected cell in the given color
private void ColorCell(int x, int y, Color color)
{
cells[x, y].GetComponent<SpriteRenderer>().color = color;
}
//Get the currently clicked cell
public Cell GetCellOnPositionMouse(Vector3 position)
{
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>();
}
return result;
}
}
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 64bccbbe14a86034a904585f5656f5e4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+11
View File
@@ -0,0 +1,11 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class King : Character
{
public override bool[,] GetValidMoves(Cell[,] cells, Game game)
{
return null;
}
}
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9fd968408c0465c4bb0d7960a1be6a9b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+64
View File
@@ -0,0 +1,64 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Knight : Character
{
private static readonly Vector2[] moves =
{
//(2, 1 combinations)
//up
new Vector2(-1, 2),
new Vector2(1, 2),
//down
new Vector2(-1, -2),
new Vector2(1, -2),
//right
new Vector2(2, 1),
new Vector2(2, -1),
//left
new Vector2(-2, 1),
new Vector2(-2, -1),
};
public override bool[,] GetValidMoves(Cell[,] cells, Game game)
{
bool[,] result = new bool[8, 8];
foreach (Vector2 move in moves)
{
Vector3 testPosition = transform.position + (Vector3)move;
Cell testCell = game.GetCellOnPosition(testPosition + Vector3.back);
if(testCell != null)
{
int x, y;
string nameX = testCell.gameObject.name.Substring(5, 1);
string nameY = testCell.gameObject.name.Substring(7, 1);
x = int.Parse(nameX);
y = int.Parse(nameY);
if (IsPositionInGrid(x, y))
{
if (testCell.connected == null)
{
result[x, y] = true;
}
else
{
Character testCharacter = testCell.connected;
if (testCharacter.team != game.currentTeam)
{
result[x, y] = true;
}
}
}
}
}
return result;
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 413f30f504354374eac5ff0f4a42653d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+106
View File
@@ -0,0 +1,106 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pawn : Character
{
public override bool[,] GetValidMoves(Cell[,] cells, Game game)
{
bool[,] result = new bool[8, 8];
Vector3 offsetDirection;
int yStartPos;
if (game.currentTeam == Teams.White)
{
offsetDirection = Vector3.up;
yStartPos = 5;
}
else
{
offsetDirection = Vector3.down;
yStartPos = 2;
}
Vector3 testPosition = transform.position + offsetDirection;
Cell testCell = game.GetCellOnPosition(testPosition);
if (testCell != null)
{
int x, y;
string nameX = testCell.gameObject.name.Substring(5, 1);
string nameY = testCell.gameObject.name.Substring(7, 1);
x = int.Parse(nameX);
y = int.Parse(nameY);
if (testCell.connected == null)
{
result[x, y] = true;
if (x == yStartPos)
{
testPosition = transform.position + offsetDirection * 2;
testCell = game.GetCellOnPosition(testPosition);
if (testCell != null)
{
nameX = testCell.gameObject.name.Substring(5, 1);
nameY = testCell.gameObject.name.Substring(7, 1);
x = int.Parse(nameX);
y = int.Parse(nameY);
if (testCell.connected == null)
{
result[x, y] = true;
}
}
}
}
testPosition = transform.position + offsetDirection + Vector3.left;
testCell = game.GetCellOnPosition(testPosition);
if (testCell != null)
{
nameX = testCell.gameObject.name.Substring(5, 1);
nameY = testCell.gameObject.name.Substring(7, 1);
x = int.Parse(nameX);
y = int.Parse(nameY);
if (testCell.connected != null)
{
if(testCell.connected.team != game.currentTeam)
{
result[x, y] = true;
}
}
}
testPosition = transform.position + offsetDirection + Vector3.right;
testCell = game.GetCellOnPosition(testPosition);
if (testCell != null)
{
nameX = testCell.gameObject.name.Substring(5, 1);
nameY = testCell.gameObject.name.Substring(7, 1);
x = int.Parse(nameX);
y = int.Parse(nameY);
if (testCell.connected != null)
{
if(testCell.connected.team != game.currentTeam)
{
result[x, y] = true;
}
}
}
}
return result;
}
}
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 451b5186406195f4c87dd2a0934390db
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+11
View File
@@ -0,0 +1,11 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Queen : Character
{
public override bool[,] GetValidMoves(Cell[,] cells, Game game)
{
return null;
}
}
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4ecf79ff7b31c234f89070f40365082e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+67
View File
@@ -0,0 +1,67 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rook : Character
{
private static readonly Vector2[,] moves =
{
{
//up
new Vector2(0, 1),
new Vector2(0, 2),
new Vector2(0, 3),
new Vector2(0, 4),
new Vector2(0, 5),
new Vector2(0, 6),
new Vector2(0, 7),
new Vector2(0, 8.5f), //.5 because of calculation error in game ^^
},
{
//down
new Vector2(0, -1),
new Vector2(0, -2),
new Vector2(0, -3),
new Vector2(0, -4),
new Vector2(0, -5),
new Vector2(0, -6),
new Vector2(0, -7),
new Vector2(0, -8.5f),
},
{
//right
new Vector2(1, 0),
new Vector2(2, 0),
new Vector2(3, 0),
new Vector2(4, 0),
new Vector2(5, 0),
new Vector2(6, 0),
new Vector2(7, 0),
new Vector2(8.5f, 0)
},
{
//left
new Vector2(-1, 0),
new Vector2(-2, 0),
new Vector2(-3, 0),
new Vector2(-4, 0),
new Vector2(-5, 0),
new Vector2(-6, 0),
new Vector2(-7, 0),
new Vector2(-8.5f, 0)
}
};
public override bool[,] GetValidMoves(Cell[,] cells, Game game)
{
bool[,] result = new bool[8, 8];
return result;
}
}
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e472b40b9823e414690354398c441ee1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: