• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Enemy Objects Dimming HUD

SaraTonen

Member
I have my player object set up so that if it's within a specific range of the view (behind either portions of the HUD), then the HUD draws at a lower alpha, otherwise it resets to 1. This all works great but for some reason when I try to apply the same logic to enemy instances, it won't work properly. There are two variables that are tracked, enemy_behind_ehud and enemy_behind_chud. If either one is true, then the other won't or can't be and if the enemy is destroyed while it is dimming the HUD then the variable doesn't reset. There's quite a bit of code here but I wanted to make sure it was everything. The variable show_hud is turned on and off for room transfers.

Code:
//PLAYER DRAW GUI
//Draw HUD
if (show_hud == true)
{
    var cx = camera_get_view_x(view_camera[0]);
    var cy = camera_get_view_y(view_camera[0]);
    if (x <= cx+63 && y <= cy+33)
    {
        player_behind_ehud = true;
    }
    else player_behind_ehud = false;
    
    if (x <= cx+50 && y >= cy+220)
    {
        player_behind_chud = true;
    }
    else player_behind_chud = false;
    
    //Energy HUD
    if (player_behind_ehud == true) || (enemy_behind_ehud == true)
    {
        draw_set_alpha(.5);
        //Draw Energy/Controller HUD Underlay
        draw_set_color(c_black);
        draw_rectangle(9, 10, 63, 33, false); 
        draw_set_color(c_white);
        
        //Draw Energy Bar/Text
        draw_healthbar(11, 17, 62, 19, (energy/max_energy) * 100, c_black, c_red, c_white, 0, true, true);
        draw_healthbar(11, 17, 62, 17, (etank_capacity/100) * 100, c_black, c_aqua, c_aqua, 0, false, true);
        draw_set_font(Font1);
        draw_set_halign(fa_center);
        draw_set_valign(fa_middle);
        draw_text(36, 26,  string(round(energy)) + "/" + string(max_energy));
        draw_set_halign(fa_left);
        draw_set_valign(fa_top);
        draw_set_color(c_white);

        //Draw Full/Empty (white/red) Energy Tanks
        for (var i = 0; i < energy_tank; i++)
        {
            draw_rectangle(11 + (i * 4), 12, 12  +(  i* 4), 13, false);
        }
        draw_set_color(c_red);
        if (max_energy_tank > 0 && energy_tank < max_energy_tank)
        {
            var _num = max_energy_tank - energy_tank;
            for(var i = 0; i < _num; i++)
            {
                draw_rectangle((7+(4*max_energy_tank))-(i*4), 12, (8+(4*max_energy_tank))-(i*4), 13, false);
            }
        }
        draw_set_color(c_white);
    }
    else
    {
        //Draw Energy/Controller HUD Underlay
        draw_set_color(c_black);
        draw_set_alpha(.65);
        draw_rectangle(9, 10, 63, 33, false); 
        draw_set_color(c_white);
        draw_set_alpha(1);
        
        //Draw Energy Bar/Text
        draw_healthbar(11, 17, 62, 19, (energy/max_energy) * 100, c_black, c_red, c_white, 0, true, true);
        draw_healthbar(11, 17, 62, 17, (etank_capacity/100) * 100, c_black, c_aqua, c_aqua, 0, false, true);
        draw_set_font(Font1);
        draw_set_halign(fa_center);
        draw_set_valign(fa_middle);
        draw_text(36, 26,  string(round(energy)) + "/" + string(max_energy));
        draw_set_halign(fa_left);
        draw_set_valign(fa_top);
        draw_set_color(c_white);

        //Draw Full/Empty (white/red) Energy Tanks
        for (var i = 0; i < energy_tank; i++)
        {
            draw_rectangle(11 + (i * 4), 12, 12  +(  i* 4), 13, false);
        }
        draw_set_color(c_red);
        if (max_energy_tank > 0 && energy_tank < max_energy_tank)
        {
            var _num = max_energy_tank - energy_tank;
            for(var i = 0; i < _num; i++)
            {
                draw_rectangle((7+(4*max_energy_tank))-(i*4), 12, (8+(4*max_energy_tank))-(i*4), 13, false);
            }
        }
        draw_set_color(c_white);
    }
    
    //Controls HUD 
    if (player_behind_chud == true) || (enemy_behind_chud == true)
    {
        draw_set_alpha(.5)
        //Draw Energy/Controller HUD Underlay
        draw_set_color(c_black);
        draw_rectangle(5, 220, 50, 263, false);
        draw_set_color(c_white);
        
        //Draw Controller HUD
        if (has_dash) draw_sprite(spr_hud_face1, is_dashing, 28, 256); else draw_sprite(spr_hud_face1, 2, 28, 256);
        if (control_type = 0 ) draw_sprite(spr_hud_face, 0, 28, 248); else draw_sprite(spr_hud_face, 4, 28, 248);
    
        if (etank_capacity > 0 && energy < max_energy) draw_sprite(spr_hud_face2, key_recharge, 42, 242); else draw_sprite(spr_hud_face2, 2, 42, 242);
        if (control_type = 0 ) draw_sprite(spr_hud_face, 1, 33, 242); else draw_sprite(spr_hud_face, 5, 33, 242);
    
        draw_sprite(spr_hud_face3, key_attack_charged, 14, 242);
        if (control_type = 0 ) draw_sprite(spr_hud_face, 2, 23, 242); else draw_sprite(spr_hud_face, 6, 23, 242);
    
        if (has_parry) draw_sprite(spr_hud_face4, is_parrying, 28, 228); else draw_sprite(spr_hud_face4, 2, 28, 228);
        if (control_type = 0 ) draw_sprite(spr_hud_face, 3, 28, 236); else draw_sprite(spr_hud_face, 7, 28, 236);
    
        if (control_type = 0 ) draw_sprite(spr_hud_mbr, key_free_aim, 12, 228); else draw_sprite(spr_hud_freeaim, key_free_aim, 12, 228);
        if (control_type = 0 )
        {
            draw_sprite(spr_hud_lshift, key_thrusters, 44, 228);
        }
        else
        {
            if (has_thrusters)
            {
                draw_sprite(spr_hud_thrusters, key_thrusters, 44, 228);
            }
            else draw_sprite(spr_hud_thrusters, 2, 44, 228);
        }
        
    }
    else
    {
        draw_set_alpha(1);
        //Draw Energy/Controller HUD Underlay
        draw_set_alpha(.65);
        draw_set_color(c_black);
        draw_rectangle(5, 220, 50, 263, false);
        draw_set_alpha(1);
        draw_set_color(c_white);
        
        //Draw Controller HUD
        if (has_dash) draw_sprite(spr_hud_face1, is_dashing, 28, 256); else draw_sprite(spr_hud_face1, 2, 28, 256);
        if (control_type = 0 ) draw_sprite(spr_hud_face, 0, 28, 248); else draw_sprite(spr_hud_face, 4, 28, 248);
    
        if (etank_capacity > 0 && energy < max_energy) draw_sprite(spr_hud_face2, key_recharge, 42, 242); else draw_sprite(spr_hud_face2, 2, 42, 242);
        if (control_type = 0 ) draw_sprite(spr_hud_face, 1, 33, 242); else draw_sprite(spr_hud_face, 5, 33, 242);
    
        draw_sprite(spr_hud_face3, key_attack_charged, 14, 242);
        if (control_type = 0 ) draw_sprite(spr_hud_face, 2, 23, 242); else draw_sprite(spr_hud_face, 6, 23, 242);
    
        if (has_parry) draw_sprite(spr_hud_face4, is_parrying, 28, 228); else draw_sprite(spr_hud_face4, 2, 28, 228);
        if (control_type = 0 ) draw_sprite(spr_hud_face, 3, 28, 236); else draw_sprite(spr_hud_face, 7, 28, 236);
    
        if (control_type = 0 ) draw_sprite(spr_hud_mbr, key_free_aim, 12, 228); else draw_sprite(spr_hud_freeaim, key_free_aim, 12, 228);
        if (control_type = 0 )
        {
            draw_sprite(spr_hud_lshift, key_thrusters, 44, 228);
        }
        else
        {
            if (has_thrusters)
            {
                draw_sprite(spr_hud_thrusters, key_thrusters, 44, 228);
            }
            else draw_sprite(spr_hud_thrusters, 2, 44, 228);
        }
    }    
}

//ENEMY INSTANCE STEP
//Dim the HUD if Behind
var cx = camera_get_view_x(view_camera[0]);
var cy = camera_get_view_y(view_camera[0]);
if (x <= cx+63 && y <= cy+33)
{
    obj_player.enemy_behind_ehud = true;
}
else obj_player.enemy_behind_ehud = false;

if (x <= cx+50 && y >= cy+220)
{
    obj_player.enemy_behind_chud = true; 
}
else obj_player.enemy_behind_chud = false;
 

Simon Gust

Member
You let your enemies overwrite enemy_behind_chud / ehud all nilly willy. If the last enemy's step event detects the enemy is not behind the hud, it will set enemy_behind_chud / ehud to false and all your progress is wasted.
You can prevent this by removing the else part (where chud and ehud are set back to false) and set it to false yourself at the start of the frame.
GML:
// PLAYER DRAW GUI
var cx = camera_get_view_x(view_camera[0]);
var cy = camera_get_view_y(view_camera[0]);

var ehud = false;
if (x <= cx + 63 && y <= cy + 33) 
{
    ehud = true;
}
else
{
    with (obj_enemy)
    {
        if (x <= cx + 63 && y <= cy + 33)
        {
            ehud = true;
            break;
        }
    }
}

var chud = false;
if (x <= cx + 50 && y >= cy + 220) 
{
    chud = true;
}
else
{
    with (obj_enemy)
    {
        if (x <= cx + 50 && y >= cy + 220) 
        {
            chud = true;
            break;
        }
    }
}

// now draw hud elements
if (ehud) // draw ehud elements at lower alpha
if (chud) // draw chud elements at lower alpha
here I have restructured the code to make it centralized inside the player object. A with statement takes care of the enemies and their contribution to ehud / chud variables.
A break shortcuts the execution, so that if one enemy is detected inside the hud, the rest don't also check their positions.
 

SaraTonen

Member
You let your enemies overwrite enemy_behind_chud / ehud all nilly willy. If the last enemy's step event detects the enemy is not behind the hud, it will set enemy_behind_chud / ehud to false and all your progress is wasted.
You can prevent this by removing the else part (where chud and ehud are set back to false) and set it to false yourself at the start of the frame.
GML:
// PLAYER DRAW GUI
var cx = camera_get_view_x(view_camera[0]);
var cy = camera_get_view_y(view_camera[0]);

var ehud = false;
if (x <= cx + 63 && y <= cy + 33)
{
    ehud = true;
}
else
{
    with (obj_enemy)
    {
        if (x <= cx + 63 && y <= cy + 33)
        {
            ehud = true;
            break;
        }
    }
}

var chud = false;
if (x <= cx + 50 && y >= cy + 220)
{
    chud = true;
}
else
{
    with (obj_enemy)
    {
        if (x <= cx + 50 && y >= cy + 220)
        {
            chud = true;
            break;
        }
    }
}

// now draw hud elements
if (ehud) // draw ehud elements at lower alpha
if (chud) // draw chud elements at lower alpha
here I have restructured the code to make it centralized inside the player object. A with statement takes care of the enemies and their contribution to ehud / chud variables.
A break shortcuts the execution, so that if one enemy is detected inside the hud, the rest don't also check their positions.
I knew I had a problem with how and when the 'behind' variables were being set but I just kept producing similar results any time I messed with it. Gonna go mess with your code if you don't mind. Really trying to learn GML.
 
Top