📄 npcsprite.java
字号:
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.*;
import java.util.Vector;
public class NpcSprite
{
private Image Image_npc;
public int npc_curspX,npc_curspY,npc_curViewPosX,npc_way=0,npc_kind,npc_curframe;
public int npc_width,npc_height;
private int npc_rows,npc_cols,npc_frames,npc_currow,npc_curcol;
private int left_u_tile,left_d_tile,right_u_tile,right_d_tile,down_l_tile,down_r_tile,up_l_tile,up_r_tile;//npc周围的tile
private boolean left_u_canmove=false,left_d_canmove=false,right_u_canmove=false,right_d_canmove=false,up_l_canmove=false,up_r_canmove=false,down_l_canmove=false,down_r_canmove=false;
public boolean left_canmove=false,right_canmove=false,up_canmove=false,down_canmove=false;
public boolean npc_life=true;
public int npc_dead=10;
private GameScreen gs;
public NpcSprite(Image img,int x,int y,int width,int height,int kind,int curViewPosX,GameScreen gs)
{
this.gs=gs;
this.Image_npc=img;
this.npc_curspX=x;
this.npc_curspY=y;
this.npc_kind=kind;
this.npc_curViewPosX=curViewPosX;
this.npc_width=width;
this.npc_height=height;
if(img.getWidth()%width==0&&img.getHeight()%height==0)
{
this.npc_rows=img.getHeight()/height;
this.npc_cols=img.getWidth()/width;
this.npc_frames=npc_rows*npc_cols;
}
else//图片宽度或高度不是精灵宽度或高度的整数倍!
{
System.out.println("Can't not create sprite!");
this.npc_frames=0;
}
}
public void setFrame(int num)
{
if(npc_frames!=0)
{
this.npc_curframe=num;
this.npc_currow=num/npc_cols;
this.npc_curcol=num%npc_cols;
}
else
{
this.npc_curframe=-1;
}
}
public int getFrame()
{
return this.npc_curframe;
}
public void CanMove(int x,int y)
{
if(y>156)
y=156;
if(x<1)
x=1;
this.left_u_tile=World.tileMap[y/World.TILE_HEIGHT][(x-1)/World.TILE_WIDTH];
this.left_d_tile=World.tileMap[(y+npc_height-1)/World.TILE_HEIGHT][(x-1)/World.TILE_WIDTH];
this.right_u_tile=World.tileMap[y/World.TILE_HEIGHT][(x+npc_width)/World.TILE_WIDTH];
this.right_d_tile=World.tileMap[(y+npc_height-1)/World.TILE_HEIGHT][(x+npc_width)/World.TILE_WIDTH];
this.up_l_tile=World.tileMap[(y-1)/World.TILE_HEIGHT][(x+1)/World.TILE_WIDTH];
this.up_r_tile=World.tileMap[(y-1)/World.TILE_HEIGHT][(x+npc_width-2)/World.TILE_WIDTH];
this.down_l_tile=World.tileMap[(y+npc_height)/World.TILE_HEIGHT][(x+1)/World.TILE_WIDTH];
this.down_r_tile=World.tileMap[(y+npc_height)/World.TILE_HEIGHT][(x+npc_width-2)/World.TILE_WIDTH];
//假设各个方向均不能移动
this.left_canmove=false;
this.left_u_canmove=false;
this.left_d_canmove=false;
this.right_canmove=false;
this.right_u_canmove=false;
this.right_d_canmove=false;
this.up_canmove=false;
this.up_l_canmove=false;
this.up_r_canmove=false;
this.down_canmove=false;
this.down_l_canmove=false;
this.down_r_canmove=false;
//判断那些方向可以移动
for(int i=0;i<World.CANPASS_TILE.length;i++)
{
if(this.left_u_tile==World.CANPASS_TILE[i])
this.left_u_canmove=true;
if(this.left_d_tile==World.CANPASS_TILE[i])
this.left_d_canmove=true;
if(this.right_u_tile==World.CANPASS_TILE[i])
this.right_u_canmove=true;
if(this.right_d_tile==World.CANPASS_TILE[i])
this.right_d_canmove=true;
if(this.up_l_tile==World.CANPASS_TILE[i])
this.up_l_canmove=true;
if(this.up_r_tile==World.CANPASS_TILE[i])
this.up_r_canmove=true;
if(this.down_l_tile==World.CANPASS_TILE[i])
this.down_l_canmove=true;
if(this.down_r_tile==World.CANPASS_TILE[i])
this.down_r_canmove=true;
}
if(left_u_canmove==true&&left_d_canmove==true)
this.left_canmove=true;
if(right_u_canmove==true&&right_d_canmove==true)
this.right_canmove=true;
if(up_l_canmove==true&&up_r_canmove==true)
this.up_canmove=true;
if(down_l_canmove==true&&down_r_canmove==true)
this.down_canmove=true;
if(npc_kind==4)
{
this.left_canmove=true;
this.right_canmove=true;
this.up_canmove=true;
this.down_canmove=true;
}
}
public void draw(Graphics g)
{
g.setClip(npc_curspX,npc_curspY,npc_width,npc_height);
g.drawImage(Image_npc,npc_curspX-npc_curcol*npc_width,npc_curspY-npc_currow*npc_height,g.TOP|g.LEFT);
}
private int cos=50000,sin=86603;
private int v0=4,vy=v0*sin,g=42;
public int y1=0,y2=0,y0=-1,stopcurspY=-1,t=0;
public boolean npc_bjump=false;
public void whichjump(int i)//设置精灵跳跃的幅度
{
switch(i)
{
case(0)://小跳
this.v0=8;
this.g=42;
break;
case(1)://大跳
this.v0=5;
this.g=22;
break;
case(2)://踩怪物弹起
this.v0=6;
this.g=60;
break;
}
}
public void jump()//跳跃
{
if(y0==-1)
y0=npc_curspY;
y1=y2;
y2=(vy*t-g*t*t*400)/100000;
stopcurspY=-1;//上升时可以跳到的最大高度(在大不能向上走了)
if((y2-y1)>0)//上升
{
for(int i=0;i<(y2-y1);i++)
{
CanMove(gs.currentViewPosX-gs.getWidth()/2+npc_curspX,gs.currentViewPosY-gs.getHeight()/2+npc_curspY);
if(up_canmove==true)
npc_curspY--;
else
{
if(stopcurspY==-1)
stopcurspY=npc_curspY;
while(npc_curspY<=stopcurspY)
{
t+=2;
y1=y2;
y2=(vy*t-g*t*t*400)/100000;
npc_curspY-=(y2-y1);
}
}
}
}
else//下降
{
for(int i=0;i<(y1-y2);i++)
{
CanMove(gs.currentViewPosX-gs.getWidth()/2+npc_curspX,gs.currentViewPosY-gs.getHeight()/2+npc_curspY);
if(down_canmove==true)
npc_curspY++;
else
{
t=-3;
y1=0;
y2=0;
y0=-1;
npc_bjump=false;
break;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -