Author Topic: Java suggestion needed  (Read 1753 times)

Offline fireside

  • double
  • *****
  • Posts: 605
    • View Profile
Java suggestion needed
« on: March 04, 2008, 06:25:13 pm »
I was wondering if someone would have a suggestion for this.  My gravity objects will have an active property.  When the object is active, I want it to be tested in the game loop for gravity, but not tested when it is not active.  So, what I have in mind, but am not sure how to implement, is a container for active objects.  This would be accessible from the main game loop.  The gravity objects would add themselves to this container when they are active and remove themselves when they are not active.  How could I make these objects have access to the container in the main class?   
click here->Fireside 7 Games<-

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11884
    • View Profile
Re: Java suggestion needed
« Reply #1 on: March 04, 2008, 07:34:35 pm »
Personally, i prefer to keep those objects inside the container all the time, i.e. the container has a kind of process()-method that does the work on active ones and ignores inactives ones. If you store them in one list, iterate over them and do something if they are active or if you keep two lists in the container, one with all active, one with all inactive objects, is up to you. I usually prefer one list that stores them all, as long as this isn't a performance problem (what it isn't for just a few dozen objects).
I'm not saying that this is the best's just what i'm used to and what you can find in my sources (for example in the ParticleManager in the "advanced example" available in the download section).

Another solution: Make the container dumber and the objects more clever, i.e. let the container simply call a process()-method on each object and  let the object do the work. This approach may be more OO, because it keeps work that belongs to a single object inside the object. However, it's not what i'm usually doing in games programming.
« Last Edit: March 04, 2008, 07:39:52 pm by EgonOlsen »