コード例 #1
1
ファイル: Paddle.cs プロジェクト: Gevil/Projects
        public Paddle(ContentManager theContent, string theAssetName, Input theInput, PaddlePosition thePosition)
            : base(theContent, theAssetName)
        {
            SourceRectangle = new Rectangle(0, 0, 1, 1);
            Scale = new Vector2(10.0f, 100.0f);
            mColor = Color.SlateBlue;

            mPaddlePosition = thePosition;
            switch (thePosition)
            {
                case PaddlePosition.Left:
                    {
                        Boundary = new Rectangle(140, 185, 10, 833);
                        break;
                    }

                case PaddlePosition.Right:
                    {
                        Boundary = new Rectangle(1130, 185, 10, 833);
                        break;
                    }
            }

            Position = Center(Boundary);

            mInput = theInput;
        }
コード例 #2
0
ファイル: Paddle.cs プロジェクト: Gevil/Projects
        public Paddle(ContentManager theContent, string theAssetName, Input theInput, PaddlePosition thePosition)
            : base(theContent, theAssetName)
        {
            SourceRectangle = new Rectangle(0, 0, 1, 1);
            Scale           = new Vector2(10.0f, 100.0f);
            mColor          = Color.SlateBlue;

            mPaddlePosition = thePosition;
            switch (thePosition)
            {
            case PaddlePosition.Left:
            {
                Boundary = new Rectangle(140, 185, 10, 833);
                break;
            }

            case PaddlePosition.Right:
            {
                Boundary = new Rectangle(1130, 185, 10, 833);
                break;
            }
            }

            Position = Center(Boundary);

            mInput = theInput;
        }
コード例 #3
0
 public bool MoveRelative(PaddlePosition displacement)
 {
     try
     {
         short pulses = (short)(UnitToPulse(displacement.Position, -0x3FF, 0x3FF));
         UpdateOutput(string.Format("Move paddle {0} by {1}...", (char)displacement.PaddleId, displacement.PaddleId));
         ELLDevicePort.SendStringI16(Address, $"r{(char)displacement.PaddleId}", pulses);
         return(WaitForPosition(displacement.PaddleId));
     }
     catch (OverflowException)
     {
         UpdateOutput(string.Format("Parameter out of range {0}...", displacement.Position));
     }
     return(false);
 }
コード例 #4
0
 public bool MoveAbsolute(PaddlePosition position)
 {
     try
     {
         short pulses = (short)(UnitToPulse(position.Position, 0, 0x3FF));
         UpdateOutput(string.Format("Move paddle {0} to {1}...", (char)position.PaddleId, position.PaddleId));
         ELLDevicePort.SendStringI16(Address, $"a{(char)position.PaddleId}", pulses);
         return(WaitForPosition(position.PaddleId));
     }
     catch (OverflowException)
     {
         UpdateOutput(string.Format("Parameter out of range {0}...", position.Position));
     }
     return(false);
 }
コード例 #5
0
 private bool WaitForPosition(PaddleIDs paddleID, int msTimeout = MoveTimeout)
 {
     try
     {
         string        command   = $"p{(char)paddleID}";
         string        response  = $"P{(char)paddleID}";
         List <string> responses = new List <string> {
             "GS", response
         };
         int counter = 10;
         while (true)
         {
             string msg = ELLDevicePort.WaitForResponse(Address, responses, msTimeout);
             if (!string.IsNullOrEmpty(msg))
             {
                 bool returnValue;
                 if (TestStatus(msg, command, true, ref counter, out returnValue))
                 {
                     return(returnValue);
                 }
                 if (msg.Substring(1, 2) == response)
                 {
                     if (msg.Length != 7)
                     {
                         return(false);
                     }
                     PaddlePosition position = GetPosition(msg.Substring(3).ToBytes(4).ToInt(true), paddleID);
                     UpdateParameter(MessageUpdater.UpdateTypes.PaddlePosition, Address, position);
                     return(true);
                 }
             }
         }
     }
     catch (ELLException ex)
     {
         UpdateOutput($"Get Device Status: {ex.Message}", true);
     }
     return(false);
 }
コード例 #6
0
        public void Run(PaddlePosition paddlePosition)
        {
            if (paddlePosition == PaddlePosition.Left)
            {
                playerPaddle.Position =
                    new Vector2f(10 + playerPaddle.Size.X, (float)VideoMode.DesktopMode.Height / 2);
                opponentPaddle.Position = new Vector2f(VideoMode.DesktopMode.Width - 10 - opponentPaddle.Size.X,
                                                       (float)VideoMode.DesktopMode.Height / 2);
            }
            else
            {
                opponentPaddle.Position =
                    new Vector2f(10 + opponentPaddle.Size.X, (float)VideoMode.DesktopMode.Height / 2);
                playerPaddle.Position = new Vector2f(VideoMode.DesktopMode.Width - 10 - playerPaddle.Size.X,
                                                     (float)VideoMode.DesktopMode.Height / 2);
            }

            ball.RandomAngle();
            ball.Position = new Vector2f((float)VideoMode.DesktopMode.Width / 2, (float)VideoMode.DesktopMode.Height / 2);

            var initState = new InitialState
            {
                StartBallAngle      = ball.Angle,
                StartBallPosition   = ball.Position,
                StartPaddlePosition = playerPaddle.Position
            };

            connection.SendMessage(JsonConvert.SerializeObject(initState));

            var windowColor = new Color(0, 0, 0);

            app.Draw(result);
            app.Display();

            while (app.IsOpen)
            {
                app.DispatchEvents();

                app.Clear(windowColor);

                GameState gameState;
                try
                {
                    gameState = JsonConvert.DeserializeObject <GameState>(connection.ReceiveMessage());
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    OnClose(app, EventArgs.Empty);
                    return;
                }
                opponentHeight.SetCursorHeight(gameState.OpponentHeight);
                ball.SetPosition(gameState.BallPosition);
                ball.Angle = gameState.BallAngle;
                ball.UpdatePosition();

                playerPaddle.UpdatePosition();
                opponentPaddle.UpdatePosition();

                result.SetText(gameState.ResultMessage);

                app.Draw(playerPaddle);
                app.Draw(opponentPaddle);
                app.Draw(ball);
                app.Draw(result);

                foreach (var border in borders)
                {
                    app.Draw(border);
                }

                app.Display();

                var clientState = new ClientState();
                CheckCollisions(clientState);
                clientState.cursorHeight    = playerPaddle.Position.Y;
                clientState.newBallPosition = ball.Position;
                clientState.pause           = pause;
                if (resetRequested)
                {
                    clientState.resetRequested = true;
                    resetRequested             = false;
                }
                try
                {
                    connection.SendMessage(JsonConvert.SerializeObject(clientState));
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    OnClose(app, EventArgs.Empty);
                    return;
                }
            }
        }