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

📄 movingobject.java

📁 一个j2me的小游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import com.nokia.mid.ui.DirectGraphics;
import com.nokia.mid.ui.DirectUtils;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;


public class MovingObject implements GameConstant
{

    static Image weaponImg = null;
    protected int DEFAULT_HIT_POWER;
    protected int hitPower;
    protected int speedX;
    protected int speedY;
    protected int stage;
    protected int direction;
    protected int moveDis;
    protected boolean destroy;
    protected boolean canAttack;
    protected int type;
    protected Rectangle curLoc;

    public MovingObject(int objType, int locX, int locY, int width, int height)
    {
        DEFAULT_HIT_POWER = 1;
        hitPower = DEFAULT_HIT_POWER;
        speedX = 3;
        speedY = 3;
        stage = 0;
        direction = 1;
        moveDis = 0;
        destroy = false;
        canAttack = false;
        type = 0;
        curLoc = new Rectangle();
        type = objType;
        curLoc.SetBounds(locX, locY, width, height);
        destroy = false;
        hitPower = DEFAULT_HIT_POWER;
        speedX = 3;
        speedY = 3;
        stage = 0;
        try
        {
            if(weaponImg == null)
            {
                weaponImg = Image.createImage("/Images/Player/weapon.png");
            }
        }
        catch(Exception ioe)
        {
            return;
        }
    }

    public void Initial(int objType, int hitPow, int speedX, int speedY, int locX, int locY, int width, 
            int height)
    {
        type = objType;
        curLoc.SetBounds(locX, locY, width, height);
        DEFAULT_HIT_POWER = hitPow;
        hitPower = hitPow;
        this.speedX = speedX;
        this.speedY = speedY;
        stage = 0;
        destroy = false;
        canAttack = false;
        if(Math.abs(speedX) > Math.abs(speedY))
        {
            direction = speedX <= 0 ? 2 : 3;
        } else
        {
            direction = speedY <= 0 ? 0 : 1;
        }
        moveDis = Math.abs(speedX + speedY);
    }

    public void Run()
    {
        if(!IsDestroy())
        {
            int absDis = Math.abs(speedX + speedY);
            if(moveDis < absDis)
            {
                SetDestroy(true);
            } else
            {
                moveDis = MoveToDirection(absDis);
            }
        }
    }

    public void Reset()
    {
        curLoc.SetLocation(0, 0);
        destroy = false;
        hitPower = DEFAULT_HIT_POWER;
        speedX = 3;
        speedY = 3;
        stage = 0;
        moveDis = 0;
        direction = 1;
        destroy = false;
        canAttack = false;
    }

    public int GetType()
    {
        return type;
    }

    public void SetType(int objType)
    {
        type = objType;
    }

    public void SetLocation(int x, int y)
    {
        curLoc.SetX(x);
        curLoc.SetY(y);
    }

    public int GetLocationX()
    {
        return curLoc.GetX();
    }

    public int GetLocationY()
    {
        return curLoc.GetY();
    }

    public void SetWidth(int width)
    {
        curLoc.SetWidth(width);
    }

    public int GetWidth()
    {
        return curLoc.GetWidth();
    }

    public void SetHeight(int height)
    {
        curLoc.SetHeight(height);
    }

    public int GetHeight()
    {
        return curLoc.GetHeight();
    }

    public Rectangle GetBounds()
    {
        return curLoc;
    }

    public int GetCenterLocationX()
    {
        return GetLocationX() + GetWidth() / 2;
    }

    public int GetCenterLocationY()
    {
        return GetLocationY() + GetHeight() / 2;
    }

    public void SetDirection(int newDir)
    {
        direction = newDir;
    }

    public int GetDirection()
    {
        return direction;
    }

    public void SetDestroy(boolean isDestroy)
    {
        destroy = isDestroy;
    }

    public boolean CanAttack()
    {
        return canAttack;
    }

    public boolean IsDestroy()
    {
        return destroy;
    }

    public int GetHitPower()
    {
        return hitPower;
    }

    public void SetHitPower(int newPower)
    {
        hitPower = newPower;
    }

    public int GetSpeedX()
    {
        return speedX;
    }

    public void SetSpeedX(int speed)
    {
        speedX = speed;
    }

    public int GetSpeedY()
    {
        return speedY;
    }

    public void SetSpeedY(int speed)
    {
        speedY = speed;
    }

    public void MoveX(int moveoffset)
    {
        curLoc.MoveX(moveoffset);
    }

    public void MoveY(int moveoffset)
    {
        curLoc.MoveY(moveoffset);
    }

    public void Move(int moveoffsetX, int moveoffsetY)
    {
        curLoc.MoveX(moveoffsetX);
        curLoc.MoveY(moveoffsetY);
    }

    public int MoveToDirection(int dis)
    {
        int canMoveDis = 0;
        switch(direction)
        {
        case 0: // '\0'
            canMoveDis = CheckCollision(dis, 0);
            MoveY(-canMoveDis);
            return canMoveDis;

        case 1: // '\001'
            canMoveDis = CheckCollision(dis, 1);
            MoveY(canMoveDis);
            return canMoveDis;

        case 2: // '\002'
            canMoveDis = CheckCollision(dis, 2);
            MoveX(-canMoveDis);
            return canMoveDis;

        case 3: // '\003'
            canMoveDis = CheckCollision(dis, 3);
            MoveX(canMoveDis);
            return canMoveDis;
        }
        return -1;
    }

    public int CheckCollision(int moveOffset, int direct)
    {
        int widthOffset = curLoc.GetWidth() / 2;
        int curPosX = GetCenterLocationX();
        int curPosY = GetCenterLocationY();
        try
        {
            switch(direct)
            {
            default:
                break;

            case 0: // '\0'
            {
                int minX = curPosX - widthOffset;
                int maxX = curPosX + widthOffset;
                int leftPosX = minX / 30;
                int rightPosX = maxX / 30;
                int posY1 = curPosY / 30;
                int leftRoomData = Room.GetObjectId(leftPosX, posY1);
                int rightRoomData = Room.GetObjectId(rightPosX, posY1);
                int objPosY = (posY1 + 1) * 30;
                int leftDisY = objPosY - Room.GetObstructionDistance(leftRoomData, direct);
                int rightDisY = objPosY - Room.GetObstructionDistance(rightRoomData, direct);
                int moveTo = curPosY - moveOffset;
                if(moveTo <= rightDisY && curPosY >= rightDisY && rightRoomData != -1)
                {
                    int objPosX = Room.GetObstructionDistance(rightRoomData, 3);
                    int lWidth = 30 - Room.GetObstructionDistance(rightRoomData, 2);
                    int rWidth = objPosX;
                    int width = lWidth - rWidth;
                    if(width > 0)
                    {
                        objPosX += rightPosX * 30;
                        int leftBound = objPosX;
                        int rightBound = objPosX + width;
                        if(minX < rightBound && maxX > leftBound || minX > leftBound && maxX < rightBound || minX < leftBound && maxX > leftBound || minX < rightBound && maxX > rightBound)
                        {
                            return curPosY - rightDisY;
                        }
                    }
                }
                if(moveTo <= leftDisY && curPosY >= leftDisY && leftPosX != rightPosX && leftRoomData != -1)
                {
                    int objPosX = Room.GetObstructionDistance(leftRoomData, 3);
                    int lWidth = Room.GetObstructionDistance(leftRoomData, 2);
                    int rWidth = 30 - objPosX;
                    int width = rWidth - lWidth;
                    if(width > 0)
                    {
                        objPosX += leftPosX * 30;
                        int leftBound = objPosX;
                        int rightBound = objPosX + width;
                        if(minX < rightBound && maxX > leftBound || minX > leftBound && maxX < rightBound || minX < leftBound && maxX > leftBound || minX < rightBound && maxX > rightBound)
                        {
                            return curPosY - leftDisY;
                        }
                    }
                }
                int posY2 = moveTo / 30;
                if(posY1 != posY2)
                {
                    while(--posY1 >= posY2) 
                    {
                        leftRoomData = Room.GetObjectId(leftPosX, posY1);
                        rightRoomData = Room.GetObjectId(rightPosX, posY1);
                        leftDisY = (objPosY -= 30) - Room.GetObstructionDistance(leftRoomData, direct);
                        rightDisY = objPosY - Room.GetObstructionDistance(rightRoomData, direct);
                        if(rightRoomData != -1)
                        {
                            int objPosX = Room.GetObstructionDistance(rightRoomData, 3);
                            int lWidth = 30 - Room.GetObstructionDistance(rightRoomData, 2);
                            int rWidth = objPosX;
                            int width = lWidth - rWidth;
                            if(width > 0)
                            {
                                objPosX += rightPosX * 30;
                                int leftBound = objPosX;
                                int rightBound = objPosX + width;
                                if(minX < rightBound && maxX > leftBound || minX > leftBound && maxX < rightBound || minX < leftBound && maxX > leftBound || minX < rightBound && maxX > rightBound)
                                {
                                    return curPosY < rightDisY || moveTo > rightDisY ? moveOffset : curPosY - rightDisY;
                                }
                            }
                        }
                        if(leftPosX != rightPosX && leftRoomData != -1)
                        {
                            int objPosX = Room.GetObstructionDistance(leftRoomData, 3);
                            int lWidth = Room.GetObstructionDistance(leftRoomData, 2);
                            int rWidth = 30 - objPosX;
                            int width = rWidth - lWidth;
                            if(width > 0)
                            {
                                objPosX += leftPosX * 30;
                                int leftBound = objPosX;
                                int rightBound = objPosX + width;
                                if(minX < rightBound && maxX > leftBound || minX > leftBound && maxX < rightBound || minX < leftBound && maxX > leftBound || minX < rightBound && maxX > rightBound)
                                {
                                    return curPosY < leftDisY || moveTo > leftDisY ? moveOffset : curPosY - leftDisY;
                                }
                            }
                        }
                    }
                }
                break;
            }

⌨️ 快捷键说明

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