So, our goal for this episode is to get water functioning. For our water implementation, we'll just make another rectangle (they're very useful shapes, aren't they?) to represent a body of water. Our initial set up for water is just like that of ice, so let's go ahead and set up a body of water. Declarations:
Dim Waters As ArrayList
'Holds all of the bodies of water in the level.
Dim Water As Rectangle
'Holds the location of a body of water.
Dim WaterBrsh As Brush
'The color of the water. |
WaterBrsh = New SolidBrush(Color.FromArgb(128, 200, 255, 230))
'Very clear water.
Waters = New ArrayList
'Create the list of all waters.
Water = New Rectangle(3200, 250, 400, 300)
Waters.Add(Water)
'Add this new water to the list of all water. |
For Each Water In Waters
Water.Offset(-ScreenLeft, -ScreenTop)
GFX.FillRectangle(WaterBrsh, Water)
Water.Offset(ScreenLeft, ScreenTop)
'Draws the water.
Next |
Dim InWater As Boolean
'Is the player swimming? |
LV = 0
'Initialize these values.
If InWater Then
InWater = False
'Initialize these values.
Do
Water = DirectCast(Waters.Item(LV), Rectangle)
Water.Y += HALFHEIGHT
Water.Height -= HALFHEIGHT
'This adjustment is necessary for walking so that the player doesn't walk to the edge of the water
'and suddenly be in water.
If PlayerLoc.IntersectsWith(Water) Then
InWater = True
End If
LV += 1
Loop Until LV = Waters.Count OrElse InWater = True
Else
Do
Water = DirectCast(Waters.Item(LV), Rectangle)
Water.Y += HALFHEIGHT
Water.Height -= HALFHEIGHT
'This adjustment is necessary for walking so that the player doesn't walk to the edge of the water
'and suddenly be in water.
If PlayerLoc.IntersectsWith(Water) Then
InWater = True
'Move the player to the edge of this wall.
PlayerVeloc >>= 2
'Wet impulse, we've splashed into the water (play splash sound effect here).
End If
LV += 1
Loop Until LV = Waters.Count OrElse InWater = True
End If
|
ElseIf InWater Then
PlayerLoc.Offset(-(MOVESPEED >> 2) - (MOVESPEED >> 1), 0)
'Halfspeed underwater.
ElseIf InWater Then
PlayerLoc.Offset(-(MOVESPEED >> 1), 0)
'Halfspeed underwater
ElseIf InWater Then
PlayerLoc.Offset((MOVESPEED >> 2) + (MOVESPEED >> 1), 0)
'Halfspeed underwater.
ElseIf InWater Then
PlayerLoc.Offset(MOVESPEED >> 1, 0)
'Halfspeed underwater. |
Const SUBTERMINAL As Integer = TERMINALVELOCITY * 3 \ 4
'The maximum velocity that the character reaches while falling underwater.
Const SUBGRAVITY As Integer = GRAVITY \ 2
'Underwater acceleration factor ... how much the speed changes at each tick while falling underwater.
Const SUBGRAVITYTH As Integer = SUBGRAVITY * 3 >> 1
'This represents how much the player moved due to underwater acceleration.
Const SUBINITIALVELOCITY As Integer = SBARINITIALVELOCITY * 3 \ 4
'The initial velocity for the player's jump using space bar underwater.
Const SUBLOWJUMPVELOCITY As Integer = LOWJUMPVELOCITY * 3 \ 4
'Initial velocity for a low jump underwater. |
If InWater Then
PlayerLoc.Offset(0, PlayerVeloc + SUBGRAVITY >> 1)
'dD (.OffSet) = Vo (PlayerVeloc) + 0.5 * A (GRAVITY >> 1)
PlayerVeloc += SUBGRAVITY
'dV = (PlayerVeloc +=) A (GRAVITY)
'Moves the character according to its velocity, gravity, and location.
If PlayerVeloc >= SUBTERMINAL Then 'We're in TV.
PlayerVeloc = SUBTERMINAL 'Limit the velocity.
End If
Else
PlayerLoc.Offset(0, PlayerVeloc + GRAVITY >> 1)
'dD (.OffSet) = Vo (PlayerVeloc) + 0.5 * A (GRAVITY >> 1)
PlayerVeloc += GRAVITY
'dV = (PlayerVeloc +=) A (GRAVITY)
'Moves the character according to its velocity, gravity, and location.
If PlayerVeloc >= TERMINALVELOCITY Then 'We're in TV.
PlayerVeloc = TERMINALVELOCITY 'Limit the velocity.
End If
End If |
If InWater Then
IsJumping = True
'The jump is initialized.
MobileIndex = -1
'We are not standing on anything now, because we are jumping.
If GoDown Then
PlayerVeloc = Me.SUBLOWJUMPVELOCITY
'Use low jump velocity if down is pressed.
Else
PlayerVeloc = Me.SUBINITIALVELOCITY
'Use space bar initial velocity.
End If |