【GAMEMAKER】Shop and inventory

 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;
    }
}
if !is_moving
{
    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_setup
Sprite:
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:

///Very Basic Example
global.cash=10000;
//In game you may declare gold like here or load from a saved ini file

global.inventory[1,1]="Rum"; //Item Name
global.inventory[1,2]=spr_rum; //Item Sprite
global.inventory[1,3]=50; //Cost
global.inventory[1,4]=10; // Current Inventory
global.inventory[1,5]=25; // Max Inventory

global.inventory[2,1]="Banana"; //Item Name
global.inventory[2,2]=spr_banana; //Item Sprite
global.inventory[2,3]=12; //Cost
global.inventory[2,4]=4; // Current Inventory
global.inventory[2,5]=10; // Max Inventory

global.inventory[3,1]="Gun"; //Item Name
global.inventory[3,2]=spr_gun; //Item Sprite
global.inventory[3,3]=120; //Cost
global.inventory[3,4]=2; // Current Inventory
global.inventory[3,5]=100; // Max Inventory

global.inventory[4,1]="Chest"; //Item Name
global.inventory[4,2]=spr_chest; //Item Sprite
global.inventory[4,3]=1000; //Cost
global.inventory[4,4]=0; // Current Inventory
global.inventory[4,5]=50; // Max Inventory
Information about object: obj_shop
Sprite: spr_shop
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Collision Event with object obj_player:
execute code:

room_goto(room_shop);
Information about object: obj_shopkeeper
Sprite: spr_shop_head
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:

img=0;
is_talking=false;
Step Event:
execute code:

if is_talking
{
    img++;
}
if is_talking && img==30
{
    img=0;
    is_talking=false;
}
Draw Event:
execute code:

draw_sprite(spr_shop_mouth,img,x,y+35);
draw_self();
Information about object: obj_shopkeeper_bg
Sprite: spr_keeper_frame
Solid: false
Visible: true
Depth: 10
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Information about object: obj_item_parent
Sprite: spr_item_frame
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children
obj_item_1
obj_item_2
obj_item_3
obj_item_4
Mask:
No Physics Object
Draw Event:
execute code:

/* Example Data:
global.inventory[1,1]="Rum"; //Item Name
global.inventory[1,2]=spr_rum; //Item Sprite
global.inventory[1,3]=50; //Cost
global.inventory[1,4]=10; // Current Inventory
global.inventory[1,5]=25; // Max Inventory
*/

//draw bg
draw_self();

//draw data from array using myid from child object
draw_set_font(font_shop_mini);
draw_set_halign(fa_center);
draw_set_valign(fa_middle);
draw_set_colour(c_black);
draw_text(x,y-72,global.inventory[myid,1]);
draw_sprite(global.inventory[myid,2],0,x,y-30);
draw_set_colour(c_yellow);
draw_text(x,y+15,string(global.inventory[myid,3])+" Coins");
draw_set_colour(c_black);
draw_text(x,y+40,string(global.inventory[myid,4])+" / "+string(global.inventory[myid,5]));
Information about object: obj_item_1
Sprite: spr_item_frame
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent: obj_item_parent
Children:
Mask:
No Physics Object
Create Event:
execute code:

myid=1;

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

myid=2;

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

myid=3;

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

myid=4;
Information about object: obj_shop_sign
Sprite: spr_shop_sign
Solid: false
Visible: true
Depth: 10
Persistent: false
Parent:
Children:
Mask:
No Physics Object

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

draw_self();
draw_set_font(font_shop_big);
draw_set_halign(fa_center);
draw_set_valign(fa_middle);
draw_set_colour(c_black);
draw_text(x-2,y-122,"Current#"+string(global.cash));
draw_set_colour(c_yellow);
draw_text(x,y-120,"Current#"+string(global.cash));
Information about object: obj_buy_parent
Sprite: spr_buy
Solid: false
Visible: true
Depth: -100
Persistent: false
Parent:
Children
obj_buy_1
obj_buy_2
obj_buy_3
obj_buy_4
Mask:
No Physics Object
Create Event:
execute code:

image_speed=0;
image_index=0;
Step Event:
execute code:

//set as red or green button
if global.cash>=global.inventory[myid,3]//if player has enough cash
{
    image_index=0;
}
else
{
    image_index=1;
}
if global.inventory[myid,4]==global.inventory[myid,5]//if maxed out
{
    image_index=1;
}
Mouse Event for Left Pressed:
execute code:

///basic shop buy
if global.inventory[myid,4]==global.inventory[myid,5] //check if maxed out already
{
    audio_play_sound(snd_overkill,1,false);
    obj_shopkeeper.is_talking=true; //make shopkeeper talk
}

else if global.cash<global.inventory[myid,3]//do this if not enough cash
{
    audio_play_sound(snd_not_enough_cash,1,false);
    obj_shopkeeper.is_talking=true; //make shopkeeper talk
}

//next part for successful purchase
else 
{
    audio_play_sound(snd_purchase_complete,1,false);
    obj_shopkeeper.is_talking=true; //make shopkeeper talk
    global.cash-=global.inventory[myid,3];
    global.inventory[myid,4]++;
}
Information about object: obj_buy_1
Sprite: spr_buy
Solid: false
Visible: true
Depth: -100
Persistent: false
Parent: obj_buy_parent
Children:
Mask:
No Physics Object
Create Event:
execute code:

myid=1;
event_inherited();
Information about object: obj_buy_2
Sprite: spr_buy
Solid: false
Visible: true
Depth: -100
Persistent: false
Parent: obj_buy_parent
Children:
Mask:
No Physics Object
Create Event:
execute code:

myid=2;
event_inherited();
Information about object: obj_buy_3
Sprite: spr_buy
Solid: false
Visible: true
Depth: -100
Persistent: false
Parent: obj_buy_parent
Children:
Mask:
No Physics Object
Create Event:
execute code:

myid=3;
event_inherited();

Information about object: obj_buy_4
Sprite: spr_buy
Solid: false
Visible: true
Depth: -100
Persistent: false
Parent: obj_buy_parent
Children:
Mask:
No Physics Object
Create Event:
execute code:

myid=4;
event_inherited();

No comments:

I Quit my Job 1 Year Ago Today to do YouTube Full Time

Here is what I've learned ͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏    ...

Contact Form

Name

Email *

Message *