📄 mysprite.java
字号:
if(currentDirect==DIR_UP)
{
this.setFrameSequence(new int[]{3,4,5});
}
else if(currentDirect==DIR_LEFT)
{
this.setFrameSequence(new int[]{6,7,8});
}
else if(currentDirect==DIR_RIGHT)
{
this.setFrameSequence(new int[]{9,10,11});
}
else if(currentDirect==DIR_DOWN)
{
this.setFrameSequence(new int[]{0,1,2});
}
}
public void move()
{
if(walking)
{
Box currentPos=this.getCollisionRectanglePosition(); //脚部box
//System.out.println("currentPos="+currentPos);
if(currentDirect==DIR_UP)
{
if(this.getY()>minY)
{
//super.move(0,-speed);
this.setPrevMoveArea(new Box(currentPos.x,currentPos.y-speed,currentPos.w,currentPos.h));
}
//lastMoveX=0;
//lastMoveY=-speed;
}
if(currentDirect==DIR_DOWN)
{
if(this.getY()<maxY-this.getHeight())
{
//super.move(0,speed);
this.setPrevMoveArea(new Box(currentPos.x,currentPos.y+speed,currentPos.w,currentPos.h));
}
//lastMoveX=0;
//lastMoveY=speed;
}
if(currentDirect==DIR_LEFT)
{
if(this.getX()>minX)
{
//super.move(-speed,0);
this.setPrevMoveArea(new Box(currentPos.x-speed,currentPos.y,currentPos.w,currentPos.h));
}
//lastMoveX=-speed;
//lastMoveY=0;
}
if(currentDirect==DIR_RIGHT)
{
if(this.getX()<maxX-this.getWidth())
{
//super.move(speed,0);
this.setPrevMoveArea(new Box(currentPos.x+speed,currentPos.y,currentPos.w,currentPos.h));
}
//lastMoveX=speed;
//lastMoveY=0;
}
if(!canCrossBlock) //如果不能越过障碍物
{
haveCollision=checkCollision();
if(haveCollision) //有障碍物
{
//System.out.println(this.getName()+"有障碍物");
//找避开的路
Point point=findRoad();
//super.move(-lastMoveX,-lastMoveY); //监测到碰撞,取消最后一次移动
//lastMoveX=0;
//lastMoveY=0;
if(point!=null) //找到避开的方向
{
super.move(speed*point.x,speed*point.y);
}
else
{
if(!isMainRole && !continueFrame)this.setFrame(0); //非主角动作停在第一祯
}
}
else //无障碍物
{
if(canTouchMine)
{
if(this.getPrevMoveArea().x-this.getCollisionRectanglePosition().x!=0 || this.getPrevMoveArea().y-this.getCollisionRectanglePosition().y!=0)
{
mineTimeCounter--;
//System.out.println("mineTimeCounter="+mineTimeCounter);
}
}
super.move(this.getPrevMoveArea().x-this.getCollisionRectanglePosition().x,this.getPrevMoveArea().y-this.getCollisionRectanglePosition().y);
}
}else //能越障碍物
{
if(canTouchMine)
{
if(this.getPrevMoveArea().x-this.getCollisionRectanglePosition().x!=0 || this.getPrevMoveArea().y-this.getCollisionRectanglePosition().y!=0)
{
mineTimeCounter--;
//System.out.println("mineTimeCounter="+mineTimeCounter);
}
}
super.move(this.getPrevMoveArea().x-this.getCollisionRectanglePosition().x,this.getPrevMoveArea().y-this.getCollisionRectanglePosition().y);
}
}
//System.out.println(isMainRole);
if(isMainRole)
{
if(walking)this.nextFrame();
}
else
{
if(continueFrame)
{
this.nextFrame();
}else if(walking && !haveCollision)this.nextFrame();
}
}
protected boolean checkCollision()
{
if(!isWalking())return false;
Box playerBox=null;
if(this.getCollisionRectangle()!=null)
{
//playerBox=this.getCollisionRectanglePosition();
playerBox=this.getPrevMoveArea();
}
//System.out.println("playerBox="+playerBox);
if(BlockLib.checkCollision(playerBox))
{
return true;
}
/*
//检测与其他角色的碰撞
Vector npcVC=npcFactory.getSpriteVC();
for(int i=0;npcVC!=null && i<npcVC.size();i++)
{
MySprite npc=(MySprite)npcVC.elementAt(i);
Box box=npc.getCollisionRectangle();
if(box!=null)
{
Box npcBox=new Box();
npcBox.x=npc.getX()+box.x;
npcBox.y=npc.getY()+box.y;
npcBox.w=box.w;
npcBox.h=box.h;
Box playerBox=new Box();
playerBox.x=player1.getCollisionRectangle().x+player1.getX();
playerBox.y=player1.getCollisionRectangle().y+player1.getY();
playerBox.w=player1.getCollisionRectangle().w;
playerBox.h=player1.getCollisionRectangle().h;
if(Tools.checkBoxInter(playerBox,npcBox))
{
return true;
}
}
}
//检测与敌人的碰撞
Vector enemyVC=enemyFactory.getSpriteVC();
for(int i=0;enemyVC!=null && i<enemyVC.size();i++)
{
MySprite enemy=(MySprite)enemyVC.elementAt(i);
Box box=enemy.getCollisionRectangle();
if(box!=null)
{
Box npcBox=new Box();
npcBox.x=enemy.getX()+box.x;
npcBox.y=enemy.getY()+box.y;
npcBox.w=box.w;
npcBox.h=box.h;
Box playerBox=new Box();
playerBox.x=player1.getCollisionRectangle().x+player1.getX();
playerBox.y=player1.getCollisionRectangle().y+player1.getY();
playerBox.w=player1.getCollisionRectangle().w;
playerBox.h=player1.getCollisionRectangle().h;
if(Tools.checkBoxInter(playerBox,npcBox))
{
return true;
}
}
}
*/
return false;
}
//找避开障碍的方向
private Point findRoad()
{
if(currentDirect==DIR_RIGHT || currentDirect==DIR_LEFT)
{
Box playerBox=this.getPrevMoveArea();
//向上和下同时检测
int up=playerBox.h;
int down=playerBox.h;
Box checkBoxDown=new Box(playerBox.x,playerBox.y+playerBox.h+5,playerBox.w,1);
Box checkBoxUp=new Box(playerBox.x,playerBox.y-5,playerBox.w,1);
//检查上下是否有路
if(!BlockLib.checkCollision(checkBoxDown))
{
return new Point(0,1);
}
if(!BlockLib.checkCollision(checkBoxUp))
{
return new Point(0,-1);
}
}else
if(currentDirect==DIR_UP || currentDirect==DIR_DOWN)
{
Box playerBox=this.getPrevMoveArea();
//向上和下同时检测
int left=playerBox.w;
int right=playerBox.w;
Box checkBoxRight=new Box(playerBox.x+playerBox.w,playerBox.y,1,playerBox.h);
Box checkBoxLeft=new Box(playerBox.x-1,playerBox.y,1,playerBox.h);
//检查左右是否有路
if(!BlockLib.checkCollision(checkBoxRight))
{
return new Point(1,0);
}
if(!BlockLib.checkCollision(checkBoxLeft))
{
return new Point(-1,0);
}
}
return null;
}
public void setChat(boolean c)
{
canChat=c;
if(canChat==true)
{
setChatArea();
}
}
public void setWalk(boolean w)
{
walking=w;
}
public boolean isWalking()
{
return walking;
}
public ChatArea getChatArea() {
return chatArea;
}
public Vector getMsgVC() {
return msgVC;
}
public void setMsgVC(Vector msgVC) {
this.msgVC = msgVC;
}
//到下一句话
public void nextMsg()
{
msgPoint++;
if(msgVC==null)
{
msgPoint=-1;
}
else if(msgPoint>msgVC.size()-1)
{
msgPoint=msgVC.size()-1;
}
}
boolean threadRunning;
public void startAutoMove()
{
if(canMove)
{
if(!isAutoMoving)
{
Box bound=new Box(minX,minY,maxX-minX,maxY-minY);
Point point=new Point(this.getX(),this.getY());
pointCreator=new PointCreator(bound,point);
if(traceTarget!=null)
{
pointCreator.setTraceTarget(traceTarget);
pointCreator.setOwner(this);
}
if(startDelay!=-1)pointCreator.setStartDelay(startDelay);
if(delay!=-1)pointCreator.setDelay(delay);
pointCreator.start();
//this.maxX=Canvas1.groundWidth;
//this.maxY=Canvas1.groundHeight;
this.isAutoMoving=true;
if(!threadRunning)
{
(new Thread(this)).start();
threadRunning=true;
}
}
}
}
public void stopAutoMove()
{
this.isAutoMoving=false;
if(pointCreator!=null)pointCreator.stop();
threadRunning=false;
}
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run()
{
while(isAutoMoving)
{
if(pointCreator!=null)
{
Point point=pointCreator.getPoint();
if(point.x-this.getX()<-speed)
{
changeDirect(MySprite.DIR_LEFT);
walking=true;
}
else if(point.x-this.getX()>speed)
{
changeDirect(MySprite.DIR_RIGHT);
walking=true;
}
else if(point.y-this.getY()<-speed)
{
changeDirect(MySprite.DIR_UP);
walking=true;
}
else if(point.y-this.getY()>speed)
{
changeDirect(MySprite.DIR_DOWN);
walking=true;
}else
{
walking=false;
//this.setFrame(0);
}
//System.out.println("walking="+walking);
this.move();
}
try {
Thread.sleep(1000/60);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
threadRunning=false;
}
public void reduLife(int sub)
{
life=life-sub;
if(life<0)life=0;
}
public void addLife(int add)
{
life=life+add;
if(life>maxLife)life=maxLife;
}
public void addMoney(int money)
{
this.money+=money;
}
public boolean addExp(int add)
{
exp=exp+add;
if(exp>=nextExp) //升级
{
exp=exp-nextExp;
upLv=lv+"->"+(lv+1);
MyRandom ran=new MyRandom();
ran.setMinInt(1);
ran.setMaxInt(3);
int addPower=ran.nextInt();
int addDefence=ran.nextInt();
ran.setMinInt(5);
ran.setMaxInt(8);
int addLife=ran.nextInt();
ran.setMinInt(4);
ran.setMaxInt(6);
int addMagic=ran.nextInt();
ran.setMinInt(1);
ran.setMaxInt(3);
int addSpeed=ran.nextInt();
upPower=this.getTotalPower()+"->"+(this.getTotalPower()+addPower);
upDefence=this.getTotalDefence()+"->"+(this.getTotalDefence()+addDefence);
upMaxLife=maxLife+"->"+(maxLife+addLife);
upMaxMagic=this.getTotalMagic()+"->"+(this.getTotalMagic()+addMagic);
upActSpeed=actSpeed+"->"+(actSpeed+addSpeed);
lv++;
//升级经验涨幅从60%-1%按级别递减
int expScale=62-lv;
if(expScale<=0)expScale=1;
nextExp+=nextExp*expScale/100;
maxLife+=addLife;
life=maxLife;
maxMagic+=addMagic;
magic=this.getTotalMagic();
power+=addPower;
defence+=addDefence;
actSpeed+=addSpeed;
return true;
}
return false;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
/**
* @return Returns the canChat.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -