android.gameengine.icadroids.renderer
Class Viewport

java.lang.Object
  extended by android.gameengine.icadroids.renderer.Viewport

public class Viewport
extends java.lang.Object

The viewport follows a player object. When you use the viewport, make sure you have add a gameplayer to the game! Viewport is a Singleton, that means that if you want to use methods from it, you must make a new object like 'Viewport vp = Viewport.getInstance()'. To activate the viewport, you must set useViewPort to 'true'.

Author:
Leon & Lex

Field Summary
static int PLAYER_BOTTOM
          The constant for placing the player on the bottom side of the screen
static int PLAYER_FIXED
          The constant for fixing the viewport, it won't adjust to player
static int PLAYER_HCENTER
          The constant for placing the player horizontally centered
static int PLAYER_LEFT
          The constant for placing the player on the left side of the screen
static int PLAYER_RIGHT
          The constant for placing the player on the right side of the screen
static int PLAYER_TOP
          The constant for placing the player on the top side of the screen
static int PLAYER_VCENTER
          The constant for placing the player vertically centered
protected  int screenHeight
           
protected  int screenWidth
           
static boolean useViewport
          If you want to make use of the viewport, set useViewport on true
protected  float zoomFactor
          The amount of zooming for the viewport. 1.0 is the standard view, lower than 1.0 will cause the viewport to zoom out, higher than 1.0 will cause the viewport to zoom in.
 
Method Summary
static Viewport getInstance()
          Returns an instance of the private Viewport class.
 int getMaxX()
          Gets right edge of the game world.
 int getMaxY()
          Gets bottom edge of the game world.
 int getMinX()
          Gets left edge of the game world.
 int getMinY()
          Gets top edge of the game world.
 int getRandomX(int width)
          Get random x between left edge of world and right edge minus the specified width.
 int getRandomY(int height)
          Get random y between top edge of world and bottom edge minus the specified height.
 Point getViewportLocation()
          Gets the position of the viewport.
 int getViewportX()
          Gets the x-position of the viewport.
 int getViewportY()
          Gets the y-position of the viewport.
 float getZoomFactor()
          Gets the zoomfactor for zooming in.
 boolean isInViewport(GameObject item)
          Check if the given gameitem lies within the viewport
static int random(int range)
          Generates a random integer between 0 (zero) and the given range, not including range.
 void setBounds(int minX, int minY, int maxX, int maxY)
          Set the boundaries of this game.
 void setPlayer(MoveableGameObject player)
          Sets the object that will be the player character
 void setPlayerPositionOnScreen(int pos)
          Sets the position of the player on the screen.
 void setPlayerPositionTolerance(double ht, double vt)
          Set the tolerance of the positioning.
 void setViewport(int x, int y)
          Set the position of the viewport manually.
protected  void setViewportLimits()
          This method sets the viewport limits.
 void setZoomFactor(float zoomFactor)
          Sets the zoomfactor for the viewport.
 void update()
          This method centers the player in the middle of the viewport
protected  void updateViewportFirstTime()
          This method sets the viewport for the first time.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

useViewport

public static boolean useViewport
If you want to make use of the viewport, set useViewport on true


PLAYER_HCENTER

public static final int PLAYER_HCENTER
The constant for placing the player horizontally centered

See Also:
Constant Field Values

PLAYER_VCENTER

public static final int PLAYER_VCENTER
The constant for placing the player vertically centered

See Also:
Constant Field Values

PLAYER_LEFT

public static final int PLAYER_LEFT
The constant for placing the player on the left side of the screen

See Also:
Constant Field Values

PLAYER_RIGHT

public static final int PLAYER_RIGHT
The constant for placing the player on the right side of the screen

See Also:
Constant Field Values

PLAYER_TOP

public static final int PLAYER_TOP
The constant for placing the player on the top side of the screen

See Also:
Constant Field Values

PLAYER_BOTTOM

public static final int PLAYER_BOTTOM
The constant for placing the player on the bottom side of the screen

See Also:
Constant Field Values

PLAYER_FIXED

public static final int PLAYER_FIXED
The constant for fixing the viewport, it won't adjust to player

See Also:
Constant Field Values

screenWidth

protected int screenWidth

screenHeight

protected int screenHeight

zoomFactor

protected float zoomFactor
The amount of zooming for the viewport. 1.0 is the standard view, lower than 1.0 will cause the viewport to zoom out, higher than 1.0 will cause the viewport to zoom in.

Method Detail

setPlayer

public void setPlayer(MoveableGameObject player)
Sets the object that will be the player character

Parameters:
player - The object that will act as a player

getInstance

public static Viewport getInstance()
Returns an instance of the private Viewport class.

Returns:
An instance of the Viewport class

setViewportLimits

protected final void setViewportLimits()
This method sets the viewport limits.


updateViewportFirstTime

protected void updateViewportFirstTime()
This method sets the viewport for the first time.


update

public void update()
This method centers the player in the middle of the viewport


random

public static final int random(int range)
Generates a random integer between 0 (zero) and the given range, not including range.

Parameters:
range - the maximum number that may be returned
Returns:
a pseudo-random integer

isInViewport

public final boolean isInViewport(GameObject item)
Check if the given gameitem lies within the viewport

Parameters:
item - the gameitem that needs to be checked
Returns:
true, if it lies in the viewport, false otherwise

setPlayerPositionOnScreen

public final void setPlayerPositionOnScreen(int pos)
Sets the position of the player on the screen. In many games, the game world is bigger than the screen. When you specify the player position, you can make the screen (viewport) move along with the player. The parameter tells how this must be done. For example, you can specify HCENTER or VCENTER, if you want to keep the player more or less in the middle of the viewport, or BOTTOM if you want the player in the bottom of the screen.
Note: if you position the player at one of the edges of the screen, like BOTTOM, this means that you can not move out of the screen that way, but you can move up a bit. How much that is, is specified by setPlayerPositionTolerance.

Parameters:
pos - one of the following combinations:
  • PLAYER_FIXED
    Viewport won't move. If your world is bigger than the screen, the player can move out of sight on all sides!

  • PLAYER_TOP, PLAYER_VCENTER, PLAYER_BOTTOM
    Player is at top, center or bottom of viewport. Horizontal positioning is not controled, you can move out of view at the sides!

  • PLAYER_LEFT, PLAYER_HCENTER, PLAYER_RIGHT
    Player is at left, center or right of viewport. Vertical positioning is not controled, you can move out of view at the top or bottom!

  • PLAYER_TOP | PLAYER_LEFT, PLAYER_TOP | PLAYER_HCENTER, PLAYER_TOP | PLAYER_RIGHT
    PLAYER_VCENTER | PLAYER_LEFT, PLAYER_VCENTER | PLAYER_HCENTER, PLAYER_VCENTER | PLAYER_RIGHT
    PLAYER_BOTTOM | PLAYER_LEFT, PLAYER_BOTTOM | PLAYER_HCENTER, PLAYER_BOTTOM | PLAYER_RIGHT
    Combinations of vertical and horizontal positioning. The following image illustrates this type of positioning:


setPlayerPositionTolerance

public final void setPlayerPositionTolerance(double ht,
                                             double vt)
Set the tolerance of the positioning. When tolerance is zero, the viewport moves immediately when the player moves. When tolerance is 1, you can move to the edge of the screen before the viewport moves. Values in between result in a smaller or bigger delay before the viewport moves.
Example: In a left-to right platform game, you may position the player at LEFT, VCENTER. If you set the horizontal tolerance at 0.3, you may move to the right 30% of the screen before the viewport moves along. If you set vertical tolerance at 0.8, you can move 80% of the way up, before the viewport moves up also.

Parameters:
ht - horizontal tolerance, a value between 0 and 1
vt - vertical tolerance, a value between 0 and 1

setBounds

public void setBounds(int minX,
                      int minY,
                      int maxX,
                      int maxY)
Set the boundaries of this game. If you want the game world to be as big as the screen, you need not use this method. By default the bounds will be set to the size of the screen. That is: minX=0, maxX=screenWidth, minY=0, maxY=screenHeight.

Parameters:
minX - the minimal horizontal coordinate
minY - the minimal vertical coordinate
maxX - the maximal horizontal coordinate
maxY - the maximal vertical coordinate

getMinX

public int getMinX()
Gets left edge of the game world.

Returns:
int x-position of left edge, as specified by setBounds.

getMaxX

public int getMaxX()
Gets right edge of the game world.

Returns:
int, x-position of right edge, as specified by setBounds.

getMinY

public int getMinY()
Gets top edge of the game world.

Returns:
int y-position of top edge, as specified by setBounds.

getMaxY

public int getMaxY()
Gets bottom edge of the game world.

Returns:
int y-position of bottom edge, as specified by setBounds.

getRandomX

public int getRandomX(int width)
Get random x between left edge of world and right edge minus the specified width. In this way you can find a random position for an item of the given width and the item will fit completely into the world.

Parameters:
width -
Returns:
a random x-position

getRandomY

public int getRandomY(int height)
Get random y between top edge of world and bottom edge minus the specified height. In this way you can find a random position for an item of the given height and the item will fit completely into the world.

Parameters:
height -
Returns:
a random y-position

setViewport

public void setViewport(int x,
                        int y)
Set the position of the viewport manually. You must do this if you set the player position in viewport to FIXED. If it is not fixed, it is no use setting the viewport, because the viewport will adjust to the player position.

Parameters:
x - xpos of viewport
y - ypos of viewport

getViewportX

public int getViewportX()
Gets the x-position of the viewport.

Returns:
x position of the viewport

getViewportY

public int getViewportY()
Gets the y-position of the viewport.

Returns:
y position of the viewport

getViewportLocation

public Point getViewportLocation()
Gets the position of the viewport.

Returns:
a Point-object containing the x- and y-position of the viewport

getZoomFactor

public float getZoomFactor()
Gets the zoomfactor for zooming in.

Returns:
the value of the zoomfactor

setZoomFactor

public void setZoomFactor(float zoomFactor)
Sets the zoomfactor for the viewport. A higher zoomfactor will zoom the viewport in further.

Parameters:
zoomFactor - the zoomfactor for the viewport.