⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sprite.java

📁 一个 JAVA 写的 J2ME 开源模拟器
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	                        dx = newH - oldH;
	                        dy = 0;
	                        break;
	                    case TRANS_ROT90:
	                        dx = newH - oldH;
	                        dy = newW - oldW;
	                        break;
	                    case TRANS_ROT270:
	                        dx = 0;
	                        dy = 0;
	                        break;
	                    case TRANS_MIRROR_ROT90:
	                        dx = 0;
	                        dy = newW - oldW;
	                        break;
	                    default: // cant really happen, but the return keeps the
	                            // compiler happy (otherwise it'll report variable
	                            // may not be initialized)
	                        return;
	                }
	                // now change position to keep the refPixel in place
	                move(dx, dy);
	            }	                
	        }
    	}
    }
    
    public final void paint(Graphics g) {
        if (!isVisible())
            return;
        
        int f = (sequence == null)? frame : sequence[frame];
        int w = getWidth();
        int h = getHeight();
        int fx = w * (f % cols);
        int fy = h * (f / cols);        
        
        g.drawRegion(img, fx, fy, w, h, transform, getX(), getY(), Graphics.TOP | Graphics.LEFT);
    }
    
    public int getRawFrameCount() {
        return cols * rows;
    }
    
    public void setTransform (int transform) {
        if (this.transform == transform)
            return;
        
        int width = getWidth();
        int height = getHeight();
        int currentTransform = this.transform;
        
        // calculate the coordinates of refPixel in the new transform
        // relative to x, y
        
        int newRefX, newRefY;

        switch(transform) {
            case TRANS_NONE:
                newRefX = refX;
                newRefY = refY;
                break;
            case TRANS_MIRROR_ROT180:
                newRefX = width - refX;
                newRefY = height - refY;
                break;
            case TRANS_MIRROR:
                newRefX = width - refX;
                newRefY = refY;
                break;
            case TRANS_ROT180:
                newRefX = refX;
                newRefY = height - refY;
                break;
            case TRANS_MIRROR_ROT270:
                newRefX = height - refY;
                newRefY = refX;
                break;
            case TRANS_ROT90:
                newRefX = height - refY;
                newRefY = width - refX;
                break;
            case TRANS_ROT270:
                newRefX = refY;
                newRefY = refX;
                break;
            case TRANS_MIRROR_ROT90:
                newRefX = refY;
                newRefY = width - refX;
                break;
            default:
                throw new IllegalArgumentException();
        }

        // calculate the coordinates of refPixel in the current transform
        // relative to x, y
        
        int curRefX, curRefY;

        switch(currentTransform) {
            case TRANS_NONE:
                curRefX = refX;
                curRefY = refY;
                break;
            case TRANS_MIRROR_ROT180:
                curRefX = width - refX;
                curRefY = height - refY;
                break;
            case TRANS_MIRROR:
                curRefX = width - refX;
                curRefY = refY;
                break;
            case TRANS_ROT180:
                curRefX = refX;
                curRefY = height - refY;
                break;
            case TRANS_MIRROR_ROT270:
                curRefX = height - refY;
                curRefY = refX;
                break;
            case TRANS_ROT90:
                curRefX = height - refY;
                curRefY = width - refX;
                break;
            case TRANS_ROT270:
                curRefX = refY;
                curRefY = refX;
                break;
            case TRANS_MIRROR_ROT90:
                curRefX = refY;
                curRefY = width - refX;
                break;
            default: // cant really happen, but the return keeps the
                    // compiler happy (otherwise it'll report variable
                    // may not be initialized)
                return;
        }
        
        // now change position to keep the refPixel in place
        move(curRefX - newRefX, curRefY - newRefY);
        this.transform = transform;
    }

    
    /**
     * Helper methods that check for collisions
     * They are at the end of the file because of the 
     * length of the code
     *
     * For both methods, the second and third parameters 
     * are significant only if o is an Image, 
     * otherwise they are ignored
     */
    private synchronized boolean collidesWith(Object o, 
                int oX, int oY) {
    
        int tX = 0, tY = 0, tW = 0, tH = 0;
        int oW = 0, oH = 0;

        Sprite t = this;
        boolean another = true;
        
        
        while (another) {
            int sX, sY, sW, sH;

            int cX = t.collX;
            int cY = t.collY;
            int cW = t.collWidth;
            int cH = t.collHeight;
            
            // if there is a zero in a dimension
            // then it cannot intersect with anything!
            if (cW == 0 || cH == 0) {
                return false;
            }

            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 - 1) - cH;
                    sY = t.getY() + cX;
                    sW = cH;
                    sH = cW;
                    break;
                case TRANS_MIRROR_ROT90:
                    sX = t.getX() + (t.getHeight() - cY - 1) - cH;
                    sY = t.getY() + (t.getWidth() - cX - 1) - cW;
                    sW = cH;
                    sH = cW;
                    break;
                case TRANS_ROT270:
                    sX = t.getX() + cY;
                    sY = t.getY() + (t.getWidth() - cX - 1) - 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;

        if (o instanceof TiledLayer) {
            // if o is a tiledLayer then
            // it is possible to have not a
            // collision if every intersection tile
            // has a zero value 
            TiledLayer layer = (TiledLayer) o;
            // 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;
            }
            
            Image img = layer.img;

            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);

                    if (cell != 0)
                        return true;
                }
            }

            // if no non zero cell was found
            // there is no collision
            return false;
        } else {
            // if the other object is an image or sprite
            // collision happened
            return true;
        }
    }    

    private synchronized boolean collidesWithPixelLevel(Object o, 
                int oX, int oY) {
        
        
        boolean another = true;
        Sprite t = this;

        // the compiler bitchs if we dont initialize this
        int tX = 0, tY = 0, tW = 0, tH = 0;

⌨️ 快捷键说明

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