Fog of War

Started by KodiakBehr, Mon 29/08/2011 14:44:12

Previous topic - Next topic

KodiakBehr

Probably biting off more than I can chew, but is it technically possible to create a game that creates a "fog-of-war" whereby a player can only see a few feet in front of their face, and uncovers incrementally more and more of their environment as they explore?

If so, any bright ideas as to how one would go about such a thing?

BlueAngel

There is probably a more clever way but can’t you hide the world (the room) behind an object that you tint?

I made a map (a room) for my last game that was blank and with objects (places) appearing as the player had been there.


KodiakBehr

I suppose that's an option, having checkpoints in a room where the "fog" diminishes.  I was hoping for a more elegant solution as this would require a lot of tinted objects and checkpoints to achieve the effect I was going for.

Monsieur OUXX

Quote from: KodiakBehr on Mon 29/08/2011 15:40:27
I was hoping for a more elegant solution as this would require a lot of tinted objects and checkpoints to achieve the effect I was going for.

Not necessarily.
At each game loop, when the scene and GUIs are entirely drawn, you can decide to draw black pixels int he places that haven't been uncovered yet.
 

KodiakBehr

I'm still learning the full capabilities of this engine.  Would you be able to give a hypothetical example of what that would look like?

Lt. Smash

#5
Hmm, here's an idea how you could do that:

First you create an image at room size being 50% black (and 50% transparent)
Then you assign this image to an object (let's call it layer) that fully hides the room and a fully black image to another object.

In repeatedly_execute_always you create two dynamicsprites, one from layer1's sprite and one from the original sprite.
You loop through the characters that reveal the fog of war.
Then take the x and y coords of each character and use them to draw a filled circle on both of the layers using COLOR_TRANSPARENT.

Something like this: (only for one character)
Code: ags

spriteLayer1 = DynamicSprite.CreateFromExistingSprite(object[0].Graphic, true);
spriteLayer2 = DynamicSprite.CreateFromExistingSprite(ORIGINAL_SPRITE, true);

surfaceLayer1 = spriteLayer1.GetDrawingSurface();
surfaceLayer2 = spriteLayer2.GetDrawingSurface();

surfaceLayer1.DrawingColor = COLOR_TRANSPARENT;
surfaceLayer2.DrawingColor = COLOR_TRANSPARENT;

surfaceLayer1.DrawCircle(cCharacter.x - X_OFFSET, cCharacter.y - Y_OFFSET, CIRCLE_SIZE);
surfaceLayer2.DrawCircle(cCharacter.x - X_OFFSET, cCharacter.y - Y_OFFSET, CIRCLE_SIZE);

surfaceLayer1.Release();
surfaceLayer2.Release();

object[0].Graphic = spriteLayer1.Graphic;
object[1].Graphic = spriteLayer2.Graphic;


If you want to completely hide certain characters (units) inside the already revealed areas of the room (like in strategy games), you'll have to loop through all those "non-fog-revealers" and check if their x,y coords are within the radius of the "fog-revealers". If true, set Visible; if not, set Invisible ;)

Very quick solution but I hope this helps...

EDIT: One object should be pitch black because I forgot that two 50% transparent objects merge to a 75% black (or 25% transparent) image.

KodiakBehr

Very interesting.  I look forward to trying this.

Lt. Smash

Here's a little demo to see the thing in action:

http://www.file-upload.net/download-3700459/WarFog.zip.html

I've implemented a very quick'n'dirty hack to hide objects in the fog... Taking some more time, you could make that look somewhat nicer ;)

SMF spam blocked by CleanTalk