⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bullet.java

📁 J2ME超级坦克大战源码
💻 JAVA
字号:

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;


public final class Bullet extends Sprite implements Actor {
    
    /**
     * bullet can break brick walls and enmeny tanks. ****
     */
    public static final int GRADE_DEFAULT=1;
    /**
     * bullet can break concrete walls. ****
     */
    public static final int GRADE_BREAK_CONCRETE_WALL=2;
    
    /**
     * bullet can break water and snow. *****
     */
    public static final int GRADE_BREAK_WATER=3;
    
    
    /**
     * Tank should know about the battle field.
     */
    private static BattleField battleField;
    
    /**
     * Tank should know about the layer manager.
     */
    private static LayerManager layerManager;
    
    /**
     * bullet strength
     */
    private int strength=GRADE_DEFAULT;
    
    /**
     * shot by player or enmeny tanks
     */
    private boolean friendly=false;
    
    /**
     * maximun number of tanks in the battle field.
     */
    private static final int POOL_SIZE = 20;
    
    /**
     * This pool store all tanks include player and enemy tanks.
     */
    private static Bullet BULLET_POOL[];
    
    /**
     * bullet direction
     */
    private int direction=BattleField.NONE;
    
    /**
     * bullet direction
     */
    private int speed=ResourceManager.TILE_WIDTH/2;
    
    /**
     * when move, the delta distance, dx,dy can not be nozero at the same time.
     */
    private int dx, dy;
    
    /**
     * initial the bullet pool ,the game will resume the bullet object.
     */
    static {
        BULLET_POOL = new Bullet[POOL_SIZE];
        for(int i=0;i<POOL_SIZE;i++){
            BULLET_POOL[i]=new Bullet(GRADE_DEFAULT,BattleField.NONE,0);
            BULLET_POOL[i].setVisible(false);
        }
    }
    
    
    
    private Bullet(int strength,int direction,int speed) {
        super(ResourceManager.getInstance().getImage(ResourceManager.BULLET),
                ResourceManager.TILE_WIDTH/4,ResourceManager.TILE_WIDTH/4);
        defineReferencePixel(ResourceManager.TILE_WIDTH/8,
                ResourceManager.TILE_WIDTH/8);
        this.strength=strength;
        this.direction=direction;
        this.speed=speed;
    }
   
    public void setFriendly(boolean friend){
        this.friendly=friend;
    }
    
   
    public void setSpeed(int speed){
        this.speed=speed;
    }
    
    
    public void setDirection(int direction){
        if(direction==BattleField.NONE) return;
        this.direction=direction;
        setFrame(direction);
        switch(direction){
            case BattleField.NORTH:
                dx = 0;
                dy = -speed;
                break;
            case BattleField.EAST:
                dx = speed;
                dy = 0;
                break;
            case BattleField.SOUTH:
                dx = 0;
                dy = speed;
                break;
            case BattleField.WEST:
                dx = -speed;
                dy = 0;
                break;
        }
    }
    
    
    public void setStrength(int strength){
        this.strength=strength;
    }
    
    
   
    public void explode() {
        setVisible(false);
        Explosion.explode(getRefPixelX(), getRefPixelY(), Explosion.SMALL);
    }

    
    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;
                }
            }
        }
    }
    
    
    public static void setLayerManager(LayerManager manager) {
        layerManager = manager;
        if (layerManager != null){
            for (int i = 0; i < POOL_SIZE; i++)
                layerManager.append(BULLET_POOL[i]);
        }
    }
    
    
    public static void setBattleField(BattleField field) {
        battleField = field;
    }
    
    
    
    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;
    }
    
    
    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;
    }
    
    
    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 + -