About three weeks ago we started a new course called Introduction to Game Development. In this course we are going to create a game from a concept which another group has made in the previous course, Game Analysis and Game Design. My group chose the concept called Fancy Mansion, in which you play as a burglar who is breaking in to a mansion owned by a man called Mr. Fancy. One of the main features in the game is that of light and darkness. You only see the environment next to you and you have a boomerang that emits light. This was the first task I took on solving.
The world without the dark sprite
There are two light sources, one around the player and one around the boomerang, or ‘glowerang’ as we call it. These light sources are basically sprites, images with a transparent (high alpha value) circle in the middle.
Light sprite with higher alpha value in the middle.
To make it work as intended I had to create a black texture in which I draw the light sprites, and then use this texture in a sprite, which I then draw in the game window. In this way I create a dark layer over the world to simulate light and darkness. This sprite follows the players movement so that it always covers the whole screen.
Light around the player.
One problem with this was the position of the light sprite within the black texture. I wanted the light to follow the player and I knew the players coordinates in the world. But because the light was not drawn in the world, but in the black texture which had its own coordinate system, I had to recalculate the position within this texture.
In a similar way I draw the light that emits from the glowerang. As it is an object in the world, I knew the coordinates and could calculate where in the black texture the light sprite of the glowerang should be drawn. By taking the x-coordinate of the glowerang and subtracting the x-coordinate of the player and then adding half the width of the screen, and doing the same thing with the y-coordinate and half the height, I could get the position of where it had to be drawn.
Glowerang thrown (without the actual boomerang, sorry!).
After some research on simulating shadows and darkness in games with SFML, I found this way of doing it kind of easy. You could use a method called ray-casting, but I don’t feel comfortable enough implementing this, as it is a bit more complicated.