private void PickupOrDrop() { if (Carrying != null) { Carrying.OnDropped(this); if (Carrying is PuzzlePiece pl && !pl.IsPlacedOnGrid) { Carrying.AnimateMove(Position + new Vector2(0, -10)); _dropItem.Play(); } Carrying.Color = Color.White; Carrying = null; return; } if (TargetEntity != null) { Carrying = TargetEntity; Carrying.Depth = Depths.PlayerCarry; Carrying.OnPickedUp(this); _pickupItem.Play(); } }
public override void Update(GameTime gameTime) { if (!IsActive) { return; } var gamePad = GamePad.GetState(Index); var kb = Keyboard.GetState(); if (JustPressed(gamePad, Buttons.A) || JustTriggered(gamePad, x => x.Right) || JustPressed(kb, Keys.L, Keys.C) || (Index == PlayerIndex.One && Input.KeyPressed(Keys.Space))) { PickupOrDrop(); } var pressedRotateLeft = JustPressed(gamePad, Buttons.LeftShoulder) || JustPressed(gamePad, Buttons.X) || JustPressed(kb, Keys.J, Keys.V) || (Index == PlayerIndex.One && Input.KeyPressed(Keys.Z)); var pressedRotateRight = JustPressed(gamePad, Buttons.RightShoulder) || JustPressed(gamePad, Buttons.B) || JustPressed(kb, Keys.K, Keys.B) || (Index == PlayerIndex.One && Input.KeyPressed(Keys.X)); if (Carrying != null) { Carrying?.AnimateMove(Position + new Vector2(0, -20), 1); if (pressedRotateLeft) { RotateCarried(-1); } if (pressedRotateRight) { RotateCarried(1); } } if (pressedRotateLeft || pressedRotateRight) { var change = (pressedRotateLeft ? -1 : 0) + (pressedRotateRight ? 1 : 0); Q19Game.Instance.Scene.Visit(e => { if (e is PuzzlesPreview pp) { pp.GoTo(change); } }); } var moveInput = GetGamePadMovement(gamePad); if (Q19Game.DebugIsActivePlayer(Index)) { moveInput += GetKeyboardMovement(kb, PlayerIndex.One); } else if (Index < PlayerIndex.Three) { moveInput += GetKeyboardMovement(kb, Index); } if (_disableInput) { var currentTarget = _autoMoveTargets[0]; var toTarget = currentTarget - Position; var distanceToTarget = toTarget.Length(); moveInput = toTarget; if (distanceToTarget < Speed * (float)gameTime.ElapsedGameTime.TotalSeconds) { _autoMoveTargets.RemoveAt(0); if (!_autoMoveTargets.Any()) { _disableInput = false; } } } var isMoving = moveInput != Vector2.Zero; if (isMoving) { var movement = Vector2.Normalize(moveInput) * Speed * (float)gameTime.ElapsedGameTime.TotalSeconds; #region level bound collision var result = Q19Game.Instance.Scene.Level.IsWithinBounds(this, movement); if (result.CollidingX) { movement.X = 0; } if (result.CollidingY) { movement.Y = 0; } #endregion Position += movement; } _movementSounds.UpdateMovementSound(isMoving, gameTime); var pickupEntities = Q19Game.Instance.GetAllPickupEntities(); var closestDistance = float.MaxValue; TargetEntity = null; foreach (var pickupEntity in pickupEntities) { var distance = Vector2.DistanceSquared(pickupEntity.Position, Position); if (distance < closestDistance) { closestDistance = distance; TargetEntity = pickupEntity; } } if (closestDistance > PickupDistance * PickupDistance) { TargetEntity = null; } else { TargetEntity.TouchFocus(PlayerColor); } // Highlight fire and piece when close enough to burn a piece if (Carrying != null) { Carrying.Color = Color.White; Q19Game.Instance.Scene.Visit(e => { if (e is Fire f && f.CarriedBy == null && Vector2.DistanceSquared(Carrying.Position, f.Position) < Fire.FireTossDistance * Fire.FireTossDistance) { f.TouchFocus(PlayerColor); Carrying.Color = Color.Red; } }); } _previousGamePadState = gamePad; _previousKeyboardState = kb; base.Update(gameTime); }