Controlling Player Movement
Speaking of having the player move, you should implement this code as well. Considering that sometimes the player cannot move in a specific direction, you want to first check whether the move the player wants to make is valid and then do the move if necessary. Add the methods in Listing 8.4 to your Level class to handle these cases.
Listing 8.4. Controlling Player Movement
/// <summary> /// Can the player move horizontally /// </summary> /// <param name="right">Is the player moving right</param> /// <returns>true if the move can be made; false otherwise</returns> private bool CanPlayerMoveHorizontal(bool right) { if ( ((playerIndex % SquareSize) == 0) && (!right)) { return false; } else if ( ((playerIndex % SquareSize) ... |
Get Beginning 3D Game Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.