📄 flightmanager.java
字号:
else session.setAsClientMode();
if( !session.initBlueTooch("Flight") )
return false;
// 匿名内部Thread,访问远程服务。
Thread fetchThread = new Thread()
{
public void run()
{
while( !session.isConnected())
{
showInfo("waiting for conn");
try
{
sleep(100);
}
catch (InterruptedException e1)
{
// TODO 自动生成 catch 块
e1.printStackTrace();
}
}
//display.setCurrent(session);
System.out.println("done");
conn = session.getConnections();
try
{
ins = conn.openDataInputStream();
}
catch (IOException e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
}
try
{
outs = conn.openDataOutputStream();
}
catch (IOException e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
}
showInfo("you can begin init the map");
}
};
fetchThread.start();
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
}
return true;
}
public void gameInit()
{
//战斗机布局
if ((this.Canvas.allAction & LPKeyMask.MASK_KEY_UP_FLAG) != 0) //响应上键
{
//System.out.println("响应上键");
this.Canvas.allAction &= ~LPKeyMask.MASK_KEY_UP_FLAG; //清楚响应标志
if( dot.y == I_VECTOR_Y )dot.y = A_VECTOR_Y;
else dot.y++;
this.Canvas.repaint();
this.Canvas.serviceRepaints();
//y -= this.Canvas.deltaY;
}
else if ((this.Canvas.allAction & LPKeyMask.MASK_KEY_DOWN_FLAG) != 0) //响应下键
{
//System.out.println("响应下键");
this.Canvas.allAction &= ~LPKeyMask.MASK_KEY_DOWN_FLAG; //清楚响应标志
if( dot.y == A_VECTOR_Y )dot.y = I_VECTOR_Y;
else dot.y--;
this.Canvas.repaint();
this.Canvas.serviceRepaints();
//y+=this.Canvas.deltaY;
}
else if ((this.Canvas.allAction & LPKeyMask.MASK_KEY_LEFT_FLAG ) != 0 ) //相应左键
{
this.Canvas.allAction &= ~LPKeyMask.MASK_KEY_LEFT_FLAG;
//填入动作相应
if( dot.x == 0 )dot.x = 8;
else dot.x--;
this.Canvas.repaint();
this.Canvas.serviceRepaints();
//x-=this.Canvas.deltaX;
}
else if ((this.Canvas.allAction & LPKeyMask.MASK_KEY_RIGHT_FLAG ) != 0 ) //相应右键
{
this.Canvas.allAction &= ~LPKeyMask.MASK_KEY_RIGHT_FLAG;
//填入动作相应
if( dot.x == 8 )dot.x = 0;
else dot.x++;
this.Canvas.repaint();
this.Canvas.serviceRepaints();
//x+=this.Canvas.deltaX;
}
else if ((this.Canvas.allAction & LPKeyMask.MASK_KEY_STAR_FLAG) != 0 ) //响应*键
{
//*键,作用是将战斗机转向,逆时针转,即flightType--
this.Canvas.allAction &= ~LPKeyMask.MASK_KEY_STAR_FLAG; //清除响应标志
if( this.Canvas.flightType == 0 )
this.Canvas.flightType=3;
else this.Canvas.flightType--;
this.Canvas.repaint();
this.Canvas.serviceRepaints();
}
else if ((this.Canvas.allAction & LPKeyMask.MASK_KEY_POUND_FLAG) != 0 ) //响应#键
{
// 响应“#”键,作用与相反
this.Canvas.allAction &= ~LPKeyMask.MASK_KEY_POUND_FLAG; //清除响应标志
if( this.Canvas.flightType == 3 )
this.Canvas.flightType=0;
else this.Canvas.flightType++;
this.Canvas.repaint();
this.Canvas.serviceRepaints();
}
else if ((this.Canvas.allAction & LPKeyMask.MASK_KEY_OK_FLAG) != 0) //响应确定键
{
//System.out.println("响应OK键");
this.Canvas.allAction &= ~LPKeyMask.MASK_KEY_OK_FLAG; //清楚响应标志
if (intMyMap[dot.x][dot.y] == 2)
{
delFlight(dot.x, dot.y);
}
else if( intMyMap[dot.x][dot.y] == 0 )
{
if( addFlight(this.Canvas.flightType,dot.x,dot.y) )
{
System.out.println("将此战斗机固定下来,开始新的战斗机安排");
}
else System.out.println("无法安置战斗机");
}
this.Canvas.repaint();
this.Canvas.serviceRepaints();
}//else
}
public boolean addFlight(int type, int x, int y)
{
if ( this.Canvas.fwLog.flightCount >= 3 )
{
System.out.println("满员");
return false;
}
int[] tmp = fmodel.toMap(type, x, y);
// 当战斗机处于地图之外,toMap方法将会置tmp[0]=-1,否则tmp[0]将是第一个x坐标
if( tmp[0] == -1 ) return false;
if( intMyMap[x][y] != 0 )
return false;
for(int i = 2; i < 20; i++)
if(intMyMap[tmp[i++]][tmp[i]]!=0)
return false;
for(int i=2;i<20; i++)
intMyMap[tmp[i++]][tmp[i]]=1;
intMyMap[x][y] = 2;
this.Canvas.fwLog.setFlightPoint(type,x,y);
if ( this.Canvas.fwLog.flightCount == 3 )
gameState = GAMESTATE_INIT_DONE;
return true;
}
public boolean delFlight(int x, int y)
{
System.out.println("delete flight");
int type = this.Canvas.fwLog.getFlightType(x, y);
if(type == -1)return false;
int[] tmp = fmodel.toMap(type, x, y);
if( intMyMap[x][y] != 2 )
return false;
for (int i = 2; i < 20; i++)
intMyMap[tmp[i++]][tmp[i]]=0;
intMyMap[x][y] = 0;
this.Canvas.fwLog.resetFlightPoint(x,y);
return true;
}
public boolean ChoiseFirst()
{
if ((this.Canvas.allAction & LPKeyMask.MASK_KEY_UP_FLAG ) != 0 ) //相应上键
{
this.Canvas.allAction &= ~LPKeyMask.MASK_KEY_UP_FLAG;
//填入动作相应
if( this.Canvas.isFirst == 0 )
this.Canvas.isFirst = 1;
else this.Canvas.isFirst = 0;
this.Canvas.repaint();
this.Canvas.serviceRepaints();
//x-=this.Canvas.deltaX;
}
else if ((this.Canvas.allAction & LPKeyMask.MASK_KEY_DOWN_FLAG ) != 0 ) //相应下键
{
this.Canvas.allAction &= ~LPKeyMask.MASK_KEY_DOWN_FLAG;
if( this.Canvas.isFirst == 0 )
this.Canvas.isFirst = 1;
else this.Canvas.isFirst = 0;
this.Canvas.repaint();
this.Canvas.serviceRepaints();
//x+=this.Canvas.deltaX;
}
else if ((this.Canvas.allAction & LPKeyMask.MASK_KEY_OK_FLAG) != 0) //响应确定键
{
//System.out.println("响应OK键");
this.Canvas.allAction &= ~LPKeyMask.MASK_KEY_OK_FLAG; //清楚响应标志
if( this.Canvas.isFirst == 0 )
this.isFirst = true;
else this.isFirst = false;
gameState = GAMESTATE_INIT;
beginNewGame();
this.Canvas.MyMapBack = Image.createImage(this.Canvas.buffer);
this.Canvas.repaint();
this.Canvas.serviceRepaints();
}//else
return true;
}
public boolean gameInitDone()
{
if ((this.Canvas.allAction & LPKeyMask.MASK_KEY_UP_FLAG ) != 0 ) //相应上键
{
this.Canvas.allAction &= ~LPKeyMask.MASK_KEY_UP_FLAG;
//填入动作相应
if( this.Canvas.isInitDone == 0 )
this.Canvas.isInitDone = 1;
else this.Canvas.isInitDone = 0;
this.Canvas.repaint();
this.Canvas.serviceRepaints();
//x-=this.Canvas.deltaX;
}
else if ((this.Canvas.allAction & LPKeyMask.MASK_KEY_DOWN_FLAG ) != 0 ) //相应下键
{
this.Canvas.allAction &= ~LPKeyMask.MASK_KEY_DOWN_FLAG;
if( this.Canvas.isInitDone == 0 )
this.Canvas.isInitDone = 1;
else this.Canvas.isInitDone = 0;
this.Canvas.repaint();
this.Canvas.serviceRepaints();
//x+=this.Canvas.deltaX;
}
else if ((this.Canvas.allAction & LPKeyMask.MASK_KEY_OK_FLAG) != 0) //响应确定键
{
//System.out.println("响应OK键");
this.Canvas.allAction &= ~LPKeyMask.MASK_KEY_OK_FLAG; //清楚响应标志
if( this.Canvas.isInitDone == 0 )
{
gameState = GAMESTATE_GAMELOOP;
try
{
outs.writeBoolean(true);
showInfo("waiting...");
while(!ins.readBoolean());
//System.out.println("other is ready:");
gameIsBegin = true;
showInfo("宣战!");
// for (int i = 0; i < 8; i++)
// {
// for (int j = 0; j < 8; j++)
// {
// System.out.println(intMyMap[i][j]+" ");
// }
// System.out.println("n");
// }
if(!isFirst)
{
Thread recvThread = new Thread()
{
public void run()
{
recvDot();
}
};
recvThread.start();
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
catch (IOException e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
else gameState = GAMESTATE_INIT;
this.Canvas.repaint();
this.Canvas.serviceRepaints();
}//else
return true;
}
public int getFlightPointX()
{
return this.Canvas.mapbaseX+dot.x*this.Canvas.deltaX;
}
public int getFlightPointY()
{
return this.Canvas.mapbaseY+(9-dot.y)*this.Canvas.deltaY;
}
public int FlightType=0;
public int gameState;
public boolean isFirst; //true是先手
public int count; //本方开炮数
public int opcount; //对手开炮数
public int [][]intMyMap = new int[9][9];
public int [][]intOpMap = new int[9][9];
//public int x=0;
//public int y=0;
public Dot dot = new Dot();
public FWmainCanvas Canvas;
private FlightModel fmodel = new FlightModel();
public Session session;
public Image tmpBuffer;
public StreamConnection conn;
public DataInputStream ins;
public DataOutputStream outs;
public String buf;
public static final int GAMESTATE_MENU = 1;
public static final int GAMESTATE_INIT= 2;
public static final int GAMESTATE_INIT_DONE= 3;
public static final int GAMESTATE_GAMELOOP = 4;
public static final int GAMESTATE_PAUSE = 5;
public static final int GAMESTATE_REINIT = 6;
public static final int GAMESTATE_BT_INIT = 7;
public static final int GAMESTATE_CHOISEFIRST = 8;
public static final int GAMESTATE_SHOWINFO = 0;
public static final int A_VECTOR_Y = 0;
public static final int B_VECTOR_Y = 1;
public static final int C_VECTOR_Y = 2;
public static final int D_VECTOR_Y = 3;
public static final int E_VECTOR_Y = 4;
public static final int F_VECTOR_Y = 5;
public static final int G_VECTOR_Y = 6;
public static final int H_VECTOR_Y = 7;
public static final int I_VECTOR_Y = 8;
public static final int MAP_EMPTY = 0;
public static final int MAP_FLIGHT_BODY = 1;
public static final int MAP_FLIGHT_HEAD = 2;
public static final int MAP_SHOT_EMPTY = 3;
public static final int MAP_SHOT_BODY = 4;
public static final int MAP_SHOT_HEAD = 5;
}
class Dot
{
public Dot()
{
x=0;
y=0;
}
public int x,y;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -