Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: KodiakBehr on Mon 29/08/2011 14:44:12

Title: Fog of War
Post by: KodiakBehr on Mon 29/08/2011 14:44:12
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?
Title: Re: Fog of War
Post by: BlueAngel on Mon 29/08/2011 15:12:13
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.

Title: Re: Fog of War
Post by: KodiakBehr on Mon 29/08/2011 15:40:27
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.
Title: Re: Fog of War
Post by: Monsieur OUXX on Mon 29/08/2011 16:17:17
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.
Title: Re: Fog of War
Post by: KodiakBehr on Mon 29/08/2011 16:31:57
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?
Title: Re: Fog of War
Post by: Lt. Smash on Mon 29/08/2011 21:51:31
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)

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.
Title: Re: Fog of War
Post by: KodiakBehr on Tue 30/08/2011 16:00:09
Very interesting.  I look forward to trying this.
Title: Re: Fog of War
Post by: Lt. Smash on Tue 30/08/2011 20:47:20
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 ;)