1
\$\begingroup\$

I'm developing a game to learn more about Java, and I've run into a problem: My character (the guy) moves, but I can not figure out how to automatically change the order of how the sprites are drawn. Each sprite has a depth, which is just the y value of the bottom of the sprite. If a sprite has a higher depth, then it is displayed in front of a sprite with a lower depth. Here are a few pictures of the problem:

enter image description here

enter image description here

As you can see the first picture, the guy should be in front of the girl.

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 is there a better solution?

\$\endgroup\$
1
\$\begingroup\$

I was thinking about putting them into an array list, and ordering them based upon their depth

YES.

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).

\$\endgroup\$
  • \$\begingroup\$ Define an interface that has the position <..> Alternatively, one can use subclasses, where all objects that appear on the world extend it. I only mention it so future readers can have more information on what to search. \$\endgroup\$ – TomTsagk Oct 1 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.