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; import java.awt.image.BufferedImage; public class BaseSprite extends BaseLayer { private Image[] mFrames; private int mFrameIndex; private int[] mFrameSequence; private Rectangle mBounds; 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)); } 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)); } 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 getWidth() { return mFrames[0].getWidth(null); } public void move(int dx, int dy) { super.move(dx, dy); } public void nextFrame() { mFrameIndex++; if (mFrameIndex == mFrameSequence.length) { mFrameIndex = 0; } } public void paint(Graphics g) { if (isVisible()){ g.drawImage(mFrames[mFrameIndex],getX(),getY(),null); } } public void prevFrame() { mFrameIndex--; if (mFrameIndex < 0) { mFrameIndex = mFrameSequence.length - 1; } } 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); } }