The Tile Occupier


Managing WorldItems

WorldItems maintains an ArrayList of TileOccupier objects (called items) ordered by increasing tile row. Figure 13-10 shows that row 0 is the row furthest back in the game, and the last row is nearest the front. When the ArrayList objects are drawn, the ones in the rows further back will be drawn first, matching the intended z-ordering of the rows.

A TileOccupier object can represent a block, pickup, or sprite.


The ArrayList changes over time. The most frequent change is to add sprites temporarily, so they can be drawn in their correct positions relative to the blocks and pickups. Pickups are deleted as they are collected by the player.

The WorldItems constructor stores floor information. This is used to translate the tile coordinates of the TileOccupiers into pixel locations on the floor:

     // max pixel width/height of a tile     private int tileWidth, tileHeight;     // 'start of first even row' coordinate     private int evenRowX, evenRowY;     // 'start of first odd row' coordinate     private int oddRowX, oddRowY;     private ArrayList items;            // a row-ordered list of TileOccupier objects     public WorldItems(int w, int h, int erX, int erY, int orX, int orY)     { tileWidth = w; tileHeight = h;       evenRowX = erX; evenRowY = erY;       oddRowX = orX; oddRowY = orY;       items = new ArrayList( );     }

Adding an Entity

Adding an entity (a pickup or a block) requires the creation of a TileOccupier object and its placement in the items ArrayList sorted by its row/column position:

     public void addItem(String name, int type, int x, int y,                                              BufferedImage im)     { TileOccupier toc;       if (y%2 == 0) // even row         toc = new TileOccupier(name, type, x, y, im,                                       evenRowX, evenRowY,                                       tileWidth, tileHeight);       else         toc = new TileOccupier(name, type, x, y, im,                                       oddRowX, oddRowY,                                       tileWidth, tileHeight);       rowInsert(toc, x, y);     }

Each TileOccupier object must calculate its pixel location on the floor, which requires the tile coordinate of the occupier (x, y), the dimensions of a tile (tileWidth and tileHeight), and the start coordinate of the first even or odd row. If the TileOccupier is positioned on an even row (i.e., y%2 = 0), then it's passed to the even row coordinate; if not, it is passed to the odd coordinate.

addItem( ) only deals with blocks or pickups, so the type argument will be BLOCK or PICKUP. The creation of a SPRITE entity is handled by a separate method, posnSprite( ), which is similar to addItem( ). posnSprite( ) adds a sprite reference to the information in the TileOccupier object. rowInsert( ) inserts the TileOccupier object into the ArrayList in increasing row order. Within a row, the objects are ordered by increasing column position.

Drawing Entities

WorldDisplay's draw( ) displays all the entities using a z-ordering that draws the rows further back first. Since the TileOccupier objects are stored in the ArrayList in increasing row order, this is achieved by cycling through them from start to finish:

     public void draw(Graphics g, int xOffset, int yOffset)     {       TileOccupier item;       for(int i = 0; i < items.size( ); i++) {         item = (TileOccupier) items.get(i);         item.draw(g, xOffset, yOffset);   // draw the item       }     }

The TileOccupier draw( ) call is passed the x-and y-offsets of the floor image from the JPanel's top-left corner. They are used to draw the entity offset by the same amount as the floor.

Pickup Methods

WorldItems contains several pickup-related methods. They all employ a similar algorithm, involving a loop through the items list looking for a specified pickup. Then a method is called upon the located TileOccupier object instance.

As a concrete example, I'll consider the implementation of nearestPickup( ). It's supplied with a tile coordinate and returns the coordinate of the nearest pickup:

     public Point nearestPickup(Point pt)     {       double minDist = 1000000;   // dummy large value (a hack)       Point minPoint = null;       double dist;       TileOccupier item;       for(int i=0; i < items.size( ); i++) {         item = (TileOccupier) items.get(i);         if (item.getType( ) == WorldDisplay.PICKUP) {           dist = pt.distanceSq( item.getTileLoc( ) );                                     // get squared dist. to pickup           if (dist < minDist) {             minDist = dist;                // store smallest dist             minPoint = item.getTileLoc( );  // store associated pt           }         }       }       return minPoint;     }  // end of nearestPickup( )

The pickups are found by searching for the PICKUP type. The square of the distance between the input point and a pickup is calculated, thereby avoiding negative lengths, and the current minimum distance and the associated pickup point is stored.



Killer Game Programming in Java
Killer Game Programming in Java
ISBN: 0596007302
EAN: 2147483647
Year: 2006
Pages: 340

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net