Creating a RogueLike Game View with C#
In a RogueLike the game view (GV) is a rectangular area of the map occupied by the player that is displayed on screen, an example of which is shown below. A gameview consists of two parts: a size and an origin (the x and y coordinates which define the top left corner). The origin is calculated from the player's current coordinates by subtracting half the GV width from player X and half the GV height from player Y, and making adjustments to them under certain conditions described below.
This article describes how to calculate the coordinates required for a game view.
Terminology
The following terms are required in order to calculate the GV origin coordinates GVOriginX and GVOriginY:
- PlayerX, PlayerY - The coordinates of the player's current location
- GVWidth, GVHeight ?- The size of the game view
- MapWidth, MapHeight - The size of the map the player is exploring.
It is assumed that MapWidth > GVWidth and MapHeight > GVHeight.
For the player to be displayed dead centre in the GV GVWidth and GVHeight must be odd numbers.
Calculations
This origin of the GV is defined as:
- GVOriginX = playerX - GVWidth / 2
- GVOriginY = playerY - GVHeight / 2
Therefore, the bottom right corners coordinates of the GV are GVOriginX + GVWidth and GVOriginY + GVHeight.
However, there are obvious conditions where GVOriginX and / or GVOriginY are less than 0, or the bottom right coordinates exceed the MapHeight and / or MapWidth, so we need to make the followings checks and correct as appropriate after calculating generating GVOriginX and GVOriginY:
Check | Correction if true |
---|---|
GVOriginX < 0 | GVOriginX = 0 |
GVOriginY < 0 | GVOriginY = 0 |
GVOriginX + GVWidth > MapWidth | GVOriginX -= (GVOriginX + iViewWidth - MapWidth) |
GVOriginY + GWHeigtht > MapHeight | GVOriginY -= (GVOriginY + iViewHeight - MapHeight) |
The effect of making these changes will cause the player to be displayed off centre and closer to the edge being moved towards, as shown below. If none of the above corrections are required, the player will be shown in centre of GV as shown in the picture at the start of this article.
Code
A Visual Studio demonstrating the above method in a simple demo which allows a player to explore a map using the keys Q,W,E,A,S,D,Z,X and C can be found here.
Github: here.
Have I seen this before?
The observant amongst you will notice that this code comes from my Evil Science article?Field of Vision using recursive shadow casting: C# .Net 3.5 implementation, but I thought I'd use the code again with emphasis on how to draw the Game View.
May 6th, 2014 - 05:28
Hi!
In the algorithm for calculating the field of view of a small error crept. Even if the right of the player there’s a snag – the algorithm mistakenly sees through him. Most likely, an error has crept into the case 4, the method ScanOctant.
Here you can see examples of the errors:
https://dl.dropboxusercontent.com/u/18685822/fov_1.png
https://dl.dropboxusercontent.com/u/18685822/fov_2.png
May 7th, 2014 - 21:18
Hi MdWzrd
You’re right – there was a bug and it was in octant 4 as you pointed out. This has now been fixed, and the github source and VS download have been updated to fix this. I’ve also thrown in a few small tweaks to make the code slightly faster.
Cheers
The Evil Scientist