mgdk
Class MMap

java.lang.Object
  |
  +--javax.microedition.lcdui.game.Layer
        |
        +--javax.microedition.lcdui.game.TiledLayer
              |
              +--mgdk.MMap

public class MMap
extends javax.microedition.lcdui.game.TiledLayer

Extends TiledLayer. Adds ability to load a map from a RecordSet or a resource file and to save it to a RecordSet.

See Also:
TiledLayer

Field Summary
 int clearColor
          Color used to clear the buffer.
 
Constructor Summary
MMap(int width, int height, java.lang.String imgName, int cellWidth, int cellHeight)
          Creates an empty map and loads the specified image.
 
Method Summary
protected static MMap create(byte[] data, java.lang.String imgName, int blockW, int blockH)
          Used internally to create a map from the supplied data.
 javax.microedition.lcdui.Image getBuffer()
          Returns the maps buffer image, which you for example can use to look up pixels.
 int getCell(int number)
          Get the tilenumber for the specified cell.
 int getCellNumber(int cellX, int cellY)
          Get cellnumber for cell at [cellX, cellY].
 int getCellNumberAtPixel(int screenX, int screenY)
          Get the cellnumber for the cell found at the specified screen coordinates (in pixels).
 int getPixelX(int number)
          Get the coordinate of the leftmost pixel in the cell with the specified number.
 int getPixelY(int number)
          Get the coordinate of the topmost pixel in the cell with the specified number.
static MMap loadFromFile(java.lang.String filename, java.lang.String imgName, int cellWidth, int cellHeight)
          Loads a map from a file located in /res.
static MMap loadFromRS(java.lang.String recName, java.lang.String imgName, int cellWidth, int cellHeight)
          Have to return a new MMap since you can not resize am existing one... ?
 void modifyCellValue(int number, int change)
          Change the value of the specified cell.
 void paint(javax.microedition.lcdui.Graphics g, int x, int y)
          Paint the map to the specified graphics object.
 void saveToRS(java.lang.String recName)
          Save this map in a RecordStore (permanent storage).
 void setCell(int number, int value)
          Sets the tilenumber of the specified cell.
 void updateBuffer(boolean clear)
          Updates the buffer image to show the maps current state.
 
Methods inherited from class javax.microedition.lcdui.game.TiledLayer
createAnimatedTile, fillCells, getAnimatedTile, getCell, getCellHeight, getCellWidth, getColumns, getRows, paint, setAnimatedTile, setCell, setStaticTileSet
 
Methods inherited from class javax.microedition.lcdui.game.Layer
getHeight, getWidth, getX, getY, isVisible, move, setPosition, setVisible
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

clearColor

public int clearColor
Color used to clear the buffer.

Constructor Detail

MMap

public MMap(int width,
            int height,
            java.lang.String imgName,
            int cellWidth,
            int cellHeight)
Creates an empty map and loads the specified image.

Parameters:
width - Map width in cells (number of columns)
height - Map height in cells (number of rows)
imgName - Filename of image used for cell tiles (pictures in the cells)
cellWidth - The width in pixels of each cell
cellHeight - The height in pixels of each cell
Method Detail

saveToRS

public void saveToRS(java.lang.String recName)
Save this map in a RecordStore (permanent storage).

Parameters:
recName - Name of the RecordStore used to save the map

create

protected static MMap create(byte[] data,
                             java.lang.String imgName,
                             int blockW,
                             int blockH)
Used internally to create a map from the supplied data.


loadFromRS

public static MMap loadFromRS(java.lang.String recName,
                              java.lang.String imgName,
                              int cellWidth,
                              int cellHeight)
Have to return a new MMap since you can not resize am existing one... ? Should the name of the image and tilesizes be included in the saved data??

Parameters:
recName - Name of the RecordStore where map data is stored
imgName - Filename of image used for cell tiles (pictures in the cells)
cellWidth - The width in pixels of each cell
cellHeight - The height in pixels of each cell

loadFromFile

public static MMap loadFromFile(java.lang.String filename,
                                java.lang.String imgName,
                                int cellWidth,
                                int cellHeight)
Loads a map from a file located in /res. File format: Byte 0 - Map Width (W) 1 byte Byte 1 - Map Height (H) 1 byte Byte 2 - Cell data W*H bytes

Parameters:
filename - resource file to load from, should be a binary file
imgName - Filename of image used for cell tiles (pictures in the cells)
cellWidth - The width in pixels of each cell
cellHeight - The height in pixels of each cell

updateBuffer

public void updateBuffer(boolean clear)
Updates the buffer image to show the maps current state. Buffer image is created at the first call.

Parameters:
clear - Fills the background with clearColor.

getBuffer

public javax.microedition.lcdui.Image getBuffer()
Returns the maps buffer image, which you for example can use to look up pixels.


paint

public void paint(javax.microedition.lcdui.Graphics g,
                  int x,
                  int y)
Paint the map to the specified graphics object. If the map uses a buffer it will be painted, otherwise the map is drawn directly. Use updateBuffer() to update the buffer :o) Cannot override paint since it is final *grr*


getCell

public int getCell(int number)
Get the tilenumber for the specified cell.

Parameters:
number - Cellnumber, converted to x,y.

getCellNumber

public int getCellNumber(int cellX,
                         int cellY)
Get cellnumber for cell at [cellX, cellY]. Cellnumbers start at 0 -> columns*rows.


getCellNumberAtPixel

public int getCellNumberAtPixel(int screenX,
                                int screenY)
Get the cellnumber for the cell found at the specified screen coordinates (in pixels).

Returns:
The cellnumber or -1 if outside map.

getPixelX

public int getPixelX(int number)
Get the coordinate of the leftmost pixel in the cell with the specified number. Can be used to place a sprite in a cell.

Parameters:
number - Cellnumber, translated to x, y.

getPixelY

public int getPixelY(int number)
Get the coordinate of the topmost pixel in the cell with the specified number. Can be used to place a sprite in a cell.

Parameters:
number - Cellnumber, translated to x, y.

setCell

public void setCell(int number,
                    int value)
Sets the tilenumber of the specified cell.

Parameters:
number - Cellnumber, translated into x, y.
value - Tilenumber.

modifyCellValue

public void modifyCellValue(int number,
                            int change)
Change the value of the specified cell.