/**
为MIDP1.0写的Sprite类的升级版本
* <p>Title: 精灵类</p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2004</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
import javax.microedition.lcdui.* ;
public class MySprite {
final public static int ByCollideXY = 0 ;
final public static int ByCollideArea = 1 ;
final public static int ByCollideRadius = 2 ;
public Image image;
public int positionX;
public int positionY;
public int numFrames;
public int currentFrame;
public int[][] framesInfo;//本版本吧各个帧的尺寸存到数组中,而不是象1.0中等大
public boolean visible=true;
public int DX;
public int DY;
public int collideX=0;//当两个角色交叉横向距离超过collideX时认为已经碰撞
public int collideY=0;//当两个角色交叉纵向距离超过collideY时认为已经碰撞
public int collideArea=0;//当两个角色互相入侵面积超过collideArea时认为已经碰撞(注意:这里的入侵面积!=交叉面积)
public int collideRadius=0;//碰撞半径
public int state=0;
public MySprite(Image image,int numFrames) throws Exception {
this.image = image;
this.numFrames = numFrames;
//set frameInfo
framesInfo=new int[numFrames][4];
for (int i=0;i<numFrames;i++)
{
framesInfo[i][0]=i*image.getWidth()/numFrames;//左上角X坐标
framesInfo[i][1]=0;//左上角Y坐标
framesInfo[i][2]=image.getWidth()/numFrames;//本帧宽度
framesInfo[i][3]=image.getHeight();//本帧高度
}
///////////////
this.currentFrame=0;
}
public void draw(Canvas theCanvas,Graphics g)
{
if (visible)
{
g.setClip(positionX, positionY, framesInfo[currentFrame][2], framesInfo[currentFrame][3]);
g.drawImage(image, positionX - framesInfo[currentFrame][0], positionY-framesInfo[currentFrame][1],0);
g.setClip(0, 0, theCanvas.getWidth(), theCanvas.getHeight());
}
}
public void step()
{
if (visible)
{
this.positionX += DX;
this.positionY += DY;
}
}
public boolean collidesWith(MySprite sprite)
{
return this.collidesWith(sprite,ByCollideArea);
}
public boolean collidesWith(MySprite sprite, int CheckType)
{
if (! visible || ! sprite.visible)
{
return false;
}
if (CheckType==ByCollideArea)
{
//当两个角色互相入侵面积超过collideArea时认为已经碰撞(注意:这里的入侵面积!=交叉面积)
int inX=sprite.positionX+sprite.framesInfo[sprite.currentFrame][2]-this.positionX
>this.positionX+this.framesInfo[currentFrame][2]-sprite.positionX
?this.positionX+this.framesInfo[currentFrame][2]-sprite.positionX
:sprite.positionX+sprite.framesInfo[sprite.currentFrame][2]-this.positionX
;
int inY=sprite.positionY+sprite.framesInfo[sprite.currentFrame][3]-this.positionY
>this.positionY+this.framesInfo[currentFrame][3]-sprite.positionY
?this.positionY+this.framesInfo[currentFrame][3]-sprite.positionY
:sprite.positionY+sprite.framesInfo[sprite.currentFrame][3]-this.positionY
;
if (inX>0 && inY>0 && inX*inY>this.collideArea+sprite.collideArea)
{
return true;
}
else
{
return false;
}
}
else if (CheckType==ByCollideXY)
{
if (sprite.positionX+sprite.framesInfo[sprite.currentFrame][2]-sprite.collideX>this.positionX+this.collideX
&& this.positionX+this.framesInfo[currentFrame][2]-this.collideX>sprite.positionX+sprite.collideX
&& sprite.positionY+sprite.framesInfo[sprite.currentFrame][3]-sprite.collideY>this.positionY+this.collideY
&& this.positionY+this.framesInfo[currentFrame][3]-this.collideY>sprite.positionY+sprite.collideY)
{
return true;
}
else
{
return false;
}
}
else //if (CheckType==ByCollideRadius)
{
int inX=Math.abs(this.positionX+this.framesInfo[currentFrame][2]/2-sprite.positionX-(sprite.framesInfo[sprite.currentFrame][2]+1)/2);
int inY=Math.abs(this.positionY+this.framesInfo[currentFrame][3]/2-sprite.positionY-(sprite.framesInfo[sprite.currentFrame][3]+1)/2);
if (inX*inX+inY*inY<(collideRadius+sprite.collideRadius)*(sprite.collideRadius+sprite.collideRadius))
{
return true;
}
else
{
return false;
}
}
}
public boolean collidesWith(Image image, int x, int y)
{
return this.collidesWith(image, x, y,ByCollideArea);
}
public boolean collidesWith(Image image, int x, int y, int CheckType)
{
if (! visible)
{
return false;
}
if (CheckType==ByCollideArea)
{
//当两个角色互相入侵面积超过collideArea时认为已经碰撞(注意:这里的入侵面积!=交叉面积)
int inX=x+image.getWidth()-this.positionX
>this.positionX+this.framesInfo[currentFrame][2]-x
?this.positionX+this.framesInfo[currentFrame][2]-x
:x+image.getWidth()-this.positionX
;
int inY=y+image.getHeight()-this.positionY
>this.positionY+this.framesInfo[currentFrame][3]-y
?this.positionY+this.framesInfo[currentFrame][3]-y
:y+image.getHeight()-this.positionY
;
if (inX>0 && inY>0 && inX*inY>this.collideArea)
{
return true;
}
else
{
return false;
}
}
else if (CheckType==ByCollideXY)
{
if (x+image.getWidth()>this.positionX
&& this.positionX+this.framesInfo[currentFrame][2]-this.collideX>x
&& y+image.getHeight()>this.positionY
&& this.positionY+this.framesInfo[currentFrame][3]-this.collideY>y)
{
return true;
}
else
{
return false;
}
}
else //if (CheckType==ByCollideRadius)
{
int inX=Math.abs(this.positionX+this.framesInfo[currentFrame][2]/2-x-(image.getWidth()+1)/2);
int inY=Math.abs(this.positionY+this.framesInfo[currentFrame][3]/2-y-(image.getHeight()+1)/2);
if (inX*inX+inY*inY<collideRadius*collideRadius)
{
return true;
}
else
{
return false;
}
}
}
}