?? tank.java
字號:
//------------------------------------------------------------------------------
// COPYRIGHT 2008 GUIDEBEE
// ALL RIGHTS RESERVED.
// GUIDEBEE CONFIDENTIAL PROPRIETARY
///////////////////////////////////// REVISIONS ////////////////////////////////
// Date Name Tracking # Description
// --------- ------------------- ---------- --------------------------
// 16JAN2008 James Shen Initial Creation
////////////////////////////////////////////////////////////////////////////////
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//The Software shall be used for Good, not Evil.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
//Any questions, feel free to drop me a mail at james.shen@guidebee.biz.
//--------------------------------- PACKAGE ------------------------------------
package com.pstreets.game.battlecity;
//--------------------------------- IMPORTS ------------------------------------
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
//[------------------------------ MAIN CLASS ----------------------------------]
////////////////////////////////////////////////////////////////////////////////
//--------------------------------- REVISIONS ----------------------------------
// Date Name Tracking # Description
// -------- ------------------- ------------- --------------------------
// 16JAN2008 James Shen Initial Creation
////////////////////////////////////////////////////////////////////////////////
/**
* Base class for all tanks.
* <p>
* <hr><b>© Copyright 2008 Guidebee, Inc. All Rights Reserved.</b>
* @version 1.00, 16/01/08
* @author Guidebee, Inc.
*/
public abstract class Tank extends Sprite implements Actor{
/**
* Tank should know about the battle field.
*/
protected static BattleField battleField;
/**
* Tank should know about the layer manager.
*/
protected static LayerManager layerManager;
/**
* the speed of the tank, default speed is 6 ,half of the
* width of each tile.
*/
protected int speed=DEFAULT_SPEED;
/**
* The direction in which player is driving.
*/
protected int direction = BattleField.NONE;
/**
* default speed for tank.
*/
protected static final int DEFAULT_SPEED=ResourceManager.TILE_WIDTH/4;
/**
* maximun number of tanks in the battle field.
*/
protected static final int POOL_SIZE = 21;
/**
* This pool store all tanks include player and enemy tanks.
*/
protected static Tank TANK_POOL[];
/**
* time monitored to avoid the tank move to fast.
*/
protected long driveStartTime = 0;
/**
* minimum time period between each move
*/
private static final long minimumDrivePeriod = 40;
/**
* Should the tank shoot?
*/
protected boolean shoot = false;
/**
* new boun timer
*/
protected int newBornTimer=0;
/**
* initial the tank pool ,the game will resume the tank object.
* the first tank is the player's tank.
*/
static {
TANK_POOL = new Tank[POOL_SIZE];
TANK_POOL[0] = new PlayerTank();
//max 7 simple tanks
for(int i=1;i<8;i++){
TANK_POOL[i] = new SimpleTank(false);
}
//max 4 fast tanks
for(int i=8;i<12;i++){
TANK_POOL[i] = new FastTank(false);
}
//max 4 smart tanks
for(int i=12;i<16;i++){
TANK_POOL[i] = new SmartTank(false);
}
//max 4 heavy tanks
for(int i=16;i<21;i++){
TANK_POOL[i] = new HeavyTank(false);
}
}
////////////////////////////////////////////////////////////////////////////
//--------------------------------- REVISIONS ------------------------------
// Date Name Tracking # Description
// --------- ------------------- ------------- ----------------------
// 16JAN2008 James Shen Initial Creation
////////////////////////////////////////////////////////////////////////////
/**
* Creates a new animated tank using frames contained in the provided Image.
* @param battleField the battle field where the tank in.
* @param image the Image to use for Sprite.
* @param frameWidth the width, in pixels, of the individual raw frames.
* @param frameHeight the height, in pixels, of the individual raw frames.
*/
protected Tank(Image image,int frameWidth,int frameHeight){
super(image,frameWidth,frameHeight);
defineReferencePixel(frameWidth / 2, frameHeight / 2);
}
////////////////////////////////////////////////////////////////////////////
//--------------------------------- REVISIONS ------------------------------
// Date Name Tracking # Description
// --------- ------------------- ------------- ----------------------
// 16JAN2008 James Shen Initial Creation
////////////////////////////////////////////////////////////////////////////
/**
* Tank moves.
*/
public void drive(){
long tickTime = System.currentTimeMillis();
boolean canDrive=(tickTime-driveStartTime)>minimumDrivePeriod;
boolean onSnow=battleField.isOnSnow(getX(),getY());
int extraPace=0;
if(onSnow) extraPace=speed;
if(canDrive){
switch (direction) {
case BattleField.NORTH:
if ((getY() > 0)&&
!battleField.containsImpassableArea
(getX(),getY() - speed, getWidth(), speed)) {
tryMove(0, -speed-extraPace);
}
break;
case BattleField.EAST:
if ((getX() < battleField.getWidth() - getWidth()) &&
!battleField.containsImpassableArea
(getX() + getWidth(), getY(), speed, getHeight())) {
tryMove(speed+extraPace, 0);
}
break;
case BattleField.SOUTH:
if ((getY() < battleField.getHeight() - getHeight())&&
!battleField.containsImpassableArea
(getX(),getY() + getHeight(), getWidth(), speed)) {
tryMove(0, speed+extraPace);
}
break;
case BattleField.WEST:
if ((getX() > 0)&&
!battleField.containsImpassableArea
(getX() - speed,getY(), speed, getHeight())) {
tryMove(-speed-extraPace, 0);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -