📄 powerup.java
字号:
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public final class Powerup extends Sprite implements Actor{
/**
* invulnerable animation.
*/
public static final int INVULNERABLE=1;
/**
* Player's home
*/
public static final int HOME=2;
/**
* Player's home been destroyed.
*/
public static final int HOME_DESTROYED=3;
/**
* Tank symbol gives an extra life.
*/
public static final int TANK = 4;
/**
* Clock freezes all enemy tanks for a period of time.
*/
public static final int CLOCK = 5;
/**
* Shovel adds steel walls around the base for a period of time.
*/
public static final int SHOVEL = 6;
/**
* Bomb destroys all visible enemy tanks.
*/
public static final int BOMB = 7;
/**
* Star improves player's tank. maxium 8 grades.
*/
public static final int STAR = 8;
/**
* Shield makes player's tank invulnerable to attack for a period of time.
*/
public static final int SHIELD = 9;
/**
* Tank should know about the battle field.
*/
private static BattleField battleField;
/**
* Tank should know about the layer manager.
*/
private static LayerManager layerManager;
/**
* No image.
*/
private static final int NOTHING = 10;
/**
* maximun number of power ups in the battle field.
*/
private static final int POOL_SIZE = 10;
/**
* This pool store all powerups.
*/
private static Powerup POWERUP_POOL[];
/**
* time monitored to avoid the powerup flashes too fast.
*/
private long timeTaken = MILLIS_PER_TICK;
/**
* minimum time period between each flash
*/
private static final int MILLIS_PER_TICK = 50;
/**
* initial the powerup pool.
*/
static {
POWERUP_POOL = new Powerup[POOL_SIZE];
for(int i=0;i<POOL_SIZE;i++){
POWERUP_POOL[i]=new Powerup(NOTHING);
POWERUP_POOL[i].setVisible(false);
}
POWERUP_POOL[0].type=INVULNERABLE;
}
/**
* the type of the powerup
*/
private int type=NOTHING;
/**
* varible to toggle the powerup image to make it animation.
*/
private boolean showNextFrame=false;
/**
* the start time of the powup.
*/
private long startTime=0;
/**
* the poweup live time, default 3 minutes.
*/
private static long livePeriod=180000;
public void setType(int type){
this.type=type;
}
public int getType(){
return this.type;
}
private Powerup(int type)
{
super(ResourceManager.getInstance().getImage(ResourceManager.BONUS),
ResourceManager.TILE_WIDTH,ResourceManager.TILE_WIDTH);
defineReferencePixel(ResourceManager.TILE_WIDTH/8,
ResourceManager.TILE_WIDTH/8);
this.type=type;
}
public static boolean isHomeDestroyed()
{
boolean bRet=false;
for(int i=1;i<POOL_SIZE;i++){
if(POWERUP_POOL[i].isVisible() &&
POWERUP_POOL[i].getType()==Powerup.HOME_DESTROYED ){
bRet=true;
break;
}
}
return bRet;
}
public static boolean isHittingHome(Bullet bullet){
boolean bRet=false;
Powerup home=null;
for(int i=1;i<POOL_SIZE;i++){
if(POWERUP_POOL[i].isVisible() &&
POWERUP_POOL[i].getType()==Powerup.HOME ){
home=POWERUP_POOL[i];
}
}
if(home!=null){
bRet=bullet.collidesWith(home,false);
if(bRet){
home.setType(Powerup.HOME_DESTROYED);
}
}
return bRet;
}
public static void putNewPowerup(int type){
for(int i=1;i<POOL_SIZE;i++){
if(!POWERUP_POOL[i].isVisible()){
POWERUP_POOL[i].setType(type);
battleField.initPowerupPos(POWERUP_POOL[i]);
POWERUP_POOL[i].setVisible(true);
POWERUP_POOL[i].startTime=System.currentTimeMillis();
break;
}
}
}
public void tick(){
if(type==NOTHING || !isVisible()) return;
long tickTime = System.currentTimeMillis();
long refreshPeriod=MILLIS_PER_TICK;
//invulnerable powerup is controlled by the player tank.
if(type!=INVULNERABLE &&type!=HOME && type!=HOME_DESTROYED){
if(tickTime-startTime>livePeriod){
setFrame(NOTHING);
setVisible(false);
return;
}
}else{
refreshPeriod=MILLIS_PER_TICK/10;
}
if(timeTaken>=refreshPeriod){
showNextFrame=!showNextFrame;
if(type==INVULNERABLE){
if(showNextFrame){
setFrame(0);
}else{
setFrame(1);
}
}else if(type==HOME || type==HOME_DESTROYED){
setFrame(type);
}else{
if(showNextFrame){
setFrame(type);
}else{
setFrame(NOTHING);
}
}
timeTaken = System.currentTimeMillis() - tickTime;
} else{
timeTaken+=1;
}
}
public static void checkPlayerTank(PlayerTank tank){
Powerup powerup;
for(int i=1;i<POOL_SIZE;i++){
if(POWERUP_POOL[i].isVisible()){
powerup=POWERUP_POOL[i];
if(powerup.collidesWith(tank,false)){
tank.upgrade(powerup);
if(!(powerup.type==powerup.HOME ||
powerup.type==Powerup.HOME_DESTROYED)){
powerup.setVisible(false);
Score.show(tank.getX(),tank.getY(),Score.SCORE_500);
tank.addScore(500);
}
}
}
}
}
public static Powerup getInvulnerable(){
return POWERUP_POOL[0];
}
public static void removeAllPowerups() {
for (int i = 1; i < POOL_SIZE; ++i) {
Powerup powerup = POWERUP_POOL[i];
powerup.setVisible(false);
}
}
public static void setLayerManager(LayerManager manager) {
layerManager = manager;
if (layerManager != null){
for (int i = 0; i < POOL_SIZE; i++)
layerManager.append(POWERUP_POOL[i]);
}
}
public static void setBattleField(BattleField field) {
battleField = field;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -