Entity Creation and Messaging in a VB.NET Text-Based Game - Implementing Movement
(Page 2 of 4 )
The next step is to add some life to PacingManEntity. Each time the player makes a move, PacingManEntity (and other entities, for that matter) needs to make a move as well. The easiest way to implement this is to add a Move method to Entity. In Entity, this method will be blank. Then, entities can override this method with their own behaviors. After each move of the player, the Move method will be called for each entity, allowing each entity to perform some sort of action.
The first step, then, is to add a Move procedure to the Entity class. Since the intent here is to have the method be overridden by subclasses, we need to modify it with the Overridable keyword:
Public Overridable Sub Move()
End Sub
In the PacingManEntity class, we need to override this procedure and provide functionality. Let's make a Pacing Man pace back and forth, from right to left. He will reverse direction whenever he hits an obstruction. This will require a way to keep track of what direction a PacingManEntity object is currently going. So, we'll need to first add a field to the PacingManEntity class. A Boolean called goingRight should do the job. If it's set to True, then the entity is moving to the right. If it's set to False, then the entity is moving to the left. Let's set it to True initially, causing him to move right from the start:
Private goingRight As Boolean = True
Now it's time to override Move. In order to override a procedure, the Overrides keyword is used:
Public Overrides Sub Move()
MyBase.Move()
End Sub
Visual Studio will automatically call the parent class's method first. In our case, the parent class's method has nothing in it, though.
Remember, we need to change directions if there is an obstruction in the way (if TryMove returns False). However, since a Pacing Man only moves from right to left, if there are obstructions in both directions, then we'll have him simply forfeit his turn. In this situation, he can wait until the next turn to see if one of the obstructions has cleared. Here are the contents of PacingManEntity's Move procedure:
' Determine the change in X based on direction
Dim changeX As Integer = 1
If Not goingRight Then
changeX = -1
End If
' Move, or else change direction and move, or else quit
If Not TryMove(Me, changeX, 0) Then
changeX *= -1
goingRight = Not goingRight
TryMove(Me, changeX, 0)
End If
One of the things you should notice is the Me keyword. The Me keyword in Visual Basic refers to the current object. It's equivalent to the “this” or “self” operator in other languages.
Now we just need to call this new method from within Main. Entities should only move if the player has made a move. So, after this:
If Not playerMoved Then
Continue While
End If
Place this:
For Each toMove As Entity In entities
toMove.Move()
Next
This will loop over all of the entities and give each an opportunity to make a move.
Run the program. Each time the player makes a move, the pacing entity should make a move. When the entity hits a wall, he should reverse direction.
Next: Checking for Entity Collision >>
More Visual Basic.NET Articles
More By Peyton McCullough