enemy.java
来自「基于J2ME的手机游戏软件。可以控制游戏人物在地图上上下左右行走;可以在地图上放」· Java 代码 · 共 345 行
JAVA
345 行
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;
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;
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;
}
//自动路径搜索
private int[] searchRoad(int targetX, int targetY,
int currentX, int currentY,
int[][] bufMap) {
int[] result = new int[2];
//Add code on here:
//
result[0] = -1;
result[1] = -1;
return result;
}
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();
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 {
direction = findDirect();
}
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 {
direction = findDirect();
}
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 {
direction = findDirect();
}
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 {
direction = findDirect();
}
break;
case DIR_STATIC: {
//Add code on here:
}
break;
}
sprite.cycle();
if (collidesDetect(world.getActor())) {
world.getActor().dead();
// System.out.println("collides");
}
}
else if (!disuse) {
bodyRemainCyc--;
if (bodyRemainCyc < 0) {
disuse = true;
}
}
}
//寻路的简单替代
private int findDirect()
{
Random r = new Random();
int d = Math.abs(r.nextInt()) % 5;
if(d == 0)
{
d = 1;
}
return d;
}
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 + -
显示快捷键?