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

📄 combatcanvas.java

📁 欢迎使用蓝牙联网坦克大战
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

            sendString(str);
        } else {
            try {
                out.writeInt(SIGNAL_SCREEN_SIZE_REQ);
                out.flush();
            } catch (IOException e) {
                log.error("Error in askForScreenSizeToClient: " + e.getMessage());
                e.printStackTrace();
            }
        }
    //logSignal(SIGNAL_SCREEN_SIZE_REQ);
    }

    /**
     * Send GUI data to the client
     *
     */
    public void sendRandomGUIToClient() {
        if (connectionProtocol == BlueConfig.PROTOCOL_L2CAP) {

            String str = String.valueOf(SIGNAL_RANDOM_GUI_DATA);
            str += "*";

            sendString(str);

        //logSignal(SIGNAL_RANDOM_GUI_DATA);
        } else {
            try {
                out.writeInt(SIGNAL_RANDOM_GUI_DATA);

            } catch (IOException e) {
                log.error("Error in sendRandomGUIToClient: " + e.getMessage());
                e.printStackTrace();
            }
        }
        initDone();
    }

    public void sendStartShootData(int speed) {
        if (connectionProtocol == BlueConfig.PROTOCOL_L2CAP) {


            String str = String.valueOf(SIGNAL_SHOOT);
            str += "*";

            str += String.valueOf(speed);
            str += "*";

            sendString(str);


        } else {
            try {
                out.writeInt(SIGNAL_SHOOT);
                out.writeInt(speed);
                //out.writeInt(degrees);

                out.flush();
            } catch (IOException e) {
                log.error("Error in startShoot: " + e.getMessage());
                e.printStackTrace();
            }
        }

    }

    public void sendEnemyTankData(int dir, int num) {
        if (connectionProtocol == BlueConfig.PROTOCOL_L2CAP) {


            String str = String.valueOf(SIGNAL_ENEMY_TANK);
            str += "*";

            str += String.valueOf(dir);
            str += "*";

            str += String.valueOf(num);
            str += "*";

            sendString(str);




        } else {
            try {
                out.writeInt(SIGNAL_ENEMY_TANK);
                out.writeInt(dir);
                out.writeInt(num);
                out.flush();
            } catch (IOException e) {
                log.error("Error in send enemy tank data: " + e.getMessage());
                e.printStackTrace();
            }
        }

    }

    public void sendEnemyBullet(int shoot, int num) {

        if (connectionProtocol == BlueConfig.PROTOCOL_L2CAP) {


            String str = String.valueOf(SIGNAL_ENEMY_BULLET);
            str += "*";

            str += String.valueOf(shoot);
            str += "*";

            str += String.valueOf(num);
            str += "*";

            sendString(str);





        } else {
            try {
                out.writeInt(SIGNAL_ENEMY_BULLET);
                out.writeInt(shoot);

                out.writeInt(num);


                out.flush();
            } catch (IOException e) {
                log.error("Error in shootEnemyBullet: " + e.getMessage());
                e.printStackTrace();
            }
        }




    }
    
    public void sendMapMessage(int col, int row){
        
         String str = String.valueOf(SIGNAL_MAP);
            str += "*";

            str += String.valueOf(col);
            str += "*";

            str += String.valueOf(row);
            str += "*";

            sendString(str);
        
    }

    public void sendDeath(int num) {

        String str = String.valueOf(SIGNAL_DEATH);
        str += "*";

        str += String.valueOf(num);
        str += "*";

        sendString(str);

    }

    public void sendStartToServer(int start) {
        if (connectionProtocol == BlueConfig.PROTOCOL_L2CAP) {

            String str = String.valueOf(SIGNAL_START_ENEMY);
            str += "*";

            str += String.valueOf(start);
            str += "*";

            sendString(str);


        } else {
            try {
                out.writeInt(SIGNAL_START_ENEMY);
                out.writeInt(start);

                out.flush();
            } catch (IOException e) {
                log.error("Error in sendStartToServer: " + e.getMessage());
                e.printStackTrace();
            }
        }

    }

    private void sendInt(int number) {
        Utility.sendMessage((L2CAPConnection) conn, String.valueOf(number));
    }

    private void sendString(String str) {
        Utility.sendMessage((L2CAPConnection) conn, str);
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
        }
    }

//    private int readInt() {
//        String msg = Utility.readMessage((L2CAPConnection) conn, waitBeforeReceiveData);
//        return Integer.valueOf(msg).intValue();
//    }
    private int readString() {

        String msg = Utility.readMessage((L2CAPConnection) conn, waitBeforeReceiveData);
        int signal = -1;
        int[] position = new int[6];
        String str;
        if (msg.length() != 0) {


            position[0] = msg.indexOf("*");
            str = msg.substring(0, position[0]);

            signal = Integer.valueOf(msg.substring(0, position[0])).intValue();

            if (signal == SIGNAL_RESTART) {
                restart();
            } else if (signal == SIGNAL_SCREEN_SIZE_REQ) {


                sendString(String.valueOf(SIGNAL_SCREEN_SIZE_ACK) + "*");



            } else if (signal == SIGNAL_SCREEN_SIZE_ACK) {

                sendString(String.valueOf(SIGNAL_SCREEN_SIZE_CONFIRM) + "*");

            } else if (signal == SIGNAL_SCREEN_SIZE_CONFIRM) {

                sendString(String.valueOf(SIGNAL_SCREEN_SIZE_CONFIRM_ACK) + "*");


            } else if (signal == SIGNAL_SCREEN_SIZE_CONFIRM_ACK) {

                sendRandomGUIToClient();
            } else if (signal == SIGNAL_RANDOM_GUI_DATA) {

                initDone();
            } else if (signal == SIGNAL_START_ENEMY) {

                position[1] = msg.indexOf("*", position[0] + 1);
                int start = Integer.valueOf(msg.substring(position[0] + 1, position[1])).intValue();
                startServerEnemy(start);
            } else if (signal == SIGNAL_SHOOT) {


                position[1] = msg.indexOf("*", position[0] + 1);
                int speed = Integer.valueOf(msg.substring(position[0] + 1, position[1])).intValue();

                resetPosition(speed);
            } else if (signal == SIGNAL_ENEMY_BULLET) {

                position[1] = msg.indexOf("*", position[0] + 1);
                position[2] = msg.indexOf("*", position[1] + 1);
                int shoot = Integer.valueOf(msg.substring(position[0] + 1, position[1])).intValue();

                int num = Integer.valueOf(msg.substring(position[1] + 1, position[2])).intValue();
                startEnemyBullet(shoot, num);

            } else if(signal ==SIGNAL_MAP){
                position[1] = msg.indexOf("*", position[0] + 1);
                position[2] = msg.indexOf("*", position[1] + 1);
                int col = Integer.valueOf(msg.substring(position[0] + 1, position[1])).intValue();

                int row = Integer.valueOf(msg.substring(position[1] + 1, position[2])).intValue();
                resetMap(col,row);
                
                
            }else if (signal == SIGNAL_DEATH) {

                position[1] = msg.indexOf("*", position[0] + 1);
                int num = Integer.valueOf(msg.substring(position[0] + 1, position[1])).intValue();
                setDeath(num);

            } else {

                log.debug("Unkonwn signal (" + signal + "), probably connection closed - " + Thread.currentThread());
            }

            if (signal == SIGNAL_ENEMY_TANK) {




                position[1] = msg.indexOf("*", position[0] + 1);
                position[2] = msg.indexOf("*", position[1] + 1);
                int dir = Integer.valueOf(msg.substring(position[0] + 1, position[1])).intValue();

                int num = Integer.valueOf(msg.substring(position[1] + 1, position[2])).intValue();

                setEnemyTank(dir, num);
            }
        }





        return signal;




    }

    class Reader implements Runnable {

        private boolean done = false;

        public Reader() {
        }

        /**
         * set 'done' flag to true, which will exit the while loop
         */
        public void stop() {
            done = true;
        }

        public void run() {
            log.debug("Reader thread started!");
            setInitPaintMessage("Reader started");
            try {
                while (!done) {
                    int signal = readString();


                } // while !done
            } catch (Exception e) {
//                log.error("Error in Reader: " + e.getMessage());
//                e.printStackTrace();
            }
        }
    }
}

⌨️ 快捷键说明

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