Click here to Skip to main content
15,844,477 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm thinking of building my first game for java. In the game I'm imagining it will be possible for the player or any of the NPCs to be polymorphed into a different creature type. Each creature type would have its own modifiers to stats and possibly have some sprecial properties of its own. I'm pretty sure I'll be building something like a "Mobile_Object" class and the PC and NPC will inhertit from that class. But I'm not really sure how to handle all the possible sub-types that the PC and NPCs can be. What would be the best way to arrange the data structures for something like that? One big class that has all the sub types built into it, or some form of sub class, or (more likely) something else I'm simply not aware of?

You are thinking in a wrong direction. The short answer is: use OOP. I don't want to try to understand the idea of OOP is one Quick Answer. Just a hint: late binding. You just need to learn OOP in its heart.

Share this answer
See this tutorial[^]. Start with a base class that has all the common properties and methods, and inherit that for all the 'real' objects.
Share this answer
Steel Fox 5-May-13 3:58am    
Well, yes that was the basic idea. The problem I am trying to work out is how to arrange the sub classes. For example I get that I would make something like an Mobile_Object class and that would have some properties like HP, Strength, movement. PC and NPC would both be thier own classes that would inherit from the Mobile_Object class. But then what would be the best way to handle the various forms the PC and the NPC might have at any given time. In my mind each form would be some kind of sub class that each have thier own modifiers to stats in Mobile_Object types and even some extra methods that PC and NPC wouldn't normally have.

For example: Say I already have an NPC object that is inheriting from Mobile_Object. NPC is trundling around the screen and gets hit with an effect and now I run something like NPC.Changeshape("ant") or NPC.Changeshape(shapeType.ant). The ant shapeType reduces some of the NPCs stats but also gives it a new ability "burrow". Now I would immagine the stat changes and the method for "burrow" exist in an "ant" class object someWhere, but I don't see how to let the NPC object have access to methods in the ant object.

I'm affraid the best method might be to make a giant Mobile_Object class that has all of the forms the Mobile_Object might take hanging out in there and just handle all of the form changes through flags.
Richard MacCutchan 5-May-13 4:03am    
I have no idea what PC ands NPC are but your description suggests that each has a property of "CurrentShape". You could also have extra methods that are defined in an interface and implemented by each object.
Steel Fox 5-May-13 5:12am    
Just in case you run into one of us tabletop gaming geeks again PC = Player Character - character controled directly by the player. NPC = Non-Player Character - Character controled by the game master or in this case the program itself.

So what you are saying is that the various shapes ("ant" in the example) would be Interfaces and the Mobile_Objects would dynamically implement the interfaces as needed? I thought that implement only happened when a class was defined. Can you dynamically change what interface a class is implementing?
Richard MacCutchan 5-May-13 7:24am    
1. This is an international forum and using abbreviations makes it less easy for people to understand questions. Don't assume that everyone understands what is common usage to you.

2. Implementation of interfaces (or anything else) happens at design/coding time.

3. You may like to try one of the various game developer websites, where you will probably find people who understand the issues better.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900