📄 enemysprite16.java
字号:
/**
* <p>Title: Transpanzer</p>
* <p>Description:
* You cannot remove this copyright and notice.
* You cannot use this file any part without the express permission of the author.
* All Rights Reserved</p>
* @author Jjyo
* @email jjyo@163.com
* @version 1.0.0
*/
import java.util.Random;
public class EnemySprite16 extends FSprite{
GameManage gm;
int dy;
int dx;
int attack;
int speed;
int tmpX;
int tmpY;
int frame[]={0,1,2,3};
int index;
int frameSpeed;
boolean isWater;
boolean isCollision;
public EnemySprite16(GameManage gm){
this.gm=gm;
rect1.dx=16;
rect1.dy=16;
rect2.dx=16;
rect2.dy=16;
frameSpeed=5;
}
//与地图水平碰撞检测
protected boolean checkTileCollisionHorizon(){
if (this.dx> 0) {
for (int i = (x + Maps.x)
/ Maps.tileSize + 1; i <= (x + Maps.x )
/ Maps.tileSize + 1; i++) {
if (i < 0 || i > Maps.w - 1) {
continue;
}
int yTile1 = (y + Maps.x + 2)/ Maps.tileSize;
int yTile2 = (y + Maps.x + 14)/ Maps.tileSize;
if (yTile2 > Maps.h - 1 || yTile1 < 0) {
return false;
}
if (((Maps.mapArray[yTile1][i] >> 8) & 0x0f) == 0x0f) {
if(isCollision)
dx = 0;
return true;
}
if (((Maps.mapArray[yTile2][i] >> 8) & 0x0f) == 0x0f) {
if(isCollision)
dx = 0;
return true;
}
if (((Maps.mapArray[yTile1][i] >> 8) & 0x0c) == 0x0c) {
if(isCollision)
dx=0;
return true;
}
}
} else if (dx < 0) {
for (int i = (x + Maps.x)
/ Maps.tileSize; i <= (x + Maps.x)
/ Maps.tileSize; i++) {
if (i < 0 || i > Maps.w - 1) {
continue;
}
int yTile1 = (y + Maps.x + 2) / Maps.tileSize;
int yTile2 = (y + Maps.x + 14)/ Maps.tileSize;
if (yTile2 > Maps.h - 1 || yTile1 < 0) {
return false;
}
if (((Maps.mapArray[yTile1][i] >> 8) & 0x0f) == 0x0f) {
if(isCollision)
dx = 0;
return true;
}
if (((Maps.mapArray[yTile2][i] >> 8) & 0x0f) == 0x0f) {
if(isCollision)
dx=0;
return true;
}
if (((Maps.mapArray[yTile1][i] >> 8) & 0x0c) == 0x0c) {
if(isCollision)
dx=0;
return true;
}
}
}
return false;
}
boolean checkTileCollisionHorizon2(){
int i;
rect1.dx = 16;
rect1.dy = 16;
rect2.dx = 16;
rect2.dy = 16;
rect1.y = this.y + Maps.y;
rect1.x = this.x + Maps.x+dx;
if (dx < 0) { //负数人物向左走,正数向右走
i = rect1.x / Maps.tileSize;
} else { //人物向右走
i = rect1.x / Maps.tileSize +1;
}
if (i < 0 || i > Maps.w - 1)
return false; //超出地图,不作判断
int yTile1 = rect1.y / Maps.tileSize; //地图为16X16,人物为32X32,需要判断两块地图块
int yTile2 = rect1.y / Maps.tileSize + 1;
if (yTile1 < 0 || yTile2 > Maps.h - 1)
return false; //超出地图,不作判断
if (((Maps.mapArray[yTile1][i] >> 8) & 0x0f) == 0x0f) {
rect2.x = i * 16;
rect2.y = yTile1 * 16;
if (Rect.IntersectRect(rect1, rect2)) {
return true;
}
}
if (((Maps.mapArray[yTile2][i] >> 8) & 0x0f) == 0x0f) {
rect2.x = i * 16;
rect2.y = yTile2 * 16;
if (Rect.IntersectRect(rect1, rect2)) {
return true;
}
}
return false;
}
//与地图的纵向碰撞检测
protected boolean checkTileCollisionVertical(){
if (this.dy > 0) {
for (int i = (y + Maps.y)
/ Maps.tileSize + 1; i <= (y + Maps.y )
/ Maps.tileSize + 1; i++) {
if (i < 0 || i > Maps.h - 1) {
continue;
}
int xTile1 = (x + Maps.x + 2)/ Maps.tileSize;
int xTile2 = (x + Maps.x + 14)/ Maps.tileSize;
if (xTile2 > Maps.w - 1 || xTile1 < 0) {
return false;
}
if (((Maps.mapArray[i][xTile1] >> 8) & 0x0f) == 0x0f) {
if(isCollision){
dy = (i - 1) * Maps.tileSize
- (y + Maps.y);
}
return true;
}
if (((Maps.mapArray[i][xTile2] >> 8) & 0x0f) == 0x0f) {
if(isCollision){
dy = (i - 1) * Maps.tileSize
- (y + Maps.y);
}
return true;
}
if (((Maps.mapArray[i][xTile1] >> 8) & 0x0c) == 0x0c) {
if(isCollision){
dy = (i - 1) * Maps.tileSize
- (y + Maps.y);
}
return true;
}
}
} else if (dy < 0) {
for (int i = (y + Maps.y)
/ Maps.tileSize; i <= (y + Maps.y)
/ Maps.tileSize; i++) {
if (i < 0 || i > Maps.h - 1) {
continue;
}
int xTile1 = (x + Maps.x + 2) / Maps.tileSize;
int xTile2 = (x + Maps.x + 14)/ Maps.tileSize;
if (xTile2 > Maps.w - 1 || xTile1 < 0) {
return false;
}
if (((Maps.mapArray[i][xTile1] >> 8) & 0x0f) == 0x0f) {
if(isCollision)
dy = 0;
return true;
}
if (((Maps.mapArray[i][xTile2] >> 8) & 0x0f) == 0x0f) {
if(isCollision)
dy=0;
return true;
}
if (((Maps.mapArray[i][xTile1] >> 8) & 0x0c) == 0x0c) {
if(isCollision)
dy = 0;
return true;
}
}
}
return false;
}
//子弹碰撞检测 16X
protected boolean checkBulletDamage(){
rect1.dx = 8;
rect1.dy = 8;
rect2.dx = 16;
rect2.dy = 16;
for(int i=gm.lgr.tankBullet.length-1;i>=0;i--){
if(!gm.lgr.tankBullet[i].isHidden){
rect1.x=gm.lgr.tankBullet[i].x;
rect1.y=gm.lgr.tankBullet[i].y+4;
rect2.x=x;
rect2.y=y;
if(Rect.IntersectRect(rect1, rect2)){
if(gm.lgr.tankBullet[i].pow<16)
gm.lgr.tankBullet[i].isHidden=true;
attack=gm.lgr.tankBullet[i].pow;
return true;
}
}
}
return false;
}
//子弹碰撞检测 8X
protected boolean checkBulletDamage8(){
rect1.dx = 8;
rect1.dy = 8;
rect2.dx = 16;
rect2.dy = 8;
for(int i=gm.lgr.tankBullet.length-1;i>=0;i--){
if(!gm.lgr.tankBullet[i].isHidden){
rect1.x=gm.lgr.tankBullet[i].x;
rect1.y=gm.lgr.tankBullet[i].y+4;
rect2.x=x;
rect2.y=y+8;
if(Rect.IntersectRect(rect1, rect2)){
if(gm.lgr.tankBullet[i].pow<16)
gm.lgr.tankBullet[i].isHidden=true;
attack=gm.lgr.tankBullet[i].pow;
return true;
}
}
}
return false;
}
public void checkTileValue(){
char value;
rect1.y = this.y + Maps.y+8;
rect1.x = x + Maps.x + 8;
int xTile = rect1.x / Maps.tileSize;
int yTile = rect1.y / Maps.tileSize;
value=(char)(Maps.mapArray[yTile][xTile] >> 8);
switch(value){
case 0x01: //隐身
frameCnt=-1;
break;
case 0x04: //水
isWater=true;
break;
}
}
protected void nextFrame(int frameNum,int frameIndex){
if(index>=frameNum||index<0)
index=0;
frameCnt=frame[index]+frameIndex;
if(++timeCnt%frameSpeed==0)
index++;
}
}
/**
*------------------------------------------小石人----------------------------------------------
*/
class Stoner extends EnemySprite16{
private byte direction;
private static final byte left=0x01;
private static final byte right=0x02;
private static final byte up=0x04;
private static final byte down=0x08;
public Stoner(GameManage gm,int x,int y) {
super(gm);
this.x=x;
this.y=y;
this.speed=1;
pow=12;
direction=left;
isHidden=false;
isCollision=true;
}
public void action(){
if(!isHidden){
if (x > TankGameCanvas.width + 16 || x < -16 || y < -16
|| y > TankGameCanvas.height + 16) {
return ;
}
attack=0;
switch(direction){
case 0x01: //left
dx=-speed;
dy+=2;
if(!checkTileCollisionVertical()){ //纵向
direction=down;
}
if(dy==0){
if(checkTileCollisionHorizon()){ //水平
direction=up;
}
}
nextFrame(2,0);
break;
case 0x02: //right
dx=speed;
dy-=2;
if(!checkTileCollisionVertical()){ //纵向
direction=up;
}
if(dy==0){
if(checkTileCollisionHorizon()){ //水平
direction=down;
}
}
nextFrame(2,4);
break;
case 0x04: //up
dy=-speed;
dx-=2;
if(!checkTileCollisionHorizon()){
direction=left;
}
if(dx==0){
if(checkTileCollisionVertical()){
direction=right;
}
}
nextFrame(2,6);
break;
case 0x08: //down
dx+=2;
dy=speed;
if(!checkTileCollisionHorizon()){ //水平
direction=right;
}
if(dx==0){
if(checkTileCollisionVertical()){ //纵
direction=left;
}
}
nextFrame(2,2);
break;
}
if(checkBulletDamage()){
frameCnt=-1;
}
pow-=attack;
x+=dx;
y+=dy;
timeCnt++;
if(pow<=0){
isHidden=true;
gm.creatBombEffect(x-8, y-8, 1);
if(timeCnt%4==0){
gm.creatGoods(x, y, Goods.POW);
}
}
}
}
}
/**
* ----------------------石 鸟------------------------------------------------------------
*/
class Guano extends EnemySprite16{
private int n;
public Guano(GameManage gm,int x,int y,boolean isLeft) {
super(gm);
this.pow=12;
if(isLeft){
speed=-6;
}else{
speed=6;
}
this.x=x;
this.y=y;
isHidden=false;
frameSpeed=2;
}
public void action(){
if(!isHidden){
if (x > TankGameCanvas.width || x < -32 || y < -32
|| y > TankGameCanvas.height + 32) {
speed=-speed;
}
n++;
if(n>=20)
n=0;
if(n>=0&&n<10){
dy=speed;
}else if(n>10&&n<=20){
dy=-speed;
}
dx=speed;
if(dx<0){
nextFrame(4,8);
}else{
nextFrame(4,12);
}
if(checkBulletDamage()){
frameCnt=-1;
}
pow-=attack;
x+=dx;
y+=dy;
timeCnt++;
if(pow<=0){
gm.creatBombEffect(x-8, y-8, 1);
isHidden=true;
if(timeCnt%4==0){
gm.creatGoods(x, y, Goods.HOV);
}
}
}
}
}
/**
* 炮座--------------------------------------------------------------------------------------
*/
class Barbette extends EnemySprite16{
boolean isTurn;
Random random;
public Barbette(GameManage gm,int x,int y) {
super(gm);
pow=12;
this.x=x;
this.y=y;
this.isHidden=false;
isTurn=true;
frameCnt=21;
frameSpeed=2;
random=new Random();
}
public void action(){
if(!isHidden){
if (x > TankGameCanvas.width -32 || x < 32 || y < 16
|| y > TankGameCanvas.height - 16) {
return ;
}
attack=0;
if(timeCnt++%40==0){
isTurn=!isTurn;
}
if(isTurn){
int k=random.nextInt();
k=Math.abs(k%10);
nextFrame(2,20);
if(timeCnt%3==0){
if(k<4){
gm.creatEnemyBullet(x, y+8, -6, 8, 1);
}else if(k>=4&&k<6){
gm.creatEnemyBullet(x, y+8, 7, 8, 1);
}else if(k>=6&&k<8){
gm.creatEnemyBullet(x, y+8, 2, 8, 1);
}else{
gm.creatEnemyBullet(x, y+8, -3, 8, 1);
}
}
}else{
frameCnt=21;
}
if(checkBulletDamage()){
frameCnt=-1;
}
pow-=attack;
if(pow<=0){
if(pow<=0){
gm.creatBombEffect(x-8, y-8, 1);
isHidden=true;
}
}
}
}
}
/**
* ----------------------外星人---------------------------
*/
class ET extends EnemySprite16{
private int xSpeed;
private int ySpeed;
public ET(GameManage gm,int x,int y) {
super(gm);
this.x=x;
this.y=y;
this.isHidden=false;
this.pow=4;
dx=-2;
dy=2;
isCollision=false;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -