package com.samodelkin; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Point; import java.awt.Rectangle; import java.awt.geom.AffineTransform; public class BaseSprite extends BaseLayer { private Image[] mFrames; private int mFrameIndex; private int[] mFrameSequence; private Rectangle mBounds; private AffineTransform trans; private int degress, refPixelX, refPixelY; public BaseSprite(Image image) { super(); Image[] mFrames = new Image[]{image}; mFrameIndex = 0; mFrameSequence = new int[]{0}; mBounds = new Rectangle(0, 0, image.getWidth(null), image.getHeight(null)); trans = new AffineTransform(); trans.setToTranslation(0, 0); degress = 0; refPixelX = this.getWidth() / 2; refPixelY = this.getHeight() / 2; } public BaseSprite(Image[] images) { super(); mFrames = images; mFrameIndex = 0; mFrameSequence = new int[images.length]; for (int n = 0; n < images.length; n++) { mFrameSequence[n] = n; } mBounds = new Rectangle(0, 0, images[0].getWidth(null), images[0].getHeight(null)); trans = new AffineTransform(); degress = 0; refPixelX = this.getWidth() / 2; refPixelY = this.getHeight() / 2; } public boolean collidesWith(BaseSprite s) { boolean c = false; if (this.isVisible()) { Rectangle rect = new Rectangle(); Rectangle.intersect(getBoundsRect(), s.getBoundsRect(), rect); if (rect.getWidth() > 0 && rect.getHeight() > 0) { c = true; } } return c; } public boolean collidesWith(Point point) { boolean c = false; if (this.isVisible()) { Rectangle rect = new Rectangle(); Rectangle.intersect(getBoundsRect(), new Rectangle(point.x, point.y, 1, 1), rect); if (rect.getWidth() > 0 && rect.getHeight() > 0) { c = true; } } return c; } public boolean collidesWith(Rectangle rect) { boolean c = false; if (this.isVisible()) { Rectangle rect2 = new Rectangle(); Rectangle.intersect(getBoundsRect(), rect, rect2); if (rect2.getWidth() > 0 && rect2.getHeight() > 0) { c = true; } } return c; } public void defineCollisionRect(Rectangle rect) { mBounds.setBounds(rect); } public Rectangle getBoundsRect() { return mBounds; } public int getFrameIndex() { return mFrameIndex; } public int getFrameSequenceLength() { return mFrameSequence.length; } public int getHeight() { return mFrames[0].getHeight(null); } public int getRefX() { return this.refPixelX; } public int getRefY() { return this.refPixelY; } public int getTurn() { return this.degress; } public int getWidth() { return mFrames[0].getWidth(null); } public void move(int dx, int dy) { super.move(dx, dy); trans.translate(dx, dy); } public void nextFrame() { mFrameIndex++; if (mFrameIndex == mFrameSequence.length) { mFrameIndex = 0; } } public void paint(Graphics g) { if (isVisible()){ Graphics2D g2d=(Graphics2D)g; g2d.drawImage(mFrames[mFrameIndex], trans, null); } } public void prevFrame() { mFrameIndex--; if (mFrameIndex < 0) { mFrameIndex = mFrameSequence.length - 1; } } public void resetRotate() { degress = 0; trans.setToRotation(Math.toRadians(degress)); } public void rotate(int degress) { this.degress += degress; if (this.degress < 0) { this.degress = 360 + this.degress; } } public void setFrame(int frame) { this.mFrameIndex = frame; } public void setFrameSequence(int[] frameSequence) { mFrameSequence = new int[frameSequence.length]; System.arraycopy(frameSequence, 0, mFrameSequence, 0, frameSequence.length); } public void setPosition(int x, int y) { super.setPosition(x, y); trans.setToTranslation(x, y); } public void setRefPixel(int refX, int refY) { this.refPixelX = refX; this.refPixelY = refY; } }