Какой код я должен изменить, чтобы сделать эту многопользовательскую игру в шашки com vs com ?
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CheckerBoard : MonoBehaviour { public Piece[,] pieces = new Piece[8, 8]; public GameObject whitePiece; public GameObject blackPiece; private Vector3 boardoffset = new Vector3(-4.0f, 0, -4.0f); private Vector3 pieceoffset = new Vector3(0.5f, 0, 0.5f); private Piece SelectedPiece; private List<piece> forcedPieces; public bool isWhite; private bool isWhiteTurn; private bool hasKilled; private Vector2 mouseover; private Vector2 startDrag; private Vector2 endDrag; private void Start() { isWhiteTurn = true; isWhite = true; forcedPieces = new List<piece>(); GenerateBoard(); } private void Update() { UpdateMouseOver(); //if it is my turn if((isWhite)?isWhiteTurn:!isWhiteTurn) { int x = (int)mouseover.x; int y = (int)mouseover.y; if (SelectedPiece != null) { UpdatePieceDrag(SelectedPiece); } if (Input.GetMouseButtonDown(0)) { SelectPiece(x, y); } if (Input.GetMouseButtonUp(0)) TryMove((int)startDrag.x, (int)startDrag.y, x, y); } } private void UpdateMouseOver() { //if it is my turn if(!Camera.main) { Debug.Log("Unable to find main camera"); return; } RaycastHit hit; if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),out hit,25.0f,LayerMask.GetMask("Board"))) { mouseover.x = (int)(hit.point.x - boardoffset.x); mouseover.y = (int)(hit.point.z - boardoffset.z); } else { mouseover.x = -1; mouseover.y = -1; } } private void GenerateBoard() { // generate white pieces for(int y=0; y<3; y++) { bool oddRow = (y % 2 == 0); for(int x=0; x<8; x+=2) { //generate our pieces GeneratePiece((oddRow) ? x : x +1, y); } } //generate black pieces for (int y = 7; y > 4; y--) { bool oddRow = (y % 2 == 0); for (int x = 0; x < 8; x += 2) { //generate thier pieces GeneratePiece((oddRow) ? x : x + 1, y); } } } private void GeneratePiece(int x, int y) { bool isPieceWhite = (y > 3) ? false : true; GameObject go = Instantiate(isPieceWhite?whitePiece:blackPiece) as GameObject; go.transform.SetParent(transform); Piece p = go.GetComponent<piece>(); pieces[x, y] = p; MovePiece(p, x, y); } private void MovePiece(Piece p, int x, int y) { p.transform.position = (Vector3.right * x) + (Vector3.forward * y) + boardoffset + pieceoffset; } private void SelectPiece(int x, int y) { //Out of bound if (x < 0 || x >= 8 || y < 0 || y >= 8) return; Piece p = pieces[x, y]; if(p != null && p.isWhite == isWhite) { if (forcedPieces.Count == 0) { SelectedPiece = p; startDrag = mouseover; } else { //look for the piece under our forced pieces list if (forcedPieces.Find(fp => fp == p) == null) return; SelectedPiece = p; startDrag = mouseover; } } } private void TryMove(int x1,int y1,int x2,int y2) { forcedPieces = ScanForPossibleMove(); //Multiplayer Support startDrag = new Vector2(x1, y1); endDrag = new Vector2(x2, y2); SelectedPiece = pieces[x1, y1]; //error if(x2 < 0 || x2 >= 8 || y2 < 0 || y2 >= 8) { if (SelectedPiece != null) { MovePiece(SelectedPiece, x1,y1); } startDrag = Vector2.zero; SelectedPiece = null; return; } if(SelectedPiece != null) { //if it has not moved if( endDrag == startDrag) { MovePiece(SelectedPiece, x1, y1); startDrag = Vector2.zero; SelectedPiece = null; return; } //check if its a valid move if(SelectedPiece.validMove(pieces, x1, y1, x2, y2)) { //did we kill anything //if this is a jump if(Mathf.Abs(x2 - x1) == 2) { Piece p = pieces[(x1 + x2) / 2, (y1 + y2) / 2]; if(p != null) { pieces[(x1 + x2) / 2, (y1 + y2) / 2] = null; Destroy(p.gameObject); hasKilled = true; } } //we are supposed to killaa anything? if(forcedPieces.Count != 0 && !hasKilled) { MovePiece(SelectedPiece, x1, y1); startDrag = Vector2.zero; SelectedPiece = null; return; } pieces[x2, y2] = SelectedPiece; pieces[x1, y1] = null; MovePiece(SelectedPiece,x2, y2); EndTurn(); } else { MovePiece(SelectedPiece, x1, y1); startDrag = Vector2.zero; SelectedPiece = null; return; } } } private void EndTurn() { int x = (int)endDrag.x; int y = (int)endDrag.y; //promotions if(SelectedPiece != null) { if(SelectedPiece.isWhite && !SelectedPiece.isKing && y == 7) { SelectedPiece.isKing = true; SelectedPiece.transform.Rotate(Vector3.right * 180); } else if(!SelectedPiece.isWhite && !SelectedPiece.isKing && y == 0) { SelectedPiece.isKing = true; SelectedPiece.transform.Rotate(Vector3.right * 180); } } SelectedPiece = null; startDrag = Vector2.zero; if (ScanForPossibleMove(SelectedPiece, x, y).Count != 0 && hasKilled) return; isWhiteTurn = !isWhiteTurn; isWhite = !isWhite; hasKilled = false; CheckVictory(); } private void CheckVictory() { var ps = FindObjectsOfType<piece>(); bool hasWhite = false, hasBlack = false; for(int i = 0; i < ps.Length; i++) { if (ps[i].isWhite) hasWhite = true; else hasBlack = true; } if (!hasWhite) Victory(false); if (!hasBlack) Victory(true); } public void Victory(bool isWhite) { if (isWhite) Debug.Log("White player has won"); else Debug.Log("Black player has won"); } private List<piece> ScanForPossibleMove() { forcedPieces = new List<piece>(); //check all the pieces for (int i = 0; i < 8; i++) for (int j = 0; j < 8; j++) if (pieces[i, j] != null && pieces[i, j].isWhite == isWhiteTurn) if (pieces[i, j].IsForceToMove(pieces, i, j)) forcedPieces.Add(pieces[i, j]); return forcedPieces; } private List<piece> ScanForPossibleMove(Piece p, int x, int y) { forcedPieces = new List<piece>(); if (pieces[x, y].IsForceToMove(pieces, x, y)) forcedPieces.Add(pieces[x, y]); return forcedPieces; } private void UpdatePieceDrag(Piece p) { //if it is my turn if (!Camera.main) { Debug.Log("Unable to find main camera"); return; } RaycastHit hit; if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 25.0f, LayerMask.GetMask("Board"))) { p.transform.position = hit.point + Vector3.up; } } }
[edit]добавлен блок кода-OriginalGriff [/edit]
Что я уже пробовал:
я пытался начать с мыши, но это не сработало