📄 aimanager.java
字号:
package com.ejoy;
import java.util.Random;
import java.util.Vector;
import javax.microedition.lcdui.Image;
//Download by http://www.codefans.net
public class AIManager implements Runnable
{
public Vector m_NPCPool;
public int m_currentXPos;
private MySprite m_role;//主角精灵
public final static int npcNum=2;//npc总量
public Random rd;//随机量
//两精灵在x轴,y轴的最小间距
public final static int ySpace=50;
public final static int xSpace=30;
//移动速度
public final int npcSpeed=3;
public int frame=0;
public AIManager(MySprite ms)
{
m_NPCPool=new Vector();
m_role=ms;
rd=new Random();
System.out.println("AIManager构造函数!");
}
public void setCurrentXPos()
{
m_currentXPos=m_role.getRefPixelY();
}
public void run()
{
System.out.println("AIManager::run()函数");
while(true)
{
createNPCCondition();
long lastTime=System.currentTimeMillis();
logic();
long currentTime=System.currentTimeMillis();
int esp=(int)(currentTime-lastTime);
if(esp<1000/GameMIDlet.FPS)
{
try
{
Thread.sleep(1000/GameMIDlet.FPS-esp);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
//npc出现条件,根据主角行走的位移决定
public void createNPCCondition()
{
setCurrentXPos();
switch(m_currentXPos)
{
case GameMIDlet.SCREEN_HEIGHT/2:
createNPC();
initNPCPosition();
break;
case GameMIDlet.SCREEN_HEIGHT:
if(m_NPCPool.isEmpty())
{
createNPC();
initNPCPosition();
}
break;
case GameMIDlet.SCREEN_HEIGHT*3/2:
if(m_NPCPool.isEmpty())
{
createNPC();
initNPCPosition();
}
break;
}
}
//产生npc,包括产生兵种类型及个数,总数不超过3个,
public void createNPC()
{
System.out.println("createNPC函数");
int npcNameStyle=rd.nextInt(4)+2;
int npcnum=rd.nextInt(npcNum)+1;
try
{
switch(npcNameStyle)
{
case MySprite.NPC_CB:
for(int i=1;i<=npcnum;i++)
m_NPCPool.addElement(new MySprite(Image.createImage(GameMIDlet.PATH_CB_STAND),MySprite.NPC_CB_SPWIDTH,MySprite.NPC_CB_SPHEIGHT,MySprite.NPC_CB));
break;
case MySprite.NPC_DB:
for(int i=1;i<=npcnum;i++)
m_NPCPool.addElement(new MySprite(Image.createImage(GameMIDlet.PATH_DB_STAND),MySprite.NPC_DB_SPWIDTH,MySprite.NPC_DB_SPHEIGHT,MySprite.NPC_DB));
break;
case MySprite.NPC_TM:
for(int i=1;i<npcnum;i++)
m_NPCPool.addElement(new MySprite(Image.createImage(GameMIDlet.PATH_TM_STAND),MySprite.NPC_TM_SPWIDTH,MySprite.NPC_TM_SPHEIGHT,MySprite.NPC_TM));
break;
case MySprite.NPC_ZL:
m_NPCPool.addElement(new MySprite(Image.createImage(GameMIDlet.PATH_ZL_STAND),MySprite.NPC_ZL_SPWIDTH,MySprite.NPC_ZL_SPHEIGHT,MySprite.NPC_ZL));
break;
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
//初始化npc精灵的初始位置
public void initNPCPosition()
{
System.out.println("initNPCPosition函数");
MySprite tmp=null;
int rondomX=rd.nextInt(GameMIDlet.SCREEN_WIDTH-tmp.spriteWidth);//npc随机出现的宽度值
if(!m_NPCPool.isEmpty())
{
for(int i=0;i<m_NPCPool.size();i++)
{
tmp=(MySprite)m_NPCPool.elementAt(i);
tmp.setRefPixelPosition(GameMIDlet.SCREEN_WIDTH-tmp.spriteWidth/2-rondomX, GameMIDlet.SCREEN_HEIGHT-tmp.spriteHeight/2);
}
}
}
//AI action
public void logic()
{
System.out.println("logic函数");
MySprite tmp=null;
if(!m_NPCPool.isEmpty())
{
for(int i=0;i<m_NPCPool.size();i++)
{
int dir=rd.nextInt(2);
tmp=(MySprite)m_NPCPool.elementAt(i);
switch(dir)
{
case 0://x方向
for(int j=0;j<3;j++)//防止频繁的改变方向
{
if(tmp.getXDistanceFromSprite(m_role)>npcSpeed)
{
tmp.changeToWalkSprite();
tmp.setRefPixelPosition(tmp.getRefPixelX()-npcSpeed, tmp.getRefPixelY());
}
if(tmp.getXDistanceFromSprite(m_role)<-npcSpeed)
{
tmp.changeToWalkSprite();
tmp.setRefPixelPosition(tmp.getRefPixelX()+npcSpeed, tmp.getRefPixelY());
}
}
break;
case 1://y方向
for(int j=0;j<5;j++)//防止频繁的改变方向
{
if(tmp.getYDistanceFromSprite(m_role)>npcSpeed)
{
tmp.setRefPixelPosition(tmp.getRefPixelX(),tmp.getRefPixelY()-npcSpeed);
}
if(tmp.getYDistanceFromSprite(m_role)<-npcSpeed)
{
tmp.setRefPixelPosition(tmp.getRefPixelX(), tmp.getRefPixelY()+npcSpeed);
}
}
break;
}
tmp.setFrame((frame++)%tmp.getFrameSequenceLength());
}
}
}
public boolean adjustAndCollision()
{
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -