GTGE API

com.golden.gamedev.object.background.abstraction
Class AbstractTileBackground

java.lang.Object
  extended by com.golden.gamedev.object.Background
      extended by com.golden.gamedev.object.background.abstraction.AbstractTileBackground
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
TileBackground

public abstract class AbstractTileBackground
extends Background

The base abstract class to create tiling background, the subclass need to perform the background tile rendering.

See Also:
renderTile(Graphics2D, int, int, int, int), Serialized Form

Field Summary
 
Fields inherited from class com.golden.gamedev.object.Background
screen, x, y
 
Constructor Summary
AbstractTileBackground(int horiz, int vert, int tileWidth, int tileHeight)
          Creates new AbstractTileBackground as big as horiz, vert tiles, where each tile is as big as tileWidth, tileHeight.
 
Method Summary
 int getOffsetX()
          Returns current background tile offset-x.
 int getOffsetY()
          Returns current background tile offset-y.
 Point getTileAt(double screenX, double screenY)
          Returns background tile position of specified coordinate or null if the coordinate is out of background viewport/boundary.
 int getTileHeight()
          Returns the height of the tile.
 int getTileWidth()
          Returns the width of the tile.
 int getTileX()
          Returns current tile-x position.
 int getTileY()
          Returns current tile-y position.
 int getTotalHorizontalTiles()
          Returns background total horizontal tiles.
 int getTotalVerticalTiles()
          Returns background total vertical tiles.
 void render(Graphics2D g, int xbg, int ybg, int x, int y, int w, int h)
          Renders background from specified position and clipping area to specified graphics context.
abstract  void renderTile(Graphics2D g, int tileX, int tileY, int x, int y)
          Renders tile at tileX, tileY position to specified x, y coordinate.
 void setLocation(double xb, double yb)
          Sets background location to specified coordinate.
 void setSize(int horiz, int vert)
          Sets the size of this background (in tiles).
 void setTileLocation(int xs, int ys)
          Sets the background location to specified tile.
protected  void setTileSize(int tileWidth, int tileHeight)
          Sets the background tile size.
 
Methods inherited from class com.golden.gamedev.object.Background
getClip, getDefaultBackground, getHeight, getWidth, getX, getY, move, render, setClip, setClip, setToCenter, setToCenter, update
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractTileBackground

public AbstractTileBackground(int horiz,
                              int vert,
                              int tileWidth,
                              int tileHeight)
Creates new AbstractTileBackground as big as horiz, vert tiles, where each tile is as big as tileWidth, tileHeight.

The actual dimension of the background is :
width = horiz*tileWidth, height = vert*tileHeight.

Method Detail

render

public void render(Graphics2D g,
                   int xbg,
                   int ybg,
                   int x,
                   int y,
                   int w,
                   int h)
Description copied from class: Background
Renders background from specified position and clipping area to specified graphics context.

This method to simplify background subclass rendering, the subclass only need to render the background from specified x, y coordinate with specified clipping area.

For example:

    Background backgr;
    Graphics2D g;

    backgr.render(g, 100, 100,
                  5, 10, 100, 200);
 
Means the background must render itself from background coordinate 100, 100 to specified graphics context, starting from 5, 10 screen pixel as large as 100 x 200 dimension.

Overrides:
render in class Background
Parameters:
g - graphics context
xbg - background x-coordinate
ybg - background y-coordinate
x - screen start x clipping
y - screen start y clipping
w - clipping width
h - clipping height

renderTile

public abstract void renderTile(Graphics2D g,
                                int tileX,
                                int tileY,
                                int x,
                                int y)
Renders tile at tileX, tileY position to specified x, y coordinate.


setLocation

public void setLocation(double xb,
                        double yb)
Description copied from class: Background
Sets background location to specified coordinate. The location is bounded to background boundary (0, 0, Background.getWidth(), Background.getHeight()).

Overrides:
setLocation in class Background
Parameters:
xb - the x coordinate of the background
yb - the y coordinate of the background

setTileLocation

public void setTileLocation(int xs,
                            int ys)
Sets the background location to specified tile.


getTileX

public int getTileX()
Returns current tile-x position.


getTileY

public int getTileY()
Returns current tile-y position.


getTileAt

public Point getTileAt(double screenX,
                       double screenY)
Returns background tile position of specified coordinate or null if the coordinate is out of background viewport/boundary.

Used to detect mouse position on this background, for example :
Drawing rectangle at mouse cursor position

    public class YourGame extends Game {

       AbstractTileBackground bg;

       public void render(Graphics2D g) {
          Point tileAt = bg.getTileAt(getMouseX(), getMouseY());

          if (tileAt != null) {
             // mouse cursor is in background area
             // draw pointed tile

             // convert tile to coordinate
             int posX = (tileAt.x - bg.getTileX()) * bg.getTileWidth(),
                 posY = (tileAt.y - bg.getTileY()) * bg.getTileHeight();

             g.setColor(Color.WHITE);
             g.drawRect(posX - bg.getOffsetX() + bg.getClip().x,
                        posY - bg.getOffsetY() + bg.getClip().y,
                        bg.getTileWidth(), bg.getTileHeight());
          }
       }

    }
 


getOffsetX

public int getOffsetX()
Returns current background tile offset-x.

This offset value that makes smooth scrolling on the background tile.


getOffsetY

public int getOffsetY()
Returns current background tile offset-y.

This offset value that makes smooth scrolling on the background tile.


getTileWidth

public int getTileWidth()
Returns the width of the tile.


getTileHeight

public int getTileHeight()
Returns the height of the tile.


setTileSize

protected void setTileSize(int tileWidth,
                           int tileHeight)
Sets the background tile size.


getTotalHorizontalTiles

public int getTotalHorizontalTiles()
Returns background total horizontal tiles.


getTotalVerticalTiles

public int getTotalVerticalTiles()
Returns background total vertical tiles.


setSize

public void setSize(int horiz,
                    int vert)
Sets the size of this background (in tiles).

Overrides:
setSize in class Background
Parameters:
horiz - total background horizontal tiles
vert - total background vertical tiles

GTGE API

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