Jump to content
  • Advertisement
Sign in to follow this  
WiLD2

Data Structure (Array) - Java or otherwise

This topic is 5041 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I'm looking to implement an ArrayList in my game. I'm trying to figure out how's the best way to get this thing going. It's currently written out on paper, I haven't started programming it yet. Here's my current class listing: PlayerObject ItemObject which extends to ---Armor ---Weapon ---Item which extends to ------Health (packs) ------Amp (increased damage) ------Ammunition Now what I want to do is give my character two ItemObject arrays which can be used as inventory space and as locker space. Any good homemade ArrayList will have three methods: findItem, addItem and removeItem. I know there are any number of ways I can do this, but I'm looking for the most efficient and reusable method. I don't think I want to place these methods in my PlayerObject class. I was hoping to lean toward a seperate ItemObject child, probably call it ItemList or something and throw the methods in there. Any guidance will be appreciated, thank you very much. -Kyle "WiLD2" Newton

Share this post


Link to post
Share on other sites
Advertisement
The standard library provides all kinds of containers that might be appropriate. You might first consider whether you can get by with just an array, though. They're "friendlier" in Java than in C++ and will do just fine as long as you don't need resizing.

You do, indeed, almost certainly want a 'child' (maybe named "inventory") for the Player rather than trying to give it the methods directly. The player's primary purpose in life is to run around killing bad guys, not to hold stuff. :)

Also, don't add "Object" to your class names, it doesn't add extra meaning.

Also, don't prematurely make your inheritance more complicated than it needs to be. Do you already know of behaviour that Health, Amp and Ammunition all have in common that Armor and Weapon don't? If not, flatten the inheritance tree. You can always refactor it later. Simplifying things after the fact is harder than adding extra stuff.

Share this post


Link to post
Share on other sites
Thanks for the reply.

So you're suggesting I create a child from the Player class to work as an Inventory?

I think I understand how that would work. I'd create a class titled Inventory which extends Player and within that I'd instantiate an ItemObject?

If I'm creating a child class from the Player I suppose I could choose not to inherit the Player name, health, etc. Is that really the best way to go about doing it? I can see that it would work. Is it better to create an Inventory from the Player class rather than create an Inventory from the ItemObject class?

And yes, I could just as easily do away with the Item children "health" "amp" and "ammo" and create a factory for them within "Item" itself. Again, work in progress.

Thanks for the suggestion.

Share this post


Link to post
Share on other sites
An update:

I'd managed to figure it out. I turned my Player and Item classes into children of GamePiece. I then wrote myself a Container of GamePiece[] seperate from other classes. I've since created a Store object as well, that my Player can access when it's appropriate for him to buy/sell items.

Within the Store I use that Container item to hold wares such as Weapons, Armor and pickups.
Within my player character, he has a Container called Inventory and another Container called Locker. He always has access to his inventory but only has access to his locker when he is at the locker. Heh, the player carries the store around with him, too, which he accesses only when he's "at the store". The incredible shrinking containers, ever-portable.

Share this post


Link to post
Share on other sites
Quote:
Original post by WiLD2
I think I understand how that would work. I'd create a class titled Inventory which extends Player and within that I'd instantiate an ItemObject?


No.


public class Player {
private ItemObject[] inventory;
// other stuff
}



Oh, and you might consider making the Locker be a separate "thing":


public class Locker {
private ItemObject[] contents; // or some container class such as Vector or ArrayList instead...
public void insert(ItemObject toBeStored) {
// Put toBeStored into contents
}
public ItemObject retrieve(int whichThing) {
// remove the appropriate thing from contents
// and return it
}
}

// Within Player class:
public void storeItem(int which, Locker storage) {
// remove appropriate thing from inventory, and
// pass it to storage.insert()
}
public void getItem(int which, Locker storage) {
// call storage.retrieve(which)
// and put the result into inventory
}

Share this post


Link to post
Share on other sites
Thanks for your help, Zahlman.

I'd done just that, I had misunderstood you the first time. I've created my "Locker" as a Container item. I'd created the seperate "Container" class which takes care of adding/removing/finding items within the array.

The "Locker" that you've written there looks similar to my Container class. I've created an "Inventory" container as well that works the same as my locker.

Thanks again :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!