?? playertank.java
字號:
/**
* Player tanks handle key release event.
*/
public void keyReleased(int gameAction){
keyPressed=false;
switch (gameAction) {
case GameCanvas.UP:
if(direction == BattleField.NORTH){
direction = BattleField.NONE;
break;
}
case GameCanvas.DOWN:
if(direction == BattleField.SOUTH){
direction = BattleField.NONE;
break;
}
case GameCanvas.LEFT:
if(direction == BattleField.WEST){
direction = BattleField.NONE;
break;
}
case GameCanvas.RIGHT:
if(direction == BattleField.EAST){
direction = BattleField.NONE;
break;
}
case GameCanvas.FIRE:
shoot = false;
}
}
////////////////////////////////////////////////////////////////////////////
//--------------------------------- REVISIONS ------------------------------
// Date Name Tracking # Description
// --------- ------------------- ------------- ----------------------
// 16JAN2008 James Shen Initial Creation
////////////////////////////////////////////////////////////////////////////
/**
* Upgrade player's tank
*/
public void upgrade(Powerup powerup){
switch(powerup.getType()){
case Powerup.CLOCK:
EnemyTank.immobilizedStartTime=System.currentTimeMillis();
break;
case Powerup.STAR:
grade++;
if(grade>MAX_GRADE ) grade=MAX_GRADE;
switch(grade){
case GRADE_SPEED:
{
speed*=2;
int x=getX();
int y=getY();
x=(x / speed) *speed;
y=(y / speed) *speed;
setPosition(x,y);
}
break;
case GRADE_TWO_BULLETS:
avaiableBullets=2;
break;
case GRADE_THREE_BULLETS:
avaiableBullets=3;
break;
}
break;
case Powerup.SHIELD:
invulnerableTime=System.currentTimeMillis();
invulnerabilityTicks=invulnerablePeriod;
sheild.setPosition(getX(),getY());
sheild.setVisible(true);
break;
case Powerup.BOMB:
EnemyTank.explodeAllEmenies();
break;
case Powerup.TANK:
avaiableLife++;
break;
case Powerup.SHOVEL:
battleField.makeHomeConcreteWall();
break;
}
}
////////////////////////////////////////////////////////////////////////////
//--------------------------------- REVISIONS ------------------------------
// Date Name Tracking # Description
// --------- ------------------- ------------- ----------------------
// 16JAN2008 James Shen Initial Creation
////////////////////////////////////////////////////////////////////////////
/**
* set player avaiable lives.
* @param live new life count.
*/
public void setAvaiableLives(int live){
avaiableLife=live;
}
////////////////////////////////////////////////////////////////////////////
//--------------------------------- REVISIONS ------------------------------
// Date Name Tracking # Description
// --------- ------------------- ------------- ----------------------
// 16JAN2008 James Shen Initial Creation
////////////////////////////////////////////////////////////////////////////
/**
* Get player avaiable lives.
*/
public int getAvaiableLives(){
return avaiableLife;
}
////////////////////////////////////////////////////////////////////////////
//--------------------------------- REVISIONS ------------------------------
// Date Name Tracking # Description
// --------- ------------------- ------------- ----------------------
// 16JAN2008 James Shen Initial Creation
////////////////////////////////////////////////////////////////////////////
/**
* Tank thinks before move.
*/
public void think(){
if(grade==NEW_BORN){
newBornTimer++;
if(newBornTimer>4){
grade= MIN_GRADE;
direction=BattleField.NONE;
currentDirection=BattleField.NORTH;
newBornTimer=0;
setFrame(0);
invulnerableTime=System.currentTimeMillis();
invulnerabilityTicks=invulnerablePeriod/4;
sheild.setPosition(getX(),getY());
sheild.setVisible(true);
}else{
try{
setFrame(newBornTimer*9-1);
}catch(Exception e){
//System.out.println("Playertank");
}
}
} else{
long tickTime = System.currentTimeMillis();
if(tickTime-invulnerableTime>invulnerabilityTicks){
sheild.setVisible(false);
}else{
sheild.setPosition(getX(),getY());
sheild.setVisible(true);
}
changeDirection(direction);
if(currentDirection!=BattleField.NONE){
switchImage=!switchImage;
int offset=switchImage? 0:1;
try{
setFrame(currentDirection*9+((int)(grade-1)/2)*2 +offset);
}catch(Exception e){
//System.out.println("Playertank1");
}
}
}
}
////////////////////////////////////////////////////////////////////////////
//--------------------------------- REVISIONS ------------------------------
// Date Name Tracking # Description
// --------- ------------------- ------------- ----------------------
// 16JAN200 8 James Shen Initial Creation
////////////////////////////////////////////////////////////////////////////
/**
* Check if the player's tank is invulnerable
* @return true,the tank is invulnerable.
*/
public boolean isInvulnerable(){
return sheild.isVisible();
}
////////////////////////////////////////////////////////////////////////////
//--------------------------------- REVISIONS ------------------------------
// Date Name Tracking # Description
// --------- ------------------- ------------- ----------------------
// 16JAN200 8 James Shen Initial Creation
////////////////////////////////////////////////////////////////////////////
/**
* Tank shoots.
* @return the bullet the tank shoots.
*/
public Bullet shoot(){
Bullet bullet=null;
int bulletCount=Bullet.getPlayerBulletCount();
if(shoot && bulletCount<avaiableBullets){
int step=ResourceManager.TILE_WIDTH;
bullet=Bullet.getFreeBullet();
if(bullet!=null){
if(ResourceManager.isPlayingSound){
ResourceManager.playSound(ResourceManager.SHOOT_SOUND);
}
int x = getRefPixelX();
int y = getRefPixelY();
switch (currentDirection) {
case BattleField.NORTH:
y -= step / 2;
break;
case BattleField.EAST:
x += step / 2;
break;
case BattleField.SOUTH:
y += step / 2;
break;
case BattleField.WEST:
x -= step / 2;
break;
}
bullet.setSpeed(ResourceManager.TILE_WIDTH/2);
bullet.setDirection(currentDirection);
if(grade>=GRADE_BREAK_WATER){
bullet.setStrength(Bullet.GRADE_BREAK_WATER);
}else if(grade>=GRADE_BREAK_CONCRETE_WALL){
bullet.setStrength(Bullet.GRADE_BREAK_CONCRETE_WALL);
}else{
bullet.setStrength(Bullet.GRADE_DEFAULT);
}
bullet.setRefPixelPosition(x-1, y-1);
bullet.setFriendly(true);
bullet.setVisible(true);
}
}
return bullet;
}
////////////////////////////////////////////////////////////////////////////
//--------------------------------- REVISIONS ------------------------------
// Date Name Tracking # Description
// --------- ------------------- ------------- ----------------------
// 16JAN2008 James Shen Initial Creation
////////////////////////////////////////////////////////////////////////////
/**
* Stop the player tank
*/
public void stop(){
direction=BattleField.NONE;
shoot=false;
}
////////////////////////////////////////////////////////////////////////////
//--------------------------------- REVISIONS ------------------------------
// Date Name Tracking # Description
// --------- ------------------- ------------- ----------------------
// 16JAN2008 James Shen Initial Creation
////////////////////////////////////////////////////////////////////////////
/**
* Explode a tank.
*/
protected void explode() {
if(!isInvulnerable()){
if(grade>=GRADE_BREAK_WATER){
grade--;
}else{
avaiableLife--;
super.explode();
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -