コード例 #1
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            timeInMillis += gameTime.ElapsedGameTime.Milliseconds;

            lastKeyboardState = currentKeyboardState;
            lastGamePadState  = currentGamePadState;
            lastMousState     = currentMouseState;

            currentKeyboardState = Keyboard.GetState();
            currentGamePadState  = GamePad.GetState(PlayerIndex.One);
            currentMouseState    = Mouse.GetState();

            // Exit when the Escape key or Back button is pressed
            if (currentKeyboardState.IsKeyDown(Keys.Escape) ||
                currentGamePadState.Buttons.Back == ButtonState.Pressed)
            {
                Exit();
            }

            if (currentKeyboardState.IsKeyDown(Keys.Space))
            {
                if (ball.onGround && !ball.jumpDelayTimer.IsRunning)
                {
                    ball.ApplyForce(new Vector3(0, 300, 0));
                    Sound.playSoundOnce("Sound\\jump", Content);
                }
            }

            if (currentKeyboardState.IsKeyDown(Keys.I))
            {
                Vector3 offset = camera.DesiredPositionOffset;
                offset.Y = MathHelper.Clamp(offset.Y += 2, 20, 150);
                camera.DesiredPositionOffset = offset;
                Sound.playSoundOnce("Sound\\rotateCam", Content);
            }
            else if (currentKeyboardState.IsKeyDown(Keys.K))
            {
                Vector3 offset = camera.DesiredPositionOffset;
                offset.Y = MathHelper.Clamp(offset.Y -= 2, 20, 150);
                camera.DesiredPositionOffset = offset;
                Sound.playSoundOnce("Sound\\rotateCam", Content);
            }

            if (currentKeyboardState.IsKeyDown(Keys.Q) && !lastKeyboardState.IsKeyDown(Keys.Q))
            {
                if (ballTexture.Equals(ballTextureBeach))
                {
                    ballTexture = ballTextureMetal;
                }
                else if (ballTexture.Equals(ballTextureMetal))
                {
                    ballTexture = ballTextureBeach;
                }
                Sound.playSoundOnce("Sound\\changeBall", Content);
            }
            else if (currentKeyboardState.IsKeyDown(Keys.E) && !lastKeyboardState.IsKeyDown(Keys.E))
            {
                if (ballTexture.Equals(ballTextureBeach))
                {
                    ballTexture = ballTextureMetal;
                }
                else if (ballTexture.Equals(ballTextureMetal))
                {
                    ballTexture = ballTextureBeach;
                }
                Sound.playSoundOnce("Sound\\changeBall", Content);
            }
            if (currentKeyboardState.IsKeyDown(Keys.N))
            {
                Sound.pauseSound("Sound\\background", Content);
                Sound.playSoundLoop("Sound\\background2", Content);
            }
            if (currentKeyboardState.IsKeyDown(Keys.M))
            {
                Sound.pauseSound("Sound\\background2", Content);
                Sound.playSoundLoop("Sound\\background", Content);
            }

            bool touchTopLeft = currentMouseState.LeftButton == ButtonState.Pressed &&
                                lastMousState.LeftButton != ButtonState.Pressed &&
                                currentMouseState.X < GraphicsDevice.Viewport.Width / 10 &&
                                currentMouseState.Y < GraphicsDevice.Viewport.Height / 10;

            if (currentKeyboardState.IsKeyDown(Keys.R) ||
                currentGamePadState.Buttons.RightStick == ButtonState.Pressed)
            {
                ball.Reset();
                camera.Reset();
            }

            //if (currentKeyboardState.IsKeyDown(Keys.Z))
            //{
            //    IPEndPoint ip = new IPEndPoint(IPAddress.Parse("128.143.69.241"), 9050);
            //    Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //    try
            //    {
            //        server.Connect(ip);
            //    }
            //    catch (SocketException e)
            //    {
            //        Console.WriteLine("Unable to connect to server.");
            //        return;
            //    }
            //    server.Send(Encoding.ASCII.GetBytes("HIGHSCORE-INSERT-1-5000"));
            //    byte[] data = new byte[1024];
            //    int receivedDataLength = server.Receive(data);
            //    string stringData = Encoding.ASCII.GetString(data, 0, receivedDataLength);
            //    Console.WriteLine("DATA FROM SERVER: " + stringData);
            //}

            ball.Update(gameTime);
            camera.ChasePosition  = ball.Position;
            camera.ChaseDirection = ball.Direction;
            camera.Up             = ball.Up;
            camera.Reset();
            base.Update(gameTime);
        }