GameManager.java
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:12k
源码类别:

J2ME

开发平台:

Java

  1. package cardclient;
  2. import javax.microedition.io.SocketConnection;
  3. import java.io.InputStream;
  4. import javax.microedition.io.Connector;
  5. import java.io.OutputStream;
  6. /**
  7.  * <p>Title: CardClient</p>
  8.  *
  9.  * <p>Description: lizhenpeng</p>
  10.  *
  11.  * <p>Copyright: Copyright (c) 2005</p>
  12.  *
  13.  * <p>Company: LP&P</p>
  14.  *
  15.  * @author lipeng
  16.  * @version 1.0
  17.  */
  18. import lipeng.*;
  19. import javax.microedition.lcdui.*;
  20. public class GameManager
  21.   implements Runnable
  22. {
  23.   public GameManager()
  24.   {
  25.     inst=this;
  26.     gameState=0;
  27.     for(int i=0;i<30;i++)
  28.     {
  29.       userCard[i]=new Card();
  30.       oppoCard[i]=new Card();
  31.       userPostCard[i]=new Card();
  32.       oppoPostCard[i]=new Card();
  33.     }
  34.   }
  35.   public void run()
  36.   {
  37.     while(networkEnable)
  38.     {
  39.       try
  40.       {
  41.         in.read(recvHeadBuffer,0,4);
  42.         recvDataHead.FillData(recvHeadBuffer,0);
  43.         if(recvDataHead.size!=0)
  44.         {
  45.           recvBuffer=new byte[recvDataHead.size];
  46.           in.read(recvBuffer,0,recvDataHead.size);
  47.           //处理数据
  48.           ProcessData();
  49.         } else
  50.         {
  51.           //处理信息
  52.           ProcessData();
  53.         }
  54.       } catch(Exception e)
  55.       {
  56.         System.out.println(e);
  57.       }
  58.     }
  59.   }
  60.   public void openSocket()
  61.   {
  62.     try
  63.     {
  64.       conn=(SocketConnection)Connector.open("socket://localhost:8070");
  65.       in=conn.openInputStream();
  66.       out=conn.openOutputStream();
  67.       networkEnable=true;
  68.       sendDataHead.command=NetProtocol.LOGIN;
  69.       //16X2
  70.       sendDataHead.size=32;
  71.       byte[] buffer=userName.getBytes();
  72.       byte[] buffer2=new byte[32];
  73.       System.arraycopy(buffer,0,buffer2,0,buffer.length);
  74.       this.sendBuffer=sendDataHead.getBytes();
  75.       out.write(sendBuffer,0,sendBuffer.length);
  76.       out.write(buffer2,0,buffer2.length);
  77.       gameState=2;
  78.     } catch(Exception e)
  79.     {
  80.       System.out.print(e);
  81.     }
  82.     networkThread=new Thread(this);
  83.     networkThread.start();
  84.   }
  85.   private void hasControl()
  86.   {
  87.     this.keyAction=0;
  88.     this.iControl=true;
  89.     cursorX=0;
  90.     cursorY=MainGame.inst.h-22*3;
  91.     cardSelect=0;
  92.     currentCard=0;
  93.     isSelect=false;
  94.   }
  95.   public void ProcessData()
  96.   {
  97.     switch(recvDataHead.command)
  98.     {
  99.       case NetProtocol.HASLOGIN:
  100.       {
  101.         userList=new String[recvDataHead.size/32];
  102.         int i;
  103.         int place=0;
  104.         for(i=0;i<userList.length;i++)
  105.         {
  106.           userList[i]=new String(this.recvBuffer,place,32);
  107.           place+=32;
  108.         }
  109.         MainGame.inst.userList=new UserList();
  110.         Display.getDisplay(CardClientMIDlet.instance).setCurrent(MainGame.inst.
  111.           userList);
  112.       }
  113.       break;
  114.       case NetProtocol.STARTGAME_FIRST:
  115.       {
  116.         Display.getDisplay(CardClientMIDlet.instance).setCurrent(MainGame.inst);
  117.         int place = 0;
  118.         this.userCardNum = 54/2;
  119.         this.oppoCardNum = 54/2;
  120.         for(int i=0;i<27;i++)
  121.         {
  122.           this.userCard[i].fillData(this.recvBuffer,place);
  123.           place += 4;
  124.         }
  125.         for(int i=0;i<27;i++)
  126.         {
  127.           this.oppoCard[i].fillData(this.recvBuffer,place);
  128.           place +=4;
  129.         }
  130.         gameState = 3;
  131.         hasControl();
  132.       }
  133.         break;
  134.         case NetProtocol.STARTGAME_LAST:
  135.         {
  136.           Display.getDisplay(CardClientMIDlet.instance).setCurrent(MainGame.inst);
  137.           int place=0;
  138.           this.iControl=false;
  139.           this.userCardNum=54/2;
  140.           this.oppoCardNum=54/2;
  141.           for(int i=0;i<27;i++)
  142.           {
  143.             this.userCard[i].fillData(this.recvBuffer,place);
  144.             place+=4;
  145.           }
  146.           for(int i=0;i<27;i++)
  147.           {
  148.             this.oppoCard[i].fillData(this.recvBuffer,place);
  149.             place+=4;
  150.           }
  151.           this.gameState=3;
  152.         }
  153.       break;
  154.       case NetProtocol.OPPO_REJECT:
  155.       {
  156.         this.userPostCardNum = 0;
  157.         hasControl();
  158.       }
  159.       break;
  160.       case NetProtocol.OPPO_POSTCARD:
  161.       {
  162.         this.oppoPostCard[0].fillData(recvBuffer,0);
  163.         userPostCardNum = 0;
  164.         oppoPostCardNum=1;
  165.         hasControl();
  166.       }
  167.       break;
  168.     }
  169.   }
  170.   public void keyPressd(int keyCode,int gameKeyCode)
  171.   {
  172.     switch(gameKeyCode)
  173.     {
  174.       case Canvas.UP:
  175.         keyAction|=LPKeyMask.MASK_KEY_UP;
  176.         break;
  177.       case Canvas.DOWN:
  178.         keyAction|=LPKeyMask.MASK_KEY_DOWN;
  179.         break;
  180.       case Canvas.FIRE:
  181.         keyAction|=LPKeyMask.MASK_KEY_OK;
  182.         break;
  183.       case Canvas.LEFT:
  184.         keyAction|=LPKeyMask.MASK_KEY_LEFT;
  185.         break;
  186.       case Canvas.RIGHT:
  187.         keyAction|=LPKeyMask.MASK_KEY_RIGHT;
  188.       break;
  189.     }
  190.   }
  191.   public void keyReleased(int keyCode,int gameKeyCode)
  192.   {
  193.     switch(gameKeyCode)
  194.     {
  195.       case Canvas.FIRE:
  196.         keyAction&=~LPKeyMask.MASK_KEY_OK;
  197.         break;
  198.       case Canvas.UP:
  199.         keyAction&=~LPKeyMask.MASK_KEY_UP;
  200.         break;
  201.       case Canvas.DOWN:
  202.         keyAction&=~LPKeyMask.MASK_KEY_DOWN;
  203.         break;
  204.       case Canvas.LEFT:
  205.         keyAction&=~LPKeyMask.MASK_KEY_LEFT;
  206.         break;
  207.       case Canvas.RIGHT:
  208.         keyAction&=~LPKeyMask.MASK_KEY_RIGHT;
  209.       break;
  210.     }
  211.   }
  212.   public void menuAction()
  213.   {
  214.     if((keyAction&LPKeyMask.MASK_KEY_UP)!=0)
  215.     {
  216.       keyAction&=~LPKeyMask.MASK_KEY_UP;
  217.       if(this.menuWhichActive>0)
  218.       {
  219.         --menuWhichActive;
  220.       } else
  221.       {
  222.         menuWhichActive=2;
  223.       }
  224.     } else if((keyAction&LPKeyMask.MASK_KEY_DOWN)!=0)
  225.     {
  226.       keyAction&=~LPKeyMask.MASK_KEY_DOWN;
  227.       if(this.menuWhichActive<2)
  228.       {
  229.         ++menuWhichActive;
  230.       } else
  231.       {
  232.         menuWhichActive=0;
  233.       }
  234.     } else if((keyAction&LPKeyMask.MASK_KEY_OK)!=0)
  235.     {
  236.       keyAction&=~LPKeyMask.MASK_KEY_OK;
  237.       switch(menuWhichActive)
  238.       {
  239.         case 0:
  240.           //登陆游戏
  241.           //openSocket();
  242.           gameState = 1;
  243.           Display.getDisplay(CardClientMIDlet.instance).setCurrent(MainGame.inst.input);
  244.           break;
  245.         case 1:
  246.           //帮助
  247.           break;
  248.         case 2:
  249.           //退出
  250.         break;
  251.       }
  252.     }
  253.   }
  254.   public void gameAction()
  255.   {
  256.     if(iControl)
  257.     {
  258.       int intern=MainGame.inst.w/14;
  259.       if((keyAction&LPKeyMask.MASK_KEY_OK)!=0)
  260.       {
  261.         keyAction&=~LPKeyMask.MASK_KEY_OK;
  262.         if(this.isSelect)
  263.         {
  264.           try
  265.           {
  266.             byte[] buffer=this.userCard[this.cardSelect].getBytes();
  267.             this.sendDataHead.command=NetProtocol.POST_CARD;
  268.             this.sendDataHead.size=(char)buffer.length;
  269.             this.sendBuffer=this.sendDataHead.getBytes();
  270.             this.out.write(sendBuffer,0,sendBuffer.length);
  271.             this.out.write(buffer,0,buffer.length);
  272.             this.iControl = false;
  273.             //出牌
  274.             this.userPostCard[0]= this.userCard[cardSelect];
  275.             this.userPostCardNum = 1;
  276.             //整理牌
  277.             for(int i=cardSelect;i<this.userCardNum-1;i++)
  278.             {
  279.               userCard[i] = userCard[i+1];
  280.             }
  281.             isSelect = false;
  282.             --userCardNum;
  283.           }
  284.           catch(Exception e)
  285.           {
  286.             System.out.print(e);
  287.           }
  288.         }
  289.         else
  290.         {
  291.           try
  292.           {
  293.             this.sendDataHead.command=NetProtocol.REJECT_CARD;
  294.             this.sendDataHead.size=0;
  295.             this.sendBuffer=this.sendDataHead.getBytes();
  296.             this.out.write(sendBuffer,0,sendBuffer.length);
  297.           }
  298.           catch(Exception e)
  299.           {
  300.             System.out.print(e);
  301.           }
  302.         }
  303.       } else if((keyAction&LPKeyMask.MASK_KEY_UP)!=0)
  304.       {
  305.         keyAction&=~LPKeyMask.MASK_KEY_UP;
  306.         this.isSelect = true;
  307.         this.cardSelect = currentCard;
  308.       } else if((keyAction&LPKeyMask.MASK_KEY_DOWN)!=0)
  309.       {
  310.         keyAction&=~LPKeyMask.MASK_KEY_DOWN;
  311.         this.isSelect = false;
  312.       } else if((keyAction&LPKeyMask.MASK_KEY_LEFT)!=0)
  313.       {
  314.         keyAction&=~LPKeyMask.MASK_KEY_LEFT;
  315.         if(this.userCardNum<=14)
  316.         {
  317.           if(cursorX!=0)
  318.           {
  319.             cursorX-=intern;
  320.             currentCard--;
  321.           }
  322.           else
  323.           {
  324.             cursorX = (userCardNum-1)*intern;
  325.             currentCard = userCardNum-1;
  326.           }
  327.           cursorY = MainGame.inst.h-22*2;
  328.         }
  329.         else
  330.         {
  331.           if(cursorX!=0)
  332.           {
  333.             cursorX-=intern;
  334.             currentCard--;
  335.           } else
  336.           {
  337.             if(cursorY ==MainGame.inst.h-22*2)
  338.             {
  339.               cursorX=intern*13;
  340.               cursorY = MainGame.inst.h-22*3;
  341.               currentCard = 13;
  342.             }
  343.             else if(cursorY ==MainGame.inst.h-22*3)
  344.             {
  345.               cursorX=intern*(userCardNum-15);
  346.               currentCard = userCardNum-1;
  347.               cursorY = MainGame.inst.h-22*2;
  348.             }
  349.           }
  350.         }
  351.       } else if((keyAction&LPKeyMask.MASK_KEY_RIGHT)!=0)
  352.       {
  353.         keyAction&=~LPKeyMask.MASK_KEY_RIGHT;
  354.         if(this.userCardNum<=14)
  355.         {
  356.           if(cursorX!=(userCardNum-1)*intern)
  357.           {
  358.             cursorX+=intern;
  359.             currentCard++;
  360.           }
  361.           else
  362.           {
  363.             cursorX = 0;
  364.             currentCard = 0;
  365.           }
  366.           cursorY = MainGame.inst.h-22*2;
  367.         }
  368.         else
  369.         {
  370.           if(cursorY==MainGame.inst.h-22*2)
  371.           {
  372.             if(cursorX!=(userCardNum-15)*intern)
  373.             {
  374.               cursorX+=intern;
  375.               currentCard++;
  376.             }
  377.             else
  378.             {
  379.               cursorX=0;
  380.               currentCard = 0;
  381.               cursorY=MainGame.inst.h-22*3;
  382.             }
  383.           } else if(cursorY==MainGame.inst.h-22*3)
  384.           {
  385.             if(cursorX!=13*intern)
  386.             {
  387.               cursorX+=intern;
  388.               currentCard++;
  389.             } else
  390.             {
  391.               cursorX=0;
  392.               cursorY=MainGame.inst.h-22*2;
  393.               currentCard = 14;
  394.             }
  395.           }
  396.         }
  397.       }
  398.     }
  399.   }
  400.   public void RegiterUser(String name)
  401.   {
  402.     userName=name;
  403.     gameState=2;
  404.     Display.getDisplay(CardClientMIDlet.instance).setCurrent(MainGame.inst);
  405.   }
  406.   public void waitAction()
  407.   {
  408.     if(!isOpenSocket)
  409.     {
  410.       openSocket();
  411.       isOpenSocket = true;
  412.     }
  413.   }
  414.   public void BeginGame(String name)
  415.   {
  416.     //开始游戏
  417.     try
  418.     {
  419.       gameState=1;
  420.       Display.getDisplay(CardClientMIDlet.instance).setCurrent(MainGame.inst);
  421.       this.sendDataHead.command=NetProtocol.REQUEST_STARTGAME;
  422.       this.sendDataHead.size=32;
  423.       byte[] buffer=name.getBytes();
  424.       this.sendBuffer=new byte[32];
  425.       System.arraycopy(buffer,0,sendBuffer,0,buffer.length);
  426.       out.write(sendDataHead.getBytes(),0,4);
  427.       out.write(sendBuffer,0,sendBuffer.length);
  428.     }
  429.     catch(Exception e)
  430.     {
  431.       System.out.print(e);
  432.     }
  433.   }
  434. //网络
  435.   SocketConnection conn;
  436.   String userName;
  437.   InputStream in;
  438.   OutputStream out;
  439.   DataHead recvDataHead=new DataHead();
  440.   DataHead sendDataHead=new DataHead();
  441.   byte[] recvBuffer;
  442.   byte[] sendBuffer;
  443.   byte[] recvHeadBuffer=new byte[4];
  444.   boolean networkEnable=false;
  445.   public int gameState;
  446.   public int menuWhichActive;
  447.   public int keyAction;
  448.   String[] userList;
  449.   public int cursorX;
  450.   public int cursorY;
  451.   public int cardSelect;
  452.   public int currentCard;
  453.   public boolean isSelect;
  454.   boolean isOpenSocket;
  455.   private volatile Thread networkThread=null;
  456.   static public GameManager inst;
  457.   boolean iControl;
  458.   Card[] userCard=new Card[30];
  459.   int userCardNum;
  460.   Card[] oppoCard=new Card[30];
  461.   int oppoCardNum;
  462.   Card[] userPostCard=new Card[30];
  463.   int userPostCardNum;
  464.   Card[] oppoPostCard=new Card[30];
  465.   int oppoPostCardNum;
  466. }