Beginner's corner

Top  Previous  Next

Animated hud

Do you remember those good old games where the player had a little picture as a hud element? That picture was showing a devastated player face when his health was going down to zero and a happier face when the health was growing up to 100. That's exactly what this piece of code does.

panel player_panel // displays player's health
{
    pos_x = 0;
    pos_y = 0;
    digits = 160, 70, 3, health_font, 1, player._health;
    flags = d3d, refresh, visible;
}

panel health1_panel // displays the 1st picture
{
    bmap = health1_pcx;
    pos_x = 0;
    pos_y = 0;
    flags = d3d, refresh;
}

panel health2_panel // displays the 2nd picture
{
    bmap = health2_pcx;
    pos_x = 0;
    pos_y = 0;
    flags = d3d, refresh;
}

panel health3_panel // displays the 3rd picture
{
    bmap = health3_pcx;
    pos_x = 0;
    pos_y = 0;
    flags = d3d, refresh;
}

panel health4_panel // displays the 4th picture
{
    bmap = health4_pcx;
    pos_x = 0;
    pos_y = 0;
    flags = d3d, refresh;
}

panel health5_panel // displays the 5th picture
{
    bmap = health5_pcx;
    pos_x = 0;
    pos_y = 0;
    flags = d3d, refresh;
}

You can see that I'm using a simple panel, without any bmap to show player's health; I need a panel because the health is displayed using a "digit". The other 5 panels are nothing more than the pictures that will be displayed on the panel.

Maybe it is a good moment to tell you that if you are using the templates, player's health is stored in player._health - that's what the "digit" displays. I needed something to decrease player's health so I have created this action:

action player_killer
{
    while (player == null) {wait (1);}
    while (1)
    {
         if (vec_dist(my.x, player.x) < 50 && player._health > 0)
         {
              player._health -= 0.5 * time;
         }

         if (player._health > 80)
         {
              health1_panel.visible = on;
         }
         else
         {
              if (player._health > 60)
              {
                   health2_panel.visible = on;
              }
              else
              {
                   if (player._health > 40)
                   {
                        health3_panel.visible = on;
                   }
                   else
                   {
                        if (player._health > 20)
                        {
                             health4_panel.visible = on;
                        }
                        else
                        {
                             health5_panel.visible = on;
                        }
                   }
              }
         }
         wait (1);
    }
}

This action is attached to the guard.mdl model that is placed near the player in my example level. Of course that you can use this code for a laser barrier, etc. We can't predict if action player_killer will run before player_walk_fight that is attached to the player. If player_killer runs first, the player isn't created so player._health in the 3rd line doesn't make sense. The first line in the action fixes the problem; player_killer will start running only after the player has been created.

If the player comes closer than 50 quants and he's still alive, his health is decreased with the speed given by 0.5 * time. Here's what happens:
- if player._health > 80 we display the health1 panel;
- if player._health = 60...80 we display health2;
- if player._health = 40...60 we display health3;
- if player._health = 20...40 we display health4;
- if player._health =  0...20 we display health5;

The action is a little more complicated; download Aum12.zip and you'll see that we need to make invisible the panels that aren't supposed to appear on the screen. The code in these articles is only for learning; the fully functional versions are zipped inside Aumxx.zip.


Time demo

The new time_factor variable has made it much easier for us to control the speed in our games. Do you want to create all those time - related special effects in Max Payne? Piece of cake!

First of all I have created a panel with a slider that can adjust the speed in the game. Do you see how simple it is?

panel speed_factor
{
    bmap = slider_pcx;
    layer = 20;
    pos_x = 10;
    pos_y = 25;
    hslider = 10, -12, 200, cursor_pcx, 0, 10, time_factor;
    flags = d3d, overlay, refresh, visible;
}

Time_factor will change its value between 0 and 10 when you adjust the slider. I have placed a gun in the level; make sure that you take a look at the speed of the rockets with time_factor = 0 and time_factor = 10.

But how do we get those speed variation effects? I have provided two examples:

function init_timedemo()
{
    while (1)
    {
         if (key_q == 1)
         {
              while (key_q == 1) {wait (1);} // wait for the key release
              time_factor = 1; // reset it
              while (time_factor > 0.01)
              {
                   time_factor -= 0.05 * time;
                   wait (1);
              }
         }
         if (key_a == 1)
        {
            while (key_a == 1) {wait (1);} // wait for the key release
            time_factor = 1; // reset it
            while (time_factor < 10)
            {
                   time_factor += 0.2 * time;
                   wait (1);
             }
        }
       wait (1);
   }
}

When we press the "Q" key time_factor will decrease its value from 1 to 0.01; you'll see the actions running slower and slower. If you press "A", time_factor will grow pretty fast to 10 and all the actions in the level will run faster than you've ever seen them.