Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (808)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (872)
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  
  making an inventory work.  (Read 3188 times)
0 Members and 1 Guest are viewing this topic.
Offline cloudwindgate

Senior Newbie





« Posted 2014-08-15 03:31:57 »

I just was looking for some one to shoot these ideas with, someone with more knowledge then me Tongue.

In my swing text based game I want to have a inventory system the player can use to well use items.

I'm wondering if these ideas are good, bad, or just ugly.
(MVC pattern in use....sorta)

Controller gets Inventory from player
Controller Gets each item from an ArrayList in the inventory
for each item create a button though a "dynamic" button maker in the view class
view returns the button to have controller add an actionlistener
actionListener calls the appropriate method in the player class to use the item
controller returns to state prior to using the items though Enum gameState

I hope that isnt too bad looking.
 My issue with this is that this means each and every item would need an actionListener inside the controller class. Is that normal or is there a better way.

....I thought I might have more in my head or on paper but I don't. I feel there has to be a better way... Any help is appreciate and welcome. So thank-you.
Offline cubemaster21
« Reply #1 - Posted 2014-08-15 11:40:29 »

If I were to approach this, I would have to agree with you up until you start throwing actionlisteners in. I'm not sure if this is best practice, but I would have all the "Items" extend one abstract class. In the abstract class, I would have an abstract method called something like "doAction(Player player, Environment env)". Then, you can have each item class have its own instruction, without throwing all the instructions into one class. Of course, the issue with this is that you have to create a new class for every item.

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline cloudwindgate

Senior Newbie





« Reply #2 - Posted 2014-08-15 13:49:40 »

hmmm the many classes isnt an issue, as thats already the route I'm going. I also have an parent to those classes that i just called item, however I would still need actionListeners would i not? I'm thinking to have the button call this doAction method of the item the player wants. I guess in this. I'm a newbie so you'll have to bare with me. is it possible when creating an actionListner to pass in another parameter then the default ones? because I'm thinking then i could have one listener which stores the current index of the item its associated to. Maybe this is still wrong could elaborate further please.

Thank-you for your help
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cubemaster21
« Reply #3 - Posted 2014-08-15 14:40:07 »

I would make an extension of whatever button class you are using that has an Item variable in it. When you generate all of your buttons, add the item object to the constructor of the button. Then, when you process the actionevent, you can cast the source of the event to your new button class. From your source, you can access the item variable you had stored in it.

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline cloudwindgate

Senior Newbie





« Reply #4 - Posted 2014-08-18 17:29:12 »

Sorry for the delay in response. I'm very much learning still so please forgive me if I mess this up, but let me see if I understand what your saying.
(I'm using JButton btw)
Extend the Jbutton as a new class lets say myButton
give myButton a constuctor which gives it a connection to the item stored in the inventory
have an action event which calls to that stored item and effects the player though a collection of methods.

okay if thats right would the code in the actionevent call something like this
this.getItem.doAction(player);
(saying my button has a method called getItem returning the stored item)

I would still assume that the controller would hold the action event like in my previous statement.
Offline cubemaster21
« Reply #5 - Posted 2014-08-18 17:47:52 »

Here's some quick code I wrote up. No time to try it so tell me if it doesn't work.
This is just a mockup of what I would suggest for the "MyButton" class. (Although, I don't recommend naming it that.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public class MyButton extends JButton{

   public Item i;

   public MyButton(....., Item i){ // The "......." is whatever arguments you're already passing to create the JButton, if any.
      super(....);
      this.i = i;
   }
   public Item getItem(){
      return i;
   }
}


Here's where your button press would be handled.
1  
2  
3  
4  
5  
6  
7  
8  
public void actionPerformed(ActionEvent e){
   Object o = e.getSource();
   if(o instanceof MyButton){
      MyButton b = (MyButton)o;
      b.getItem().doAction(player);
   }
   
}

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline cloudwindgate

Senior Newbie





« Reply #6 - Posted 2014-08-24 03:15:56 »

That worked although I'm having an issue where the action isnt happening except for right after its acquired even though all the other stuff is obtained for generating the button itself so I guess its something with the actionlistener ceasing to work for some reason.


Well anyways thank-you for your help cubemaster21
Pages: [1]
  ignore  |  Print  
 
 

 
mercenarius (14 views)
2020-06-04 19:26:01

mercenarius (24 views)
2020-06-04 19:13:43

Riven (866 views)
2019-09-04 15:33:17

hadezbladez (5848 views)
2018-11-16 13:46:03

hadezbladez (2657 views)
2018-11-16 13:41:33

hadezbladez (6273 views)
2018-11-16 13:35:35

hadezbladez (1518 views)
2018-11-16 13:32:03

EgonOlsen (4749 views)
2018-06-10 19:43:48

EgonOlsen (5804 views)
2018-06-10 19:43:44

EgonOlsen (3298 views)
2018-06-10 19:43:20
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
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!