📄 abstractitem.java
字号:
/*
* @(#)AbstractItem.java 1.0 04/07/01 @(#)
*
* Copyright 2004 NTT DoCoMo, Inc. All rights reserved.
*/
import com.nttdocomo.ui.Graphics;
import com.nttdocomo.ui.Image;
/**
* AbstractItem<BR>
* The base class of the various items which appear in the game.<br>
* Gives the means to access to the image and position of an item.
* <p>
* @version 1.0
* </p>
*/
public abstract class AbstractItem {
/** Image */
private Image image = null;
/** Pixcels */
private int[] pixels = null;
/** State of the item */
private int status;
/** Normal */
public static final int STATUS_NORMAL = 0;
/** Collides */
public static final int STATUS_CONTACT = 1;
/** Horizontal position */
private int posX;
/** Vertical position */
private int posY;
/** move control counter */
private int up_cnt = 0;
private int down_cnt = 0;
private int right_cnt = 0;
private int left_cnt = 0;
/** The move direction "top" */
public static final int DIRECTION_UP = 1;
/** The move direction "upper right" */
public static final int DIRECTION_UPPER_RIGHT = 2;
/** The move direction "right" */
public static final int DIRECTION_RIGHT = 3;
/** The move direction "lower right" */
public static final int DIRECTION_LOWER_RIGHT = 4;
/** The move direction "bottom" */
public static final int DIRECTION_DOWN = 5;
/** The move direction "lower left" */
public static final int DIRECTION_LOWER_LEFT = 6;
/** The move direction "left" */
public static final int DIRECTION_LEFT = 7;
/** The move direction "upper left" */
public static final int DIRECTION_UPPER_LEFT = 8;
/** The amount of movement of the item */
private static final int DEFAULT_AMOUNT = 1;
/** The frequency of movement of the item (minimum value) */
private static final int LOW_FREQUENCY = 16;
/** The frequency of movement of the item (base value) */
private static final int BASE_FREQUENCY = 20;
/** The frequency of movement of the item (shift value) */
private static final int UNIT_FREQUENCY = 2;
/** The type of item */
private int type;
/** PLAYER */
public static final int TYPE_PLAYER = 0;
/** GOAL LINE */
public static final int TYPE_GOAL_LINE = 1;
/** OBSTACLE */
public static final int TYPE_OBSTACLE = 2;
/** BONUS POINT or 1UP ITEM */
public static final int TYPE_ITEM = 3;
/**
* Constructor.
* @param _type The type of item
*/
public AbstractItem(int _type) {
this.type = _type;
this.status = STATUS_NORMAL;
}
/**
* Constructor.
* @param _type The type of item
* @param _imageId Image id
*/
public AbstractItem(int _type, int _imageId) {
this.type = _type;
this.status = STATUS_NORMAL;
setImage(_imageId);
}
/**
* Returns the type of this item.
* @return The type of this item
*/
public int getType() {
return this.type;
}
/**
* Sets the image of the item.
* @param _imageId Image id
*/
public void setImage(int _imageId) {
this.image = MediaCollection.getImage(_imageId);
}
/**
* Acquires the image of the item.
* @return Image object
*/
public Image getImage() {
return this.image;
}
/**
* Sets the position (horizontal) of the item.
* @param _posX Position
*/
public void setPosX(int _posX) {
this.posX = _posX;
}
/**
* Acquires the position (horizontal) of the item.
* @return Position
*/
public int getPosX() {
return this.posX;
}
/**
* Sets the position (vertical) of the item.
* @param _posY Position
*/
public void setPosY(int _posY) {
this.posY = _posY;
}
/**
* Acquires the position of the item.
* @return Position
*/
public int getPosY() {
return this.posY;
}
/**
* Sets the state of the item.
* @param _status The state of the item
*/
public void setStatus(int _status) {
this.status = _status;
}
/**
* Acquires the state of the item.
* @return The state of the item
*/
public int getStatus() {
return status;
}
/**
* Sets the color of a pixel.
* @param _pixels The color of a pixel
*/
public void setPixels(int[] _pixels) {
this.pixels = _pixels;
}
/**
* Moves the Item.
* @param stage number of current stage
*/
public void move(int stage) {
this.move(DIRECTION_DOWN, stage);
}
/**
* Moves the item in the specified direction.
* @param _direction The movement direction
* @param stage number of current stage
*/
public void move(int _direction, int stage) {
move(_direction,
LOW_FREQUENCY + (stage * UNIT_FREQUENCY),
DEFAULT_AMOUNT);
}
/**
* Moves the item to the specified direction.
* @param _direction The movement direction
* @param _frequency The movement frequency
* @param _amount The amount of movement
*/
public void move(int _direction, int _frequency, int _amount) {
if (DIRECTION_UP == _direction) {
/* If the movement direction is "top". */
up(_frequency, _amount);
}
else if (DIRECTION_DOWN == _direction) {
/* If the movement direction is "bottom". */
down(_frequency, _amount);
}
else if (DIRECTION_LEFT == _direction) {
/* If the movement direction is "left". */
left(_frequency, _amount);
}
else if (DIRECTION_RIGHT == _direction) {
/* If the movement direction is "right". */
right(_frequency, _amount);
}
else if (DIRECTION_UPPER_LEFT == _direction) {
/* If the movement direction is "upper left". */
left(_frequency, _amount);
up(_frequency, _amount);
}
else if (DIRECTION_LOWER_LEFT == _direction) {
/* If the move direction is "lower left". */
left(_frequency, _amount);
down(_frequency, _amount);
}
else if (DIRECTION_UPPER_RIGHT == _direction) {
/* If the move direction is "upper right". */
right(_frequency, _amount);
up(_frequency, _amount);
}
else if (DIRECTION_LOWER_RIGHT == _direction) {
/* If the move direction is "lower right". */
right(_frequency, _amount);
down(_frequency, _amount);
}
}
/**
* Moves the item to the right.
* @param _frequency The movement frequency
* @param _amount The amount of movement
*/
private void right(int _frequency, int _amount) {
right_cnt+=_frequency;
while (right_cnt >= BASE_FREQUENCY) {
this.posX+=_amount;
right_cnt-=BASE_FREQUENCY;
}
}
/**
* Moves the item to the left.
* @param _frequency The movement frequency
* @param _amount The amount of movement
*/
private void left(int _frequency, int _amount) {
left_cnt+=_frequency;
while (left_cnt >= BASE_FREQUENCY) {
this.posX-=_amount;
left_cnt-=BASE_FREQUENCY;
}
}
/**
* Moves the item upward.
* @param _frequency The movement frequency
* @param _amount The amount of movement
*/
private void up(int _frequency, int _amount) {
up_cnt+=_frequency;
while (up_cnt >= BASE_FREQUENCY) {
this.posY-=_amount;
up_cnt-=BASE_FREQUENCY;
}
}
/**
* Moves the item downward.
* @param _frequency The movement frequency
* @param _amount The amount of movement
*/
private void down(int _frequency, int _amount) {
down_cnt+=_frequency;
while (down_cnt >= BASE_FREQUENCY) {
this.posY+=_amount;
down_cnt-=BASE_FREQUENCY;
}
}
/**
* Confirms whether this item and Player have collided.
* <br>
* @param _player Player object
* @param _g Graphics object
* @return true:collided
*/
public boolean isContact(Player _player, Graphics _g) {
/* Already collided */
if (STATUS_CONTACT == getStatus()) {
return false;
}
/* The position of the player */
int playerPosX = _player.getPosX();
int playerPosY = _player.getPosY();
int playerRightEnd = playerPosX + _player.getImage().getWidth();
int playerBottom = playerPosY + _player.getImage().getHeight();
/* The position of this object */
int thisPosX = getPosX();
int thisPosY = getPosY();
int thisRightEnd = thisPosX + getImage().getWidth();
int thisBottom = thisPosY + getImage().getHeight();
boolean isOverlap = false;
/* The left of Player has overlapped. */
if (playerPosX >= thisPosX && playerPosX <= thisRightEnd &&
playerPosY <= thisPosY && playerBottom >= thisBottom) {
isOverlap = true;
}
/* The right of Player has overlapped. */
if (playerRightEnd >= thisPosX && playerRightEnd <= thisRightEnd &&
playerPosY <= thisPosY && playerBottom >= thisBottom) {
isOverlap = true;
}
/* The bottom of Player has overlapped. */
if (playerPosX <= thisPosX && playerRightEnd >= thisRightEnd &&
playerBottom >= thisPosY && playerBottom <= thisBottom) {
isOverlap = true;
}
/* The top of Player has overlapped. */
if (playerPosX <= thisPosX && playerRightEnd >= thisRightEnd &&
playerPosY >= thisPosY && playerPosY <= thisBottom) {
isOverlap = true;
}
/* The upper left of Player has overlapped. */
if (playerPosX >= thisPosX && playerPosX <= thisRightEnd &&
playerPosY >= thisPosY && playerPosY <= thisBottom) {
isOverlap = true;
}
/* The lower left of Player has overlapped. */
if (playerPosX >= thisPosX && playerPosX <= thisRightEnd &&
playerBottom >= thisPosY && playerBottom <= thisBottom) {
isOverlap = true;
}
/* The upper right of Player has overlapped. */
if (playerRightEnd >= thisPosX && playerRightEnd <= thisRightEnd &&
playerPosY >= thisPosY && playerPosY <= thisBottom) {
isOverlap = true;
}
/* The lower right of Player has overlapped. */
if (playerRightEnd >= thisPosX && playerRightEnd <= thisRightEnd &&
playerBottom >= thisPosY && playerBottom <= thisBottom) {
isOverlap = true;
}
/*
* If the Player and this object have overlapped,
* confirms whether the non-transparent part have overlapped.
*/
if (isOverlap) {
/* Get pixel color */
int[] _pixels = _g.getPixels(thisPosX,
thisPosY,
this.image.getWidth(),
this.image.getHeight(),
null,
0);
for (int i = 0; i < this.pixels.length; i++) {
/* If not a transparent color : Collided. */
if (Color.SILVER != this.pixels[i] &&
_pixels[i] != this.pixels[i])
{
contact();
return true;
}
}
}
return false;
}
/**
* Descript the proccess when this item collides with other items.
*/
public abstract void contact();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -