Advertisement
Guest User

inventory - Step Event

a guest
Jan 22nd, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. /// @description
  2.  
  3. if(keyboard_check_pressed((ord("I")))) show_inventory = !show_inventory; // Toggle Inventory
  4. if((show_inventory) and keyboard_check_pressed(vk_escape)) show_inventory = false; // Close Inventory with Escape
  5.  
  6.  
  7. if(!show_inventory) exit; // Don't run anything under this if the inventory is shut
  8.  
  9. #region Mouse Slot Selection
  10.  
  11. mousex = device_mouse_x_to_gui(0);
  12. mousey = device_mouse_y_to_gui(0);
  13.  
  14. var cell_xbuff = (cell_size+x_buffer)*scale;
  15. var cell_ybuff = (cell_size+y_buffer)*scale;
  16.  
  17. var i_mousex = mousex - slots_x;
  18. var i_mousey = mousey - slots_y;
  19.  
  20. var nx = i_mousex div cell_xbuff;
  21. var ny = i_mousey div cell_ybuff;
  22.  
  23. //Only update slots if mouse is inside inventory
  24. if ((nx >= 0 and nx < inv_slots_width) and (ny >= 0 and ny < inv_slots_height)) {
  25. var sx = i_mousex - (nx*cell_xbuff);
  26. var sy = i_mousey - (ny*cell_ybuff);
  27.  
  28. if((sx < cell_size*scale) and (sy < cell_size*scale)) {
  29. m_slotx = nx;
  30. m_sloty = ny;
  31.  
  32.  
  33. // Find what slot is currently selected
  34. selected_slot = min(inv_slots-1 ,m_slotx + (m_sloty*inv_slots_width));
  35. }
  36. } else {
  37. selected_slot = 0;
  38. }
  39.  
  40. #endregion
  41.  
  42. #region Item Selection & Movement
  43.  
  44. var inv_grid = ds_inventory;
  45. var ss_item = inv_grid[# 0, selected_slot];
  46.  
  47. if(pickup_slot != -1) {
  48. if(mouse_check_button_pressed(mb_left)) {
  49. if(ss_item == item.none) { // If placing the item in an empty slot
  50. inv_grid[# 0, selected_slot] = inv_grid[# 0, pickup_slot];
  51. inv_grid[# 1, selected_slot] = inv_grid[# 1, pickup_slot];
  52.  
  53. inv_grid[# 0, pickup_slot] = item.none;
  54. inv_grid[# 1, pickup_slot] = 0;
  55.  
  56. pickup_slot = -1;
  57.  
  58. } else if (ss_item == inv_grid[# 0, pickup_slot]) { // If placing the item in a slot with the same item
  59. if(selected_slot != pickup_slot) {
  60.  
  61. inv_grid[# 1, selected_slot] += inv_grid[# 1, pickup_slot];
  62.  
  63. inv_grid[# 0, pickup_slot] = item.none;
  64. inv_grid[# 1, pickup_slot] = 0;
  65.  
  66. pickup_slot = -1;
  67. } else { // If the slot is the same as the one picked up from
  68. pickup_slot = -1;
  69. }
  70.  
  71. } else { // If placing the item on a slot with a different item
  72. var ss_item_num = inv_grid[# 1, selected_slot];
  73. inv_grid[# 0, selected_slot] = inv_grid[# 0, pickup_slot];
  74. inv_grid[# 1, selected_slot] = inv_grid[# 1, pickup_slot];
  75.  
  76. inv_grid[# 0, pickup_slot] = ss_item;
  77. inv_grid[# 1, pickup_slot] = ss_item_num;
  78. }
  79. }
  80. } else if(ss_item != inv_grid[# 0, 0]){
  81. //Drop Item Into World
  82. if(keyboard_check_pressed(ord("Q"))) {
  83. inv_grid[# 1, selected_slot] -=1; //Remove one item from stack
  84.  
  85. //Check if it was the last item
  86. if(inv_grid[# 1, selected_slot] == 0) {
  87. inv_grid[# 0, selected_slot] = item.none;
  88. }
  89.  
  90. //Spawn the Item
  91. var inst = instance_create_layer(e_player.x, e_player.y, "Instances", e_item);
  92. with(inst) {
  93. item_num = ss_item;
  94. x_frame = item_num mod (s_width/cell_size);
  95. y_frame = item_num div (s_width/cell_size);
  96. }
  97. show_debug_message("An item was dropped!")
  98. }
  99.  
  100. //Pickup Item From Slot
  101. if(mouse_check_button_pressed(mb_left)) {
  102. pickup_slot = selected_slot;
  103. }
  104. }
  105. #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement