private bool IsPeice(PlayerPeice peice, BoardSegment segment) { if (peice) { return(true); } else { if (segment) { if (segment.state == SegmentOccupationState.Empty) { this.peice = Board.peices[segment.pos.indexX, segment.pos.indexY].GetComponent <PlayerPeice>(); } if (this.peice) { return(false); } else { return(true); } } //return false; } return(false); }
private void MovePiece(BoardSegment newPosition) { image.transform.position = newPosition.segment.transform.position; _previousPosition = _currentBoardSegment; _currentBoardSegment = newPosition; _currentBoardSegment.OccupyThisSegment(this); _previousPosition.OccupyThisSegment(null); GameManager.ChangeActiveTeam(); }
public void AttemptToMove(BoardSegment newPosition) { if (!IsLegitimateMove(newPosition)) { Debug.Log(this + " cant move to that position from here."); return; } if (newPosition.occupation.Key) { if (newPosition.occupation.Value.team != team) { GameManager.TakePiece(newPosition); MovePiece(newPosition); } Debug.Log("Space occupation."); return; } MovePiece(newPosition); }
protected override BoardSegment[] ReturnAllPotentialPositions(BoardSegment originalSegment, KeyValuePair <Directions, int>[] moveSet) { var returnVariable = new List <BoardSegment>(); foreach (var move in moveSet) { // ReSharper disable once SwitchStatementMissingSomeEnumCasesNoDefault switch (move.Key) //Maybe refactor this check into one that's performed in the base class?? { case Directions.North: if (originalSegment.row >= ChessBoard.Board.Rows - 1) { break; } returnVariable.Add(GameManager.instance.board.ReturnNorthSegment(originalSegment)); break; case Directions.NorthEast: if (originalSegment.column <= 0) { break; } returnVariable.Add(GameManager.instance.board.ReturnNorthEastSegment(originalSegment)); break; case Directions.NorthWest: if (originalSegment.column >= ChessBoard.Board.Columns - 1) { break; } returnVariable.Add(GameManager.instance.board.ReturnNorthWestSegment(originalSegment)); break; } } return(returnVariable.ToArray()); }
protected virtual BoardSegment[] ReturnAllPotentialPositions(BoardSegment originalSegment, KeyValuePair <Directions, int>[] moveSet) { Debug.LogError("This is the base class for chess pieces not an actual piece, override this method and set specific conditions for your piece"); return(null); }
private bool IsLegitimateMove(BoardSegment newPosition) { var potentialMoves = ReturnAllPotentialPositions(_currentBoardSegment, movesList); return(potentialMoves.Any(move => newPosition == move)); }
public void Initialise(BoardSegment startingSegment) { SetMovesCapabilities(); _currentBoardSegment = startingSegment; }
//TODO: Generate an *x8 board //TODO: Chanvge the color of the board segements //TODO: Populate the game board with player peices //TODO: Impliment singelton /* * // Start is called before the first frame update * void Start() * { * BuildBoard(); * * } * * * void Update() * { * * } */ #region BoardGeneration public GameObject[,] BuildBoard(byte[] serverState, bool rebuildBoard = false) { //TODO: Place Peices on board according to the server state if (boardSpaces != null) { if (rebuildBoard) { DeconstructBoard(); } else { return(boardSpaces); } } boardSpaces = new GameObject[boardSize, boardSize]; peices = new GameObject[boardSize, boardSize]; int totalIterations = 0; float segmentSpaceing = 1 + offset; for (int y = 0; y < boardSize; y++) { for (int x = 0; x < boardSize; x++) { GameObject b = InstantiateSegmant(new Vector3((segmentSpaceing) * x, 0, (segmentSpaceing) * y)); //spawn board segmants boardSpaces[x, y] = b; //store a refrence to the segmant BoardSegment bs = b.GetComponent <BoardSegment>(); // bs.init(new BoardPOS(x, y), blackMat, whiteMat); //print("totalIterations: " + totalIterations); //int sState = (int)serverState[totalIterations]; //print("serverState: " + (SegmentOccupationState)serverState[totalIterations]); GameObject peice = bs.PopulateStart((SegmentOccupationState)serverState[totalIterations]); if (peice) { peices[x, y] = peice; PlayerPeice peiceScript = peice.GetComponent <PlayerPeice>(); peiceScript.acessIndex = new Vector2(x, y); } if (y % 2 != 0 && y != 0) //every other row { if (totalIterations % 2 == 0) //if we are even { SwapToBlack(b); } } else if (totalIterations % 2 != 0 && totalIterations != 0)//if we are odd { SwapToBlack(b); } totalIterations++; } } return(boardSpaces); }
public bool HandleMove(byte[] updatedState) { Vector2 currentIndex = new Vector2(-1, -1); Vector2 targetIndex = new Vector2(-1, -1); //determine which spaces can need to be updated for (int i = 0; i < updatedState.Length; i++) { int y = i / boardSize; int x = i % boardSize; if (updatedState[i] == 0) { if (updatedState[i] != (int)boardSpaces[x, y].GetComponent <BoardSegment>().state) { currentIndex = new Vector2(x, y); } } else { if (updatedState[i] != (int)boardSpaces[x, y].GetComponent <BoardSegment>().state) { targetIndex = new Vector2(x, y); } } } if (targetIndex.x < 0 || targetIndex.y < 0 || currentIndex.x < 0 || currentIndex.y < 0) { return(false); //there was no change in board state } BoardSegment targetSegmant = boardSpaces[(int)targetIndex.x, (int)targetIndex.y].GetComponent <BoardSegment>(); //BoardSegment currentSegmant = boardSpaces[(int)currentIndex.x, (int)currentIndex.y].GetComponent<BoardSegment>(); //remove "taken peices" SegmentOccupationState futureState = (SegmentOccupationState)updatedState[targetSegmant.pos.packetIndex]; if (OccupiedByEnemey(targetSegmant.state, futureState)) { GameObject p = peices[(int)targetIndex.x, (int)targetIndex.y]; if (p != null) { peices[(int)targetIndex.x, (int)targetIndex.y] = null; //print("DESTROYING"); Destroy(p); } else { //print("alwats null"); return(false); } } //move peices on gameboard peices[(int)currentIndex.x, (int)currentIndex.y].GetComponent <PlayerPeice>().MovePeice(targetSegmant.snapPointHover.position, targetSegmant.snapPointPlaced.position); //update gamestate peices[(int)targetIndex.x, (int)targetIndex.y] = peices[(int)currentIndex.x, (int)currentIndex.y]; peices[(int)currentIndex.x, (int)currentIndex.y] = null; peices[(int)targetIndex.x, (int)targetIndex.y].GetComponent <PlayerPeice>().acessIndex = targetIndex; boardSpaces[(int)targetIndex.x, (int)targetIndex.y].GetComponent <BoardSegment>().state = boardSpaces[(int)currentIndex.x, (int)currentIndex.y].GetComponent <BoardSegment>().state; boardSpaces[(int)currentIndex.x, (int)currentIndex.y].GetComponent <BoardSegment>().state = SegmentOccupationState.Empty; return(true); }
}//update private void HandleSelection() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //print("Casting"); RaycastHit hit; if (Physics.Raycast(ray, out hit))//, 50000, peiceMask)) { PlayerPeice p; BoardSegment b = null; //bool isPeice = true; //print("castHit"); GameObject obj = hit.collider.gameObject; if (obj != null) { p = obj.GetComponentInParent <PlayerPeice>(); b = obj.GetComponentInParent <BoardSegment>(); if (p == null && b == null)//if it's not a player peice { return; } } else { return; } if (p) { if (p.owner == playerState) { if (p == peice) //we have clicked on our selected peice again { peiceSelected = false; peice = null; } else //we have clicked on one of our peices { peice = p; ControllerGameClient.singleton.SendPacketToServer(PacketBuilder.Hover((int)peice.acessIndex.x, (int)peice.acessIndex.y)); peiceSelected = true; } } else if (peiceSelected) //we have a piece selected and we are clicking on the opponents peice { SendMoveToServer((int)p.acessIndex.x, (int)p.acessIndex.y); } } else if (peiceSelected && b) //we have selected a segmant and we have a piece selected { SendMoveToServer(b.pos.indexX, b.pos.indexY); } /* * //Debug.Log(hit.collider.gameObject.name); * peice = hit.collider.gameObject.GetComponentInParent<PlayerPeice>(); * segment = hit.collider.gameObject.GetComponent<BoardSegment>(); * * * if (IsPeice(peice, segment)) { * * ControllerGameClient.singleton.SendPacketToServer(PacketBuilder.Hover((int)peice.acessIndex.x, (int)peice.acessIndex.y)); * } * //Debug.Log(peice.peiceType + " " + peice.owner); */ } /* * else if (Physics.Raycast(ray, out hit, 50000, boardMask) && peiceSelected) { * * segment = hit.collider.gameObject.GetComponent<BoardSegment>(); * * * * * * }//raycasthit */ }
public static void TakePiece(BoardSegment vitimPosition, Action callBack = null) { vitimPosition.occupation.Value.PieceAttacked(); callBack?.Invoke(); }