Entity Creation and Messaging in a VB.NET Text-Based Game - Checking for Entity Collision
(Page 3 of 4 )
You may, however, have noticed a problem with TryMove. If the entity trying to move faces a wall, then TryMove returns False as it should since no move can be made. However, if the entity trying to move faces another entity, then TryMove will return True.
You can test this out by getting in the way of the PacingManEntity. He'll walk right through you. Clearly, this is not what we want, but, thankfully, this can be easily fixed by modifying TryMove. TryMove simply needs to loop through all of the entities to determine if the destination tile is inhabited by another entity, or if the player inhabits the destination tile.
All of this is easy, though. We simply need to compare the X and Y properties of each entity to the x- and y-coordinates of the destination tile. Here's the entire TryMove method, rewritten:
Function TryMove(ByVal toBeMoved As Entity, ByVal xChange As Integer, ByVal ychange As Integer) As Boolean
Dim x As Integer = toBeMoved.X + xChange
Dim y As Integer = toBeMoved.Y + ychange
For Each gameEntity As Entity In entities
If gameEntity.X = x And gameEntity.Y = y Then
Return False
End If
Next
If map(x, y).Passable = True _
And (player.X <> x Or player.Y <> y) Then
RedrawTile(toBeMoved.X, toBeMoved.Y)
toBeMoved.X = x
toBeMoved.Y = y
DrawEntity(toBeMoved)
Return True
End If
Return False
End Function
Now TryMove works properly, checking not only the Passable property of the destination tile but also the location of the player and all of the other entities. The player and the PacingManEntity should no longer walk over each other. Instead, they should stop, blocked.
Next: Implementing a Message System >>
More Visual Basic.NET Articles
More By Peyton McCullough