GameManager.java
资源名称:J2ME&Game.rar [点击查看]
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:12k
源码类别:
J2ME
开发平台:
Java
- package cardclient;
- import javax.microedition.io.SocketConnection;
- import java.io.InputStream;
- import javax.microedition.io.Connector;
- import java.io.OutputStream;
- /**
- * <p>Title: CardClient</p>
- *
- * <p>Description: lizhenpeng</p>
- *
- * <p>Copyright: Copyright (c) 2005</p>
- *
- * <p>Company: LP&P</p>
- *
- * @author lipeng
- * @version 1.0
- */
- import lipeng.*;
- import javax.microedition.lcdui.*;
- public class GameManager
- implements Runnable
- {
- public GameManager()
- {
- inst=this;
- gameState=0;
- for(int i=0;i<30;i++)
- {
- userCard[i]=new Card();
- oppoCard[i]=new Card();
- userPostCard[i]=new Card();
- oppoPostCard[i]=new Card();
- }
- }
- public void run()
- {
- while(networkEnable)
- {
- try
- {
- in.read(recvHeadBuffer,0,4);
- recvDataHead.FillData(recvHeadBuffer,0);
- if(recvDataHead.size!=0)
- {
- recvBuffer=new byte[recvDataHead.size];
- in.read(recvBuffer,0,recvDataHead.size);
- //处理数据
- ProcessData();
- } else
- {
- //处理信息
- ProcessData();
- }
- } catch(Exception e)
- {
- System.out.println(e);
- }
- }
- }
- public void openSocket()
- {
- try
- {
- conn=(SocketConnection)Connector.open("socket://localhost:8070");
- in=conn.openInputStream();
- out=conn.openOutputStream();
- networkEnable=true;
- sendDataHead.command=NetProtocol.LOGIN;
- //16X2
- sendDataHead.size=32;
- byte[] buffer=userName.getBytes();
- byte[] buffer2=new byte[32];
- System.arraycopy(buffer,0,buffer2,0,buffer.length);
- this.sendBuffer=sendDataHead.getBytes();
- out.write(sendBuffer,0,sendBuffer.length);
- out.write(buffer2,0,buffer2.length);
- gameState=2;
- } catch(Exception e)
- {
- System.out.print(e);
- }
- networkThread=new Thread(this);
- networkThread.start();
- }
- private void hasControl()
- {
- this.keyAction=0;
- this.iControl=true;
- cursorX=0;
- cursorY=MainGame.inst.h-22*3;
- cardSelect=0;
- currentCard=0;
- isSelect=false;
- }
- public void ProcessData()
- {
- switch(recvDataHead.command)
- {
- case NetProtocol.HASLOGIN:
- {
- userList=new String[recvDataHead.size/32];
- int i;
- int place=0;
- for(i=0;i<userList.length;i++)
- {
- userList[i]=new String(this.recvBuffer,place,32);
- place+=32;
- }
- MainGame.inst.userList=new UserList();
- Display.getDisplay(CardClientMIDlet.instance).setCurrent(MainGame.inst.
- userList);
- }
- break;
- case NetProtocol.STARTGAME_FIRST:
- {
- Display.getDisplay(CardClientMIDlet.instance).setCurrent(MainGame.inst);
- int place = 0;
- this.userCardNum = 54/2;
- this.oppoCardNum = 54/2;
- for(int i=0;i<27;i++)
- {
- this.userCard[i].fillData(this.recvBuffer,place);
- place += 4;
- }
- for(int i=0;i<27;i++)
- {
- this.oppoCard[i].fillData(this.recvBuffer,place);
- place +=4;
- }
- gameState = 3;
- hasControl();
- }
- break;
- case NetProtocol.STARTGAME_LAST:
- {
- Display.getDisplay(CardClientMIDlet.instance).setCurrent(MainGame.inst);
- int place=0;
- this.iControl=false;
- this.userCardNum=54/2;
- this.oppoCardNum=54/2;
- for(int i=0;i<27;i++)
- {
- this.userCard[i].fillData(this.recvBuffer,place);
- place+=4;
- }
- for(int i=0;i<27;i++)
- {
- this.oppoCard[i].fillData(this.recvBuffer,place);
- place+=4;
- }
- this.gameState=3;
- }
- break;
- case NetProtocol.OPPO_REJECT:
- {
- this.userPostCardNum = 0;
- hasControl();
- }
- break;
- case NetProtocol.OPPO_POSTCARD:
- {
- this.oppoPostCard[0].fillData(recvBuffer,0);
- userPostCardNum = 0;
- oppoPostCardNum=1;
- hasControl();
- }
- break;
- }
- }
- public void keyPressd(int keyCode,int gameKeyCode)
- {
- switch(gameKeyCode)
- {
- case Canvas.UP:
- keyAction|=LPKeyMask.MASK_KEY_UP;
- break;
- case Canvas.DOWN:
- keyAction|=LPKeyMask.MASK_KEY_DOWN;
- break;
- case Canvas.FIRE:
- keyAction|=LPKeyMask.MASK_KEY_OK;
- break;
- case Canvas.LEFT:
- keyAction|=LPKeyMask.MASK_KEY_LEFT;
- break;
- case Canvas.RIGHT:
- keyAction|=LPKeyMask.MASK_KEY_RIGHT;
- break;
- }
- }
- public void keyReleased(int keyCode,int gameKeyCode)
- {
- switch(gameKeyCode)
- {
- case Canvas.FIRE:
- keyAction&=~LPKeyMask.MASK_KEY_OK;
- break;
- case Canvas.UP:
- keyAction&=~LPKeyMask.MASK_KEY_UP;
- break;
- case Canvas.DOWN:
- keyAction&=~LPKeyMask.MASK_KEY_DOWN;
- break;
- case Canvas.LEFT:
- keyAction&=~LPKeyMask.MASK_KEY_LEFT;
- break;
- case Canvas.RIGHT:
- keyAction&=~LPKeyMask.MASK_KEY_RIGHT;
- break;
- }
- }
- public void menuAction()
- {
- if((keyAction&LPKeyMask.MASK_KEY_UP)!=0)
- {
- keyAction&=~LPKeyMask.MASK_KEY_UP;
- if(this.menuWhichActive>0)
- {
- --menuWhichActive;
- } else
- {
- menuWhichActive=2;
- }
- } else if((keyAction&LPKeyMask.MASK_KEY_DOWN)!=0)
- {
- keyAction&=~LPKeyMask.MASK_KEY_DOWN;
- if(this.menuWhichActive<2)
- {
- ++menuWhichActive;
- } else
- {
- menuWhichActive=0;
- }
- } else if((keyAction&LPKeyMask.MASK_KEY_OK)!=0)
- {
- keyAction&=~LPKeyMask.MASK_KEY_OK;
- switch(menuWhichActive)
- {
- case 0:
- //登陆游戏
- //openSocket();
- gameState = 1;
- Display.getDisplay(CardClientMIDlet.instance).setCurrent(MainGame.inst.input);
- break;
- case 1:
- //帮助
- break;
- case 2:
- //退出
- break;
- }
- }
- }
- public void gameAction()
- {
- if(iControl)
- {
- int intern=MainGame.inst.w/14;
- if((keyAction&LPKeyMask.MASK_KEY_OK)!=0)
- {
- keyAction&=~LPKeyMask.MASK_KEY_OK;
- if(this.isSelect)
- {
- try
- {
- byte[] buffer=this.userCard[this.cardSelect].getBytes();
- this.sendDataHead.command=NetProtocol.POST_CARD;
- this.sendDataHead.size=(char)buffer.length;
- this.sendBuffer=this.sendDataHead.getBytes();
- this.out.write(sendBuffer,0,sendBuffer.length);
- this.out.write(buffer,0,buffer.length);
- this.iControl = false;
- //出牌
- this.userPostCard[0]= this.userCard[cardSelect];
- this.userPostCardNum = 1;
- //整理牌
- for(int i=cardSelect;i<this.userCardNum-1;i++)
- {
- userCard[i] = userCard[i+1];
- }
- isSelect = false;
- --userCardNum;
- }
- catch(Exception e)
- {
- System.out.print(e);
- }
- }
- else
- {
- try
- {
- this.sendDataHead.command=NetProtocol.REJECT_CARD;
- this.sendDataHead.size=0;
- this.sendBuffer=this.sendDataHead.getBytes();
- this.out.write(sendBuffer,0,sendBuffer.length);
- }
- catch(Exception e)
- {
- System.out.print(e);
- }
- }
- } else if((keyAction&LPKeyMask.MASK_KEY_UP)!=0)
- {
- keyAction&=~LPKeyMask.MASK_KEY_UP;
- this.isSelect = true;
- this.cardSelect = currentCard;
- } else if((keyAction&LPKeyMask.MASK_KEY_DOWN)!=0)
- {
- keyAction&=~LPKeyMask.MASK_KEY_DOWN;
- this.isSelect = false;
- } else if((keyAction&LPKeyMask.MASK_KEY_LEFT)!=0)
- {
- keyAction&=~LPKeyMask.MASK_KEY_LEFT;
- if(this.userCardNum<=14)
- {
- if(cursorX!=0)
- {
- cursorX-=intern;
- currentCard--;
- }
- else
- {
- cursorX = (userCardNum-1)*intern;
- currentCard = userCardNum-1;
- }
- cursorY = MainGame.inst.h-22*2;
- }
- else
- {
- if(cursorX!=0)
- {
- cursorX-=intern;
- currentCard--;
- } else
- {
- if(cursorY ==MainGame.inst.h-22*2)
- {
- cursorX=intern*13;
- cursorY = MainGame.inst.h-22*3;
- currentCard = 13;
- }
- else if(cursorY ==MainGame.inst.h-22*3)
- {
- cursorX=intern*(userCardNum-15);
- currentCard = userCardNum-1;
- cursorY = MainGame.inst.h-22*2;
- }
- }
- }
- } else if((keyAction&LPKeyMask.MASK_KEY_RIGHT)!=0)
- {
- keyAction&=~LPKeyMask.MASK_KEY_RIGHT;
- if(this.userCardNum<=14)
- {
- if(cursorX!=(userCardNum-1)*intern)
- {
- cursorX+=intern;
- currentCard++;
- }
- else
- {
- cursorX = 0;
- currentCard = 0;
- }
- cursorY = MainGame.inst.h-22*2;
- }
- else
- {
- if(cursorY==MainGame.inst.h-22*2)
- {
- if(cursorX!=(userCardNum-15)*intern)
- {
- cursorX+=intern;
- currentCard++;
- }
- else
- {
- cursorX=0;
- currentCard = 0;
- cursorY=MainGame.inst.h-22*3;
- }
- } else if(cursorY==MainGame.inst.h-22*3)
- {
- if(cursorX!=13*intern)
- {
- cursorX+=intern;
- currentCard++;
- } else
- {
- cursorX=0;
- cursorY=MainGame.inst.h-22*2;
- currentCard = 14;
- }
- }
- }
- }
- }
- }
- public void RegiterUser(String name)
- {
- userName=name;
- gameState=2;
- Display.getDisplay(CardClientMIDlet.instance).setCurrent(MainGame.inst);
- }
- public void waitAction()
- {
- if(!isOpenSocket)
- {
- openSocket();
- isOpenSocket = true;
- }
- }
- public void BeginGame(String name)
- {
- //开始游戏
- try
- {
- gameState=1;
- Display.getDisplay(CardClientMIDlet.instance).setCurrent(MainGame.inst);
- this.sendDataHead.command=NetProtocol.REQUEST_STARTGAME;
- this.sendDataHead.size=32;
- byte[] buffer=name.getBytes();
- this.sendBuffer=new byte[32];
- System.arraycopy(buffer,0,sendBuffer,0,buffer.length);
- out.write(sendDataHead.getBytes(),0,4);
- out.write(sendBuffer,0,sendBuffer.length);
- }
- catch(Exception e)
- {
- System.out.print(e);
- }
- }
- //网络
- SocketConnection conn;
- String userName;
- InputStream in;
- OutputStream out;
- DataHead recvDataHead=new DataHead();
- DataHead sendDataHead=new DataHead();
- byte[] recvBuffer;
- byte[] sendBuffer;
- byte[] recvHeadBuffer=new byte[4];
- boolean networkEnable=false;
- public int gameState;
- public int menuWhichActive;
- public int keyAction;
- String[] userList;
- public int cursorX;
- public int cursorY;
- public int cardSelect;
- public int currentCard;
- public boolean isSelect;
- boolean isOpenSocket;
- private volatile Thread networkThread=null;
- static public GameManager inst;
- boolean iControl;
- Card[] userCard=new Card[30];
- int userCardNum;
- Card[] oppoCard=new Card[30];
- int oppoCardNum;
- Card[] userPostCard=new Card[30];
- int userPostCardNum;
- Card[] oppoPostCard=new Card[30];
- int oppoPostCardNum;
- }