/// <summary> /// Updates the task logic. Leads the human toward the action object and then lets him perform the action. /// </summary> /// <param name="gameTime">Game time</param> public override void Update(GameTime gameTime) { if (gameTime.TotalGameTime - lastUpdatedWaypoints > RecomputeWaypointsTimeout && actionObject.Position.Quarter == Holder.Position.Quarter) { goStraightToObject = false; Vector2 wayVect = (actionObject.Position.PositionInQuarter - Holder.Position.PositionInQuarter); float direction = (wayVect.GetAngle() + 1 * MathHelper.PiOver2) % MathHelper.TwoPi; Quadrangle viewLine = Quadrangle.CreateBand(Holder.Pivot.PositionInQuarter, direction, Holder.Size.X, wayVect.Length()); goStraightToObject = !Grid.IsInCollision(viewLine, c => !(c is Human)); if (!goStraightToObject) { RecomputeWaypoints(Holder.Position, actionObject.Position); } lastUpdatedWaypoints = gameTime.TotalGameTime; } if (goStraightToObject) { Holder.GoThisWay(actionObject.Position, (float)gameTime.ElapsedGameTime.TotalSeconds); } else { base.Update(gameTime); } if (actionObject.IsAvailableFor(Holder)) { if (!started) { actionObject.StartAction(Holder, gameTime); actionStart = gameTime.TotalGameTime; started = true; } else { if (gameTime.TotalGameTime - actionStart > actionObject.ActionDuration) { actionObject.EndAction(Holder, gameTime); complete = true; } } } else { if (WayPoints.Count == 0 && !IsComplete()) { RecomputeWaypoints(Holder.Position, actionObject.Position); lastUpdatedWaypoints = gameTime.TotalGameTime; } } }
/// <summary> /// Updates the player's logic - reads input and do actions. It suppresses the human's automatic behaviour. /// </summary> /// <param name="gameTime"></param> public void Update(GameTime gameTime) { KeyboardState keyboardState = Keyboard.GetState(); MouseState mouseState = Mouse.GetState(); float seconds = (float)gameTime.ElapsedGameTime.TotalSeconds; if (Math.Abs(Health) + 1 > 0 && System.Windows.Forms.Form.ActiveForm == (System.Windows.Forms.Control.FromHandle(Game.Window.Handle) as System.Windows.Forms.Form)) { Vector2 actualLastPosition = this.Position.PositionInQuarter; if (keyboardState.IsKeyDown(Keys.LeftControl) && keyboardState.IsKeyDown(GodModeSwitch) && gameTime.TotalGameTime - lastKeyPressedGameTime[GodModeSwitch] > KeyPressedTimeout) { InGodMode = !InGodMode; lastKeyPressedGameTime[GodModeSwitch] = gameTime.TotalGameTime; } if (keyboardState.IsKeyDown(Game.Settings.StepLeft)) { Step(true, seconds); } if (keyboardState.IsKeyDown(Game.Settings.StepRight)) { Step(false, seconds); } if (keyboardState.IsKeyDown(Game.Settings.Forward)) { Go(seconds); } if (keyboardState.IsKeyDown(Game.Settings.Backward)) { GoBack(seconds); } if (keyboardState.IsKeyDown(Game.Settings.TurnLeft)) { Rotate(true, seconds); } if (keyboardState.IsKeyDown(Game.Settings.TurnRight)) { Rotate(false, seconds); } if (keyboardState.IsKeyDown(Game.Settings.TurnUp)) { LookAngle += Human.RotateAngle * seconds; } if (keyboardState.IsKeyDown(Game.Settings.TurnDown)) { LookAngle -= Human.RotateAngle * seconds; } if (keyboardState.IsKeyDown(Game.Settings.RunWalkSwitch) && gameTime.TotalGameTime - lastKeyPressedGameTime[Game.Settings.RunWalkSwitch] > KeyPressedTimeout) { Running = !Running; lastKeyPressedGameTime[Game.Settings.RunWalkSwitch] = gameTime.TotalGameTime; } if (mouseState.LeftButton == ButtonState.Pressed) { DoToolAction(gameTime); } if (mouseState.RightButton == ButtonState.Pressed && HasAvailableAnyAction) { if (usedActionObject != FirstActionObject && usedActionObject != null) { usedActionObject.EndAction(this, gameTime); } usedActionObject = FirstActionObject; usedActionObject.StartAction(this, gameTime); } else { if (usedActionObject != null) { usedActionObject.EndAction(this, gameTime); } } SelectNextGun((mouseState.ScrollWheelValue - lastMouseWheelState) / 120); lastMouseWheelState = mouseState.ScrollWheelValue; int windowWidth = Game.Settings.ScreenSize.Width; int windowHeight = Game.Settings.ScreenSize.Height; if (Game.Settings.MouseIgnoresWindow || (mouseState.X >= 0 && mouseState.X < windowWidth && mouseState.Y >= 0 && mouseState.Y < windowHeight)) { const float maxLookAngle = 0.52f; azimuth += ((mouseState.X - defaultMousePosition.X) / (float)windowWidth) * Game.Settings.MouseXSensitivity * MouseXSensitivityCoef * seconds * Human.RotateAngle * (Game.Settings.MouseXInvert ? -1 : 1); LookAngle += ((mouseState.Y - defaultMousePosition.Y) / (float)windowWidth) * Game.Settings.MouseYSensitivity * MouseYSensitivityCoef * seconds * Human.RotateAngle * (Game.Settings.MouseYInvert ? 1 : -1); if (LookAngle > maxLookAngle) { LookAngle = maxLookAngle; } if (LookAngle < -maxLookAngle) { LookAngle = -maxLookAngle; } //lastMousePosition = new Point(mouseState.X, mouseState.Y); } Mouse.SetPosition(defaultMousePosition.X, defaultMousePosition.Y); LastPosition = actualLastPosition; } CheckHits(false, gameTime); //Supress human instincts //base.Update(gameTime); }