コード例 #1
0
ファイル: ChessBoard.cs プロジェクト: RobinTo/SimpleChess
        public override void Update(GameTime gameTime)
        {
            if (!connected)
            {
                if (reconnectTimer <= 0)
                {
                    connected = networkManager.TryConnect();
                    reconnectTimer = 5.0; // Added a small timer to enable closing of client window.
                }
                else
                    reconnectTimer -= gameTime.ElapsedGameTime.TotalSeconds;
            }
            else if (!hasOpponent)
            {
                if (networkManager.isDataAvailableClient())
                {
                    if (networkManager.ReturnByte() == 101)
                    {
                        hasOpponent = true;
                    }
                }
            }
            else if (!disconnected)
            {
                if (networkManager.isDataAvailableClient())
                {
                    switch (networkManager.ReturnByte())
                    {
                        case 0:
                            // It was just a ping
                            break;
                        case 98:
                            UndoMove(moves[moves.Count - 1]);
                            break;
                        case 99:
                            receiveMove r = networkManager.ReceiveMove();
                            MovePiece(r.id, r.x, r.y);
                            break;
                        case 100:
                            // errorString = "Mostander frakoblet. \nEscape for nytt spill.";
                            disconnected = true;
                            break;
                        default:

                            break;
                    }
                }

                if (connectTimer > 0)
                    connectTimer -= gameTime.ElapsedGameTime.TotalSeconds;
                else
                {
                    if (!networkManager.AreWeStillConnected())
                    {
                        disconnected = true;
                    }
                    connectTimer = 5;
                }

                newMouse = Mouse.GetState();
                newKey = Keyboard.GetState();

                if (newKey.IsKeyDown(Keys.Back) && oldKey.IsKeyUp(Keys.Back))
                {
                    if (moves.Count > 0)
                    {
                        networkManager.SendUndo();
                        UndoMove(moves[moves.Count - 1]);
                    }
                }
                else if (newKey.IsKeyDown(Keys.Left) && oldKey.IsKeyUp(Keys.Left))
                {
                    if (moves.Count > 0)
                    {
                        networkManager.SendUndo();
                        UndoMove(moves[moves.Count - 1]);
                    }
                }
                else if (newKey.IsKeyDown(Keys.Enter) && oldKey.IsKeyUp(Keys.Enter))
                {
                    rotate = !rotate;
                }

                if (newMouse.RightButton == ButtonState.Pressed && oldMouse.RightButton != ButtonState.Pressed)
                {
                    selectedPiece = null;
                    move = false;
                    selectedY = 0;
                    selectedX = 0;
                }
                else if (newMouse.LeftButton == ButtonState.Pressed && oldMouse.LeftButton != ButtonState.Pressed)
                {
                    int newClickX = 1 + (newMouse.X - xPos) / 80;
                    int newClickY = 1 + (newMouse.Y - yPos) / 80;
                    int markX = newClickX;
                    int markY = newClickY;

                    if (rotate)
                    {
                        newClickX = GameConstants.boardWidth - newClickX + 1;
                        newClickY = GameConstants.boardHeight - newClickY + 1;
                    }

                    if (newClickX > 0 && newClickX <= 8 && newClickY > 0 && newClickY <= 8)
                    {
                        if (!move)
                        {
                            foreach (ChessPiece piece in pieces)
                            {
                                if (piece.X == newClickX && piece.Y == newClickY && piece.Visible)
                                {
                                    markY = newClickY;
                                    markX = newClickX;
                                    selectedPiece = piece;
                                    move = true;
                                }
                            }
                        }
                        else if (!(selectedPiece.X == newClickX && selectedPiece.Y == newClickY))
                        {
                            markY = newClickY;
                            markX = newClickX;

                            networkManager.SendMove(selectedPiece.Id, newClickX, newClickY);
                            MovePiece(selectedPiece.Id, newClickX, newClickY);
                            move = false;
                        }
                    }

                    selectedX = markX;
                    selectedY = markY;
                }

                oldMouse = newMouse;
                oldKey = newKey;
            }
            base.Update(gameTime);
        }
コード例 #2
0
ファイル: ChessBoard.cs プロジェクト: RobinTo/SimpleChess
        protected override void LoadContent()
        {
            ContentManager content = Game.Content;
            blackSquare = content.Load<Texture2D>("black");
            whiteSquare = content.Load<Texture2D>("white");

            pawn = content.Load<Texture2D>("whitePesant");
            castle = content.Load<Texture2D>("whiteTower");
            knight = content.Load<Texture2D>("whiteHorse");
            springer = content.Load<Texture2D>("whiteSpringer");
            king = content.Load<Texture2D>("whiteKing");
            queen = content.Load<Texture2D>("whiteQueen");

            font = content.Load<SpriteFont>("font");
            infoFont = content.Load<SpriteFont>("TextFont");

            try
            {
                String ipString = System.IO.File.ReadAllText("ipAddresse.txt");
                networkManager.StartClient(ipString);
            }
            catch (Exception e)
            {
                errorString = "Feil i Innstillingene.";
            }

            for (int i = 1; i <= GameConstants.boardWidth; i++)
            {
                ChessPiece pawnPiece = new ChessPiece(id, pawn, i, 2, Color.White);
                pieces.Add(pawnPiece);
                id++;
            }
            for (int i = 1; i <= GameConstants.boardWidth; i++)
            {
                ChessPiece pawnPiece = new ChessPiece(id, pawn, i, 7, Color.Black);
                pieces.Add(pawnPiece);
                id++;
            }

            for (int i = 0; i <= 1; i++)
            {
                ChessPiece whiteCastles = new ChessPiece(id, castle,  i == 0 ? 1 : 8, 1, Color.White);
                pieces.Add(whiteCastles);
                id++;
            }
            for (int i = 0; i <= 1; i++)
            {
                ChessPiece blackCastles = new ChessPiece(id, castle, i == 0 ? 1 : 8, 8, Color.Black);
                pieces.Add(blackCastles);
                id++;
            }

            for (int i = 0; i <= 1; i++)
            {
                ChessPiece whiteKnight = new ChessPiece(id, knight, i == 0 ? 2 : 7, 1, Color.White);
                pieces.Add(whiteKnight);
                id++;
            }
            for (int i = 0; i <= 1; i++)
            {
                ChessPiece blackKnight = new ChessPiece(id, knight, i == 0 ? 2 : 7, 8, Color.Black);
                pieces.Add(blackKnight);
                id++;
            }

            for (int i = 0; i <= 1; i++)
            {
                ChessPiece blackSpringer = new ChessPiece(id, springer, i == 0 ? 3 : 6, 8, Color.Black);
                pieces.Add(blackSpringer);
                id++;
            }
            for (int i = 0; i <= 1; i++)
            {
                ChessPiece whiteSpringer = new ChessPiece(id, springer, i == 0 ? 3 : 6, 1, Color.White);
                pieces.Add(whiteSpringer);
                id++;
            }

            ChessPiece whiteKing = new ChessPiece(id, king, 4, 1, Color.White);
            pieces.Add(whiteKing);
            id++;
            ChessPiece whiteQueen = new ChessPiece(id, queen, 5, 1, Color.White);
            pieces.Add(whiteQueen);
            id++;
            ChessPiece blackKing = new ChessPiece(id, king, 4, 8, Color.Black);
            pieces.Add(blackKing);
            id++;
            ChessPiece blackQueen = new ChessPiece(id, queen, 5, 8, Color.Black);
            pieces.Add(blackQueen);
            id++;

            base.LoadContent();
        }
コード例 #3
0
ファイル: ChessBoard.cs プロジェクト: RobinTo/SimpleChess
        public void UndoMove(move m)
        {
            foreach (ChessPiece piece in pieces)
            {
                if (piece.Id == m.id)
                {
                    piece.X = m.oldX;
                    piece.Y = m.oldY;

                    if (m.shotID > 0)
                    {
                        foreach (ChessPiece dupiece in pieces)
                        {
                            if (dupiece.Id == m.shotID)
                                dupiece.Visible = true;
                        }
                    }
                    selectedPiece = null;
                    selectedX = 0;
                    selectedY = 0;
                    move = false;
                }
            }
            moves.Remove(m);
        }