Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!

1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Member Since 02 Sep 2012
Offline Last Active Apr 27 2013 04:03 AM

Topics I've Started

Handling many objects

02 September 2012 - 06:52 AM

I'm currently making a 2d RPG in Java and its engine is coming along quite nicely.. however I am a bit confused on how I should handle a certain situation. I have a class Unit which is any game character (subclasses for NPCs, Player, etc). The problem is there are always going to be several instances of this class and each one will need to update independently each time the canvas is rendered.

What's the best way to manage this? Currently each time gameUpdate() runs (optimally 60 times per second), it loops through an ArrayList that contains each Unit. Is there any way to forego using an array to handle each object? It seems like using an array like this will come back to haunt me when the game is more complex.

This is what I have now and I'd like to improve it or scrap it altogether.
[source lang="java"]package engine;import java.util.*;public class ObjectHandler extends ArrayList<GameObject> { private static final long serialVersionUID = -8598610051757901108L; public ObjectHandler() { System.out.println("ObjectHandler loaded."); } public static ObjectHandler getObjectHandler() { if (ref == null) ref = new ObjectHandler(); return ref; } // This method is ran every time // the game is updated. gameUpdate() public void update() { for (int i = 0; i < size(); i++) { get(i).move(); } } // This method is ran every time // the game is updated. gameRender() public void drawList() { String name; for (int i = 0; i < size(); i++) { name = null; name = get(i).name; } } /* OBJECTS ADD THEMSELVES IN THEIR CONSTRUCTOR * public void add(String unitName) { Unit unit = new Unit(unitName); //System.out.println(unit.name); add(unit); }*/ private static ObjectHandler ref;}[/source]