In this episode we are going to let the player become smushed when a moving wall presses him against another wall. This should be really simple... all we do is move the player if he touches the moving wall after it moves. If, after being moved by a moving wall, the player comes in contact with another wall, then the player is smushed. So, first we check each moving wall immediately after it moves. If the player touches the wall, we need to determine which way it is moving. This is only valid before the .MoveWall subroutine, because that changes the direction of the wall's movement, but we won't know if the player collides with the wall until after the .MoveWall subroutine. So, what do we do? Well, the biggest thing is that our MovingWall structure should be refined so that this check could be done after the wall moves but before the wall velocities change. But, I do have a workaround, as you will see soon (by merely using a bigger If statement). First, we need a boolean to determine if the Player has came in contact with the first wall.
Dim TouchingWall As Boolean 'This is true when the player has touched a wall. |
If MobileWall.dX < 0 Then 'The wall is moving left. MobileWall.MoveWall() 'This does all of the movement for the wall. If MobileWall.Loc.IntersectsWith(PlayerLoc) Then 'Move the player to the left side of this wall. PlayerLoc.Offset(MobileWall.Loc.Left - PlayerLoc.Right, 0) TouchingWall = True End If ElseIf MobileWall.dX > 0 Then 'The wall is moving right. MobileWall.MoveWall() 'This does all of the movement for the wall. If MobileWall.Loc.IntersectsWith(PlayerLoc) Then 'Move the player to the right side of this wall. PlayerLoc.Offset(MobileWall.Loc.Right - PlayerLoc.Left, 0) TouchingWall = True End If End If |
If MobileWall.dX < 0 Then 'The wall is moving left. MobileWall.MoveWall() 'This does all of the movement for the wall. If MobileWall.Loc.IntersectsWith(PlayerLoc) Then If Not TouchingWall Then 'Move the player to the left side of this wall. PlayerLoc.Offset(MobileWall.Loc.Left - PlayerLoc.Right, 0) TouchingWall = True Else IsGoner = True 'The player has been smushed. Lose a life. PlayerVeloc = SBARINITIALVELOCITY End If End If ElseIf MobileWall.dX > 0 Then 'The wall is moving right. MobileWall.MoveWall() 'This does all of the movement for the wall. If MobileWall.Loc.IntersectsWith(PlayerLoc) Then If Not TouchingWall Then 'Move the player to the right side of this wall. PlayerLoc.Offset(MobileWall.Loc.Right - PlayerLoc.Left, 0) TouchingWall = True Else IsGoner = True 'The player has been smushed. Lose a life. PlayerVeloc = SBARINITIALVELOCITY End If End If Else MobileWall.MoveWall() 'This does all of the movement for the wall. End If |
If TouchingWall Then For Each Wall In Walls If PlayerLoc.IntersectsWith(Wall) Then 'The player is smushed if he is touching another wall, 'because he has been pushed at the same time by a moving wall. IsGoner = True 'The player has been smushed. Lose a life. PlayerVeloc = SBARINITIALVELOCITY End If Next End If |
Dim R As Integer For R = 0 To MobileWalls.Count - 1 MobileWall = DirectCast(MobileWalls(R), MovingWall) 'We have to use DirectCast since arraylist contains structures and we are changing the property values. If R = MobileIndex Then 'R will never be -1 because of our For Loop. 'We need to move the Player, the left end, and the right end. LeftEnd += MobileWall.dX RightEnd += MobileWall.dX PlayerLoc.Offset(MobileWall.dX, MobileWall.dY) End If If MobileWall.dX < 0 Then 'The wall is moving left. MobileWall.MoveWall() 'This does all of the movement for the wall. If MobileWall.Loc.IntersectsWith(PlayerLoc) Then If Not TouchingWall Then 'Move the player to the left side of this wall. PlayerLoc.Offset(MobileWall.Loc.Left - PlayerLoc.Right, 0) TouchingWall = True If PlayerLoc.Right < LeftEnd AndAlso Not IsJumping Then IsJumping = True 'We are not really jumping, but we are falling. MobileIndex = -1 'We are not standing on anything now, because we are jumping. PlayerVeloc = 0 'Since we are falling from rest, the initial velocity will be zero. End If Else IsGoner = True 'The player has been smushed. Lose a life. PlayerVeloc = SBARINITIALVELOCITY End If End If ElseIf MobileWall.dX > 0 Then 'The wall is moving right. MobileWall.MoveWall() 'This does all of the movement for the wall. If MobileWall.Loc.IntersectsWith(PlayerLoc) Then If Not TouchingWall Then 'Move the player to the right side of this wall. PlayerLoc.Offset(MobileWall.Loc.Right - PlayerLoc.Left, 0) TouchingWall = True If PlayerLoc.Left > RightEnd AndAlso Not IsJumping Then IsJumping = True 'We are falling. MobileIndex = -1 'We are not standing on anything now, because we are jumping. PlayerVeloc = 0 'A fall from rest. End If Else IsGoner = True 'The player has been smushed. Lose a life. PlayerVeloc = SBARINITIALVELOCITY End If End If Else MobileWall.MoveWall() 'This does all of the movement for the wall. End If MobileWalls(R) = MobileWall Next |
For Each Wall In Walls If PlayerLoc.IntersectsWith(Wall) Then 'Collided with a wall while riding the moving wall. If MobileWall.dX < 0 Then 'Move the player to the right side of this wall... can't collide with a wall if we're not moving. PlayerLoc.Offset(Wall.Right - PlayerLoc.Left, 0) If PlayerLoc.Left > RightEnd AndAlso Not IsJumping Then IsJumping = True 'We are falling. MobileIndex = -1 'We are not standing on anything now, because we are jumping. PlayerVeloc = 0 'A fall from rest. End If Else 'Move the player to the left side of this wall. PlayerLoc.Offset(Wall.Left - PlayerLoc.Right, 0) If PlayerLoc.Right < LeftEnd AndAlso Not IsJumping Then IsJumping = True 'We are not really jumping, but we are falling. MobileIndex = -1 'We are not standing on anything now, because we are jumping. PlayerVeloc = 0 'Since we are falling from rest, the initial velocity will be zero. End If End If End If Next |