I was thinking about putting them into an array list, and ordering them based upon their depth
but they are different object types. The guy is an object of class character, and the girl is an object of class NPC. Is there a way to put both of these object types into one array list, or should is their another solution.
Define an interface that has the position - let us say,
IHasPosition, and implement this interface in the different types. You will also need to write a
Comparator<IHasPosition> (an anonymous class will do).
Then, the simplest solution is to a generic List
ArrayList<IHasPosition> where you can put all the objects that are in view, as long as they have the right interface, regardless of their actual class... and call
Collections.sort passing the comparator.
Thinking about it, you probably should use an
IDrawable that has the position and whatever else is needed to draw the objects.
Other solutions include:
- using a tree structure, where the nodes point to your objects and can be kept sorted.
- using a drawable component type, instead of an interface, and use composition (Composition over Inheritance). The game objects would have a drawable component.
- A more orthodox way of doing things is to have one single base class for every game object (that is, no Composition, only Inheritance).
Software enginering (there is an stackexchange site for that) has been moving to "composition over inheritance". the topic is beyond the scope of this question, so I suggest searching for that, also considering your language of choice (the language matters because some have multiple inheritance, some have traits, some have interfaces, some have default interface method implementations).