sprite.java

来自「手机射击游戏源代码,nokia s60模拟器开发包,eclipse工具开发.不可」· Java 代码 · 共 508 行

JAVA
508
字号
package src;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class Sprite extends Layer {
	public Sprite(Image image) {
		super(image.getWidth(), image.getHeight());
		initializeFrames(image, image.getWidth(), image.getHeight(), false);
		initCollisionRectBounds();
//		setTransformImpl(0);
	}

	public Sprite(Image image, int frameWidth, int frameHeight) {
		super(frameWidth, frameHeight);
		if (frameWidth < 1 || frameHeight < 1
				|| image.getWidth() % frameWidth != 0
				|| image.getHeight() % frameHeight != 0) {
			throw new IllegalArgumentException();
		} else {
			initializeFrames(image, frameWidth, frameHeight, false);
			initCollisionRectBounds();
//			setTransformImpl(0);
			return;
		}
	}

	public Sprite(Sprite s) {
		super(s == null ? 0 : s.getWidth(), s == null ? 0 : s.getHeight());
		if (s == null) {
			throw new NullPointerException();
		} else {
			sourceImage = Image.createImage(s.sourceImage);
			numberFrames = s.numberFrames;
			frameCoordsX = new int[numberFrames];
			frameCoordsY = new int[numberFrames];
			System.arraycopy(s.frameCoordsX, 0, frameCoordsX, 0, s
					.getRawFrameCount());
			System.arraycopy(s.frameCoordsY, 0, frameCoordsY, 0, s
					.getRawFrameCount());
			x = s.x;
			y = s.y;
//			dRefX = s.dRefX;
//			dRefY = s.dRefY;
			collisionRectX = s.collisionRectX;
			collisionRectY = s.collisionRectY;
			collisionRectWidth = s.collisionRectWidth;
			collisionRectHeight = s.collisionRectHeight;
			srcFrameWidth = s.srcFrameWidth;
			srcFrameHeight = s.srcFrameHeight;
//			setTransformImpl(s.t_currentTransformation);
			this.visible = s.visible;
			frameSequence = new int[s.getFrameSequenceLength()];
			setFrameSequence(s.frameSequence);
			setFrame(s.getFrame());
//			setRefPixelPosition(s.getRefPixelX(), s.getRefPixelY());
			return;
		}
	}

//	public void defineReferencePixel(int x, int y) {
//		dRefX = x;
//		dRefY = y;
//	}
//
//	public void setRefPixelPosition(int x, int y) {
//		this.x = x - dRefX;
//		this.y = y - dRefY;
//	}
//
//	public int getRefPixelX() {
//		return x + dRefX;
//	}
//
//	public int getRefPixelY() {
//		return y + dRefY;
//	}

	public void setFrame(int sequenceIndex) {
		if (sequenceIndex < 0 || sequenceIndex >= frameSequence.length) {
			throw new IndexOutOfBoundsException();
		} else {
			this.sequenceIndex = sequenceIndex;
			return;
		}
	}

	public final int getFrame() {
		return sequenceIndex;
	}

	public int getRawFrameCount() {
		return numberFrames;
	}

	public int getFrameSequenceLength() {
		return frameSequence.length;
	}

	public void nextFrame() {
		sequenceIndex = (sequenceIndex + 1) % frameSequence.length;
	}

	public void prevFrame() {
		if (sequenceIndex == 0)
			sequenceIndex = frameSequence.length - 1;
		else
			sequenceIndex--;
	}

	public final void paint(Graphics g) {
		if (g == null)
			throw new NullPointerException();
		if (visible) {
//			System.out.println("x=" + x + ", y=" + y + ", srcFrameWidth=" + srcFrameWidth + ", srcFrameHeight=" + srcFrameHeight);
//			g.drawImage(sourceImage, 0, 0, Graphics.LEFT | Graphics.TOP);
			g.setClip(this.x, this.y, this.srcFrameWidth, this.srcFrameHeight);
//			System.out.println("frameCoordsX=" + frameCoordsX[frameSequence[sequenceIndex]] + ", frameCoordsY=" + frameCoordsY[frameSequence[sequenceIndex]]);
			g.drawImage(sourceImage, x-frameCoordsX[frameSequence[sequenceIndex]], y-frameCoordsY[frameSequence[sequenceIndex]], Graphics.LEFT | Graphics.TOP);
		}
		//            g.drawRegion(sourceImage, frameCoordsX[frameSequence[sequenceIndex]], frameCoordsY[frameSequence[sequenceIndex]], srcFrameWidth, srcFrameHeight, t_currentTransformation, x, y, 20);
	}

	public void setFrameSequence(int sequence[]) {
		if (sequence == null) {
			sequenceIndex = 0;
			customSequenceDefined = false;
			frameSequence = new int[numberFrames];
			for (int i = 0; i < numberFrames; i++)
				frameSequence[i] = i;

			return;
		}
		if (sequence.length < 1)
			throw new IllegalArgumentException();
		for (int i = 0; i < sequence.length; i++)
			if (sequence[i] < 0 || sequence[i] >= numberFrames)
				throw new ArrayIndexOutOfBoundsException();

		customSequenceDefined = true;
		frameSequence = new int[sequence.length];
		System.arraycopy(sequence, 0, frameSequence, 0, sequence.length);
		sequenceIndex = 0;
	}

	public void setImage(Image img, int frameWidth, int frameHeight) {
		if (frameWidth < 1 || frameHeight < 1
				|| img.getWidth() % frameWidth != 0
				|| img.getHeight() % frameHeight != 0)
			throw new IllegalArgumentException();
		int noOfFrames = (img.getWidth() / frameWidth)
				* (img.getHeight() / frameHeight);
		boolean maintainCurFrame = true;
		if (noOfFrames < numberFrames) {
			maintainCurFrame = false;
			customSequenceDefined = false;
		}
		if (frameWidth != frameWidth || frameHeight != frameHeight) {
//			int oldX = x + dRefX;
//			int oldY = y + dRefY;
			setWidthImpl(frameWidth);
			setHeightImpl(frameHeight);
			initializeFrames(img, frameWidth, frameHeight, maintainCurFrame);
			initCollisionRectBounds();
//			x = oldX - dRefX;
//			y = oldY - dRefY;
//			computeTransformedBounds(t_currentTransformation);
		} else {
			initializeFrames(img, frameWidth, frameHeight, maintainCurFrame);
		}
	}

	public void defineCollisionRectangle(int x, int y, int width, int height) {
		if (width < 0 || height < 0) {
			throw new IllegalArgumentException();
		} else {
			collisionRectX = x;
			collisionRectY = y;
			collisionRectWidth = width;
			collisionRectHeight = height;
//			setTransformImpl(t_currentTransformation);
			return;
		}
	}
//
//	public void setTransform(int transform) {
//		setTransformImpl(transform);
//	}

	public final boolean collidesWith(Sprite s) {
		if (!s.visible || !visible)
			return false;
		int otherLeft = s.x + s.collisionRectX;
		int otherTop = s.y + s.collisionRectY;
		int otherRight = otherLeft + s.collisionRectWidth;
		int otherBottom = otherTop + s.collisionRectHeight;
		int left = x + collisionRectX;
		int top = y + collisionRectY;
		int right = left + collisionRectWidth;
		int bottom = top + collisionRectHeight;
		return intersectRect(otherLeft, otherTop, otherRight, otherBottom,
				left, top, right, bottom);
	}

	public final boolean collidesWith(TiledLayer t) {
		if (!t.visible || !visible)
			return false;
		int tLx1 = t.x;
		int tLy1 = t.y;
		int tLx2 = tLx1 + t.width;
		int tLy2 = tLy1 + t.height;
		int tW = t.getCellWidth();
		int tH = t.getCellHeight();
		int sx1 = x + collisionRectX;
		int sy1 = y + collisionRectY;
		int sx2 = sx1 + collisionRectWidth;
		int sy2 = sy1 + collisionRectHeight;
		int tNumCols = t.getColumns();
		int tNumRows = t.getRows();
		if (!intersectRect(tLx1, tLy1, tLx2, tLy2, sx1, sy1, sx2, sy2))
			return false;
		int startCol = sx1 > tLx1 ? (sx1 - tLx1) / tW : 0;
		int startRow = sy1 > tLy1 ? (sy1 - tLy1) / tH : 0;
		int endCol = sx2 >= tLx2 ? tNumCols - 1 : (sx2 - 1 - tLx1) / tW;
		int endRow = sy2 >= tLy2 ? tNumRows - 1 : (sy2 - 1 - tLy1) / tH;
		for (int row = startRow; row <= endRow; row++) {
			for (int col = startCol; col <= endCol; col++)
				if (t.getCell(col, row) != 0)
					return true;

		}

		return false;
	}

	public final boolean collidesWith(Image image, int x, int y) {
		if (!visible)
			return false;
		int otherLeft = x;
		int otherTop = y;
		int otherRight = x + image.getWidth();
		int otherBottom = y + image.getHeight();
		int left = this.x + collisionRectX;
		int top = this.y + collisionRectY;
		int right = left + collisionRectWidth;
		int bottom = top + collisionRectHeight;
		return intersectRect(otherLeft, otherTop, otherRight, otherBottom,
				left, top, right, bottom);
	}

	private void initializeFrames(Image image, int fWidth, int fHeight,
			boolean maintainCurFrame) {
		int imageW = image.getWidth();
		int imageH = image.getHeight();
		int numHorizontalFrames = imageW / fWidth;
		int numVerticalFrames = imageH / fHeight;
		sourceImage = image;
		this.srcFrameWidth = fWidth;
		this.srcFrameHeight = fHeight;
		numberFrames = numHorizontalFrames * numVerticalFrames;
		frameCoordsX = new int[numberFrames];
		frameCoordsY = new int[numberFrames];
		if (!maintainCurFrame)
			sequenceIndex = 0;
		if (!customSequenceDefined)
			frameSequence = new int[numberFrames];
		int currentFrame = 0;
		for (int yy = 0; yy < imageH; yy += fHeight) {
			for (int xx = 0; xx < imageW; xx += fWidth) {
//				System.out.println("xx=" + xx + ", yy=" + yy);
				frameCoordsX[currentFrame] = xx;
				frameCoordsY[currentFrame] = yy;
				if (!customSequenceDefined)
					frameSequence[currentFrame] = currentFrame;
				currentFrame++;
			}

		}

	}

	private void initCollisionRectBounds() {
		collisionRectX = 0;
		collisionRectY = 0;
		collisionRectWidth = width;
		collisionRectHeight = height;
	}

	private boolean intersectRect(int r1x1, int r1y1, int r1x2, int r1y2,
			int r2x1, int r2y1, int r2x2, int r2y2) {
		return r2x1 < r1x2 && r2y1 < r1y2 && r2x2 > r1x1 && r2y2 > r1y1;
	}

//	private int getImageTopLeftX(int x1, int y1, int x2, int y2) {
//		int retX = x1 - x;
//		retX += frameCoordsX[frameSequence[sequenceIndex]];
//		return retX;
//	}
//
//	private int getImageTopLeftY(int x1, int y1, int x2, int y2) {
//		int retY = y1 - y;
//		retY += frameCoordsY[frameSequence[sequenceIndex]];
//		return retY;
//	}
//
//	private void setTransformImpl(int transform) {
//		x = (x + getTransformedPtX(dRefX, dRefY, t_currentTransformation))
//				- getTransformedPtX(dRefX, dRefY, transform);
//		y = (y + getTransformedPtY(dRefX, dRefY, t_currentTransformation))
//				- getTransformedPtY(dRefX, dRefY, transform);
//		computeTransformedBounds(transform);
//		t_currentTransformation = transform;
//	}

//	private void computeTransformedBounds(int transform) {
//		switch (transform) {
//		case 0: // '\0'
//			t_collisionRectX = collisionRectX;
//			t_collisionRectY = collisionRectY;
//			t_collisionRectWidth = collisionRectWidth;
//			t_collisionRectHeight = collisionRectHeight;
//			width = srcFrameWidth;
//			height = srcFrameHeight;
//			break;
//
//		case 2: // '\002'
//			t_collisionRectX = srcFrameWidth
//					- (collisionRectX + collisionRectWidth);
//			t_collisionRectY = collisionRectY;
//			t_collisionRectWidth = collisionRectWidth;
//			t_collisionRectHeight = collisionRectHeight;
//			width = srcFrameWidth;
//			height = srcFrameHeight;
//			break;
//
//		case 1: // '\001'
//			t_collisionRectY = srcFrameHeight
//					- (collisionRectY + collisionRectHeight);
//			t_collisionRectX = collisionRectX;
//			t_collisionRectWidth = collisionRectWidth;
//			t_collisionRectHeight = collisionRectHeight;
//			width = srcFrameWidth;
//			height = srcFrameHeight;
//			break;
//
//		case 5: // '\005'
//			t_collisionRectX = srcFrameHeight
//					- (collisionRectHeight + collisionRectY);
//			t_collisionRectY = collisionRectX;
//			t_collisionRectHeight = collisionRectWidth;
//			t_collisionRectWidth = collisionRectHeight;
//			width = srcFrameHeight;
//			height = srcFrameWidth;
//			break;
//
//		case 3: // '\003'
//			t_collisionRectX = srcFrameWidth
//					- (collisionRectWidth + collisionRectX);
//			t_collisionRectY = srcFrameHeight
//					- (collisionRectHeight + collisionRectY);
//			t_collisionRectWidth = collisionRectWidth;
//			t_collisionRectHeight = collisionRectHeight;
//			width = srcFrameWidth;
//			height = srcFrameHeight;
//			break;
//
//		case 6: // '\006'
//			t_collisionRectX = collisionRectY;
//			t_collisionRectY = srcFrameWidth
//					- (collisionRectWidth + collisionRectX);
//			t_collisionRectHeight = collisionRectWidth;
//			t_collisionRectWidth = collisionRectHeight;
//			width = srcFrameHeight;
//			height = srcFrameWidth;
//			break;
//
//		case 7: // '\007'
//			t_collisionRectX = srcFrameHeight
//					- (collisionRectHeight + collisionRectY);
//			t_collisionRectY = srcFrameWidth
//					- (collisionRectWidth + collisionRectX);
//			t_collisionRectHeight = collisionRectWidth;
//			t_collisionRectWidth = collisionRectHeight;
//			width = srcFrameHeight;
//			height = srcFrameWidth;
//			break;
//
//		case 4: // '\004'
//			t_collisionRectY = collisionRectX;
//			t_collisionRectX = collisionRectY;
//			t_collisionRectHeight = collisionRectWidth;
//			t_collisionRectWidth = collisionRectHeight;
//			width = srcFrameHeight;
//			height = srcFrameWidth;
//			break;
//
//		default:
//			throw new IllegalArgumentException();
//		}
//	}

//	int getTransformedPtX(int x, int y, int transform) {
//		int t_x = 0;
//		switch (transform) {
//		case 0: // '\0'
//			t_x = x;
//			break;
//		case 2: // '\002'
//			t_x = srcFrameWidth - x - 1;
//			break;
//		case 1: // '\001'
//			t_x = x;
//			break;
//		case 5: // '\005'
//			t_x = srcFrameHeight - y - 1;
//			break;
//		case 3: // '\003'
//			t_x = srcFrameWidth - x - 1;
//			break;
//		case 6: // '\006'
//			t_x = y;
//			break;
//		case 7: // '\007'
//			t_x = srcFrameHeight - y - 1;
//			break;
//		case 4: // '\004'
//			t_x = y;
//			break;
//		default:
//			throw new IllegalArgumentException();
//		}
//		return t_x;
//	}
//
//	int getTransformedPtY(int x, int y) {
//		int t_y = 0;
//		switch (transform) {
//		case 0: // '\0'
//			t_y = y;
//			break;
//
//		case 2: // '\002'
//			t_y = y;
//			break;
//
//		case 1: // '\001'
//			t_y = srcFrameHeight - y - 1;
//			break;
//
//		case 5: // '\005'
//			t_y = x;
//			break;
//
//		case 3: // '\003'
//			t_y = srcFrameHeight - y - 1;
//			break;
//
//		case 6: // '\006'
//			t_y = srcFrameWidth - x - 1;
//			break;
//
//		case 7: // '\007'
//			t_y = srcFrameWidth - x - 1;
//			break;
//
//		case 4: // '\004'
//			t_y = x;
//			break;
//
//		default:
//			throw new IllegalArgumentException();
//		}
//		return t_y;
//	}

//	public static final int TRANS_NONE = 0;
//	public static final int TRANS_ROT90 = 5;
//	public static final int TRANS_ROT180 = 3;
//	public static final int TRANS_ROT270 = 6;
//	public static final int TRANS_MIRROR = 2;
//	public static final int TRANS_MIRROR_ROT90 = 7;
//	public static final int TRANS_MIRROR_ROT180 = 1;
//	public static final int TRANS_MIRROR_ROT270 = 4;
	private static final int INVERTED_AXES = 4;
	private static final int X_FLIP = 2;
	private static final int Y_FLIP = 1;
	private static final int ALPHA_BITMASK = 0xff000000;

	Image sourceImage;
	int numberFrames;
	int frameCoordsX[];
	int frameCoordsY[];
	int srcFrameWidth;
	int srcFrameHeight;
	int frameSequence[];
	private int sequenceIndex;
	private boolean customSequenceDefined;
//	int dRefX;
//	int dRefY;
	int collisionRectX;
	int collisionRectY;
	int collisionRectWidth;
	int collisionRectHeight;
//	int t_currentTransformation;
//	int t_collisionRectX;
//	int t_collisionRectY;
//	int t_collisionRectWidth;
//	int t_collisionRectHeight;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?