Hello
I've been working on an Eye of the Beholder kind of game. For a good idea, I attached a few screenshots. Some of the art is temporary.
I have the following algorithm that checks what is in the actor's current sight. It somewhat takes an extreme approach that gets everything around the player, and then you can make calls on the current direction he is facing.
I reduced the code for readability.
int Actor::view(Map &map)
{
// Sight area with a depth of 3. The actor is always at the center.
char sight[] = {
0, 0, 0, 0, 0, 0, 0, // 0-6
0, 0, 0, 0, 0, 0, 0, // 7-13
0, 0, 0, 0, 0, 0, 0, // 14-20
0, 0, 0, 'A', 0, 0, 0, // 21-27
0, 0, 0, 0, 0, 0, 0, // 28-34
0, 0, 0, 0, 0, 0, 0, // 35-41
0, 0, 0, 0, 0, 0, 0 // 42-48
};
char c;
int x_center = 3;
int y_center = 3;
// Build the actor's sight table
for (int y = 0; y < 7; y++) {
for (int x = 0; x < 7; x++) {
sight[y * 7 + x] = map.peek(m_x, m_y, x - x_center, y - y_center);
}
}
// Check the actor's facing direction
switch (m_dir)
{
case EDir_N:
{
/*
WWW
W*W (North-facing)
*/
if (sight[N] == WALL && sight[E] == WALL && sight[W] == WALL)
return WALL_UP_CLOSE;
}
break;
case EDir_E:
{
/*
WW
*W (East-facing)
WW
*/
if (sight[E] == WALL && sight[N] == WALL && sight[S] == WALL)
return WALL_UP_CLOSE;
}
break;
case EDir_W:
{
/*
WW
W* (West-facing)
WW
*/
if (sight[W] == WALL && sight[N] == WALL && sight[S] == WALL)
return WALL_UP_CLOSE;
}
break;
case EDir_S:
{
/*
W*W
WWW (South facing)
*/
if (sight[S] == WALL && sight[E] == WALL && sight[W] == WALL)
return WALL_UP_CLOSE;
}
break;
}
return -1;
}
As you can see, each direction has about the same check and the same returning ID. The returning ID draws the correct batch of images to the screen.
Although I reduced the code a few times, I feel I can reduce it even more. My question is, well, how can I reduce it? The player is facing a dead end on all four directions, so I feel I could just check it once somehow. I'm always tracking the player's position, so it helps me know what to draw.
I guess right now it just bugs me that there's 4 conditions to return the same ID based on direction. Can it be possible to have only one condition and not loose focus of the current facing direction?