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

📄 battlecanvas.java

📁 《神州》RPG游戏引擎
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            }
        });
        Object[] objs=mySorter.getSortedObjs();
        battleRoleVC.removeAllElements();
        //battleSprites=new BattleSprite[objs.length];
        for(int i=0;objs!=null && i<objs.length;i++)
        {
            battleRoleVC.addElement((BattleSprite)objs[i]);
        }
    }
    
    boolean ThreadRunning;
    long t1;
    int runTimes;
    public void startGame()
    {
        isPlay=true;
        if(!ThreadRunning)
        {
            t1=System.currentTimeMillis();
            runTimes=0;
            (new Thread(this)).start();           
            ThreadRunning=true;
        }        
       
    }
    public void stop()
    {
        isPlay=false;
    }
    
    //为主角自动找目标
    private void findTarget()
    {
        if(battleHand.getEnemyIndex()<enemyVC.size())
        {
            battleMainRole.setTarget((BattleSprite)enemyVC.elementAt(battleHand.getEnemyIndex()));
            return;
        }else
        {
            for(int i=0;enemyVC!=null && i<enemyVC.size();i++)
            {
                if(ashesVC==null || !ashesVC.contains(enemyVC.elementAt(i)))
                {
                    battleMainRole.setTarget((BattleSprite)enemyVC.elementAt(i));
                    return;
                }
            }
        }        
        return;
    }
    public void nextTurn()
    {    	    
        attackTurn++;
       
        if(attackTurn>battleRoleVC.size()-1) //一轮结束
        {          
            battleMainRole.setCommandDefence(false);  //去除防御状态
            attackTurn=-1;
            
            //每轮把死的从battleRoleVC和enemyVC中去了
            clearDeathSprite();
            //把活的重新排序
            sortBySpeed();
            
            //判断是否有活的敌人
            boolean haveLiveEnemy=false;
            for(int i=0;enemyVC!=null && i<enemyVC.size();i++)
            {
                BattleSprite enemy=(BattleSprite)enemyVC.elementAt(i);                
                if(enemy.getLife()>0)
                {
                    haveLiveEnemy=true;
                }
            }
            
            //判断是否有活的己方
            boolean haveLiveFriend=false;
            for(int i=0;battleRoleVC!=null && i<battleRoleVC.size();i++)
            {
                BattleSprite role=(BattleSprite)battleRoleVC.elementAt(i);
                if(!role.isEnemy())
                {
                    if(role.getLife()>0)
                    {
                        haveLiveFriend=true;
                    }
                }
            }
            
            if(haveLiveEnemy && haveLiveFriend)
            {
                if(choosedCommand!=BattleCanvas.COMMAND_AUTO)
                    status=BattleCanvas.STATUS_COMMAND;
                else
                    nextTurn();
            }
            
        }
        else
        {
            //System.out.println(((BattleSprite)battleRoleVC.elementAt(attackTurn)).getName()+"行动");
            battleAction();
        }
    }
    
    Timer turnTimer;
    TimerTask turnTimerTask;
    
    //战斗动作
    private void battleAction()
    {
        if(attackTurn>battleRoleVC.size()-1)return;
        if(battleRoleVC.elementAt(attackTurn)==battleMainRole)  //主角行动
        {
        	//攻击
            if(choosedCommand==BattleCanvas.COMMAND_ATTACK || choosedCommand==BattleCanvas.COMMAND_SKILL || choosedCommand==BattleCanvas.COMMAND_AUTO)
            {                
                if(choosedCommand==BattleCanvas.COMMAND_AUTO)
                {
                    //自动为主角找目标
                    findTarget();
                }
                //其他对象置后                
                //battleMainRole.getTarget().setZIndex(0);
                //自己置前
                battleMainRole.setZIndex(1);
                if(ashesVC==null || !ashesVC.contains(battleMainRole.getTarget()) && !ashesVC.contains(battleMainRole))
                {
                    battleMainRole.startAttack(this);
                }
                else
                {
                    nextTurn();
                }               
            }
            else
            //防御,效果加倍防御力
            if(choosedCommand==BattleCanvas.COMMAND_DEFENCE)
            {
                nextTurn();
            }
            else
            //道具
            if(choosedCommand==BattleCanvas.COMMAND_ITEM)
            {
                //加血动作                
                if(battleMainRole.getLife()>0)
                {
                ReturnValue rv = GameMIDlet.mainRole.useMat(useMat);  //使用物品
                synBattleMainRole();  //同步战斗角色数据
                GameMIDlet.mainRole.updateBag();   //更新用户身上的物品列表                
                
                battleMainRole.showNum(rv.intKey);  //显示头上的加血
                }
                
                Timer turnTimer=new Timer();
                TimerTask turnTimerTask=new TimerTask()
                {
                    public void run()
                    {
                        nextTurn();
                    }
                };
                turnTimer.schedule(turnTimerTask,1000);                
              
            }            
            else
            {
                nextTurn();
            }
        }
        else //其他对象行动
        {   
            if(ashesVC==null || !ashesVC.contains(battleRoleVC.elementAt(attackTurn))&& !ashesVC.contains(((BattleSprite)battleRoleVC.elementAt(attackTurn)).getTarget()))
            {               
                ((BattleSprite)battleRoleVC.elementAt(attackTurn)).setZIndex(1);                
                ((BattleSprite)battleRoleVC.elementAt(attackTurn)).startAttack(this);
            }
            else
            {                
                nextTurn();
            }
            
        }    	    
    }
    private void drawLife()
    {
    	int LMargin=5;
        int TMargin=height-36;
        g.drawImage(hp,LMargin,TMargin,Style.LT);
        int lv=GameMIDlet.mainRole.getLv();
        int maxLife=GameMIDlet.mainRole.getMaxLife();
        int life=GameMIDlet.mainRole.getLife();
        int maxMagic=GameMIDlet.mainRole.getTotalMagic();
        int magic=GameMIDlet.mainRole.getMagic();
        if(magic>maxMagic)
        {
        	magic=maxMagic;
        	GameMIDlet.mainRole.setMagic(magic);
        }
        int nextExp=GameMIDlet.mainRole.getNextExp();
        int exp=GameMIDlet.mainRole.getExp();
        
        g.setFont(Style.font);        
        g.setColor(0x000000);
        g.drawString(String.valueOf(lv),LMargin+18,TMargin-6,Style.LT);
        g.setColor(0xFFFFFF);
        g.drawString(String.valueOf(lv),LMargin+17,TMargin-7,Style.LT);
        //HP
        g.setColor(0xFFFFFF);
        g.fillRect(LMargin+17,TMargin+10,60,4);
        int lifeLength=0;
        if(life>0)
        {
        	lifeLength=60*100/(maxLife*100/life);
        }        
        g.setColor(0xFF0000);
        g.fillRect(LMargin+17,TMargin+10,lifeLength,4);
        g.setColor(0xA21515);
        g.drawRect(LMargin+17,TMargin+10,60,4);
        
        //MP        
        g.setColor(0xFFFFFF);
        g.fillRect(LMargin+17,TMargin+18,60,4);
        int magicLength=0;
        if(magic>0)
        {
        	magicLength=60*100/(maxMagic*100/magic);
        } 
        g.setColor(0x0000FF);
        g.fillRect(LMargin+17,TMargin+18,magicLength,4);
        g.setColor(0x12129C);
        g.drawRect(LMargin+17,TMargin+18,60,4);
        
        //EXP        
        g.setColor(0xFFFFFF);
        g.fillRect(LMargin+17,TMargin+26,60,4);
        int expLength=0;
        if(exp>0)
        {
        	expLength=60*100/(nextExp*100/exp);
        } 
        g.setColor(0xFFFF00);
        g.fillRect(LMargin+17,TMargin+26,expLength,4);
        g.setColor(0x94741B);
        g.drawRect(LMargin+17,TMargin+26,60,4);
        
        g.setColor(0x000000);
        g.drawString(String.valueOf(life),LMargin+17+62,TMargin+12-Style.font.getHeight()/2,Style.LT);
        g.drawString(String.valueOf(magic),LMargin+17+62,TMargin+22-Style.font.getHeight()/2,Style.LT);
        g.setColor(0xFFFFFF);
        g.drawString(String.valueOf(life),LMargin+17+61,TMargin+11-Style.font.getHeight()/2,Style.LT);
        g.drawString(String.valueOf(magic),LMargin+17+61,TMargin+21-Style.font.getHeight()/2,Style.LT);
        
        
    }
    
    boolean haveAddExp,haveShowResult,showResult;
    private Timer timer2;
    private TimerTask timerTask2;
    private void showResult(boolean success)
    {
    	if(!haveShowResult)  //避免run里重复调用
    	{	    	
	    	timerTask2=new TimerTask()
	    	{
	    		public void run()
	    		{
	    			showResult=true;	    			
	    		}
	    	};	    	
	    	
	    	timer2=new Timer();
	    	timer2.schedule(timerTask2,800);	    	    	
	    	haveShowResult=true;	    	
    	}
    	
    	doShowResult(success);
    }
    private boolean havePlayEndMusic;
    private void doShowResult(boolean success)
    {    	
    	if(showResult)
    	{
    		int w=Style.font.stringWidth("字字字字字字字字");
    		int h=Style.font.getHeight()*6;
    		int x=(width-w)/2;
	    	int y=(height-h)/2;	    	
  
	    	g.setColor(Style.bgColor);
	        g.fillRect(x,y,w,h);  //对话框背景色
	        g.setColor(Style.boardColor);
	    	g.drawRect(x,y,w,h); //外框
	    	if(success)
	    	{	    	
	    		if(!havePlayEndMusic)
	    		{
	    		GameMIDlet.midlet.stopBattleSound();
		        GameMIDlet.midlet.playBattleSound(1,false,-10);
		        havePlayEndMusic=true;
	    		}
	    		Image successImg=Tools.getImage("/pic/success.png");
		    	g.drawImage(successImg,x+w/2,y+7,Style.CT);
		    	g.setFont(Style.font);
		    	g.setColor(0x000000);
		    	g.drawString("获得经验",x+5,y+31,Style.LT);    	
		    	g.drawString("获得金钱",x+5,y+31+Style.font.getHeight(),Style.LT);
		    	g.drawString("得到",x+5,y+31+Style.font.getHeight()*2,Style.LT);
		    	g.setColor(0xFF0000);
		    	g.drawString(String.valueOf(finalExp),x+10+Style.font.stringWidth("获得经验"),y+31,Style.LT);
		    	g.drawString(String.valueOf(finalMoney),x+10+Style.font.stringWidth("获得金钱"),y+31+Style.font.getHeight(),Style.LT);
                StringBuffer sb=new StringBuffer("");
                for(int i=0;defeatMats!=null && i<defeatMats.length;i++)
                {
                    if(i>0)sb.append(",");
                    sb.append(defeatMats[i].getName());
                }
                String[] itemArr=Tools.splitString(sb.toString(),w-(10+Style.font.stringWidth("得到")));
                for(int i=0;itemArr!=null && i<itemArr.length;i++)
                {
                    g.drawString(itemArr[i],x+10+Style.font.stringWidth("得到"),y+31+Style.font.getHeight()*2+Style.font.getHeight()*i,Style.LT);    
                }
                
		    		 
		    	if(!haveAddExp)
	    		{
		    		GameMIDlet.mainRole.addExp(finalExp);
		    		GameMIDlet.mainRole.addMoney(finalMoney);
		    		
                    for(int i=0;defeatMats!=null && i<defeatMats.length;i++)
                    {
                        GameMIDlet.mainRole.addMat(defeatMats[i]);
                    }
					
		    		haveAddExp=true;
	    		}
	    	}
	    	else
	    	{
	    		if(!havePlayEndMusic)
	    		{
	    		GameMIDlet.midlet.stopBattleSound();
	            GameMIDlet.midlet.playBattleSound(2,false,-10);
	            havePlayEndMusic=true;
	    		}
	    		Image failImg=Tools.getImage("/pic/fail.png");
		    	g.drawImage(failImg,width/2,height/2,Style.CC);
		    	
	    	}
	    	if(GameMIDlet.mainRole.getUpLv()==null)
	    	{
	    		status=STATUS_END;
	    	}
    	}
    }
    
    //显示升级信息
    boolean haveShowLvupInfo,showLvupInfo;
    private Timer lvupTimer;
    private TimerTask lvupTimerTask;
    private void showLvupInfo()
    {
    	if(!haveShowLvupInfo && GameMIDlet.mainRole.getUpLv()!=null)  //避免run里重复调用
    	{	    	
    		lvupTimerTask=new TimerTask()
	    	{
	    		public void run()
	    		{
	    			showLvupInfo=true;	    			
	    		}
	    	};	    	
	    	
	    	lvupTimer=new Timer();
	    	lvupTimer.schedule(lvupTimerTask,1500);
	    	haveShowLvupInfo=true;
    	}
    	doShowLvupInfo();
    }
    
    private void doShowLvupInfo()
    {
    	if(showLvupInfo)
    	{
	    	int w=Style.font.stringWidth("字字字字字字字字字");
	    	int h=Style.font.getHeight()*8;
    		int x=(width-w)/2;
	    	int y=(height-h)/2;
	    	
	    	g.setColor(Style.bgColor);
	        g.fillRect(x,y,w,h);  //对话框背景色
	        g.setColor(Style.boardColor);
	    	g.drawRect(x,y,w,h); //外框
	    	
    		Image successImg=Tools.getImage("/pic/lvup.png");
	    	g.drawImage(successImg,x+w/2,y+7,Style.CT);

⌨️ 快捷键说明

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