📄 sprite.java
字号:
this.y = oldY - dRefY; } else { // just reinitialize the animation frames. initializeFrames(img, frameWidth, frameHeight, maintainCurFrame); } } public void defineCollisionRectangle(int x, int y, int width, int height) { if (width < 0 || height < 0) { throw new IllegalArgumentException(); } collisionRectX = x; collisionRectY = y; collisionRectWidth = width; collisionRectHeight = height; } //transfrom is ignored public void setTransform(int transform) { } //pixellevel nor supported public final boolean collidesWith(Sprite s, boolean pixelLevel) { // check if either of the Sprite's are not visible if (!(s.visible && this.visible)) { return false; } // these are package private // and can be accessed directly int otherLeft = s.x + s.collisionRectX; int otherTop = s.y + s.collisionRectY; int otherRight = otherLeft + s.collisionRectWidth; int otherBottom = otherTop + s.collisionRectHeight; int left = this.x + this.collisionRectX; int top = this.y + this.collisionRectY; int right = left + this.collisionRectWidth; int bottom = top + this.collisionRectHeight; // check if the collision rectangles of the two sprites intersect if (intersectRect(otherLeft, otherTop, otherRight, otherBottom, left, top, right, bottom)) { return true; } return false; } //pixellevel not supported public final boolean collidesWith(TiledLayer t, boolean pixelLevel) { // check if either this Sprite or the TiledLayer is not visible if (!(t.visible && this.visible)) { return false; } // dimensions of tiledLayer, cell, and // this Sprite's collision rectangle // these are package private // and can be accessed directly int tLx1 = t.x; int tLy1 = t.y; int tLx2 = tLx1 + t.width; int tLy2 = tLy1 + t.height; int tW = t.cellWidth; int tH = t.cellHeight; int sx1 = this.x + this.collisionRectX; int sy1 = this.y + this.collisionRectY; int sx2 = sx1 + this.collisionRectWidth; int sy2 = sy1 + this.collisionRectHeight; // number of cells int tNumCols = t.columns; int tNumRows = t.rows; // temporary loop variables. int startCol; // = 0; int endCol; // = 0; int startRow; // = 0; int endRow; // = 0; if (!intersectRect(tLx1, tLy1, tLx2, tLy2, sx1, sy1, sx2, sy2)) { // if the collision rectangle of the sprite // does not intersect with the dimensions of the entire // tiled layer return false; } // so there is an intersection // note sx1 < sx2, tLx1 < tLx2, sx2 > tLx1 from intersectRect() // use <= for comparison as this saves us some // computation - the result will be 0 startCol = (sx1 <= tLx1) ? 0 : (sx1 - tLx1)/tW; startRow = (sy1 <= tLy1) ? 0 : (sy1 - tLy1)/tH; // since tLx1 < sx2 < tLx2, the computation will yield // a result between 0 and tNumCols - 1 // subtract by 1 because sx2,sy2 represent // the enclosing bounds of the sprite, not the // locations in the coordinate system. endCol = (sx2 < tLx2) ? ((sx2 - 1 - tLx1)/tW) : tNumCols - 1; endRow = (sy2 < tLy2) ? ((sy2 - 1 - tLy1)/tH) : tNumRows - 1; //if (!pixelLevel) { // check for intersection with a non-empty cell, for (int row = startRow; row <= endRow; row++) { for (int col = startCol; col <= endCol; col++) { if (t.cellMatrix[row][col] != 0) { return true; } } } // worst case! we scanned through entire // overlapping region and // all the cells are empty! return false; } //pixellevel not supported public final boolean collidesWith(Image image, int x, int y, boolean pixelLevel) { // check if this Sprite is not visible if (!(this.visible)) { return false; } // if image is null // image.getWidth() will throw NullPointerException int otherLeft = x; int otherTop = y; int otherRight = x + image.getWidth(); int otherBottom = y + image.getHeight(); int left = this.x + this.collisionRectX; int top = this.y + this.collisionRectY; int right = left + this.collisionRectWidth; int bottom = top + this.collisionRectHeight; // first check if the collision rectangles of the two sprites intersect if (intersectRect(otherLeft, otherTop, otherRight, otherBottom, left, top, right, bottom)) { return true; } return false; } // ----- private ----- 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; srcFrameWidth = fWidth; 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) { frameCoordsX[currentFrame] = xx; frameCoordsY[currentFrame] = yy; if (!customSequenceDefined) { frameSequence[currentFrame] = currentFrame; } currentFrame++; } } } /** * initialize the collision rectangle */ private void initCollisionRectBounds() { // reset x and y of collision rectangle collisionRectX = 0; collisionRectY = 0; // intialize the collision rectangle bounds to that of the sprite collisionRectWidth = this.width; collisionRectHeight = this.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 static final int INVERTED_AXES = 0x4; private static final int X_FLIP = 0x2; private static final int Y_FLIP = 0x1; private static final int ALPHA_BITMASK = 0xff000000; Image sourceImage; int numberFrames; // = 0; int[] frameCoordsX; int[] frameCoordsY; int srcFrameWidth; int srcFrameHeight; int[] frameSequence; private int sequenceIndex; // = 0 private boolean customSequenceDefined; // = false; int dRefX; // =0 int dRefY; // =0 int collisionRectX; // =0 int collisionRectY; // =0 int collisionRectWidth; int collisionRectHeight; int t_currentTransformation; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -