Beginner's corner

Top  Previous  Next

Fog of war

 

If you are creating a strategy game then you might need to add some "fog of war" to it. This article will teach you to create a decent effect that doesn't eat too many resources; in fact, it is up to you to set the quality versus performance ratio, depending on the PC that is targeted as "system requirements" for your game.

 

aum43_shot2

 

How does this thing work? We create an opaque layer that covers the entire map; we build it using small sprites, and we make transparent only the sprites that are really close to the player.

 

aum43_shot3

 

Do you see all those small red squares? Each one of them is a sprite. If you believe that I have placed them in the level by hand you must think that I'm a patient person. Well, I'm not that patient...

 

starter create_fog_sprites()

{

   while (player == null) {wait (1);}

   var row_index = 0;

   var column_index = 0;

   while (row_index < 32)

   {

      while (column_index < 32)

      {

         column_index += 1;

         temp.x = column_index * 128 - 2000; // -2000 to 2000 on x

         temp.y = row_index * 128 - 2000; // -2000 to 2000 on y

         temp.z = -1000;

         ent_create (fogsprite_tga, temp.x, manage_fog);

      }

      column_index = 0;

      row_index += 1;

   }

}

 

The code waits until the player is created, and then uses two nested "while" loops to place all the sprites above the ground. My demo level has about 4000 x 4000 quants and I have used a sprite of 128 x 128 pixels for the fog, so we should create 4000 / 128 = 31.25 (I chose 32) rows and columns. We've got two variables that increase their values from 0 to 31 (for a total of 32 steps), and we set the proper temp position by multiplying the current index with the width and height of the sprite (which are equal in my example). Temp.z = -1000 places the fog sprites above player's head (that's another experimental value), and then the next line creates the fog sprites at the position given by temp and attaches them the "manage_fog" function:

 

function manage_fog()

{

   my.tilt = 90;

   my.alpha = 100;

   my.oriented = on;

   my.transparent = on;

   my.passable = on;

   while (1)

   {

      if (vec_dist(player.x, my.x) < 350)

      {

         my.alpha = 0;

      }

      else

      {

        my.alpha = 100;

      }

      wait (1);

   }

}

 

The function above sets the proper orientation for the sprite, makes it passable and sets its alpha value depending on the distance between the sprite and the player. Feel free to play with "350", and don't forget that you can get a more realistic fog effect if you use smaller sized sprites.

 

 

Escalator

 

If you need to transport the player using an escalator then this snippet is just what you need.

 

aum43_shot4

 

We've got a simple action that is attached to our wmb escalator:

 

var escalator_speed = 2;

 

action escalator

{

  while (1)

  {

     my.v += escalator_speed * time;

     wait (1);

  }

}

 

This action simply changes the "v" for the wooden texture that was applied to the escalator entity, creating the illusion that it moves. I could have used another (better looking) texture, but all of us have got the "woodflhori" texture inside standard.wad.

 

starter trace_below()

{

   var trace_pos;

   while (player == null) {wait (1);}

   while (1)

   {

      vec_set(trace_pos.x, player.x);

      trace_pos.z -= 1000;

      trace_mode = ignore_me + ignore_passable + ignore_models + ignore_sprites + scan_texture;

      trace (player.x, trace_pos.x);

      if (str_cmpi ("woodflhori", tex_name))

      {

         player.y += escalator_speed * time;

         player.z += 0.5 * time; // experimental value

      }

      wait (1);

   }

}

 

The function above waits until the player is created, and then traces 1000 quants below player's feet every frame. If the trace detects the texture named "woodflhori" below player's feet, it increases player's x and z. Please note that player's y is increased with the same amount that was used for "v" in order to keep the player and the escalator in sync. You might need to use negative values for y, or replace the movement on the y axis with movement on the x axis, depending on the orientation of the escalator. Check the pictures below to see what I mean.

 

We increase player's height (z) too; this helps it to move upwards easily with a small amount and lets the gravity code to do the rest of the job, pulling the player close to the floor level. Play with 0.5 until you are happy with the result and don't forget to tweak the escalator_speed variable as well.

 

aum43_shot5

 

aum43_shot6

 

aum43_shot7

 

aum43_shot8