📄 bullet.java
字号:
dy = 0;
break;
}
}
////////////////////////////////////////////////////////////////////////////
//--------------------------------- REVISIONS ------------------------------
// Date Name Tracking # Description
// --------- ------------------- ------------- ----------------------
// 15JAN2008 James Shen Initial Creation
////////////////////////////////////////////////////////////////////////////
/**
* Set the bullet's strength.
* @param strength the bullet's hitting strength.
*/
public void setStrength(int strength){
this.strength=strength;
}
////////////////////////////////////////////////////////////////////////////
//--------------------------------- REVISIONS ------------------------------
// Date Name Tracking # Description
// --------- ------------------- ------------- ----------------------
// 15JAN2008 James Shen Initial Creation
////////////////////////////////////////////////////////////////////////////
/**
* the bullet hit something and explodes
*/
public void explode() {
setVisible(false);
Explosion.explode(getRefPixelX(), getRefPixelY(), Explosion.SMALL);
}
////////////////////////////////////////////////////////////////////////////
//--------------------------------- REVISIONS ------------------------------
// Date Name Tracking # Description
// --------- ------------------- ------------- ----------------------
// 15JAN2008 James Shen Initial Creation
////////////////////////////////////////////////////////////////////////////
/**
* Operation be done in each tick.
*/
public void tick(){
if (!isVisible() || direction==BattleField.NONE)
return;
// Move the bullet.
move(dx, dy);
int x = getRefPixelX();
int y = getRefPixelY();
PlayerTank playerTank=(PlayerTank)Tank.getTank(0);
//outside the battle field, hitting the border
if (x <= 0 || x >= battleField.getWidth() || y <= 0
|| y >= battleField.getHeight()) {
//this is to avoid explosition outside the battlefield.
if(x<=0) x=0;
if(x >= battleField.getWidth()) x= battleField.getWidth();
if(y<=0) y=0;
if(y>= battleField.getHeight())y=battleField.getHeight();
setPosition(x,y);
explode();
return;
}
// See if it hit a tank.
if (friendly) {
// See if it hit an enemy tank.
for (int i = 1; i < Tank.POOL_SIZE; i++) {
EnemyTank enemy = (EnemyTank)Tank.getTank(i);
if (enemy!=null && enemy.isVisible() &&
collidesWith(enemy, false)) {
enemy.explode();
explode();
return;
}
}
} else {
// See if it hit player tank.
if (collidesWith(playerTank, false)) {
playerTank.explode();
explode();
return;
}
}
//check to see if hit player's home
if(Powerup.isHittingHome(this)){
//TODO: Game Over
explode();
return;
}
// See if it hit a wall.
if (battleField.hitWall(x, y, strength)) {
explode();
return;
}
// See if it hit another bullet.
for (int i = 0; i < POOL_SIZE; i++) {
Bullet anotherBullet=BULLET_POOL[i];
if(this!=anotherBullet && anotherBullet.isVisible()){
if (collidesWith(anotherBullet, false)) {
explode();
BULLET_POOL[i].explode();
return;
}
}
}
}
////////////////////////////////////////////////////////////////////////////
//--------------------------------- REVISIONS ------------------------------
// Date Name Tracking # Description
// --------- ------------------- ------------- ----------------------
// 15JAN2008 James Shen Initial Creation
////////////////////////////////////////////////////////////////////////////
/**
* Set the layerManager for tanks.
*/
public static void setLayerManager(LayerManager manager) {
layerManager = manager;
if (layerManager != null){
for (int i = 0; i < POOL_SIZE; i++)
layerManager.append(BULLET_POOL[i]);
}
}
////////////////////////////////////////////////////////////////////////////
//--------------------------------- REVISIONS ------------------------------
// Date Name Tracking # Description
// --------- ------------------- ------------- ----------------------
// 15JAN2008 James Shen Initial Creation
////////////////////////////////////////////////////////////////////////////
/**
* Set the Battle field for tanks.
*/
public static void setBattleField(BattleField field) {
battleField = field;
}
////////////////////////////////////////////////////////////////////////////
//--------------------------------- REVISIONS ------------------------------
// Date Name Tracking # Description
// --------- ------------------- ------------- ----------------------
// 17JAN2008 James Shen Initial Creation
////////////////////////////////////////////////////////////////////////////
/**
* Get how many bullets the player shot.the player can shoot 3 bullets
* at most.
*/
public static int getPlayerBulletCount(){
int count=0;
for(int i=0;i<POOL_SIZE;i++){
if(BULLET_POOL[i].isVisible() && BULLET_POOL[i].friendly ){
count++;
}
}
return count;
}
////////////////////////////////////////////////////////////////////////////
//--------------------------------- REVISIONS ------------------------------
// Date Name Tracking # Description
// --------- ------------------- ------------- ----------------------
// 17JAN2008 James Shen Initial Creation
////////////////////////////////////////////////////////////////////////////
/**
* Get a free bullet from the pool.
*/
public static Bullet getFreeBullet(){
for(int i=0;i<POOL_SIZE;i++){
if(!BULLET_POOL[i].isVisible()){
BULLET_POOL[i].setVisible(true);
return BULLET_POOL[i];
}
}
return null;
}
////////////////////////////////////////////////////////////////////////////
//--------------------------------- REVISIONS ------------------------------
// Date Name Tracking # Description
// --------- ------------------- ------------- ----------------------
// 17JAN2008 James Shen Initial Creation
////////////////////////////////////////////////////////////////////////////
/**
* Stop all bullets in the game scene.
*/
public static void stopAllBullets() {
for (int i = 0; i < POOL_SIZE; ++i)
{
if(!BULLET_POOL[i].friendly){
BULLET_POOL[i].setVisible(false);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -