enemy.java~80~
来自「基于J2ME的手机游戏软件。可以控制游戏人物在地图上上下左右行走;可以在地图上放」· JAVA~80~ 代码 · 共 492 行
JAVA~80~
492 行
import javax.microedition.lcdui.Graphics;
import java.util.Random;
public class Enemy {
public static final int STEP = 2;
public static final int DIR_STATIC = 0;
public static final int DIR_UP = 1;
public static final int DIR_DOWN = 2;
public static final int DIR_LEFT = 3;
public static final int DIR_RIGHT = 4;
private static final int REMAIN_CYC = 25;
private World world;
private int posX;
private int posY;
private int direction;
private int step;
private int[][] map;
private boolean isShow;
private int width;
private int height;
private ImageSet imageSet;
private Sprite sprite;
private boolean live;
private int bodyRemainCyc;
private boolean disuse;
private int score;
private int model;
private int[][] road;
private boolean isFind = false;
private int current_p = 0;
private int patrol_model = 0;
private int patrol_count = 0;
public Enemy(World world, int x, int y, int model)
{
this.world = world;
this.model = model;
imageSet = world.getImageSet();
sprite = new Sprite(imageSet, world.ENEMY1_RUN_RIGHT, 0);
posX = x;
posY = y;
init(model);
}
private void init(int mod)
{
step = STEP;
map = world.getMap();
isShow = false;
width = 24;
height = 32;
live = true;
disuse = false;
bodyRemainCyc = REMAIN_CYC;
isFind = false;
current_p = 0;
patrol_model = 0;//默认开始为自由模式
patrol_count = 0;//寻路计数器
switch (mod)
{
case World.ENEMY1:
direction = DIR_RIGHT;
score = 100;
break;
case World.ENEMY2:
direction = DIR_UP;
score = 100;
break;
case World.ENEMY3:
break;
case World.ENEMY4:
break;
case World.BOSS:
break;
}
}
public int getX()
{
return posX;
}
public int getY()
{
return posY;
}
public void setX(int x)
{
posX = x;
}
public void setY(int y)
{
posY = y;
}
public int getStep()
{
return step;
}
public void setStep(int s)
{
step = s;
}
public int getDirection()
{
return direction;
}
public void setDirection(int d)
{
direction = d;
}
public int getWidth()
{
return width;
}
public int getHeight()
{
return height;
}
public void cycle()
{
if (live) {
int viewX = world.getViewX();
int viewY = world.getViewY();
if (posX - viewX > world.getViewWidth() + 30
|| posY - viewY > world.getViewHeight() + 30
|| posX < viewX - 30 || posY < viewY - 30)
{
isShow = false;
}
else
{
isShow = true;
}
map = world.getMap();
int tileWidth = world.getTileWidth();
int tileHeight = world.getTileHeight();
//判定当前使用那种巡逻方式
if(patrol_model == 0)
{
Random r = new Random();
isFind = false;
int n = Math.abs(r.nextInt()) % 5;
if(world.getActor().isLive() == false)
{
if(n == 0)
{
n = 1;
}
}
if(n != 0)
{
patrol_model = 1;
patrol_count = 0;
direction = n;
}
else
{
patrol_model = 2;
patrol_count = 0;
searchRoad();
isFind = true;
current_p = 0;
}
System.out.println("------------------------");
System.out.println("patrol_model = " + patrol_model);
System.out.println("isFind = " + isFind);
System.out.println("direction = " + direction);
}
if(isFind == true)
{
if(patrol_count > 100)
{
patrol_count = 0;
patrol_model = 1;
direction = DIR_UP;
}
else
patrol_model = 2;
}
patrol_count++;
System.out.println("------------------------");
System.out.println("patrol_model = " + patrol_model);
System.out.println("isFind = " + isFind);
System.out.println("direction = " + direction);
if(patrol_model == 1)//若为自由模式
{
switch (direction)
{
case DIR_UP:
if (sprite.getCurrentState() != world.ENEMY1_RUN_UP)
sprite.setCurrentState(world.ENEMY1_RUN_UP, true);
if (!collidesDetect(posX, posY - step))
{
up();
}
else
{
patrol_model = 0;
}
break;
case DIR_DOWN:
if (sprite.getCurrentState() != world.ENEMY1_RUN_DOWN)
sprite.setCurrentState(world.ENEMY1_RUN_DOWN, true);
if (!collidesDetect(posX, posY + step))
{
down();
}
else
{
patrol_model = 0;
}
break;
case DIR_LEFT:
if (sprite.getCurrentState() != world.ENEMY1_RUN_LEFT)
sprite.setCurrentState(world.ENEMY1_RUN_LEFT, true);
if (!collidesDetect(posX - step, posY))
{
left();
}
else
{
patrol_model = 0;
}
break;
case DIR_RIGHT:
if (sprite.getCurrentState() != world.ENEMY1_RUN_RIGHT)
sprite.setCurrentState(world.ENEMY1_RUN_RIGHT, true);
if (!collidesDetect(posX + step, posY))
{
right();
}
else
{
patrol_model = 0;
}
break;
case DIR_STATIC:
{
//Add code on here:
}
break;
}
}
else if(patrol_model == 2)//若为追踪模式
{
int cx = road[current_p][1] * 32;
int cy = road[current_p][0] * 32;
if (cx == posX && cy == posY)
{
if (current_p == road.length - 1)
{
isFind = false;
patrol_model = 0;
}
else
{
current_p++;
cx = road[current_p][1] * 32;
cy = road[current_p][0] * 32;
}
}
if (isFind == true)
{
if (cx == posX && cy != posY)
{
if (cy > posY)
{
down();
}
else if (cy < posY)
{
up();
}
}
else if (cx != posX && cy == posY)
{
if (cx > posX)
{
right();
}
else if (cx < posX)
{
left();
}
}
}
}
sprite.cycle();
if (collidesDetect(world.getActor()))
{
world.getActor().dead();
patrol_model = 0;
}
}
else if (!disuse)
{
bodyRemainCyc--;
if (bodyRemainCyc < 0)
{
disuse = true;
}
}
System.out.println(patrol_model);
}
//寻路的简单替代
private void searchRoad()
{
int px = world.getActor().getX();
int py = world.getActor().getY();
int pi = py / 32;
int pj = px / 32;
int ei = this.posY / 32;
int ej = this.posX / 32;
road = SearchRoad.search(world.getMap(), ei, ej, pi, pj);
System.out.println("searchRoad:"+road);
}
public void render(Graphics gra)
{
if (live)
{
if (isShow == true)
{
sprite.draw(gra, posX - world.getViewX()
, posY - world.getViewY()
, Graphics.TOP | Graphics.LEFT);
gra.setColor(0x00ff00);
gra.drawRect(posX - world.getViewX(), posY - world.getViewY()
, width - 1, height - 1);
}
}
else if (!disuse)
{
world.getImageSet().draw(gra, World.ENEMY_DEAD, 0
, posX - world.getViewX()
, posY - world.getViewY()
, Graphics.TOP | Graphics.LEFT);
gra.setColor(0xffff00);
if (world.getActor() != null)
{
gra.drawString("+" + score
, world.getActor().getX() - world.getViewX()
, world.getActor().getY() - world.getViewY() - 10
, Graphics.TOP | Graphics.LEFT);
}
}
}
public void up()
{
if (!collidesDetect(posX, posY - step))
{
posY = posY - step;
}
}
public void down()
{
if (!collidesDetect(posX, posY + step))
{
posY = posY + step;
}
}
public void right()
{
if (!collidesDetect(posX + step, posY))
{
posX = posX + step;
}
}
public void left()
{
if (!collidesDetect(posX - step, posY))
{
posX = posX - step;
}
}
private boolean collidesDetect(int x, int y)
{
int x1 = x;
int y1 = y;
int x2 = x1 + width - 1;
int y2 = y1;
int x3 = x1 + width - 1;
int y3 = y1 + height - 1;
int x4 = x1;
int y4 = y1 + height - 1;
int tileX = 0;
int tileY = 0;
int tileW = world.getTileWidth();
int tileH = world.getTileHeight();
//detected up-left
tileX = y1 / tileH;
tileY = x1 / tileW;
if (!world.canAcross(tileX, tileY, false))
{
return true;
}
//detected up-right
tileX = y2 / tileH;
tileY = x2 / tileW;
if (!world.canAcross(tileX, tileY, false))
{
return true;
}
//detected bottom-right
tileX = y3 / tileH;
tileY = x3 / tileW;
if (!world.canAcross(tileX, tileY, false))
{
return true;
}
//detected bottom-left
tileX = y4 / tileH;
tileY = x4 / tileW;
if (!world.canAcross(tileX, tileY, false))
{
return true;
}
return false;
}
private boolean collidesDetect(Actor actor)
{
int[][] a = new int[4][2];
int[][] b = new int[4][2];
int awidth = actor.getWidth();
int aheight = actor.getHeight();
int ax1 = actor.getX();
int ay1 = actor.getY();
a[0][0] = ax1;
a[0][1] = ay1;
a[1][0] = ax1 + awidth - 1;
a[1][1] = ay1;
a[2][0] = ax1 + awidth - 1;
a[2][1] = ay1 + aheight - 1;
a[3][0] = ax1;
a[3][1] = ay1 + aheight - 1;
int ex1 = posX;
int ey1 = posY;
b[0][0] = ex1;
b[0][1] = ey1;
b[1][0] = ex1 + width - 1;
b[1][1] = ey1;
b[2][0] = ex1 + width - 1;
b[2][1] = ey1 + height - 1;
b[3][0] = ex1;
b[3][1] = ey1 + height - 1;
if (World.collidesRect(a, b))
{
return true;
}
return false;
}
public void dead()
{
live = false;
world.addScore(score);
}
public boolean isLive()
{
return live;
}
public boolean isDisuse()
{
return disuse;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?