A Sprite on a Tile


The Tile Occupier

A tile occupier has a unique name, a type value (BLOCK, PICKUP, or SPRITE), a tile coordinate (xTile, yTile), and a coordinate relative to the top-left corner of the floor image (xDraw, yDraw), where the occupier's image should be drawn. The relationship between these coordinates is shown in Figure 13-11.

Figure 13-11. Positioning a tile occupier in a tile


xDraw and yDraw are relative to the floor image, so floor offsets must be added to them before the image is drawn into the JPanel. The constructor initializes the coordinate details and calls calcPosition( ) to calculate xDraw and yDraw:

     // globals     private String name;     private int type;      // BLOCK, PICKUP, or SPRITE     private BufferedImage image;     private int xTile, yTile;    // tile coordinate     private int xDraw, yDraw;              // coordinate relative to the floor image where the tile              // occupier should be drawn     private TiledSprite sprite = null;       // used when the TileOccupier is a sprite     public TileOccupier(String nm, int ty, int x, int y,               BufferedImage im, int xRowStart, int yRowStart,                                 int xTileWidth, int yTileHeight)     { name = nm;       type = ty;       xTile = x; yTile = y;       image = im;       calcPosition(xRowStart, yRowStart, xTileWidth, yTileHeight);     }

If this object is in an even row, then xRowStart and yRowStart will hold the pixel location of the first even row; otherwise, the location of the first odd row is used. The (x, y) arguments give the tile's location.

calcPosition( ) calculates the (xDraw, yDraw) coordinate relative to the floor image:

     private void calcPosition(int xRowStart, int yRowStart,                             int xTileWidth, int yTileHeight)     {       // top-left corner of image relative to its tile       int xImOffset = xTileWidth/2 - image.getWidth( )/2;   // in middle       int yImOffset = yTileHeight - image.getHeight( ) - yTileHeight/5;                      // up a little from bottom point of the diamond       // top-left corner of image relative to floor image       xDraw = xRowStart + (xTile * xTileWidth) + xImOffset;       if (yTile%2 == 0)    // on an even row         yDraw = yRowStart + (yTile/2 * yTileHeight) + yImOffset;       else       // on an odd row         yDraw = yRowStart + ((yTile-1)/2 * yTileHeight) + yImOffset;     }

The (xDraw, yDraw) coordinate will cause the TileOccupier's image to be rendered so its base appears to be resting on the tile, centered in the x-direction, and a little forward of the middle in the y-direction.

Additional Sprite Information

When a TileOccupier object is created for a sprite, the addSpriteRef( ) method is called to store a reference to the sprite:

     public void addSpriteRef(TiledSprite s)     { if (type == WorldDisplay.SPRITE)         sprite = s;     }

addSpriteRef( ) is used by the draw( ) method, as explained below.


Drawing a Tile Occupier

When the draw( ) method is called, the (xDraw, yDraw) coordinate relative to the floor image is known. Now the x-and y-offsets of the floor image relative to the JPanel must be added to get the image's position in the JPanel.

One complication is drawing a sprite. A sprite may be animated and will be represented by several images, so which one should be drawn? The task is delegated to the sprite, by calling its draw( ) method:

     public void draw(Graphics g, int xOffset, int yOffset)     {       if (type == WorldDisplay.SPRITE) {         sprite.setPosition( xDraw+xOffset, yDraw+yOffset);                                 // set its position in the JPanel         sprite.drawSprite(g);   // let the sprite do the drawing       }       else    // the entity is a PICKUP or BLOCK         g.drawImage( image, xDraw+xOffset, yDraw+yOffset, null);     }

Prior to the draw, the sprite's pixel position must be set.


draw( ) in TileOccupier is the only place where the pixel coordinates maintained by the Sprite class are manipulated. Tile coordinates, held in the TiledSprite subclass, are utilized in the rest of AlienTiles.



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