📄 actor.java
字号:
import javax.microedition.lcdui.*;
import java.util.Random;
abstract public class Actor {
protected PigWorld world;
public static final int FLYPIG = 1;
public static final int NPC = 2;
public static final int BULLET = 3;
public static final int BOSS = 4;
public static final int daoju = 5;
public final static int UP = 1;
public final static int DOWN = 2;
public final static int LEFT = 3;
public final static int RIGHT = 4;
public static final int G_TOP_LEFT = Graphics.LEFT | Graphics.TOP;
private static final Random randomizer = new Random();
protected boolean crash;
protected int direction, x, y, type, state;
protected int speedX, speedY, speedOffsetY;
protected int energy, energyMax;
protected int imageW, imageH;
protected boolean mode, release, isFire;
protected int delayTime, fireTime;
private Actor nextLink;
private Actor prevLink;
public Actor(PigWorld pWorld) {
world = pWorld;
}
public void init(int typeArg, int stateArg, int startXArg, int startYArg,
int directionArg, int speedXArg, int speedYArg) {
type = typeArg;
state = stateArg;
x = startXArg;
y = startYArg;
direction = directionArg;
speedX = speedXArg;
speedY = speedYArg;
release = false;
crash = true;
isFire = false;
fireTime = 20;
delayTime = 200;
}
abstract public void render(Graphics graphics, int offsetX, int offsetY);
public void cycle() {
switch (direction) {
case UP:
y -= speedY + speedOffsetY;
break;
case DOWN:
y += speedY - speedOffsetY;
break;
case LEFT:
x -= speedX;
y -= speedOffsetY;
break;
case RIGHT:
x += speedX;
y -= speedOffsetY;
break;
}
if (type == BOSS || type == FLYPIG) {
if (x < world.viewX) {
if (type == BOSS) {
direction = RIGHT;
}
x = world.viewX;
}
if (x > world.viewX + PigCanvas.SCRW - imageW) {
if (type == BOSS) {
direction = LEFT;
}
x = world.viewX + PigCanvas.SCRW - imageW;
}
}
if (type == FLYPIG) {
if (y < world.viewY) {
y = world.viewY;
}
if (y > world.viewY + PigCanvas.SCRH - imageH) {
y = world.viewY + PigCanvas.SCRH - imageH;
}
}
if (type == BOSS) {
if (y < world.viewY - imageH) {
y = world.viewY - imageH;
release = true;
}
if (y > world.viewY + 15) {
y = world.viewY + 15;
}
}
if (type == NPC || type == daoju) {
if (x < 0) {
x = 0;
}
if (x > world.MAP_W * world.TILE_H -imageW ) {
x = world.MAP_W * world.TILE_H-imageW ;
}
if (y < world.viewY - 50) {
y = world.viewY - 50;
release = true;
}
if (y > world.viewY + PigCanvas.SCRH ) {
y = world.viewY + PigCanvas.SCRH ;
release = true;
}
}
if (crash) {
world.checkCollision(this, x, y, imageW, imageH);
}
// if (type != FLYPIG) {
// if (PigCanvas.getGameScreen().getIsSlow()) {
// if (speedX > 0 || speedY > 0) {
// if (type == BOSS) {
// speedX = 1;
// }
// if (type == NPC) {
// speedY = 1;
// }
// if (type == BULLET) {
// speedY = 4;
// }
// }
// delayTime = 400;
// fireTime = 40;
//
// } else {
// if (speedX > 0 || speedY > 0) {
// if (type == BOSS) {
// speedX = 3;
// }
// if (type == NPC) {
// speedY = 2;
// }
// if (type == BULLET) {
// speedY = 6;
// }
// }
// delayTime = 200;
// fireTime = 20;
//
// }
// }
}
public void onCollision(Actor a) {
}
public final boolean collideWith(int ax, int ay, int w, int h) {
if (y + imageH < ay || y > ay + h ||
x + imageW < ax || x > ax + w) {
return false;
}
return true;
}
public final boolean collideWith(Actor a) {
return collideWith(a.x, a.y, a.imageW, a.imageH);
}
public void setSize(int w, int h) {
imageW = w;
imageH = h;
}
// public boolean isBorder(int leftX, int rightX, int topY, int bottomY) {
// if (x < leftX) {
// x = leftX;
// return true;
// }
// if (x > rightX) {
// x = rightX;
// return true;
// }
// if (y < topY) {
// y = topY;
// return true;
// }
// if(y>bottomY){
// y=bottomY;
// return true;
// }
// return false;
// }
public static final int getRand(int min, int max) {
int r = Math.abs(randomizer.nextInt());
return (r % (max - min + 1)) + min;
}
public static final boolean intersectionRect(int ax, int ay,
int aw, int ah, int bx, int by, int bw, int bh) {
if (by + bh < ay || // is the bottom b above the top of a?
by > ay + ah || // is the top of b below bottom of a?
bx + bw < ax || // is the right of b to the left of a?
bx > ax + aw) { // is the left of b to the right of a?
return false;
}
return true;
}
public static final boolean pointInRect(int px, int py, int x, int y,
int w, int h) {
if (px < x) {
return false;
}
if (px > x + w) {
return false;
}
if (py < y) {
return false;
}
if (py > y + h) {
return false;
}
return true;
}
public final Actor getNextLink() {
return nextLink;
}
public final void setNextLink(Actor nextLinkArg) {
nextLink = nextLinkArg;
}
public final Actor getPrevLink() {
return prevLink;
}
public final void setPrevLink(Actor prevLinkArg) {
prevLink = prevLinkArg;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -