Designing an inventory system

How can i best design an inventory system given the following:

The player has a 2d grid inventory with stackable items. 2 cursors are used-- a main cursor and a select cursor. Both cursors snap to grid when moved around the inventory, and they wrap from top to bottom and left to right. selecting an item with the main cursor brings up the select cursor, which is used to select where to place the selected item. The item stack will transfer wholly or partially, or swap contents depending on the state of the destination slot.

For instance, the source slot has 3 potions that can stack up to 5. The destination slot has 1 potion. All three potions are transferred from source slot to destination slot, leaving the source slot empty and the destination slot with a stack of 4 potions. However, if the destination slot already has a full stack of 5 potions, then the source slot would swap its 3 potions for the destination’s 5.

Alternatively, if there was a hamburger in the destination slot, the contents of the source and destination slots would simply be swapped.

You get the drift: a basic 2D grid inventory with standard stacking, transferring, and swapping features.

I have a working implementation, but I find that the design is very rigid and not amenable to change. This is a quick sketch:



class RedCursor extends Entity{
   
   public tick(){ 
      //first cursor's behavior defined here
   }
}

class BlueCursor extends Entity{
   public tick(){
      //second cursor's behavior defined here
   }
}

class Inventory{
   
   RedCursor redCursor;
   BlueCursor blueCursor;   

   List<Slot>;   

    public tick(){
       redCursor.tick();
       blueCursor.tick();  
    }
}

class Slot{

  List<Item>;
}

abstract class Item extends Entity{
   
   final int MAX_STACK_SIZE;
}

class Inventory is owned by class Player, so the basic flow is this:


Level.tick(){
   Player.tick(){
    Inventory.tick(){
       redCursor.tick();
       blueCursor.tick();
      }
   }
}

Without getting into too much detail, the problem with this design is that it doesn’t allow two inventories to interact with each other. The cursors are a huge problem, because they are owned by class Inventory, making it impossible for them to see outside the scope of their little Inventory world. (i.e., how could i make the cursor ‘hop’ from one inventory box to another, say to swap items from the player’s inventory into a chest?)

Any critique, help or pointers from more experienced coders is greatly appreciated!

You definitely separate GUI from game code. From your description, you should have at least 2 classes:

  1. Inventory <- DATA
  2. InventoryViewer / InventoryPanel <- GUI

Java a relatively simple drag and drop api, that you can use to manipulate data behind the inventory.
And there is probably some random listener that can be used to manipulate the cursor.

I’ll code up some designs-- good call on the separation. I’ve also learned a lot since I posted this in terms of design technique, so I’m scrapping the original system and starting anew.