Note that there are some explanatory texts on larger screens.

plurals
  1. POJava game logic, check if object is placed in one of possible positions
    primarykey
    data
    text
    <p>I am developing a 2D java game, where use has to place certain objects (such as boxes, containers etc) in proper places. </p> <ul> <li>each object, got its destination [X,Y] where player should place them.</li> </ul> <p>But the problem is, when there is more than one object of the same type, it should be up to player to decide in which position he wants to place it (instead of guessing) </p> <p>Now I am wondering how to create proper logic to handle this issue, I am loading all level data from XML file, my idea was:</p> <ul> <li>while loading level from XML, add all objects to the array list</li> <li>after loading all objects, iterate through this list, and sort all objects into new array lists, one per object type (like box, container) so later, I can iterate through those lists and check object is currently placed in any possible position.</li> </ul> <p>Though I am still not sure, some help would be appreciate. </p> <p>Edit: following answer from @EZSlaver I came to this:</p> <pre><code>private HashMap&lt;ObjectType, ArrayList&lt;GameObject&gt;&gt; map = new HashMap&lt;ObjectType, ArrayList&lt;GameObject&gt;&gt;(); public void addObjekt(GameObject objekt) { if (map.containsKey(objekt.getType())) { map.get(ObjectType.TYPE_BOX).add(objekt); } else { ArrayList&lt;GameObject&gt; arrayOfObjects = new ArrayList&lt;GameObject&gt;(); arrayOfObjects.add(objekt); map.put(objekt.getType(), arrayOfObjects); } } public static void main(String[] args) { new Test(); } private enum ObjectType { TYPE_BOX, TYPE_CONTAINER } /** * Subclass of GameObject, box (name of game object) representation */ public class Box extends GameObject { public Box(float destX, float destY) { super(ObjectType.TYPE_BOX, destX, destY); } } /** * Subclass of GameObject, Container (name of game object) representation */ public class Container extends GameObject { public Container(float destX, float destY) { super(ObjectType.TYPE_CONTAINER, destX, destY); } } /** * Base type class, any object representation */ public class GameObject { private ObjectType Type; private float destX; private float destY; public GameObject(ObjectType type, float destX, float destY) { super(); Type = type; this.destX = destX; this.destY = destY; } public ObjectType getType() { return Type; } public float getDestX() { return destX; } public float getDestY() { return destY; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload