📄 tanksprite.java
字号:
}
}
private void releaseMissiles() {
try {
for (int i = 0; i < m_bulletCount; i++) {
if (null != m_bullet && null != m_bullet[i]) {
GameLogic.m_layerManager.remove(m_bullet[i]);
m_bullet[i].stop();
m_bullet[i] = null;
}
}
m_bullet = null;
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private void releaseBullets(){
try {
for (int i = 0; i < m_bulletCount; i++) {
if (null != m_bullet && null != m_bullet[i]) {
GameLogic.m_layerManager.remove(m_bullet[i]);
m_bullet[i].stop();
m_bullet[i] = null;
}
}
m_bullet = null;
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private void releaseCommonBullet() {
try {
for (int i = 0; i < m_bulletCount; i++) {
if (null != m_bullet && null != m_bullet[i]) {
m_bullet[i].stop();
GameLogic.m_layerManager.remove(m_bullet[i]);
m_bullet[i] = null;
}
}
m_bullet = null;
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private void createCommonBullet() {
m_bulletType=GameLogic.BULLET_TYPE_COMMON;
try {
m_bullet = new BulletSprite[m_bulletCount];
int bulletSpeed = GameLogic.tank_type[m_tankType][GameLogic.
TANK_PROPERTY_ATTACT_SPEED];
m_bulletDamage = GameLogic.tank_type[m_tankType][GameLogic.
TANK_PROPERTY_BASIC_ATTACT];
for (int i = 0; i < m_bulletCount; i++) {
m_bullet[i] = new BulletSprite(GameLogic.m_bulletImage_bullet, BULLET_WIDTH,
BULLET_HEIGHT,
bulletSpeed, m_bulletDamage,
m_bulletOwner,m_tankIndex,m_bulletType);
m_bullet[i].setVisible(false);
BattleCanvas.m_layerManager.insert(m_bullet[i], 0);
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private void createBabyBullet() {
m_bulletType = GameLogic.BULLET_TYPE_BABY;
try {
m_bullet = new BulletSprite[m_bulletCount];
int bulletSpeed = GameLogic.tank_type[m_tankType][GameLogic.
TANK_PROPERTY_ATTACT_SPEED];
m_bulletDamage = GameLogic.tank_type[m_tankType][GameLogic.
TANK_PROPERTY_BASIC_ATTACT];
for (int i = 0; i < m_bulletCount; i++) {
m_bullet[i] = new BulletSprite(GameLogic.m_bulletImage_baby, BABY_BULLET_WIDTH,
BABY_BULLET_HEIGHT,
bulletSpeed, m_bulletDamage,
m_bulletOwner, m_tankIndex, m_bulletType);
BattleCanvas.m_layerManager.insert(m_bullet[i], 0);
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private void loadResource() {
m_timer = new Timer();
m_currentFrame = new int[2];
m_rand = new Random();
m_enemiesAttacked=new Vector();
createBombScene();
createLighteningSprite();
}
private void releaseResource() {
try {
releaseMissiles();
releaseCommonBullet();
releaseBombScene();
releaseLighteningSprite();
m_currentFrame=null;
m_rand=null;
if (null != m_timer) {
m_timer.cancel();
m_timer = null;
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 检测一个特定点是否进入一个Actor内
* @param px 特定点的x坐标.
* @param py 特定点的y坐标
* @return 如果特定点进入Actor范围内,那么返回true,否则为false;
* ref point is located in the middle of the rectangele
*/
public boolean isCollidingWith(int px, int py){
try {
int xPos=getRefPixelX();
int yPos=getRefPixelY();
//get the location coordinate in (left,top)corner
//judge whether the point is inside the sprite.
if(px>xPos-getWidth()/2&&px<xPos+getWidth()/2&&py>yPos-getHeight()/2&&py<yPos+getHeight()/2){
return true;
}
return false;
}
catch (Exception ex) {
ex.printStackTrace();
}
return true;
}
public boolean collidesWith(TiledLayer map, int col, int row) {
try {
int curCell = map.getCell(col, row);
int leftTopX=0,leftTopY=0,rightTopX=0,rightTopY=0,leftBottomX=0,leftBottomY=0,rightBottomX=0,rightBottomY=0;
if (curCell == GameLogic.MAP_ELEMENT_GROUND || curCell == GameLogic.MAP_ELEMENT_GRASS ||
curCell == GameLogic.MAP_ELEMENT_GRASS_RIVERSIDE ||
curCell == GameLogic.MAP_ELEMENT_BRUSHWOOD) {
return false;
}
leftTopX = col*GameLogic.MAP_CELL_WIDTH;
leftTopY = (row+1)*GameLogic.MAP_CELL_HEIGHT;
rightTopX = leftTopX+GameLogic.MAP_CELL_WIDTH;
leftBottomY = leftTopY + GameLogic.MAP_CELL_HEIGHT;
rightTopY=leftTopY;
leftBottomX=leftTopX;
rightBottomX=rightTopX;
rightBottomY=leftBottomY;
if(isCollidingWith(leftTopX,leftTopY)||
isCollidingWith(rightTopX,rightTopY)||
isCollidingWith(leftBottomX,leftBottomY)||
isCollidingWith(rightBottomX,rightBottomY)){
return true;
}
}
catch (Exception ex) {
ex.printStackTrace();
}
return false;
}
public boolean collidesWith(Sprite s,int refPoint){
try {
int leftTopX=0,leftTopY=0,rightTopX=0,rightTopY=0,leftBottomX=0,leftBottomY=0,rightBottomX=0,rightBottomY=0;
//judge the four points of the rectangle
if(refPoint==ANCHOR_CENTER){
leftTopX=s.getRefPixelX()-s.getWidth()/2;
leftTopY=s.getRefPixelY()-s.getHeight()/2;
rightTopX=s.getRefPixelX()+s.getWidth()/2;
leftBottomY=s.getRefPixelY()+s.getHeight()/2;
}else if(refPoint==ANCHOR_LEFTTOP){
leftTopX=s.getRefPixelX();
leftTopY=s.getRefPixelY();
rightTopX=s.getRefPixelX()+getWidth();
leftBottomY=s.getRefPixelY()+getHeight();
}
rightTopY=leftTopY;
leftBottomX=leftTopX;
rightBottomX=rightTopX;
rightBottomY=leftBottomY;
if(isCollidingWith(leftTopX,leftTopY)||
isCollidingWith(rightTopX,rightTopY)||
isCollidingWith(leftBottomX,leftBottomY)||
isCollidingWith(rightBottomX,rightBottomY)){
return true;
}
}
catch (Exception ex) {
ex.printStackTrace();
}
return false;
}
public TankSprite(Image image,
int frameWidth,
int frameHeight,
int tankType,
int tankIndex) {
super(image, frameWidth, frameHeight);
m_frameWidth = frameWidth;
m_frameHeight = frameHeight;
m_tankType = tankType;
m_tankIndex=tankIndex;
init();
loadResource();
}
/**
*
(1)得到当前tank所处的角度。
(2)根据操作方式(turn left/right)得到tank的新角度。
TRANS_NONE<->TRANS_ROT90<->TRANS_ROT180<->TRANS_ROT270
(3)切换到新的显示。改变炮口位置。
*/
public void turn(int iStep, int iAngel) {
mLastWasTurn = true;
if (iStep > 0) {
m_currentDirection = GameLogic.TANK_DIRECTION_RIGHT;
}
else {
m_currentDirection = GameLogic.TANK_DIRECTION_LEFT;
}
mKX += iStep;
setRefPixelPosition(mKX, mKY);
mLastDelta = iStep;
}
public void forward(int iStep, int iAngel) {
mLastWasTurn = false;
if (iStep > 0) {
m_currentDirection = GameLogic.TANK_DIRECTION_UP;
}
else {
m_currentDirection = GameLogic.TANK_DIRECTION_DOWN;
}
//directions remain the same
mKY -= iStep;
setRefPixelPosition(mKX, mKY);
mLastDelta = iStep;
}
public void add_points(int points) {
if (m_doublePoints) {
m_tankPoints += points * 2;
}
else {
m_tankPoints += points;
}
}
public void changeEnemyTankType(int tankType) {
/**
* 1.create random enemy tank,from type one to type three;
* 2.
*/
try {
if(m_tankType==tankType){
return;
}
m_tankType=tankType;
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public void changeBullet(int bulletType) {
if (bulletType == m_bulletType) {
return;
}
m_bulletType = bulletType;
int bulletSpeed = GameLogic.tank_type[m_tankType][GameLogic.
TANK_PROPERTY_ATTACT_SPEED];
if (m_bulletType == GameLogic.BULLET_TYPE_MISSILE) {
m_bulletDamage = GameLogic.BULLET_MISSILE_DAMAGE;
releaseBullets();
createMissiles();
}
else if (m_bulletType == GameLogic.BULLET_TYPE_COMMON) {
m_bulletDamage = GameLogic.tank_type[m_tankType][GameLogic.
TANK_PROPERTY_BASIC_ATTACT];
releaseBullets();
createCommonBullet();
}else if(m_bulletType==GameLogic.BULLET_TYPE_BABY){
releaseBullets();
createBabyBullet();
}
//pause();
}
//坦克升级
//坦克升级
private void tank_upgrade() {
try {
if (m_tankType >= GameLogic.TANK_RETIRED &&
m_tankType <= GameLogic.TANK_RETIRED_PLUSPLUS) {
m_tankType++;
if (m_tankType >= GameLogic.TANK_RETIRED_PLUSPLUS) {
m_tankType = GameLogic.TANK_RETIRED_PLUSPLUS;
}
}
else if (m_tankType >= GameLogic.TANK_COMMON &&
m_tankType <= GameLogic.TANK_COMMON_PLUSPLUS) {
m_tankType++;
if (m_tankType >= GameLogic.TANK_COMMON_PLUSPLUS) {
m_tankType = GameLogic.TANK_COMMON_PLUSPLUS;
}
}
else if (m_tankType >= GameLogic.GREEN_TANK_SPECIAL &&
m_tankType <= GameLogic.GREEN_TANK_SPECIAL_PLUSPLUS) {
m_tankType++;
if (m_tankType >= GameLogic.GREEN_TANK_SPECIAL_PLUSPLUS) {
m_tankType = GameLogic.GREEN_TANK_SPECIAL_PLUSPLUS;
}
}
else if (m_tankType >= GameLogic.TANK_FUTURE &&
m_tankType <= GameLogic.TANK_FUTURE_PLUSPLUS) {
m_tankType++;
if (m_tankType >= GameLogic.TANK_FUTURE_PLUSPLUS) {
m_tankType = GameLogic.TANK_FUTURE_PLUSPLUS;
}
}
//reset tank related params
m_tankBlood = GameLogic.tank_type[m_tankType][GameLogic.
TANK_PROPERTY_BASIC_BLOOD]; //current life's blood
//bomb count remain unchanged ;
/*m_bombCount = GameLogic.tank_type[m_tankType][GameLogic.
TANK_PROPERTY_BOMB_COUNT]; //the bomb count of current life*/
m_bulletCount = GameLogic.tank_type[m_tankType][GameLogic.
TANK_PROPERTY_ATTACT_FREQUENCY];
m_bulletSpeed = GameLogic.tank_type[m_tankType][GameLogic.
TANK_PROPERTY_ATTACT_SPEED];
m_tankSpeed = GameLogic.tank_type[m_tankType][GameLogic.
TANK_PROPERTY_BASIC_MOVE_PIXEL];
/**
* 1.start tank upgrade gif;
* 2.tank display frame tank->tank_white->tank_plus->tank_plus_white->tank_plus
*/
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private void displayTankUpgradeScene() {
try {
/**
* 1.start tank upgrade gif;
* 2.get current tank direction and confirm its initial frame;
* 3.tank display frame tank->tank_white->tank_plus->tank_plus_white->tank_plus
*/
} catch (Exception e) {
e.printStackTrace();
}
}
public void update(int item) {
//property item
//bomb
switch (item) {
//properties,listed below
case GameLogic.PROPERTY_ELEMENT_INVINCIBLY:
//还在无敌模式下,重新开始计算
if(m_invicible){
if(null !=m_timerTaskInvicible){
m_timerTaskInvicible.cancel();
}
m_timerTaskInvicible = null;
}
if (null == m_timerTaskInvicible) {
m_timerTaskInvicible = new BulletScheduler(this);
m_timerTaskInvicible.setTask(BulletScheduler.TASK_TANK_INVICIBLE);
m_timer.schedule(m_timerTaskInvicible, INVICIBLE_TIMER_DELAY);
m_invicible = true;//开启坦克无敌模式
}
break;
case GameLogic.PROPERTY_ELEMENT_BOMB_ADD:
m_bulletDamageAddTimes++;
if (m_bulletDamageAddTimes > MAX_BULLET_DAMAGE_ADD_TIMES) {
return;
}
//攻击值增加%50
m_bulletDamage += m_bulletDamage / 2;
//坦克升级
tank_upgrade();
break;
case GameLogic.PROPERTY_ELEMENT_ADD_TRACK_MISSILE:
m_missileCount+=GameLogic.BULLET_MISSILE_COUNT_PER;
if(m_missileCount>=GameLogic.BULLET_MISSILE_COUNT_MAX){
m_missileCount=GameLogic.BULLET_MISSILE_COUNT_MAX;
}
changeBullet(GameLogic.BULLET_TYPE_MISSILE);
break;
case GameLogic.PROPERTY_ELEMENT_WRENCH:
m_tankBlood = GameLogic.tank_type[m_tankType][GameLogic.
TANK_PROPERTY_BASIC_BLOOD];
break;
case GameLogic.PROPERTY_ELEMENT_ADD_LIFE:
if (GameLogic.USER_TANK == m_tankOwner) {
m_tankLife++;
if (m_tankLife >= GameLogic.MAX_TANK_LIFE) {
m_tankLife = GameLogic.MAX_TANK_LIFE;
}
}
break;
case GameLogic.PROPERTY_ELEMENT_DOUBLE_POINTS:
//重新开始计时
if(m_doublePoints){
m_timerTaskDoublePoints.cancel();
}
m_timerTaskDoublePoints = null;
if (null == m_timerTaskDoublePoints) {
m_timerTaskDoublePoints = new BulletScheduler(this);
m_timerTaskDoublePoints.setTask(BulletScheduler.TASK_TANK_DOUBLE_POINTS);
m_timer.schedule(m_timerTaskDoublePoints, DOUBLE_POINTS_TIMER_DELAY);
m_doublePoints = true;//开启积分获得加倍模式
}
break;
case GameLogic.PROPERTY_ELEMENT_STRENGTHEN_HEADQUARTERS:
break;
default:
break;
}
}
public void undo() {
if (mLastWasTurn) {
mKX -= mLastDelta;
}
else {
mKY += mLastDelta;
}
mLastDelta = 0;
}
//make sure that the useBomb will not be interrupted by other threads
public void useBomb() {
if (m_currentState == 0 || m_currentState == GameLogic.GAME_STATE_END) {
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -