Processing the Joystick Input

The last input device method, GetJoystickState, will deal with the JoyState structure. As mentioned before, this structure has many attributes that will be filled according to the joystick type.

In order to guarantee that our class will work even with the simplest joysticktype peripheral, we'll keep it as simple as possible; but some processing will still be needed. We'll generate the JoystickPressed event according to the Buttons array, simply by passing the button index to the application; and, for the JoystickMove event, we'll pass the (x,y) coordinates and, to help the applications to create simpler code, the joystick direction, according to the enDirection enumeration defined in the GameEngine class. To calculate this direction, we have to check the X and y joystick data members; each of these will range from 0 to 10,000, according to the values depicted in Figure 5-4.

Joystick Values
Figure 5-4: The values for the X and Y joystick members for each joystick position

If the application nequires greatet precision, it can use the ijoy^ticlc ctootdinsitesdirectly passed as a parameter to this event. The following code listing shows the full code for the GetJoystickState method:

Public Sub GetJoystickState()

Dim direction AOs enDiraction = clsGameEngine.enDirection.None

' Lock the UI class so it can't overwrite ' the JoystnckState structure during a race condition. SyncLock (Me)

Dim B As Byte, buttonNumber As Integer

' There's a lot of extra information coming from the joystick devices, ' but we'll deal here only with buttons and simple 2-D movement buttonNumber = 0

For Each B In JoystickState.GetButtons buttonNumber += 1 If (B <> 0) Then

RaiseEvent JoystickButtonPressed(buttonNumber) End If


' Calculates the direction of the movement ' Center position is 5000, 5000

' X goes from 0 to 10000, left to right

' Y goes from 0 to 10000, top to down

' We'll ignore movements < 1000 from the center

Select Case JoystickState.X

Case Is < 4000 ' X is to the west (left) Select Case JoystickState.Y

direction = clsGameEngine.enDirection.NorthWest Case Is > 6000 ' Y is to the south (down)

direction = clsGameEngine.enDirection.SouthWest Case Else ' Y is centered direction = clsGameEngine.enDirection.West End Select

Case Is > 6000 ' X is to the east (right) Select Case JoystickState.Y

direction = clsGameEngine.enDirection.NorthEast Case Is > 6000 ' Y is to the south (down)

direction = clsGameEngine.enDirection.SouthEast Case Else ' Y is centered direction = clsGameEngine.enDirection.East End Select Case Else ' X is centered

Select Case JoystickState.Y

direction = clsGameEngine.enDirection.North Case Is > 6000 ' Y is to the south (down)

direction = clsGameEngine.enDirection.South Case Else ' Y is centered direction = clsGameEngine.enDirection.None End Select End Select

If direction <> clsGameEngine.enDirection.None Then

RaiseEvent JoystickMove(JoystickState.X, JoystickState.Y, direction) End If

End SyncLock End Sub

Now that we have a)l of our" classes in code, we can begin the new version of the River Pla.Net game, starting with the game proposal.

+1 0

Post a comment