/// <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) { KeyboardState currState = Keyboard.GetState(); // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { this.Exit(); } // Exit the game if the escape key is pressed. if (Keyboard.GetState().IsKeyDown(Keys.Escape)) { this.Exit(); } if (!currState.Equals(prevState)) { if (Keyboard.GetState().IsKeyDown(Keys.F5)) { // Increment the formation counter. // Set a new formation for the gobling squad whenever F5 is pressed. } // ResetMap(); if (Keyboard.GetState().IsKeyDown(Keys.F6)) { m_bSetTilesToSolid = !m_bSetTilesToSolid; SetTilesToSolid(); } } if (!prevState.Equals(currState)) { /* * if (Keyboard.GetState().IsKeyDown(Keys.Left)) * m_TestLayer.ScrollLeft(); * if (Keyboard.GetState().IsKeyDown(Keys.Right)) * m_TestLayer.ScrollRight(); * if (currState.IsKeyDown(Keys.Up)) * m_TestLayer.ScrollUp(); * if (currState.IsKeyDown(Keys.Down)) * m_TestLayer.ScrollDown(); */ } prevState = currState; MouseState currMouse = Mouse.GetState(); if (!currMouse.Equals(prevMouse)) { m_mousePostion.X = currMouse.X; m_mousePostion.Y = currMouse.Y; if (currMouse.LeftButton == ButtonState.Pressed) { m_sMouseClick.Play(); bool bClicked = false; // Check if a tree was clicked. for (int i = 0; i < m_TreeList.Count; i++) { if (m_TreeList[i].IsTreeClicked(m_mousePostion)) { m_Goblin.MoveDistanceInFrontOfEntity(m_TreeList[i], 0); bClicked = true; break; } } for (int i = 0; i < m_LumberMills.Count; i++) { // Check if a lumber mill was clicked. if (m_LumberMills[i].IsClicked(m_mousePostion)) { m_Goblin.MoveDistanceInFrontOfEntity(m_LumberMills[i], 0); bClicked = true; break; } } // If neither a lumber mill or a tree was clicked and the // click occurred inside the clickable area then it must have // been a tile click. if (!bClicked && !IsClickOutsideClickArea(m_mousePostion)) { // Set the state of the gobling to moving if we click anywhere else // that is not a tree or a lumber mill. ((GoblinLumberjack)m_Goblin).GetStateMachine().ChangeState(GLumberjack_MOVE.GetInstance()); ((GoblinLumberjack)m_Goblin).SetTargetTree(null); int x; int y = currMouse.Y / (int)(64 * 0.25); if (y % 2 == 1) { x = (currMouse.X - 32) / 64; } else { x = (int)currMouse.X / 64; } Vector2 dest = new Vector2(); if (y % 2 == 1) { dest.X = x * 64 + 32; } else { dest.X = x * 64; } dest.Y = y * (int)(64 * 0.25) - (int)(64 * 0.50); if (dest.X >= 0 && dest.X < m_DrawWidth * 64 && dest.Y >= 0 && dest.Y < m_DrawHeight * (int)(64 * 0.25)) { //m_Goblin.MoveTo(dest); //m_goblinSquad.MoveTo(MapHandler.GetInstance().GetTileIndex(dest)); } } } } prevMouse = currMouse; // TODO: Add your update logic here float currentUpdate = (float)gameTime.TotalGameTime.TotalMilliseconds; if (nextUpdate <= currentUpdate) { //tileCounter = (tileCounter+1) % (mapTexture.Width / 64 * mapTexture.Height / 64); //m_TestTile.SetTileID(tileCounter); nextUpdate = currentUpdate + updateDelay; } //m_Goblin.Update(gameTime); for (int i = 0; i < m_GoblinList.Count; i++) { m_GoblinList[i].Update(gameTime); } for (int i = 0; i < m_LumberMills.Count; i++) { m_LumberMills[i].Update(gameTime); } // Update the golbing squad. m_TestLayer.Update(gameTime); base.Update(gameTime); }
public void Update(float dt) { Vector3 prevPosition = position; randomShakedt += dt; #if XBOX // Get XBOX Input prevGamePadState = gamePadState; gamePadState = GamePad.GetState(ActivePlayer.PlayerIndex); // Adjust position Vector3 moveDir = look; if (freeFlyEnabled) { if (gamePadState.IsButtonDown(Buttons.LeftShoulder)) { position += up * dt * moveSpeed; } if (gamePadState.IsButtonDown(Buttons.RightShoulder)) { position -= up * dt * moveSpeed; } moveDelta = 1.0f; } else { moveDir.Y = 0; moveDir.Normalize(); // Adjust movement bleedoff speed if (gamePadState.ThumbSticks.Left.Length() > 0.0f) { moveDelta += dt * 5.0f; prevLeftStickY = 0.0f; } else { moveDelta -= dt * 5.0f; if (prevLeftStickY == 0.0f) { prevLeftStickY = prevGamePadState.ThumbSticks.Left.Y; } if (prevGamePadState.IsButtonDown(Buttons.LeftThumbstickDown) || prevGamePadState.IsButtonDown(Buttons.LeftThumbstickLeft) || prevGamePadState.IsButtonDown(Buttons.LeftThumbstickRight)) { moveDelta = 0.0f; } if (moveDelta > 0.0f && prevLeftStickY > 0.25f) { position += moveDir * dt * moveSpeed * moveDelta; } } if (moveDelta > 1.0f) { moveDelta = 1.0f; } if (moveDelta < 0.0f) { moveDelta = 0.0f; } // Update twist angle if (gamePadState.IsButtonUp(Buttons.LeftThumbstickLeft) && gamePadState.IsButtonUp(Buttons.LeftThumbstickRight)) { if (twistAngle > 0.0f) { twistAngle -= twistSpeed * dt; if (twistAngle < 0.0f) { twistAngle = 0.0f; } } else { twistAngle += twistSpeed * dt; if (twistAngle > 0.0f) { twistAngle = 0.0f; } } } else { twistAngle = gamePadState.ThumbSticks.Left.X * twistMaxAngle; } } position += moveDir * gamePadState.ThumbSticks.Left.Y * dt * moveSpeed * moveDelta * (gamePadState.IsButtonDown(Buttons.LeftThumbstickDown) && !freeFlyEnabled ? 0.75f : 1.0f); position += right * gamePadState.ThumbSticks.Left.X * dt * moveSpeed * (freeFlyEnabled ? 1.0f : 0.6f); // Adjust angle speeds if (freeFlyEnabled) { rotationDelta = 1.0f; } else { float increaseSpeed = 3.0f; float maxAmount = 2.0f; float movementAmount = 0.25f; if (gamePadState.ThumbSticks.Right.X >= movementAmount || gamePadState.ThumbSticks.Right.X <= -movementAmount || gamePadState.ThumbSticks.Right.Y >= movementAmount || gamePadState.ThumbSticks.Right.Y <= -movementAmount) { rotationDelta += dt * increaseSpeed; if (rotationDelta > maxAmount) { rotationDelta = maxAmount; } } else { rotationDelta = 1.0f; } } // Adjust look angles if (invertY) { pitchAngle += rotationDelta * gamePadState.ThumbSticks.Right.Y * dt * upDownSensitivity * -1.0f; } else { pitchAngle += rotationDelta * gamePadState.ThumbSticks.Right.Y * dt * upDownSensitivity; } rotationAngle -= rotationDelta * gamePadState.ThumbSticks.Right.X * dt * leftRightSensitivity; #elif WINDOWS // Get Keyboard Input KeyboardState keyboardState = Keyboard.GetState(); if (keyboardState.IsKeyDown(Keys.W)) { position += look * dt * moveSpeed * moveDelta; } if (keyboardState.IsKeyDown(Keys.S)) { position -= look * dt * moveSpeed * (freeFlyEnabled ? 1.0f : 0.6f); } if (keyboardState.IsKeyDown(Keys.D)) { position += right * dt * moveSpeed * (freeFlyEnabled ? 1.0f : 0.6f); } if (keyboardState.IsKeyDown(Keys.A)) { position -= right * dt * moveSpeed * (freeFlyEnabled ? 1.0f : 0.6f); } if (keyboardState.IsKeyDown(Keys.I)) { pitchAngle += dt; } if (keyboardState.IsKeyDown(Keys.K)) { pitchAngle -= dt; } if (keyboardState.IsKeyDown(Keys.L)) { rotationAngle -= dt; } if (keyboardState.IsKeyDown(Keys.J)) { rotationAngle += dt; } if (freeFlyEnabled) { if (keyboardState.IsKeyDown(Keys.E)) { position += up * dt * moveSpeed; } if (keyboardState.IsKeyDown(Keys.Q)) { position -= up * dt * moveSpeed; } moveDelta = 1.0f; } else { // Adjust movement bleedoff speed if (keyboardState.IsKeyDown(Keys.W)) { moveDelta += dt * 5.0f; } else { moveDelta -= dt * 7.5f; if (moveDelta > 0.0f) { position += look * dt * moveSpeed * moveDelta; } } if (moveDelta > 1.0f) { moveDelta = 1.0f; } if (moveDelta < 0.0f) { moveDelta = 0.0f; } // Set twist angle if (keyboardState.IsKeyDown(Keys.A)) { twistLeft = true; } else { twistLeft = false; } if (keyboardState.IsKeyDown(Keys.D)) { twistRight = true; } else { twistRight = false; } } // Update twist angle if (twistRight && twistLeft) { twistRight = false; twistLeft = false; } if (twistRight) { twistAngle += dt * twistSpeed; } else if (twistAngle > 0.0f) { twistAngle -= dt * 2.0f * twistSpeed; if (twistAngle < 0.0f) { twistAngle = 0.0f; } } if (twistLeft) { twistAngle -= dt * twistSpeed; } else if (twistAngle < 0.0f) { twistAngle += dt * 2.0f * twistSpeed; if (twistAngle > 0.0f) { twistAngle = 0.0f; } } // Get Mouse Input MouseState mouseState = Mouse.GetState(); if (!prevMouseState.Equals(mouseState)) { pitchAngle -= (mouseState.Y - prevMouseState.Y) * dt * upDownSensitivity; rotationAngle -= (mouseState.X - prevMouseState.X) * dt * leftRightSensitivity; Mouse.SetPosition(DEFAULT_MOUSE_X, DEFAULT_MOUSE_Y); } #endif // Lock pitch angle if (pitchAngle > MathHelper.ToRadians(pitchMaxDegrees)) { pitchAngle = MathHelper.ToRadians(pitchMaxDegrees); } else if (pitchAngle < MathHelper.ToRadians(pitchMinDegrees)) { pitchAngle = MathHelper.ToRadians(pitchMinDegrees); } // Lock twist angle if (twistAngle > twistMaxAngle) { twistAngle = twistMaxAngle; } else if (twistAngle < -twistMaxAngle) { twistAngle = -twistMaxAngle; } // Update move direction moveDirection = Vector3.Subtract(position, prevPosition); moveDirection.Normalize(); // Build new AABB aabb = new BoundingBox( new Vector3(position.X - aabbSize.X, position.Y - aabbSize.Y, position.Z - aabbSize.X), new Vector3(position.X + aabbSize.X, position.Y + aabbSize.Y / 2, position.Z + aabbSize.X)); UpdateViewMatrix(); }
void UpdateMouseStates() { currentMousePosition.X = CurrentMouseState.X; currentMousePosition.Y = CurrentMouseState.Y; prevMousePosition.X = LastMouseState.X; prevMousePosition.Y = LastMouseState.Y; if (mouseGestureType.HasFlag(MouseGestureType.LeftClick)) { mouseGestureType = mouseGestureType ^ MouseGestureType.LeftClick; } if (mouseGestureType.HasFlag(MouseGestureType.Move)) { mouseGestureType = mouseGestureType ^ MouseGestureType.Move; } if (MouseDelta.Length() != 0) { mouseGestureType = mouseGestureType | MouseGestureType.Move; } if (CurrentMouseState.LeftButton == ButtonState.Released && dragging) { leftMouseDown = false; dragging = false; dragComplete = true; dragMouseEnd = currentMousePosition; mouseGestureType |= MouseGestureType.DragComplete; mouseGestureType = mouseGestureType ^ MouseGestureType.FreeDrag; } if (!leftMouseDown && CurrentMouseState.LeftButton == ButtonState.Pressed && !CurrentMouseState.Equals(LastMouseState)) { leftMouseDown = true; dragComplete = false; dragMouseStart = currentMousePosition; } if (leftMouseDown && CurrentMouseState.LeftButton == ButtonState.Released && !CurrentMouseState.Equals(LastMouseState)) { leftMouseDown = false; mouseGestureType |= MouseGestureType.LeftClick; } if (leftMouseDown && !dragging) { Vector2 delta = dragMouseStart - currentMousePosition; if (delta.Length() > dragThreshold) { dragging = true; dragMouseStart = currentMousePosition; mouseGestureType = mouseGestureType | MouseGestureType.FreeDrag; } } }
/// <summary> /// Checks if any button, scrollwheel, or mouse movement has taken place since last update /// </summary> /// <returns>True if state changed, otherwise false</returns> public bool IsStateChanged() { return((newState.Equals(oldState)) ? false : true); }
void UpdateMouseStates() { currentMousePosition.X = CurrentMouseState.X; currentMousePosition.Y = CurrentMouseState.Y; prevMousePosition.X = LastMouseState.X; prevMousePosition.Y = LastMouseState.Y; if (mouseGestureType.HasFlag(MouseGestureType.LeftClick)) { mouseGestureType = mouseGestureType ^ MouseGestureType.LeftClick; } if (mouseGestureType.HasFlag(MouseGestureType.Move)) { mouseGestureType = mouseGestureType ^ MouseGestureType.Move; } if (MouseDelta.Length() != 0) { mouseGestureType = mouseGestureType | MouseGestureType.Move; } // If we were dragging and the left mouse button was released // then we are no longer dragging and need to throw the banana. if (CurrentMouseState.LeftButton == ButtonState.Released) { leftMouseDown = false; mouseGestureType = mouseGestureType ^ MouseGestureType.FreeDrag; //Console.WriteLine ("Dragging: " + mouseGestureType); } // Let's set the left mouse down and the mouse origin if (!leftMouseDown && CurrentMouseState.LeftButton == ButtonState.Pressed && !CurrentMouseState.Equals(LastMouseState)) { //Console.WriteLine ("left down"); leftMouseDown = true; dragMouseStart = currentMousePosition; } if (leftMouseDown && CurrentMouseState.LeftButton == ButtonState.Released && !CurrentMouseState.Equals(LastMouseState)) { leftMouseDown = false; mouseGestureType |= MouseGestureType.LeftClick; } // Here we test the distance and if over the threshold then we set the dragging to true // Current threshold is 5 pixels. if (leftMouseDown) { Vector2 delta = dragMouseStart - currentMousePosition; if (delta.Length() > dragThreshold) { dragMouseStart = currentMousePosition; mouseGestureType = mouseGestureType | MouseGestureType.FreeDrag; //Console.WriteLine ("Dragging: " + mouseGestureType); } } //Console.WriteLine(mouseGestureType); }
public void SampleMouseState() { while (true) { bool active = System.Windows.Forms.Form.ActiveForm != null; bool ignoreUntilReleased = false; MouseState state = Mouse.GetState(); // Adjust position for tutorial mode. int x = state.X - (int)BokuGame.ScreenPosition.X; int y = state.Y - (int)BokuGame.ScreenPosition.Y; int wheel = state.ScrollWheelValue; ButtonState left = state.LeftButton; ButtonState middle = state.MiddleButton; ButtonState right = state.RightButton; ButtonState x1 = state.XButton1; ButtonState x2 = state.XButton2; if (System.Windows.Forms.SystemInformation.MouseButtonsSwapped) { ButtonState tmp = left; left = right; right = tmp; } // If mouse is outside of the screen, treat all the buttons as up. if (x < 0 || y < 0 || x > BokuGame.ScreenSize.X || y > BokuGame.ScreenSize.Y) { left = ButtonState.Released; middle = ButtonState.Released; right = ButtonState.Released; x1 = ButtonState.Released; x2 = ButtonState.Released; wheel = 0; // This prevents resizing the window from looking like a left button released event. MouseInput.Left.IgnoreUntilReleased = true; } // Adjust position for tutorial mode. state = new MouseState(x, y, wheel, left, middle, right, x1, x2); // When transitioning from not active to active we want to ignore all the buttons // until they've been released. if (active && !prevActive) { ignoreUntilReleased = true; } prevActive = active; if (active && !state.Equals(curState)) { lock (MouseInput.MouseStateQueue) { // Need to decide whether to add a new entry or to just update last one. // If the queue is empty, add. // If the state change is button or wheel changes, then add a new one. // If the state change is just X,Y changes, update the last one. // This prevents the queue from growing huge if the user is just moving the mouse. if (MouseInput.MouseStateQueue.Count == 0 || state.LeftButton != curState.LeftButton || state.RightButton != curState.RightButton || state.MiddleButton != curState.MiddleButton //|| state.ScrollWheelValue != curState.ScrollWheelValue ) { // New entry. MouseInput.MouseStateQueue.Add(new MouseState2(state, ignoreUntilReleased)); } else { // Update X,Y of last entry. (We do this just by replacing the whole // entry since X,Y is the only thing that changed.) int index = MouseInput.MouseStateQueue.Count - 1; MouseInput.MouseStateQueue[index].state = state; MouseInput.MouseStateQueue[index].ignoreUntilReleased |= ignoreUntilReleased; } } curState = state; } // Sample every 5ms. Probably overkill. Thread.Sleep(5); } // end of while loop. }