ShaunJS

Inventory Mouse State Machine

Sep 1st, 2021
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. /// @desc State Machine
  2. slotHover = -1;
  3. inventoryHover = -1;
  4. inventoryDrag = -1;
  5. slotDrag = -1;
  6. itemDrag = -1;
  7.  
  8. mouseOver = function()
  9. {
  10. //empty hover results
  11. slotHover = -1;
  12. inventoryHover = -1;
  13.  
  14. //mouse coordinates
  15. var mx = mouse_x;
  16. var my = mouse_y;
  17.  
  18. //check for inventory slot hover
  19. with (oInventory)
  20. {
  21. if (point_in_rectangle
  22. (
  23. mx,
  24. my,
  25. x-6,
  26. y-6,
  27. x-6 + 12+rowLength*36,
  28. y-6 + 12+(((INVENTORY_SLOTS-1) div rowLength)+1)*36
  29. ))
  30. {
  31. //check for mouseover in each slot
  32. for (var i = 0; i < INVENTORY_SLOTS; i += 1)
  33. {
  34. var xx = x + (i mod rowLength) * 36 + 2;
  35. var yy = y + (i div rowLength) * 36 + 2;
  36. if (point_in_rectangle(mx, my, xx, yy, xx+32, yy+32))
  37. {
  38. other.slotHover = i;
  39. other.inventoryHover = id;
  40. }
  41. }
  42. }
  43. }
  44. }
  45.  
  46. stateFree = function()
  47. {
  48. mouseOver();
  49. //Start to drag an item if slot is not empty
  50. if (mouse_check_button(mb_left)) && (slotHover != -1) && (inventoryHover.inventory[slotHover] != -1)
  51. {
  52. //Enter drag state
  53. state = stateDrag;
  54. itemDrag = inventoryHover.inventory[slotHover];
  55. inventoryDrag = inventoryHover;
  56. slotDrag = slotHover;
  57. }
  58. }
  59.  
  60. stateDrag = function()
  61. {
  62. mouseOver();
  63. if (!mouse_check_button(mb_left))
  64. {
  65. //Swap with slot if hovering
  66. if (slotHover != -1)
  67. {
  68. inventoryDrag.inventory[slotDrag] = inventoryHover.inventory[slotHover]
  69. inventoryHover.inventory[slotHover] = itemDrag;
  70. }
  71.  
  72. //Return to free state
  73. state = stateFree;
  74. itemDrag = -1;
  75. inventoryDrag = -1;
  76. slotDrag = -1;
  77. }
  78. }
  79.  
  80. state = stateFree;
Advertisement
Add Comment
Please, Sign In to add comment