【GAMEMAKER】Weapon control select weapon with mouse wheel

 Information about object: obj_player

Sprite: spr_idle_down
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:

///set up
enum player_state {
    idle,
    up,
    down,
    left,
    right
    }

dir=player_state.down;
is_moving=false;
image_speed=0.5;
Step Event:
execute code:

///keypress code
if (keyboard_check(vk_left))
{
    dir=player_state.left;
    is_moving=true;
}
else
if (keyboard_check(vk_right))
{
    dir=player_state.right;
    is_moving=true;
}
else
if (keyboard_check(vk_up))
{
    dir=player_state.up;
    is_moving=true;
}
else
if (keyboard_check(vk_down))
{
    dir=player_state.down;
    is_moving=true;
}
else
{
    is_moving=false;
}

execute code:

///movement
if is_moving
{
    switch (dir)
    {
        case player_state.up:
        y -= 4;
        break;

        case player_state.down:
        y += 4;
        break;

        case player_state.left:
        x -= 4;
        break;
   
        case player_state.right:
        x += 4;
        break;
    }
}
execute code:

///animation
if is_moving
{
    switch (dir)
    {
        case player_state.up:
        sprite_index=spr_walk_up;
        break;

        case player_state.down:
        sprite_index=spr_walk_down;
        break;

        case player_state.left:
        sprite_index=spr_walk_left;
        break;
   
        case player_state.right:
        sprite_index=spr_walk_right;
        break;
    }
}
else
{
    switch (dir)
    {
        case player_state.up:
        sprite_index=spr_idle_up;
        break;

        case player_state.down:
        sprite_index=spr_idle_down;
        break;

        case player_state.left:
        sprite_index=spr_idle_left;
        break;
   
        case player_state.right:
        sprite_index=spr_idle_right;
        break;
    }
}

execute code:

///use weapon
if mouse_check_button_pressed(mb_left)
{
    if global.weapon[global.selected,2]>0
    {
        audio_play_sound(global.weapon[global.selected,4],1,false);
        global.weapon[global.selected,2]--;
    }
    else
    audio_play_sound(snd_no_ammo,1,false);
}
Collision Event with object obj_hook_crate:
execute code:

global.weapon[1,2]+=10;
with (other) instance_destroy();
Collision Event with object obj_cutlass_crate:
execute code:

global.weapon[2,2]+=10;
with (other) instance_destroy();
Collision Event with object obj_gun_crate:
execute code:

global.weapon[3,2]+=10;
with (other) instance_destroy();

Information about object: obj_hook_crate
Sprite: spr_hook_crate
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object

Information about object: obj_cutlass_crate
Sprite: spr_cutlass_crate
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object

Information about object: obj_gun_crate
Sprite: spr_gun_crate
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object

Information about object: obj_hud
Sprite: spr_hud_bg
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:

///set up, inc array to hold data
global.weapon[1,1]="HOOK";
global.weapon[1,2]=20;
global.weapon[1,3]=spr_hook_hud;
global.weapon[1,4]=snd_hook;

global.weapon[2,1]="CUTLASS";
global.weapon[2,2]=10;
global.weapon[2,3]=spr_cutlass_hud
global.weapon[2,4]=snd_cutlass;

global.weapon[3,1]="GUN";
global.weapon[3,2]=40;
global.weapon[3,3]=spr_gun_hud;
global.weapon[3,4]=snd_gun;

//set selected weapon
global.selected=1;

Step Event:
execute code:

///select weapon with mouse wheel
if mouse_wheel_up()
{
    global.selected-=1;
    if global.selected==0 global.selected=3;
}

if mouse_wheel_down()
{
    global.selected+=1;
    if global.selected==4 global.selected=1;
}
Draw Event:
execute code:

///comment to prevent default drawing
Draw GUI Event:
execute code:

///draw hud
//set alpha at 50%
draw_set_alpha(0.5);

draw_sprite(spr_hud_bg,0, 0,550);//background

//set up text
draw_set_font(drawing_font);
draw_set_halign(fa_center);
draw_set_valign(fa_middle);

//draw data
for (var loop = 1; loop < 4; loop += 1)
{
    draw_text(200+100*loop,590,global.weapon[loop,1]);//name
    draw_text(200+100*loop,650,global.weapon[loop,2]);//amount
    draw_sprite(global.weapon[loop,3],0,200+100*loop,645);//sprite
    //draw border if selected
    if loop==global.selected draw_sprite(spr_show_selected,0,200+100*loop,645);
}


//reset alpha to normal
draw_set_alpha(1);

//info
draw_text(400,50,"Touch Crate To Pick Up#Middle Mouse Button Up / Down To Select#Left Mouse To Use");

No comments:

Top 3 UX Design Articles of 2024 to Remember

Based on most subscriptions ͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­...

Contact Form

Name

Email *

Message *