📄 mappy.java
字号:
* @see #setScreenDimensions(Rectangle)
* @see #getX()
* @see #getY()
**/
public void drawBackground(Graphics gfx, boolean blnTransparency) {
Block block = new Block();
drawLayer(gfx, block.BACKGROUND, blnTransparency);
}
/**
* Draws the Blocks' foreground of the current Map layer. <br>
* <br>
* If <code>intBlockLayer</code> is unknown then an error is logged (not
* thrown). <br>
* @param gfx the <code>Graphics</code> object that the Map is to drawn on.
* @param intBlockLayer the Block's foreground layer to be drawn.
* @see #drawBackground(Graphics, boolean)
* @see #setCurrentMapLayer(int)
* @see #setScreenDimensions(Rectangle)
* @see #getX()
* @see #getY()
**/
public void drawForeground(Graphics gfx, int intBlockLayer) {
Block block = new Block();
// validate block layer
if ((intBlockLayer < block.FOREGROUND1) || (intBlockLayer > block.FOREGROUND3)) {
log.error(new ArrayIndexOutOfBoundsException("drawForeground() - 'intBlockLayer' out of bounds: " + Integer.toString(intBlockLayer)));
return;
}
drawLayer(gfx, intBlockLayer, true);
}
/**
* Returns the <code>Block</code> used at the given <code>Block</code>
* coordinates, in the current Map layer. <br>
* <br>
* The returned <code>Block</code> is <b>not</b> independant of the Map,
* so any changes made to the block are immediatly reflected in the Map.
* Also remember that there is not one <code>Block</code> for each given
* coordinate but that each coordinate points to a <code>Block</code>.
* i.e. <code>Blocks</code> are reused throughout the Map. <br>
* <br>
* If any of the <code>Block</code> coordinates given are out of range
* then an error is logged (not thrown) and <code>null</code> returned. <br>
* <br>
* @param intBlockX;
* @return the <code>Block</code> used at the given coordinate.
* @see #setCurrentMapLayer(int)
* @see #getBlockX()
* @see #getBlockY()
**/
public Block getBlock(int intBlockX, int intBlockY) {
AnimStructure animBlock;
Block block;
int intBlockIndex;
int intIndex;
// validate input parameters
if ((intBlockX < 0) | (intBlockX >= m_intMapWidth)) {
log.error(new ArrayIndexOutOfBoundsException("getBlock() - 'intBlockX' is out of bounds: " + Integer.toString(intBlockX)));
return null;
}
if ((intBlockY < 0) | (intBlockY >= m_intMapHeight)) {
log.error(new ArrayIndexOutOfBoundsException("getBlock() - 'intBlockY' is out of bounds: " + Integer.toString(intBlockY)));
return null;
}
// grab the block index from the map
intBlockIndex = (int) m_shtTheMap[m_intCurrentMapLayer][intBlockY][intBlockX];
// is it an animation block?
if (intBlockIndex < 0) {
// if so, grab the current block in the anim sequence
animBlock = m_clsMapAniStrPt[-intBlockIndex - 1];
intBlockIndex = animBlock.anframelist[animBlock.ancurframe];
}
// grab the block
block = m_clsMapBlockStrPt[intBlockIndex];
// return Block
return block;
}
/**
* Puts the block structure value in the map at the given block coords.
* negative values are animations (-1 is the first anim).
* @param intBlockX;
* @param intBlockY;
* @param intIndex;
**/
public void setBlock(int intBlockX, int intBlockY, int intIndex) {
// validate input parameters
if ((intBlockX < 0) | (intBlockX >= m_intMapWidth)) {
log.error(new ArrayIndexOutOfBoundsException("setBlock() - 'intBlockX' is out of bounds: " + Integer.toString(intBlockX)));
return;
}
if ((intBlockY < 0) | (intBlockY >= m_intMapHeight)) {
log.error(new ArrayIndexOutOfBoundsException("setBlock() - 'intBlockY' is out of bounds: " + Integer.toString(intBlockY)));
return;
}
// set the block in the map
m_shtTheMap[m_intCurrentMapLayer][intBlockY][intBlockX] = (short) intIndex;
}
/**
* Performs a collision detection test at the given pixel coordinates
* using the <code>Block</code>'s collision flags. <br>
* <br>
* If any of the pixel coordinates are out of range of the map then
* <code>false</code> is automatically returned. <br>
* <br>
* @param intX the pixel X coordinate.
* @param intY the pixel Y coordinate.
**/
public boolean collisionDetection(int intX, int intY) {
int intPixelX, intPixelY;
int intBlockX, intBlockY;
int intCollisionFlag;
Block block;
intBlockX = intX / m_intBlockWidth;
intBlockY = intY / m_intBlockHeight;
intPixelX = intX % m_intBlockWidth;
intPixelY = intY % m_intBlockHeight;
block = getBlock(intBlockX, intBlockY);
// in case the sprite is off screen and tries to get a block that is off the map
if (block == null) return false;
if (intPixelX >= (m_intBlockWidth / 2)) {
if (intPixelY >= (m_intBlockHeight / 2)) {
intCollisionFlag = block.COLLISION_BOTTOM_RIGHT;
} else {
intCollisionFlag = block.COLLISION_TOP_RIGHT;
}
} else {
if (intPixelY >= (m_intBlockHeight / 2)) {
intCollisionFlag = block.COLLISION_BOTTOM_LEFT;
} else {
intCollisionFlag = block.COLLISION_TOP_LEFT;
}
}
return (block.getFlag(intCollisionFlag));
}
//______________________________________________________________________________
//------------------------------------------------------------------------------
// Public Classes
//______________________________________________________________________________
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
/**
* Mappy Block v1.10<br>
* <br>
* This class represents a <code>Block</code> as used by <code>Mappy</code>. <br>
* <br>
* A <code>Block</code> may contain up to 4 layers of images (1 background layer
* and 3 foreground layers), 7 fields of User data, quadratic collision information
* and 4 other flags. <br>
* <br>
* @version 1.10 (10 December 2001)
* @author <a href="mailto:slimer@alien-factory.co.uk">Slimer</a> of <a href="http://www.alien-factory.co.uk">Alien-Factory</a>.
**/
//------------------------------------------------------------------------------
public class Block {
/**
* A <code>Block</code> Image layer. <br>
* <br>
* @see #setImageIndex(int, int)
* @see #getImageIndex(int)
**/
public final int BACKGROUND = 0;
/**
* A <code>Block</code> Image layer. <br>
* <br>
* @see #setImageIndex(int, int)
* @see #getImageIndex(int)
**/
public final int FOREGROUND1 = 1;
/**
* A <code>Block</code> Image layer. <br>
* <br>
* @see #setImageIndex(int, int)
* @see #getImageIndex(int)
**/
public final int FOREGROUND2 = 2;
/**
* A <code>Block</code> Image layer. <br>
* <br>
* @see #setImageIndex(int, int)
* @see #getImageIndex(int)
**/
public final int FOREGROUND3 = 3;
/**
* A minimum index for User Data. <br>
* <br>
* @see #setUserData(int, int)
* @see #getUserData(int)
**/
public final int MIN_USER_DATA = 1;
/**
* A minimum index for User Data. <br>
* <br>
* @see #setUserData(int, int)
* @see #getUserData(int)
**/
public final int MAX_USER_DATA = 7;
/**
* A flag used in collision detection. <br>
* <br>
* @see #setFlag(int, boolean)
* @see #getFlag(int)
**/
public final int COLLISION_TOP_LEFT = 0;
/**
* A flag used in collision detection. <br>
* <br>
* @see #setFlag(int, boolean)
* @see #getFlag(int)
**/
public final int COLLISION_TOP_RIGHT = 1;
/**
* A flag used in collision detection. <br>
* <br>
* @see #setFlag(int, boolean)
* @see #getFlag(int)
**/
public final int COLLISION_BOTTOM_LEFT = 2;
/**
* A flag used in collision detection. <br>
* <br>
* @see #setFlag(int, boolean)
* @see #getFlag(int)
**/
public final int COLLISION_BOTTOM_RIGHT = 3;
/**
* A User defined flag. <br>
* <br>
* @see #setFlag(int, boolean)
* @see #getFlag(int)
**/
public final int TRIGGER = 4;
/**
* A User defined flag. <br>
* <br>
* @see #setFlag(int, boolean)
* @see #getFlag(int)
**/
public final int UNUSED1 = 5;
/**
* A User defined flag. <br>
* <br>
* @see #setFlag(int, boolean)
* @see #getFlag(int)
**/
public final int UNUSED2 = 6;
/**
* A User defined flag. <br>
* <br>
* @see #setFlag(int, boolean)
* @see #getFlag(int)
**/
public final int UNUSED3 = 7;
private int m_intImageIndex[];
private int m_intUserData[];
private boolean m_blnFlags[];
//__________________________________________________________________________
//--------------------------------------------------------------------------
// Constructors
//__________________________________________________________________________
//--------------------------------------------------------------------------
/**
* Only <code>Mappy</code>can contruct Block classes. <br>
**/
protected Block() {
m_intImageIndex = new int[4];
m_intUserData = new int[7];
m_blnFlags = new boolean[8];
}
//__________________________________________________________________________
//--------------------------------------------------------------------------
// Public & Private Methods (Hmm, I'm tempted to make the setters private!)
//__________________________________________________________________________
//--------------------------------------------------------------------------
/**
* Sets the Image of the given <code>Block</code> layer. <br>
* <br>
* If either the Image layer or Image index is out of bounds then an
* error is logged (not thrown) and the method aborted. <br>
* <br>
* @param intImageLayer the layer who's image is to be updated.
* @param intNewImageIndex the index of the new Image.
* @see #getImageIndex(int)
* @see #BACKGROUND
* @see #FOREGROUND1
* @see #FOREGROUND2
* @see #FOREGROUND3
**/
public void setImageIndex(int intImageLayer, int intNewImageIndex) {
// validate array index
if ((intImageLayer < BACKGROUND) || (intImageLayer > FOREGROUND3)) {
log.error(new ArrayIndexOutOfBoundsException("setImageIndex() - 'intImageLayer' out of bounds: '" + Integer.toString(intImageLayer) + "'."));
return;
}
// validate Image Index
if ((intNewImageIndex < 0) || (intNewImageIndex >= m_intNoOfImages)) {
log.error(new NullPointerException("setImageIndex() - 'intNewImageIndex' isn't a valid Image Index: '" + Integer.toString(intNewImageIndex) + "'."));
return;
}
// set image index
m_intImageIndex[intImageLayer] = intNewImageIndex;
}
/**
* Returns the Image index of the given <code>Block</code> layer. <br>
* <br>
* If the Image layer is out of bounds then an error is logged (not
* thrown) and the method aborted. <br>
* <br>
* @param intImageLayer the <code>Block</code> layer who's Image
* index is to be returned.
* @return the Image index of the given <code>Block</code> layer.
* @see #setImageIndex(int, int)
* @see #BACKGROUND
* @see #FOREGROUND1
* @see #FOREGROUND2
* @see #FOREGROUND3
**/
public int getImageIndex(int intImageLayer) {
// validate array index
if ((intImageLayer < BACKGROUND) || (intImageLayer > FOREGROUND3)) {
log.error(new ArrayIndexOutOfBoundsException("getImageIndex() - 'intImageLayer' out of bounds: '" + Integer.toString(intImageLayer) + "'."));
return 0;
}
// return image index
return (m_intImageIndex[intImageLayer]);
}
/**
* Sets the specified User Data. <br>
* <br>
* If <code>intIndex</code> is out of bounds then an error is logged
* (not thrown) and the method aborted. <br>
* <br>
* @param intIndex the User Data to be updated.
* @param intNewUserData the new User Data.
* @see #getUserData(int)
* @see #MIN_USER_DATA
* @see #MAX_USER_DATA
**/
public void setUserData(int intIndex, int intNewUserData) {
// validate array in
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -