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

📄 animation.java

📁 一个j2me游戏代码
💻 JAVA
字号:
package pop2;

import java.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;

/**
 * <p>Title: POP2</p>
 * <p>Description: Prince of Persia 2</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: UBI soft (Beijing)</p>
 * @author unascribed
 * @version 1.0
 */
public class Animation
{
    Image m_image;
    int m_imageID = -1;

	// module data
    byte m_modules[]; // number * 4 : left, top, width, height
	// frame data
	byte frameData[]; // colbox: 1-4 attbox: 5-8
	//byte duration[]; // duration of each frames
	//byte colBox[]; // left, top, right, bottom
	//byte attBox[]; // left, top, right, bottom
	byte sprites[][]; // size = number of sprites * 4 [modID,flag(bit0:flipX,bit1:flipY), X, Y]
	                  // flag: bit7-flipY, bit6-flipX
	// action data
	byte frames[][]; // 0 frameindex, 1 duration
	byte nFrame[];
	byte mechModelID[];
	short mechModel[];

    public Animation()
    {
    }
    public int loadAnimation(DataInputStream is) throws Exception
    {
        int count, num, size, len = 0;
        int bytes;

        // image id
        m_imageID = is.readUnsignedShort();
        len += 2;
        // module
        count = is.readUnsignedShort();
        len += 2;
        m_modules = new byte[count*4];
        len += is.read(m_modules, 0, count*4);
        // frame data
        count = is.readUnsignedShort();
        len += 2;
		frameData = new byte[count*8];
		//duration = new byte[count];
		//colBox = new byte[count*4];
		//attBox = new byte[count*4];
		sprites = new byte[count][];
        for (int i = 0; i < count; i++)
        {
            size = is.readUnsignedByte();
            bytes = is.readUnsignedByte();
            num = is.readUnsignedByte();
            len += 3;
			sprites[i] = new byte[num*4];
			len += is.read(sprites[i], 0, num*4);

            //frameData[i*8] = (byte)(bytes & 0x3F);
            if ((bytes & 0x80) != 0)
            {
                len += is.read(frameData, i*8, 4);
            }
            if ((bytes & 0x40) != 0)
            {
                len += is.read(frameData, i*8+4, 4);
            }
        }
        // action data
        count = is.readUnsignedShort();
        len += 2;
        nFrame = new byte[count];
		frames = new byte[count][];
		mechModelID = new byte[count];
		mechModel = new short[count*4];
        for (int i = 0; i < count; i++)
        {
            size = is.readUnsignedByte();
            num = is.readUnsignedByte();
            len += 2;
            nFrame[i] = (byte)num;
            frames[i] = new byte[num*2];
            len += is.read(frames[i], 0, num*2);
            mechModelID[i] = (byte)is.readUnsignedByte();
            len += 1;
            size = (size - 1 - num * 2 - 1)/2;
            for (int j = 0; j < size; j++)
            {
                mechModel[i*4+j] = is.readShort();
                len += 2;
            }
        }
        //throw new Exception( "error animation" );
        return len;
    }
    boolean loadImage()
    {
        try
        {
            switch (m_imageID)
            {
            case Def.ID_MISC_PNG:
                m_image = GE.m_imageMisc;
                break;
            case Def.ID_PRINCE_PNG:
                m_image = GE.m_imagePrince;
                break;
            case Def.ID_SANDMAN_PNG:
                m_image = GE.m_imageSandman;
                break;
            case Def.ID_FIRE_PNG:
                m_image = GE.m_imageFire;
                break;
            case Def.ID_WATER_PNG:
                m_image = GE.m_imageWater;
                break;
            }
            return true;
        }
        catch ( Exception e )
        {
            //GE.handleException("load animation image <"+GE.m_text[imageID]+">", e);
        }
        return false;
    }
    public int getAction(int actionID)
    {
        if (actionID < 0 || actionID >= nFrame.length)
            return -1;
        return actionID;
    }
	public int getDuration(int actionID, int frameID)
	{
		if (actionID < 0 || actionID >= nFrame.length)
			return -1;
		if (frameID < 0 || frameID >= nFrame[actionID])
			return -1;
		return frames[actionID][frameID*2+1];
	}
    public int getFrame(int actionID, int frameID)
    {
        if (actionID < 0 || actionID >= nFrame.length)
            return -1;
        if (frameID < 0 || frameID >= nFrame[actionID])
            return -1;
        return frames[actionID][frameID*2];
    }
    // draw animation ---------------------------------------------------------------------------
    public void draw(int screenX, int screenY, boolean flipX, boolean flipY, int frameIndex)
    {
        int i, posX, posY, flag;
		int modID, modX, modY, modW, modH, clipW, clipH;

		byte[] s = sprites[frameIndex];

        int imgW = m_image.getWidth();
        int imgH = m_image.getHeight();

        for (i = 0; i < s.length; i+=4)
        {
            modID = (s[i] & 0xFF)*4;
            flag = s[i+1] & 0xFF;
            modX = m_modules[modID] & 0xFF;
			modY = m_modules[modID+1] & 0xFF;
            modW = m_modules[modID+2] & 0xFF;
			modH = m_modules[modID+3] & 0xFF;

			// mod position after frame flip
            posX = screenX;
            posY = screenY;
			if (flipX)
            {
                posX -= s[i+2];
                flag ^= 0x01;
            }
            else
                posX += s[i+2];
	        if (flipY)
            {
                posY -= s[i+3];
                flag ^= 0x02;
            }
            else
                posY += s[i+3];

            if ((flag&0x04) != 0)
            {
                if ((flag&0x01) == 0)
                    posX -= modH;
                if ((flag&0x02) != 0)
                    posY -= modW;
                clipW = modH;
                clipH = modW;
            }
            else
            {
                if ((flag&0x01) != 0)
                    posX -= modW;
                if ((flag&0x02) != 0)
                    posY -= modH;
                clipW = modW;
                clipH = modH;
            }
            switch(flag)
            {
                case 1: //flipX
                    flag = Sprite.TRANS_MIRROR;
                    break;
                case 2: //flipY
                    flag = Sprite.TRANS_MIRROR_ROT180;
                    break;
                case 3: //flipX + flipY
                    flag = Sprite.TRANS_ROT180;
                    break;
                case 4: //rotate
                    flag = Sprite.TRANS_ROT90;
                    break;
                case 5: //flipX + rotate
                    flag = Sprite.TRANS_MIRROR_ROT270;
                    break;
                case 6: //flipY + rotate
                    flag = Sprite.TRANS_MIRROR_ROT90;
                    break;
                case 7: //flipX + flipY + rotate
                    flag = Sprite.TRANS_ROT270;
                    break;
                default:
                    flag = 0;
                    break;
            }
            //if let (modX, modY) as origion, then left top point is(-modX, -modY),
            if(modX + modW > imgW)
                modW = imgW - modX;
            if(modY + modH > imgH)
                modH = imgH - modY;
            if( modW <= 0 || modH <= 0)
                continue;
            //draw to screen (mapX,mapY),
            if (posY + clipH < Def.PF_TOP || posY >= Def.PF_TOP + Def.PF_HEIGHT)
                continue;
            if (posY < Def.PF_TOP)
                GE.m_g.setClip(posX, Def.PF_TOP, clipW, clipH - Def.PF_TOP + posY);
            else if (posY + clipH > Def.PF_TOP + Def.PF_HEIGHT)
                GE.m_g.setClip(posX, posY, clipW, Def.PF_TOP + Def.PF_HEIGHT - posY);
            else
                GE.m_g.setClip(posX, posY, clipW, clipH);
            GE.m_g.drawRegion(m_image, modX, modY, modW, modH, flag, posX, posY, Graphics.LEFT|Graphics.TOP);
        }
    }
}

⌨️ 快捷键说明

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