├── .gitignore ├── Assets ├── Materials.meta ├── Materials │ ├── DarkWood.mat │ ├── DarkWood.mat.meta │ ├── LightWood.mat │ ├── LightWood.mat.meta │ ├── Texture.meta │ └── Texture │ │ ├── DarkWoodText.jpg │ │ ├── DarkWoodText.jpg.meta │ │ ├── LightWoodText.jpg │ │ └── LightWoodText.jpg.meta ├── Prefab.meta ├── Prefab │ ├── ChessBoard.meta │ ├── ChessBoard │ │ ├── CubeDark.prefab │ │ ├── CubeDark.prefab.meta │ │ ├── CubeLight.prefab │ │ └── CubeLight.prefab.meta │ ├── Pieces.meta │ ├── Pieces │ │ ├── Bishop.prefab │ │ ├── Bishop.prefab.meta │ │ ├── King.prefab │ │ ├── King.prefab.meta │ │ ├── Knight.prefab │ │ ├── Knight.prefab.meta │ │ ├── Pawn.prefab │ │ ├── Pawn.prefab.meta │ │ ├── Queen.prefab │ │ ├── Queen.prefab.meta │ │ ├── Rook.prefab │ │ └── Rook.prefab.meta │ ├── _ChessModels.prefab │ └── _ChessModels.prefab.meta ├── Scene.meta ├── Scene │ ├── Main.unity │ └── Main.unity.meta ├── Script.meta └── Script │ ├── AI.cs │ ├── AI.cs.meta │ ├── GameManager.cs │ ├── GameManager.cs.meta │ ├── PlayerControl.cs │ └── PlayerControl.cs.meta ├── ProjectSettings ├── AudioManager.asset ├── DynamicsManager.asset ├── EditorBuildSettings.asset ├── EditorSettings.asset ├── GraphicsSettings.asset ├── InputManager.asset ├── NavMeshLayers.asset ├── NetworkManager.asset ├── ProjectSettings.asset ├── QualitySettings.asset ├── TagManager.asset └── TimeManager.asset └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Unity .gitignore file. 2 | .DS_Store 3 | Library 4 | Library/* 5 | *.csproj 6 | *.sln 7 | *.userprefs 8 | Temp 9 | *.pidb 10 | Build -------------------------------------------------------------------------------- /Assets/Materials.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 4bc9249d9e6668d4cbdf47434aabb5be 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Materials/DarkWood.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RodGame/Chess/ef7ee648c98c6c9025eb8b8a33c85826a9a63b90/Assets/Materials/DarkWood.mat -------------------------------------------------------------------------------- /Assets/Materials/DarkWood.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 31fab12ba9e5fbf48afd78a6b38162ff 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Materials/LightWood.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RodGame/Chess/ef7ee648c98c6c9025eb8b8a33c85826a9a63b90/Assets/Materials/LightWood.mat -------------------------------------------------------------------------------- /Assets/Materials/LightWood.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0e1e86e793bdbf047a673389446bc523 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Materials/Texture.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: bb5d0630498f8cb4d85a934c261d5a53 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Materials/Texture/DarkWoodText.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RodGame/Chess/ef7ee648c98c6c9025eb8b8a33c85826a9a63b90/Assets/Materials/Texture/DarkWoodText.jpg -------------------------------------------------------------------------------- /Assets/Materials/Texture/DarkWoodText.jpg.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: bcecd14e5e2f4fa4fad292076ad4d515 3 | TextureImporter: 4 | serializedVersion: 2 5 | mipmaps: 6 | mipMapMode: 0 7 | enableMipMap: 1 8 | linearTexture: 0 9 | correctGamma: 0 10 | fadeOut: 0 11 | borderMipMap: 0 12 | mipMapFadeDistanceStart: 1 13 | mipMapFadeDistanceEnd: 3 14 | bumpmap: 15 | convertToNormalMap: 0 16 | externalNormalMap: 0 17 | heightScale: .25 18 | normalMapFilter: 0 19 | isReadable: 0 20 | grayScaleToAlpha: 0 21 | generateCubemap: 0 22 | seamlessCubemap: 0 23 | textureFormat: -1 24 | maxTextureSize: 1024 25 | textureSettings: 26 | filterMode: -1 27 | aniso: -1 28 | mipBias: -1 29 | wrapMode: -1 30 | nPOTScale: 1 31 | lightmap: 0 32 | compressionQuality: 50 33 | textureType: -1 34 | buildTargetSettings: [] 35 | userData: 36 | -------------------------------------------------------------------------------- /Assets/Materials/Texture/LightWoodText.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RodGame/Chess/ef7ee648c98c6c9025eb8b8a33c85826a9a63b90/Assets/Materials/Texture/LightWoodText.jpg -------------------------------------------------------------------------------- /Assets/Materials/Texture/LightWoodText.jpg.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9fa266ab5b36f6d4c9bc01581890e507 3 | TextureImporter: 4 | serializedVersion: 2 5 | mipmaps: 6 | mipMapMode: 0 7 | enableMipMap: 1 8 | linearTexture: 0 9 | correctGamma: 0 10 | fadeOut: 0 11 | borderMipMap: 0 12 | mipMapFadeDistanceStart: 1 13 | mipMapFadeDistanceEnd: 3 14 | bumpmap: 15 | convertToNormalMap: 0 16 | externalNormalMap: 0 17 | heightScale: .25 18 | normalMapFilter: 0 19 | isReadable: 0 20 | grayScaleToAlpha: 0 21 | generateCubemap: 0 22 | seamlessCubemap: 0 23 | textureFormat: -1 24 | maxTextureSize: 1024 25 | textureSettings: 26 | filterMode: -1 27 | aniso: -1 28 | mipBias: -1 29 | wrapMode: -1 30 | nPOTScale: 1 31 | lightmap: 0 32 | compressionQuality: 50 33 | textureType: -1 34 | buildTargetSettings: [] 35 | userData: 36 | -------------------------------------------------------------------------------- /Assets/Prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 358661aac715d3d4897638a92cf710b4 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Prefab/ChessBoard.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1e56a93b335316b4ca521443c1756422 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Prefab/ChessBoard/CubeDark.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RodGame/Chess/ef7ee648c98c6c9025eb8b8a33c85826a9a63b90/Assets/Prefab/ChessBoard/CubeDark.prefab -------------------------------------------------------------------------------- /Assets/Prefab/ChessBoard/CubeDark.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7afe770efff6c1a46aa73f837d3566e9 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Prefab/ChessBoard/CubeLight.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RodGame/Chess/ef7ee648c98c6c9025eb8b8a33c85826a9a63b90/Assets/Prefab/ChessBoard/CubeLight.prefab -------------------------------------------------------------------------------- /Assets/Prefab/ChessBoard/CubeLight.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f7865566db89f0d4ba39668a756e554f 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Prefab/Pieces.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e7f4f091dc155ee4ab254bd19e1aa1c6 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Prefab/Pieces/Bishop.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RodGame/Chess/ef7ee648c98c6c9025eb8b8a33c85826a9a63b90/Assets/Prefab/Pieces/Bishop.prefab -------------------------------------------------------------------------------- /Assets/Prefab/Pieces/Bishop.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: df6c401aea2ba8e4cadf1c6d91980279 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Prefab/Pieces/King.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RodGame/Chess/ef7ee648c98c6c9025eb8b8a33c85826a9a63b90/Assets/Prefab/Pieces/King.prefab -------------------------------------------------------------------------------- /Assets/Prefab/Pieces/King.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d1dacc8b41d5d5f40904b50741cc21d8 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Prefab/Pieces/Knight.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RodGame/Chess/ef7ee648c98c6c9025eb8b8a33c85826a9a63b90/Assets/Prefab/Pieces/Knight.prefab -------------------------------------------------------------------------------- /Assets/Prefab/Pieces/Knight.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 28d784b57966e244097a062cafe0ccf6 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Prefab/Pieces/Pawn.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RodGame/Chess/ef7ee648c98c6c9025eb8b8a33c85826a9a63b90/Assets/Prefab/Pieces/Pawn.prefab -------------------------------------------------------------------------------- /Assets/Prefab/Pieces/Pawn.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 5956345972927424eba3db6f03f82542 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Prefab/Pieces/Queen.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RodGame/Chess/ef7ee648c98c6c9025eb8b8a33c85826a9a63b90/Assets/Prefab/Pieces/Queen.prefab -------------------------------------------------------------------------------- /Assets/Prefab/Pieces/Queen.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ae84bbeb418ab7b458761e201acb7f36 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Prefab/Pieces/Rook.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RodGame/Chess/ef7ee648c98c6c9025eb8b8a33c85826a9a63b90/Assets/Prefab/Pieces/Rook.prefab -------------------------------------------------------------------------------- /Assets/Prefab/Pieces/Rook.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 870c72ff0a9862f46afe2e881284fad2 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Prefab/_ChessModels.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RodGame/Chess/ef7ee648c98c6c9025eb8b8a33c85826a9a63b90/Assets/Prefab/_ChessModels.prefab -------------------------------------------------------------------------------- /Assets/Prefab/_ChessModels.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7e57f87b8f6aac44a88204b927ff41b1 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Scene.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 5f6cc1e122fd5644098b1283f5bf86c6 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Scene/Main.unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RodGame/Chess/ef7ee648c98c6c9025eb8b8a33c85826a9a63b90/Assets/Scene/Main.unity -------------------------------------------------------------------------------- /Assets/Scene/Main.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: cdd45a3205b599341a2f377025273f65 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Script.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d22640aba4a8a7f409d9a2554a58ec6f 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Script/AI.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class AI : MonoBehaviour { 5 | 6 | private GameManager _GameManager; // GameObject responsible for the management of the game 7 | private int _activePlayer; 8 | private bool _player1AI; 9 | private bool _player2AI; 10 | 11 | // Use this for initialization 12 | void Start () 13 | { 14 | _GameManager = gameObject.GetComponent(); 15 | _player1AI = _GameManager.player1AI; 16 | _player2AI = _GameManager.player2AI; 17 | } 18 | 19 | // Update is called once per frame 20 | void Update () { 21 | 22 | if((_activePlayer == 1 && _player1AI == true) || (_activePlayer == -1 && _player2AI == true)) 23 | { 24 | CalculateMove(); 25 | } 26 | 27 | 28 | 29 | } 30 | 31 | void CalculateMove() 32 | { 33 | 34 | } 35 | 36 | 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Assets/Script/AI.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e2442d3b93c8d564a894ce8bfd5859e2 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/Script/GameManager.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class GameManager : MonoBehaviour { 5 | 6 | public GameObject CubeDark; 7 | public GameObject CubeLight; 8 | public GameObject[] PiecesGO = new GameObject[6]; 9 | public int activePlayer = 1; // 1 = White, -1 = Dark 10 | public bool player1AI = false; // Bool that state if player1 is a AI 11 | public bool player2AI = false; // Bool that state if player2 is a AI 12 | public Material DarkMat; 13 | public Material LightMat; 14 | public int gameState = 0; // In this state, the code is waiting for : 0 = Piece selection, 1 = Piece animation 15 | 16 | 17 | private GameObject SelectedPiece; // Selected Piece 18 | private int _boardHeight = -1; 19 | private int _pieceHeight = 0; 20 | private static int _boardSize = 8; 21 | private int[,] _boardPieces = new int[_boardSize,_boardSize]; 22 | void OnGUI() 23 | { 24 | string _activePlayerColor; 25 | if(activePlayer == 1) 26 | _activePlayerColor = "White"; 27 | else 28 | _activePlayerColor = "Dark"; 29 | 30 | 31 | GUI.Label (new Rect(10,10,200,20), ("Active Player = " + _activePlayerColor)); 32 | GUI.Label (new Rect(10,30,200,20), ("Game State = " + gameState)); 33 | } 34 | 35 | // Initialize the board area 36 | void Start() 37 | { 38 | CreateBoard(); 39 | AddPieces(); 40 | } 41 | 42 | // Create the board by placing cubes 43 | void CreateBoard() 44 | { 45 | 46 | 47 | for(int i = 0; i < _boardSize; i++) 48 | { 49 | for(int j = 0; j < _boardSize; j++) 50 | { 51 | if((i+j)%2 == 0) 52 | { 53 | Object.Instantiate(CubeDark,new Vector3(i,_boardHeight,j), Quaternion.identity); 54 | } 55 | else 56 | { 57 | Object.Instantiate(CubeLight,new Vector3(i,_boardHeight,j), Quaternion.identity); 58 | } 59 | } 60 | } 61 | } 62 | 63 | // Add all pieces are their respective position 64 | void AddPieces() 65 | { 66 | int _linePosY; 67 | int _piecePlayer; 68 | 69 | // Create all pawn at once 70 | for(int i = 0; i < _boardSize; i++) 71 | { 72 | CreatePiece("Pawn", i, 1, 1); // Create all white pawn 73 | CreatePiece("Pawn", i, 6, -1); // Create all dark pawn 74 | } 75 | 76 | // Create white pieces 77 | _linePosY = 0; 78 | _piecePlayer = 1; 79 | CreatePiece("Rook" , 0, _linePosY, _piecePlayer); 80 | CreatePiece("Knight", 1, _linePosY, _piecePlayer); 81 | CreatePiece("Bishop", 2, _linePosY, _piecePlayer); 82 | CreatePiece("Queen" , 3, _linePosY, _piecePlayer); 83 | CreatePiece("King" , 4, _linePosY, _piecePlayer); 84 | CreatePiece("Bishop", 5, _linePosY, _piecePlayer); 85 | CreatePiece("Knight", 6, _linePosY, _piecePlayer); 86 | CreatePiece("Rook" , 7, _linePosY, _piecePlayer); 87 | 88 | // Create Dark pieces 89 | _linePosY = 7; 90 | _piecePlayer = -1; 91 | CreatePiece("Rook" , 0, _linePosY, _piecePlayer); 92 | CreatePiece("Knight", 1, _linePosY, _piecePlayer); 93 | CreatePiece("Bishop", 2, _linePosY, _piecePlayer); 94 | CreatePiece("Queen" , 3, _linePosY, _piecePlayer); 95 | CreatePiece("King" , 4, _linePosY, _piecePlayer); 96 | CreatePiece("Bishop", 5, _linePosY, _piecePlayer); 97 | CreatePiece("Knight", 6, _linePosY, _piecePlayer); 98 | CreatePiece("Rook" , 7, _linePosY, _piecePlayer); 99 | } 100 | 101 | // Spawn a piece on the board 102 | void CreatePiece(string _pieceName, int _posX, int _posY, int _playerTag) 103 | { 104 | GameObject _PieceToCreate = null; 105 | int _pieceIndex = 0; 106 | //Select the right prefab to instantiate 107 | switch (_pieceName) 108 | { 109 | case "Pawn": 110 | _pieceIndex = 1; 111 | break; 112 | case "Rook": 113 | _pieceIndex = 2; 114 | break; 115 | case "Knight": 116 | _pieceIndex = 3; 117 | break; 118 | case "Bishop": 119 | _pieceIndex = 4; 120 | break; 121 | case "Queen": 122 | _pieceIndex = 5; 123 | break; 124 | case "King": 125 | _pieceIndex = 6; 126 | break; 127 | } 128 | _PieceToCreate = PiecesGO[_pieceIndex-1]; 129 | // Instantiate the piece as a GameObject to be able to modify it after 130 | _PieceToCreate = Object.Instantiate (_PieceToCreate,new Vector3(_posX, _pieceHeight, _posY), Quaternion.identity) as GameObject; 131 | _PieceToCreate.name = _pieceName; 132 | 133 | //Add material to the piece and tag it 134 | if(_playerTag == 1) 135 | { 136 | _PieceToCreate.tag = "1"; 137 | _PieceToCreate.renderer.material = LightMat; 138 | } 139 | else if(_playerTag == -1) 140 | { 141 | _PieceToCreate.tag = "-1"; 142 | _PieceToCreate.renderer.material = DarkMat; 143 | } 144 | 145 | _boardPieces[_posX,_posY] = _pieceIndex*_playerTag; 146 | } 147 | 148 | //Update SlectedPiece with the GameObject inputted to this function 149 | public void SelectPiece(GameObject _PieceToSelect) 150 | { 151 | // Unselect the piece if it was already selected 152 | if(_PieceToSelect == SelectedPiece) 153 | { 154 | SelectedPiece.renderer.material.color = Color.white; 155 | SelectedPiece = null; 156 | ChangeState (0); 157 | } 158 | else 159 | { 160 | // Change color of the selected piece to make it apparent. Put it back to white when the piece is unselected 161 | if(SelectedPiece) 162 | { 163 | SelectedPiece.renderer.material.color = Color.white; 164 | } 165 | SelectedPiece = _PieceToSelect; 166 | SelectedPiece.renderer.material.color = Color.blue; 167 | ChangeState (1); 168 | } 169 | } 170 | 171 | // Move the SelectedPiece to the inputted coords 172 | public void MovePiece(Vector2 _coordToMove) 173 | { 174 | bool validMovementBool = false; 175 | Vector2 _coordPiece = new Vector2(SelectedPiece.transform.position.x, SelectedPiece.transform.position.z); 176 | 177 | // Don't move if the user clicked on its own cube or if there is a piece on the cube 178 | if((_coordToMove.x != _coordPiece.x || _coordToMove.y != _coordPiece.y) || _boardPieces[(int)_coordToMove.x,(int)_coordToMove.y] != 0) 179 | { 180 | validMovementBool = TestMovement (SelectedPiece, _coordToMove); 181 | } 182 | 183 | if(validMovementBool) 184 | { 185 | _boardPieces[(int)_coordToMove.x, (int)_coordToMove.y] = _boardPieces[(int)_coordPiece.x, (int)_coordPiece.y]; 186 | _boardPieces[(int)_coordPiece.x , (int)_coordPiece.y ] = 0; 187 | 188 | SelectedPiece.transform.position = new Vector3(_coordToMove.x, _pieceHeight, _coordToMove.y); // Move the piece 189 | SelectedPiece.renderer.material.color = Color.white; // Change it's color back 190 | SelectedPiece = null; // Unselect the Piece 191 | ChangeState (0); 192 | activePlayer = -activePlayer; 193 | } 194 | } 195 | 196 | // If the movement is legal, eat the piece 197 | public void EatPiece(GameObject _PieceToEat) 198 | { 199 | Vector2 _coordToEat = new Vector2(_PieceToEat.transform.position.x, _PieceToEat.transform.position.z); 200 | int _deltaX = (int)(_PieceToEat.transform.position.x - SelectedPiece.transform.position.x); 201 | 202 | if(TestMovement (SelectedPiece, _coordToEat) && (SelectedPiece.name != "Pawn" || _deltaX != 0)) 203 | { 204 | Object.Destroy (_PieceToEat); 205 | _boardPieces[(int)_coordToEat.x, (int)_coordToEat.y] = 0; 206 | MovePiece(_coordToEat); 207 | } 208 | } 209 | 210 | // Test if the piece can do the player's movement 211 | bool TestMovement(GameObject _SelectedPiece, Vector2 _coordToMove) 212 | { 213 | bool _movementLegalBool = false; 214 | bool _collisionDetectBool = false; 215 | Vector2 _coordPiece = new Vector2(_SelectedPiece.transform.localPosition.x, _SelectedPiece.transform.localPosition.z); 216 | 217 | int _deltaX = (int)(_coordToMove.x - _coordPiece.x); 218 | int _deltaY = (int)(_coordToMove.y - _coordPiece.y); 219 | int activePlayerPawnPostion = 1; 220 | //Debug.Log("Piece (" + _coordPiece.x + "," + _coordPiece.x + ") - Move (" + _coordToMove.x + "," + _coordToMove.y + ")"); 221 | //Debug.Log("Delta (" + _deltaX + "," + _deltaY + ")"); 222 | // Use the name of the _SelectedPiece GameObject to find the piece used 223 | switch (_SelectedPiece.name) 224 | { 225 | 226 | case "Pawn": 227 | if(activePlayer == -1) 228 | activePlayerPawnPostion = 6; 229 | 230 | 231 | // Pawn can move 1 steap ahead of them, 2 if they are on their initial position 232 | if(_deltaY == activePlayer || (_deltaY == 2*activePlayer && _coordPiece.y == activePlayerPawnPostion)) 233 | { 234 | //Debug.Log ("_deltaY"); 235 | _movementLegalBool = true; 236 | } 237 | 238 | break; 239 | case "Rook": 240 | // Rook can move horizontally or vertically 241 | if((_deltaX != 0 && _deltaY == 0) || (_deltaX == 0 && _deltaY != 0)) 242 | { 243 | _movementLegalBool = true; 244 | } 245 | break; 246 | case "Knight": 247 | // Knight move in a L movement. distance is evaluated by a multiplication of both direction 248 | if((_deltaX != 0 && _deltaY != 0) && Mathf.Abs(_deltaX*_deltaY) == 2) 249 | { 250 | _movementLegalBool = true; 251 | } 252 | break; 253 | case "Bishop": 254 | // Bishop can only move diagonally 255 | if(Mathf.Abs(_deltaX/_deltaY) == 1) 256 | { 257 | _movementLegalBool = true; 258 | } 259 | break; 260 | 261 | case "Queen": 262 | // Queen movement is a combination of Rook and bishop 263 | if(((_deltaX != 0 && _deltaY == 0) || (_deltaX == 0 && _deltaY != 0)) || Mathf.Abs(_deltaX/_deltaY) == 1) 264 | { 265 | _movementLegalBool = true; 266 | } 267 | break; 268 | 269 | case "King": 270 | // Bishop can only move diagonally 271 | if(Mathf.Abs(_deltaX + _deltaY) == 1) 272 | { 273 | _movementLegalBool = true; 274 | } 275 | break; 276 | 277 | default: 278 | _movementLegalBool = false; 279 | break; 280 | } 281 | 282 | // If the movement is legal, detect collision with piece in the way. Don't do it with knight since they can pass over pieces. 283 | if(_movementLegalBool && SelectedPiece.name != "Knight") 284 | { 285 | _collisionDetectBool = TestCollision (_coordPiece, _coordToMove); 286 | } 287 | 288 | return (_movementLegalBool && !_collisionDetectBool); 289 | } 290 | 291 | // Test if a unit is in the path of the tested movement 292 | bool TestCollision(Vector2 _coordInitial,Vector2 _coordFinal) 293 | { 294 | bool CollisionBool = false; 295 | int _deltaX = (int)(_coordFinal.x - _coordInitial.x); 296 | int _deltaY = (int)(_coordFinal.y - _coordInitial.y); 297 | int _incX = 0; // Direction of the incrementation in X 298 | int _incY = 0; // Direction of the incrementation in Y 299 | int i; 300 | int j; 301 | 302 | // Calculate the increment if _deltaX/Y is different from 0 to avoid division by 0 303 | if(_deltaX != 0) 304 | { 305 | _incX = (_deltaX/Mathf.Abs(_deltaX)); 306 | } 307 | if(_deltaY != 0) 308 | { 309 | _incY = (_deltaY/Mathf.Abs(_deltaY)); 310 | } 311 | 312 | i = (int)_coordInitial.x + _incX; 313 | j = (int)_coordInitial.y + _incY; 314 | 315 | while(new Vector2(i, j) != _coordFinal) 316 | { 317 | 318 | if(_boardPieces[i,j] != 0) 319 | { 320 | CollisionBool = true; 321 | break; 322 | } 323 | 324 | i += _incX; 325 | j += _incY; 326 | } 327 | Debug.Log (CollisionBool); 328 | return CollisionBool; 329 | } 330 | 331 | // Change the state of the game 332 | public void ChangeState(int _newState) 333 | { 334 | gameState = _newState; 335 | } 336 | } 337 | -------------------------------------------------------------------------------- /Assets/Script/GameManager.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d1845d2970f8e7249928f00f560f40d5 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/Script/PlayerControl.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class PlayerControl : MonoBehaviour { 5 | 6 | 7 | private Camera PlayerCam; // Camera used by the player 8 | private GameManager _GameManager; // GameObject responsible for the management of the game 9 | private int _activePlayer; 10 | private bool _player1AI; 11 | private bool _player2AI; 12 | 13 | // Use this for initialization 14 | void Start () 15 | { 16 | PlayerCam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent(); // Find the Camera's GameObject from its tag 17 | _GameManager = gameObject.GetComponent(); 18 | _player1AI = _GameManager.player1AI; 19 | _player2AI = _GameManager.player2AI; 20 | 21 | } 22 | 23 | // Update is called once per frame 24 | void Update () { 25 | // Look for Mouse Inputs 26 | _activePlayer = _GameManager.activePlayer; 27 | if((_activePlayer == 1 && _player1AI == false) || (_activePlayer == -1 && _player2AI == false)) 28 | { 29 | Debug.Log ("Select"); 30 | GetMouseInputs(); 31 | } 32 | } 33 | 34 | // Detect Mouse Inputs 35 | void GetMouseInputs() 36 | { 37 | _activePlayer = _GameManager.activePlayer; 38 | Ray _ray; 39 | RaycastHit _hitInfo; 40 | 41 | // Select a piece if the gameState is 0 or 1 42 | if(_GameManager.gameState < 2) 43 | { 44 | // On Left Click 45 | if(Input.GetMouseButtonDown(0)) 46 | { 47 | _ray = PlayerCam.ScreenPointToRay(Input.mousePosition); // Specify the ray to be casted from the position of the mouse click 48 | 49 | // Raycast and verify that it collided 50 | if(Physics.Raycast (_ray,out _hitInfo)) 51 | { 52 | // Select the piece if it has the good Tag 53 | if(_hitInfo.collider.gameObject.tag == (_activePlayer.ToString())) 54 | { 55 | 56 | _GameManager.SelectPiece(_hitInfo.collider.gameObject); 57 | } 58 | } 59 | } 60 | } 61 | 62 | // Move the piece if the gameState is 1 63 | if(_GameManager.gameState == 1) 64 | { 65 | Vector2 selectedCoord; 66 | 67 | // On Left Click 68 | if(Input.GetMouseButtonDown(0)) 69 | { 70 | _ray = PlayerCam.ScreenPointToRay(Input.mousePosition); // Specify the ray to be casted from the position of the mouse click 71 | 72 | // Raycast and verify that it collided 73 | if(Physics.Raycast (_ray,out _hitInfo)) 74 | { 75 | 76 | // If the ray hit a cube, move. If it hit a piece of the other player, eat it. 77 | if(_hitInfo.collider.gameObject.tag == ("Cube")) 78 | { 79 | selectedCoord = new Vector2(_hitInfo.collider.gameObject.transform.position.x,_hitInfo.collider.gameObject.transform.position.z); 80 | _GameManager.MovePiece(selectedCoord); 81 | } 82 | else if(_hitInfo.collider.gameObject.tag == ((-1*_activePlayer).ToString())) 83 | { 84 | _GameManager.EatPiece(_hitInfo.collider.gameObject); 85 | } 86 | } 87 | } 88 | } 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /Assets/Script/PlayerControl.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 016728017ad6c2e449fcf68ab4fe8b26 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /ProjectSettings/AudioManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RodGame/Chess/ef7ee648c98c6c9025eb8b8a33c85826a9a63b90/ProjectSettings/AudioManager.asset -------------------------------------------------------------------------------- /ProjectSettings/DynamicsManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RodGame/Chess/ef7ee648c98c6c9025eb8b8a33c85826a9a63b90/ProjectSettings/DynamicsManager.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorBuildSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RodGame/Chess/ef7ee648c98c6c9025eb8b8a33c85826a9a63b90/ProjectSettings/EditorBuildSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RodGame/Chess/ef7ee648c98c6c9025eb8b8a33c85826a9a63b90/ProjectSettings/EditorSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/GraphicsSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RodGame/Chess/ef7ee648c98c6c9025eb8b8a33c85826a9a63b90/ProjectSettings/GraphicsSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/InputManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RodGame/Chess/ef7ee648c98c6c9025eb8b8a33c85826a9a63b90/ProjectSettings/InputManager.asset -------------------------------------------------------------------------------- /ProjectSettings/NavMeshLayers.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RodGame/Chess/ef7ee648c98c6c9025eb8b8a33c85826a9a63b90/ProjectSettings/NavMeshLayers.asset -------------------------------------------------------------------------------- /ProjectSettings/NetworkManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RodGame/Chess/ef7ee648c98c6c9025eb8b8a33c85826a9a63b90/ProjectSettings/NetworkManager.asset -------------------------------------------------------------------------------- /ProjectSettings/ProjectSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RodGame/Chess/ef7ee648c98c6c9025eb8b8a33c85826a9a63b90/ProjectSettings/ProjectSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/QualitySettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RodGame/Chess/ef7ee648c98c6c9025eb8b8a33c85826a9a63b90/ProjectSettings/QualitySettings.asset -------------------------------------------------------------------------------- /ProjectSettings/TagManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RodGame/Chess/ef7ee648c98c6c9025eb8b8a33c85826a9a63b90/ProjectSettings/TagManager.asset -------------------------------------------------------------------------------- /ProjectSettings/TimeManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RodGame/Chess/ef7ee648c98c6c9025eb8b8a33c85826a9a63b90/ProjectSettings/TimeManager.asset -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Chess 2 | ===== 3 | 4 | Chess game made under Unity. 5 | 6 | A tutorial is made based on this game and can be found on my blog : 7 | http://learninggeekblog.wordpress.com/ 8 | --------------------------------------------------------------------------------