GTGE API

com.golden.gamedev.object
Class AdvanceSpriteGroup

java.lang.Object
  extended by com.golden.gamedev.object.SpriteGroup
      extended by com.golden.gamedev.object.AdvanceSpriteGroup

public class AdvanceSpriteGroup
extends SpriteGroup

Subclass of SpriteGroup that designed to update and render visible on the screen sprites only.

In standard sprite group, all registered sprites in the group is updated, rendered, and check for collision in every game loop. If the game has many sprites and many of them are not visible, it is not efficient to update, render, and check for collision all of the sprites.

AdvanceSpriteGroup is designed to optimize the sprite updating and rendering by updating and rendering sprites that only visible on screen, sprites that outside the game view area are not checked.

The main operation is storing sprites that visible on screen into an inner sprite group, and the inner sprite group that will be update, render, and check for collision.


Field Summary
protected  SpriteGroup ONSCREEN_GROUP
          Inner sprite group that hold on screen sprites (inside view area sprites) of this group.
 
Constructor Summary
AdvanceSpriteGroup(String name)
          Creates new AdvanceSpriteGroup with specified name without screen offset (0, 0, 0, 0).
AdvanceSpriteGroup(String name, int screenOffset)
          Creates new AdvanceSpriteGroup with specified name, and specified screen offset.
AdvanceSpriteGroup(String name, int topOffset, int leftOffset, int bottomOffset, int rightOffset)
          Creates new AdvanceSpriteGroup with specified name, and specified screen offset on each side.
 
Method Summary
 int getGroupSize()
          Returns total non-null sprites (active + inactive) in this group.
 Sprite[] getGroupSprites()
          Returns all sprites (active, inactive, and also null sprite) in this group.
 Insets getScreenOffset()
          Returns screen offset of this group.
 int getSize()
          Returns total non-null on-screen sprites (active + inactive sprites) in this group.
 Sprite[] getSprites()
          Returns all on-screen sprites (active, inactive, and also null sprite) in this group.
 void render(Graphics2D g)
          Renders all active sprites in this group.
 void setBackground(Background backgr)
          Associates specified background with this sprite group, the background will be used by all sprites in this group.
 void setComparator(Comparator c)
          Sets comparator used for sorting sprites in this group.
 void update(long elapsedTime)
          Updates all active sprites in this group, and check the schedule for removing inactive sprites.
 
Methods inherited from class com.golden.gamedev.object.SpriteGroup
add, add, clear, getActiveSprite, getBackground, getComparator, getExpandFactor, getInactiveSprite, getName, getScanFrequence, isActive, remove, remove, removeImmutableSprites, removeInactiveSprites, reset, setActive, setExpandFactor, setName, sort, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ONSCREEN_GROUP

protected SpriteGroup ONSCREEN_GROUP
Inner sprite group that hold on screen sprites (inside view area sprites) of this group.

Constructor Detail

AdvanceSpriteGroup

public AdvanceSpriteGroup(String name,
                          int topOffset,
                          int leftOffset,
                          int bottomOffset,
                          int rightOffset)
Creates new AdvanceSpriteGroup with specified name, and specified screen offset on each side.


AdvanceSpriteGroup

public AdvanceSpriteGroup(String name,
                          int screenOffset)
Creates new AdvanceSpriteGroup with specified name, and specified screen offset.


AdvanceSpriteGroup

public AdvanceSpriteGroup(String name)
Creates new AdvanceSpriteGroup with specified name without screen offset (0, 0, 0, 0).

Method Detail

update

public void update(long elapsedTime)
Description copied from class: SpriteGroup
Updates all active sprites in this group, and check the schedule for removing inactive sprites.

Overrides:
update in class SpriteGroup
See Also:
SpriteGroup.getScanFrequence()

render

public void render(Graphics2D g)
Description copied from class: SpriteGroup
Renders all active sprites in this group. If this group is associated with a comparator, the group sprites is sort against the comparator first before rendered.

Overrides:
render in class SpriteGroup
See Also:
SpriteGroup.setComparator(Comparator)

setBackground

public void setBackground(Background backgr)
Description copied from class: SpriteGroup
Associates specified background with this sprite group, the background will be used by all sprites in this group.

Overrides:
setBackground in class SpriteGroup
See Also:
SpriteGroup.getBackground()

setComparator

public void setComparator(Comparator c)
Description copied from class: SpriteGroup
Sets comparator used for sorting sprites in this group. Specify null comparator for unsort order (the first sprite in the array will be rendered at the back of other sprite).

The comparator is used by Arrays.sort(java.lang.Object[], int, int, java.util.Comparator) to sort the sprites before rendering. For more information about how to make comparator, please read java.util.Comparator and java.util.Arrays#sort(). Example of sorting sprites based on y-axis :

    SpriteGroup ENEMY_GROUP;

    ENEMY_GROUP.setComparator(
       new Comparator() {
          public int compare(Object o1, Object o2) {
             Sprite s1 = (Sprite) o1,
                    s2 = (Sprite) o2;

             return (s1.getY() - s2.getY());
          }
       }
    };
 

Overrides:
setComparator in class SpriteGroup
Parameters:
c - the sprite comparator, null for unsort order
See Also:
Comparator, Arrays.sort(java.lang.Object[], int, int, java.util.Comparator)

getScreenOffset

public Insets getScreenOffset()
Returns screen offset of this group. Sprites that outside of screen bounds that still in this offset still categorized as on screen sprites.


getSprites

public Sprite[] getSprites()
Returns all on-screen sprites (active, inactive, and also null sprite) in this group.

Overrides:
getSprites in class SpriteGroup
See Also:
getSize()

getSize

public int getSize()
Returns total non-null on-screen sprites (active + inactive sprites) in this group.

Overrides:
getSize in class SpriteGroup
See Also:
SpriteGroup.getSprites()

getGroupSprites

public Sprite[] getGroupSprites()
Returns all sprites (active, inactive, and also null sprite) in this group.

See Also:
getGroupSize()

getGroupSize

public int getGroupSize()
Returns total non-null sprites (active + inactive) in this group.


GTGE API

Copyright © 2003-2005 Golden T Studios. All rights reserved. Use is subject to license terms.
GoldenStudios.or.id