Part 4: Are you afraid of the dark? FOV!
wow, part 4? your still with us? awesome. lets get busy
Since caves and dungeons are dark, and we're building dungeons and caves, our heroic @
was smart enough to bring a torch with him, or a candle, or some magic.. maybe a maglite
anyway, what were doing in this installment is changing the view of everything to be in the dark
except for what is illuminated by @. As is often the case, libTCOD has various functions and attributes
to aid is in programming this part of the game. We'll be using the tcodmap() class, and in order to
initialize it we need to declare the following:
all of these variables when altered end with different outcomes. try a few different values for fovAlgo
and decide the one you like. fovAlgo, as you probably guessed by the name is a selector for the various
fov algorithims that comes with libtcod. fovRad is the radius around which the light well be visible
but the most important for us right now, is to add this, right below where you declared gameMap:
This initializes the tcodmap() class as fovMap(). the extremely non technical explaination for it
is that it interacts with gameMap, player, entities, etc to produce the effect that we are after.
We'll set a boolean value to set when the fov engine needs to run through its algorithim, as it only needs to happen
when the player moves, well use the variable fovRecompute, and set it to true
placed just below the map initializers
the swift tcod wrapper's tcodmap() class pretty much automates This
whole process for you but we still need an fovInit() function so the fovMap matches our game map
I chose the inputAndRender.swift for this function, right below all of the draw and clear functions:
We make the following change to our game loop, so the fov will be
recomputed when '@' moves:
We are also going to add another value to our Tile class, so that it stays
lit once we have moved over it. We'll call this boolean "explored":
and dont forget to add the third value to the Tile initializer for creating our Tile Array:
With most of the setup out of the way, the main changes to the draw functions are what is going
to present our FOV to the player, so change your drawAll() function to look like this:
and alter drawEnt() to look like this:
With those changes made, when you compile and run your game, Your Player will be in a dark cave.
good thing he brought his flashlight.
Note that the color scheme has changed. you can set the color values to what ever youd like, the TCOD-wrapper.swift
supplies a few base ones, and the following colors have been added during this tutorial:
Cheers, Beers, and time for part five!