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

📄 battlesprite.java

📁 《神州》RPG游戏引擎
💻 JAVA
字号:
import java.util.*;

import javax.microedition.lcdui.*;

/*
 * Created on 2005-7-15
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

public class BattleSprite extends MySprite {
    private BattleRole battleRole;
    private int w,h;
    public static final int STATUS_WAIT=0;
    public static final int STATUS_ATTACK1=1;
    public static final int STATUS_ATTACK2=2;
    private BattleSprite target;  //攻击的战斗角色
    private MySprite targetRole;  //攻击的实际角色
    private BattleCanvas battleCanvas;
    private boolean isEnemy;
    private Skill battleSkill;
    private boolean showWarEffect;
	
	
    
    private int orgX,orgY;
    private int attackStep;
    private Timer timer;
    private TimerTask timerTask;
    private int attackRangeX;
    private int attackRangeY;
    private int defeatExp;
    private int defeatMoney;
    private Mat defeatMat;
    private boolean commandDefence;  //使用防御指令临时增加的防御力
    private int commandPower;  //使用药物临时增加的攻击力
    
    
    public Mat getDefeatMat() {
        return defeatMat;
    }
    public void setDefeatMat(Mat defeatMat) {
        this.defeatMat = defeatMat;
    }
    public MySprite getTargetRole()
    {
        return targetRole;
    }

    public void setTargetRole(MySprite targetRole) {
        this.targetRole = targetRole;
    }
   
	public int getDefeatExp() {
		return defeatExp;
	}
	public void setDefeatExp(int defeatExp) {
		this.defeatExp = defeatExp;
	}
    public int getOrgX() {
        return orgX;
    }
    public int getOrgY() {
        return orgY;
    }
    public void setOrgX(int orgX) {
        this.orgX = orgX;
    }
    public void setOrgY(int orgY) {
        this.orgY = orgY;
    }
    public MySprite getTarget() {
        return target;
    }
    public void setTarget(BattleSprite target) {
        this.target = target;
    }
    public BattleSprite(BattleRole battleRole,Image roleImg)
    {
        super(roleImg,battleRole.getWidth(),battleRole.getHeight());
        this.battleRole=battleRole;
        this.w=battleRole.getWidth();
        this.h=battleRole.getHeight();
        this.attackRangeX=battleRole.getAttackRangeX();
        this.attackRangeY=battleRole.getAttackRangeY();
        init();
    }
    public void setOrgPosition(int x,int y)
    {
        this.orgX=x;
        this.orgY=y;
        super.setPosition(x,y);
    }
   
    public void init()
    {
        if(battleRole.getActFrame().length>=1)this.setFrameSequence(battleRole.getActFrame()[0]); 
    }
    public void changeStatus(int status)
    {
        if(status==STATUS_WAIT)
        {
            if(battleRole.getActFrame().length>=1)this.setFrameSequence(battleRole.getActFrame()[0]); 
        }
        if(status==STATUS_ATTACK1)
        {
            if(battleRole.getActFrame().length>=2)this.setFrameSequence(battleRole.getActFrame()[1]); 
        }
        if(status==STATUS_ATTACK2)
        {
            if(battleRole.getActFrame().length>=3)this.setFrameSequence(battleRole.getActFrame()[2]); 
        }        
    }
    public void startAttack(BattleCanvas battleCanvas)
    {
        this.battleCanvas=battleCanvas;
        attackStep=0;
        timerTask=new TimerTask(){
            public void run()
            {
                attackStep++;
                attackAction();
            }
        };
        timer=new Timer();
        timer.schedule(timerTask,0,200);
    }
    private void attackAction()
    {
        //步骤1:向前移动
        if(attackStep==1)this.setPosition((target.getOrgX()-this.attackRangeX+this.orgX)/2,(target.getOrgY()-this.attackRangeY+this.orgY)/2);
        
        //步骤2:移动到对方面前
        if(attackStep==2)this.setPosition(target.getOrgX()-this.attackRangeX,target.getOrgY()-this.attackRangeY);
        
        //步骤3:做攻击动作1
        if(attackStep==3)this.nextFrame();
        
        //如果有动作2
        if(attackStep==4)
    	{
	    	this.nextFrame();
	    	
	        if(target!=null)
        	{
	        	if(this.battleSkill!=null)showWarEffect=true;
                int damage=Calc.calcDamage(this,target);
	        	target.reduLife(damage);  //扣战斗角色血(敌人)
	        	if(targetRole!=null)targetRole.reduLife(damage);  //如果是自己方则扣己方角色血
	        	target.showNum(-damage);  //显示头上的扣血
                
                //被击中的效果
	        	int yDelta=target.getOrgY()-this.getOrgY();
	        	int xDelta=target.getOrgX()-this.getOrgX();
	        	int yfh=yDelta>0?1:-1;
	        	int xfh=xDelta>0?1:-1;
	        	if(xDelta>yDelta)
	        	{
	        		int bl=xDelta*10/yDelta;
	        		target.beAttack(xfh*5*bl/10,yfh*5);
	        	}else
	        	{
	        		int bl=yDelta*10/xDelta;
	        		target.beAttack(xfh*5,yfh*5*bl/10);
                }
                //扣MP
                if(battleSkill!=null)
                {                    
                    this.setMagic(this.getMagic()-battleSkill.getUseMagic());
                    GameMIDlet.mainRole.setMagic(GameMIDlet.mainRole.getMagic()-battleSkill.getUseMagic());
                }
	        	
	        	
	        	//System.out.println("扣血"+damage);
        	}
    	}
        //步骤4:退后一步        
        if(attackStep==5)
        {
            showWarEffect=false;            
            this.setFrame(0);
            this.setPosition((target.getOrgX()-this.attackRangeX+this.orgX)/2,(target.getOrgY()-this.attackRangeY+this.orgY)/2);
        }
        
        //步骤5:回到初始位置
        if(attackStep==6)
        {
            this.setZIndex(0);
        	this.setPosition(this.orgX,this.orgY);
            timer.cancel();
            Timer turnTimer=new Timer();
            TimerTask turnTimerTask=new TimerTask()
            {
                public void run()
                {                    
                    battleSkill=null;    
                    battleCanvas.nextTurn();
                }
            };
            turnTimer.schedule(turnTimerTask,500);
        }
    }    
    
    public DamageNum damageNum;
    private Timer timer2;
    private TimerTask timerTask2;
    private int numStep;
    
    //显示头上的伤害数字
    public void showNum(int num)
    {
        numStep=0;
        if(timer2!=null)
        {
            timer2.cancel();
            timer2=null;
            timerTask2=null;
        }
        damageNum=new DamageNum();
    	damageNum.num=Math.abs(num);
    	if(num>=0)
		{
    		damageNum.color=0x0000FF;
		}
    	else
    	{
    		damageNum.color=0xFF0000;
    	}
    	damageNum.point=new Point(this.getOrgX()+this.getWidth()/2,this.getOrgY());
    	timerTask2=new TimerTask()
    	{
    		public void run()
    		{
    			doShowNum();
    		}
    	};
    	timer2=new Timer();
    	timer2.schedule(timerTask2,0,50);
    }
    private void doShowNum()
    {
    	damageNum.point.y--;
    	numStep++;
        //System.out.println("numStep="+numStep);
    	if(numStep>=20)
    	{
    		numStep=0;
    		timer2.cancel();
    		timer2=null;
    		timerTask2=null;
    		damageNum=null;
    	}
    }
    Timer beAttackTimer;
    TimerTask beAttackTimerTask;
    int beAttackX,beAttackY;
    int beAttackStep;
    public void beAttack(int x,int y)
    {
    	beAttackX=x;
    	beAttackY=y;
    	beAttackTimerTask=new TimerTask()
    	{
    		public void run()
    		{
    			doBeAttack();
    		}
    	};
    	beAttackTimer=new Timer();
    	beAttackTimer.schedule(beAttackTimerTask,0,50);
    	beAttackStep=0;
    }
    private void doBeAttack()
    {    	
    	if(beAttackStep==0)
    	{
    		this.setPosition(this.getX()+beAttackX,this.getY()+beAttackY);
    	}else
		if(beAttackStep==4)
    	{
    		this.setPosition(this.getOrgX(),this.getOrgY());    		
    		beAttackTimer.cancel();
    		beAttackTimer=null;
    		beAttackTimerTask=null;
    	}
    	beAttackStep++;
    }
    public int getDefeatMoney() {
        return defeatMoney;
    }
    public void setDefeatMoney(int defeatMoney) {
        this.defeatMoney = defeatMoney;
    }
    public boolean isCommandDefence() {
        return commandDefence;
    }
    public void setCommandDefence(boolean commandDefence) {
        this.commandDefence = commandDefence;
    }
    
    //  获得战斗时的攻击力,包括吃大力丸时临时增加的攻击力 
    public int getBattlePower()
    {
        return this.getTotalPower()+this.commandPower;
    }
    public int getCommandPower() {
        return commandPower;
    }
    public void setCommandPower(int commandPower) {
        this.commandPower = commandPower;
    }
    public boolean isEnemy() {
        return isEnemy;
    }
    public void setEnemy(boolean isEnemy) {
        this.isEnemy = isEnemy;
    }
    public Skill getBattleSkill() {
        return battleSkill;
    }
    public void setBattleSkill(Skill battleSkill) {
        this.battleSkill = battleSkill;
    }
    public boolean isShowWarEffect() {
        return showWarEffect;
    }
    public void setShowWarEffect(boolean showWarEffect) {
        this.showWarEffect = showWarEffect;
    }
}

⌨️ 快捷键说明

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