📄 sprite.java
字号:
int oW = 0, oH = 0;
while (another) {
// first calculate the actual rectangle we must check
// for this sprite
// this are for the reduced collision rectangle
int cX, cY, cW, cH;
// this are for the intersection of the bounds rectangle
// and collision rectangle, taking into account
// position and transform
int sX, sY, sW, sH;
// take the collision rectangle in account
// but since the pixels outside the frame are
// considered transparent, first we have to
// take the intersection between the collision
// rectangle and the frame bounds
if (t.collX >= t.getWidth() || t.collX + t.collWidth <= 0
|| t.collY >= t.getHeight() || t.collY + t.collHeight <= 0)
// collision rectangle outside frame bounds
return false;
cX = (t.collX >= 0)? t.collX : 0;
cY = (t.collY >= 0)? t.collY : 0;
cW = (t.collX + t.collWidth < t.getWidth())? t.collX + t.collWidth - cX : t.getWidth() - cX;
cH = (t.collY + t.collHeight < t.getHeight())? t.collY + t.collHeight - cY : t.getHeight() - cY;
switch(t.transform) {
case TRANS_NONE:
sX = t.getX() + cX;
sY = t.getY() + cY;
sW = cW;
sH = cH;
break;
case TRANS_MIRROR_ROT180:
sX = t.getX() + cX;
sY = t.getY() + (t.getHeight() - cY - 1) - cH;
sW = cW;
sH = cH;
break;
case TRANS_MIRROR:
sX = t.getX() + (t.getWidth() - cX - 1) - cW;
sY = t.getY() + cY;
sW = cW;
sH = cH;
break;
case TRANS_ROT180:
sX = t.getX() + (t.getWidth() - cX - 1) - cW;
sY = t.getY() + (t.getHeight() - cY - 1) - cH;
sW = cW;
sH = cH;
break;
case TRANS_MIRROR_ROT270:
sX = t.getX() + cY;
sY = t.getY() + cX;
sW = cH;
sH = cW;
break;
case TRANS_ROT90:
sX = t.getX() + (t.getHeight() - cY) - cH;
sY = t.getY() + cX;
sW = cH;
sH = cW;
break;
case TRANS_MIRROR_ROT90:
sX = t.getX() + (t.getHeight() - cY) - cH;
sY = t.getY() + (t.getWidth() - cX) - cW;
sW = cH;
sH = cW;
break;
case TRANS_ROT270:
sX = t.getX() + cY;
sY = t.getY() + (t.getWidth() - cX) - cW;
sW = cH;
sH = cW;
break;
default: // cant really happen, but the return keeps the
// compiler happy (otherwise it'll report variable
// may not be initialized)
return false;
}
if (o != t) {
tX = sX;
tY = sY;
tW = sW;
tH = sH;
if (o instanceof Sprite) {
// two sprites first round
// another = true;
t = (Sprite) o;
} else if (o instanceof TiledLayer) {
another = false;
TiledLayer layer = (TiledLayer) o;
oX = layer.getX();
oY = layer.getY();
oW = layer.getWidth();
oH = layer.getHeight();
} else { // o instanceof lcdui.Image
another = false;
Image img = (Image) o;
oW = img.getWidth();
oH = img.getHeight();
}
} else {
another = false;
// two sprites
// second round
oX = sX;
oY = sY;
oW = sW;
oH = sH;
}
}
// if there is no intersection
// we know there is no collision
if (tX > oX && tX >= oX + oW)
return false;
else if (tX < oX && tX + tW <= oX)
return false;
else if (tY > oY && tY >= oY + oH)
return false;
else if (tY < oY && tY + tH <= oY)
return false;
// variables keep popping out of nowhere...
// this is the intersection of the two rectangles
int rX, rY, rW, rH;
if (oX > tX) {
rX = oX;
rW = ((oX + oW < tX + tW)? oX + oW : tX + tW) - rX ;
} else {
rX = tX;
rW = ((tX + tW < oX + oW)? tX + tW : oX + oW) - rX;
}
if (oY > tY) {
rY = oY;
rH = ((oY + oH < tY + tH)? oY + oH : tY + tH) - rY ;
} else {
rY = tY;
rH = ((tY + tH < oY + oH)? tY + tH : oY + oH) - rY;
}
// ...and a lot more..
int tColIncr = 0, tRowIncr = 0, tOffset = 0;
int oColIncr = 0, oRowIncr = 0, oOffset = 0;
int f = (sequence == null)? frame : sequence[frame];
int fW = getWidth();
int fH = getHeight();
int fX = fW * (f % rows);
int fY = fH * (f / rows);
if (rgbData == null) {
rgbData = new int[fW*fH];
rgbDataAux = new int[fW*fH];
}
t = this;
another = true;
int[] tRgbData = this.rgbData;
while (another) {
int sOffset;
int sColIncr;
int sRowIncr;
switch(t.transform) {
case TRANS_NONE:
t.img.getRGB(tRgbData, 0, rW, fX + rX - t.getX(), fY + rY - t.getY(), rW, rH);
sOffset = 0;
sColIncr = 1;
sRowIncr = 0;
break;
case TRANS_ROT180:
t.img.getRGB(tRgbData, 0, rW, fX + fW - (rX - t.getX()) - rW - 1, fY + fH - (rY - t.getY()) - rH - 1, rW, rH);
sOffset = (rH * rW) - 1;
sColIncr = -1;
sRowIncr = 0;
break;
case TRANS_MIRROR:
t.img.getRGB(tRgbData, 0, rW, fX + fW - (rX - t.getX()) - rW - 1, fY + rY - t.getY(), rW, rH);
sOffset = rW - 1;
sColIncr = -1;
sRowIncr = rW << 1;
break;
case TRANS_MIRROR_ROT180:
t.img.getRGB(tRgbData, 0, rW, fX + rX - t.getX(), fY + fH - (rY - t.getY()) - rH - 1, rW, rH);
sOffset = (rH - 1) * rW;
sColIncr = 1;
sRowIncr = -(rW << 1);
break;
case TRANS_ROT90:
t.img.getRGB(tRgbData, 0, rH, fX + rY - t.getY(), fY + fH - (rX - t.getX()) - rW, rH, rW);
sOffset = (rW - 1) * rH;
sColIncr = -rH;
sRowIncr = (rH * rW) + 1;
break;
case TRANS_MIRROR_ROT90:
t.img.getRGB(tRgbData, 0, rH, fX + fW - (rY - t.getY()) - rH, fY + fH - (rX - t.getX()) - rW, rH, rW);
sOffset = (rH * rW) - 1;
sColIncr = -rH;
sRowIncr = (rH * rW) - 1;
break;
case TRANS_MIRROR_ROT270:
t.img.getRGB(tRgbData, 0, rH, fX + rY - t.getY(), fY + rX - t.getX(), rH, rW);
sOffset = 0;
sColIncr = rH;
sRowIncr = -(rH * rW) + 1;
break;
case TRANS_ROT270:
t.img.getRGB(tRgbData, 0, rH, fX + fW - (rY - t.getY()) - rH, fY + rX - t.getX(), rH, rW);
sOffset = rH - 1;
sColIncr = rH;
sRowIncr = -(rH * rW) - 1;
break;
default: // cant really happen, but the return keeps the
// compiler happy (otherwise it'll report variable
// may not be initialized)
return false;
}
if (o != t) {
tOffset = sOffset;
tRowIncr = sRowIncr;
tColIncr = sColIncr;
if (o instanceof Sprite) {
// two sprites first round
// another = true;
t = (Sprite) o;
tRgbData = this.rgbDataAux;
f = (t.sequence == null)? t.frame : t.sequence[t.frame];
fW = t.getWidth();
fH = t.getHeight();
fX = fW * (f % t.rows);
fY = fH * (f / t.rows);
} else if (o instanceof TiledLayer) {
another = false;
TiledLayer layer = (TiledLayer) o;
Image img = layer.img;
oOffset = 0;
oColIncr = 1;
oRowIncr = 0;
int lW = layer.getCellWidth();
int lH = layer.getCellHeight();
int minC = (rX - oX) / lW;
int minR = (rY - oY) / lH;
int maxC = (rX - oX + rW - 1) / lW;
int maxR = (rY - oY + rH - 1) / lH;
// travel across all cells in the collision
// rectangle
for (int row = minR; row <= maxR; row++) {
for (int col = minC; col <= maxC; col++) {
int cell = layer.getCell(col, row);
// if cell is animated get current
// associated static tile
if (cell < 0)
cell = layer.getAnimatedTile(cell);
int minX = (col == minC)? (rX - oX) % lW : 0;
int minY = (row == minR)? (rY - oY) % lH : 0;
int maxX = (col == maxC)? (rX + rW - oX - 1) % lW : lW-1;
int maxY = (row == maxR)? (rY + rH - oY - 1) % lH : lH-1;
int c = (row - minR) * lH * rW + (col - minC) * lW -
((col == minC)? 0 : (rX - oX) % lW) -
((row == minR)? 0 : (rY - oY) % lH) * rW;
// if cell is invisible we should still set
// all points as transparent to prevent
// fake positives caused by residual data
// on the rgb array
if (cell == 0) {
for (int y = minY; y <= maxY; y++,
c += rW - (maxX - minX + 1)) {
for (int x = minX; x <= maxX; x++, c++) {
rgbDataAux[c] = 0;
}
}
} else {
// make cell 0-based
cell--;
int imgCols = img.getWidth() / layer.getCellWidth();
int xSrc = lW * (cell % imgCols);
int ySrc = (cell / imgCols) * lH;
img.getRGB(rgbDataAux, c, rW, xSrc + minX,
ySrc + minY, maxX - minX + 1,
maxY - minY + 1);
}
}
}
} else { // o instanceof lcdui.Image
another = false;
Image img = (Image) o;
// get the image rgb data, and the increments
img.getRGB(rgbDataAux, 0, rW, rX - oX, rY - oY, rW, rH);
oOffset = 0;
oColIncr = 1;
oRowIncr = 0;
}
} else {
// two sprites
// second round, exit the loop
another = false;
oOffset = sOffset;
oRowIncr = sRowIncr;
oColIncr = sColIncr;
}
}
for (int row = 0; row < rH; row++, tOffset += tRowIncr, oOffset += oRowIncr) {
for (int col = 0; col < rW; col++, tOffset += tColIncr, oOffset += oColIncr) {
int rgb = rgbData[tOffset];
int rgbA = rgbDataAux[oOffset];
// look for two opaque pixels
if (((rgb & rgbA) >> 24) == -1)
return true;
}
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -