Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Designing an inventory system  (Read 7857 times)
0 Members and 1 Guest are viewing this topic.
Offline DayTripperID

Senior Devvie


Medals: 8
Projects: 1
Exp: 1-3 months


Living is good!


« Posted 2016-08-01 20:44:03 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
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:

1  
2  
3  
4  
5  
6  
7  
8  
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!

Living is good!
Offline tariqbroadnax

Junior Devvie


Medals: 2
Exp: 3 years



« Reply #1 - Posted 2016-08-10 18:39:17 »

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.
Offline DayTripperID

Senior Devvie


Medals: 8
Projects: 1
Exp: 1-3 months


Living is good!


« Reply #2 - Posted 2016-08-12 19:34:01 »

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.

Living is good!
Pages: [1]
  ignore  |  Print  
 
 

 
xxMrPHDxx (21 views)
2017-11-21 16:21:00

xxMrPHDxx (14 views)
2017-11-21 16:14:31

xxMrPHDxx (16 views)
2017-11-21 16:10:57

Ecumene (114 views)
2017-09-30 02:57:34

theagentd (150 views)
2017-09-26 18:23:31

cybrmynd (250 views)
2017-08-02 12:28:51

cybrmynd (249 views)
2017-08-02 12:19:43

cybrmynd (247 views)
2017-08-02 12:18:09

Sralse (260 views)
2017-07-25 17:13:48

Archive (878 views)
2017-04-27 17:45:51
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!