/// <summary> /// Checks both directions of a diagonal movement /// to see if movement or a bump resulting in an interaction is possible. Modifies /// the move action to switch to that direction, otherwise leaves it unmodified. /// Prioritizes the following when there are multiple options: /// 1. Move into empty space if either direction has it /// 2. Push an object if either direction has it /// 3. Open a door if either direction has it /// /// When both directions have the same condition (both doors or pushable objects), x will be preferred to y /// </summary> /// <param name="state">current state to try to slide from</param> /// <param name="action">current player action (which should have a diagonal movement). Will be modified if a slide is performed</param> /// <returns>bumptype of the final direction of movement if action is modified. Null otherwise</returns> private BumpType?TrySlide(PlayerState state, ref PlayerAction action) { Vector2Int direction = action.Direction(); if (Math.Abs(direction.x) + Math.Abs(direction.y) < 2) { //not diagonal, do nothing return(null); } Vector2Int xDirection = new Vector2Int(direction.x, 0); Vector2Int yDirection = new Vector2Int(0, direction.y); BumpType xBump = MatrixManager.GetBumpTypeAt(state.WorldPosition.RoundToInt(), xDirection, gameObject); BumpType yBump = MatrixManager.GetBumpTypeAt(state.WorldPosition.RoundToInt(), yDirection, gameObject); MoveAction?newAction = null; BumpType? newBump = null; if (xBump == BumpType.None) { newAction = PlayerAction.GetMoveAction(xDirection); newBump = xBump; } else if (yBump == BumpType.None) { newAction = PlayerAction.GetMoveAction(yDirection); newBump = yBump; } else if (xBump == BumpType.Push) { newAction = PlayerAction.GetMoveAction(xDirection); newBump = xBump; } else if (yBump == BumpType.Push) { newAction = PlayerAction.GetMoveAction(yDirection); newBump = yBump; } else if (xBump == BumpType.ClosedDoor) { newAction = PlayerAction.GetMoveAction(xDirection); newBump = xBump; } else if (yBump == BumpType.ClosedDoor) { newAction = PlayerAction.GetMoveAction(yDirection); newBump = yBump; } if (newAction.HasValue) { action.moveActions = new int[] { (int)newAction }; return(newBump); } else { return(null); } }
/// <summary> /// Checks if any bump would occur with the movement in the specified action. /// If a BumpType.Blocked occurs, attempts to slide (if movement is diagonal). Updates /// playerAction's movement if slide occurs. /// </summary> /// <param name="playerState">state moving from</param> /// <param name="playerAction">action indicating the movement, will be modified if slide occurs</param> /// <returns>the type of bump that occurs at the final destination (after sliding has been attempted)</returns> private BumpType CheckSlideAndBump(PlayerState playerState, ref PlayerAction playerAction) { BumpType bump = MatrixManager.GetBumpTypeAt(playerState, playerAction, gameObject); // if movement is blocked, try to slide if (bump == BumpType.Blocked) { return(TrySlide(playerState, ref playerAction) ?? bump); } return(bump); }