【GAMEMAKER】Usable Items

 Information about object: obj_brown_chest

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

my_id=1;
image_speed=0;
image_index=0;

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

my_id=1;

Information about object: obj_chest_parent
Sprite:
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children
obj_brown_chest
obj_green_chest
obj_red_chest
Mask:
No Physics Object

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

my_id=3;
image_speed=0;
image_index=0;

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

my_id=3;

Information about object: obj_invbutton
Sprite:
Solid: false
Visible: true
Depth: -1001
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Draw Event:
execute code:

var item = global.inventory[slot];
var click = mouse_check_button_pressed(mb_left);

if (abs(mouse_x - x) < 30) && (abs(mouse_y- y) < 38)
{
    draw_set_colour(c_white);
    draw_rectangle(x-28,y-28,x+28,y+28,0);
    if (click)
    {
        if (item != -1)
        {
            scr_itemdrop_slot(slot);
        }
        if (global.mouseItem != -1)
        {
            scr_itempickup_slot(global.mouseItem,slot)
        }
        global.mouseItem = item;
    }
    if mouse_check_button_pressed(mb_right)
    {
    
        switch (global.inventory[slot])
        {
        case 1:
        instance_create(obj_player.x,obj_player.y,obj_brown_key);
        break;
        case 2:
        instance_create(obj_player.x,obj_player.y,obj_red_key);
        break;
        case 3:
        instance_create(obj_player.x,obj_player.y,obj_green_key);
        break;
        }
        //remove item from inventory
        global.inventory[slot]=-1;
    }
}

if (item != -1)
{
    draw_sprite(spr_items,item,x,y);
}


Information about object: obj_inventory
Sprite:
Solid: false
Visible: true
Depth: -1000
Persistent: true
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:

///Set Up

global.maxItems=2;//total item slots

for (var i = 0; i < 2; i += 1)
{
    global.inventory[i] = -1;
    button[i] = instance_create(0,0,obj_invbutton)
    button[i].slot = i;
}

global.mouseItem=-1;
instance_create(0,0,obj_mouseitem);


Draw Event:
execute code:

///draw the inventory
var x1,x2,y1,y2;
x1 = view_xview[0]+75;
x2 = x1 + view_wview[0];
y1 = view_yview[0]+30;
y2 = y1 + 64;

draw_set_color(c_black);
draw_set_alpha(0.8);
draw_sprite(spr_inv_bg,0,x1,y1-25);
    
for (var i = 0; i < global.maxItems; i += 1)
{
    var ix = x1+64+(i * 60);
    var iy = y2-48;

    draw_sprite(spr_border,0,ix,iy)
    button[i].x = ix;
    button[i].y = iy;
}
draw_text(x1+100,y1+200,"P To Pick Up An Item When Touching# Click and Drag To Move & Use # Right Click To Drop");


Information about object: obj_mouseitem
Sprite: spr_collision
Solid: false
Visible: true
Depth: -1002
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Collision Event with object obj_chest_parent:
execute code:

if other.my_id==global.mouseItem
{
    with (other){
     image_index=1;
    //instance_destroy();
    }
}
Draw Event:
execute code:

var item=global.mouseItem;
if (item != -1)
{
    x = mouse_x;
    y = mouse_y;
    draw_sprite(spr_items,item,x,y);
}

Information about object: obj_pick_up_parent
Sprite:
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children
obj_brown_key
obj_green_key
obj_red_key
Mask:
No Physics Object
Collision Event with object obj_player:
execute code:

///Detect Keypress & Check For Empty Slot
if keyboard_check_pressed(ord('P'))
{
    if scr_itempickup(my_id)//if slot available, add to slot
    {
        instance_destroy();//then destroy instance]
    }
    else
    {
   
        switch (global.inventory[0])
        {
        case 1:
        instance_create(obj_player.x+50,obj_player.y,obj_brown_key);
        break;
        case 2:
        instance_create(obj_player.x+50,obj_player.y,obj_red_key);
        break;
        case 3:
        instance_create(obj_player.x+50,obj_player.y,obj_green_key);
        break;
        global.inventory[0]=-1;
        scr_itempickup(my_id);
        instance_destroy();
        }
    }
}
        


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;
    }
}



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

my_id=2;
image_speed=0;
image_index=0;

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

my_id=2;


No comments:

My New Favorite Music Genre Is Polish Noir

These 10 artists create music that is mystical, melancholy, magical, and more. ͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏    ...

Contact Form

Name

Email *

Message *