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

📄 datamanager.java

📁 Vyger offers a D & D and Rogue-like environment in a graphical online roleplay game.
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
   * @param connection the NetConnection object associated to this connection.
   */
    public void connectionCreated( NetConnection connection ) {

      synchronized( connectionLock ) {
        this.connection = connection;
        connectionLock.notifyAll();
      }

      connection.setContext(this);

      if (currentProfileConfig.getLocalClientID() == -1) {  
          if (connection==null) {
              Debug.signal( Debug.ERROR, this, "Connection closed by AccountServer" );
              return;
          }

          Debug.signal( Debug.NOTICE, null, "New account created !" );
          return;
      }

      // The key is valid, we are connected to the GameServer
       Debug.signal( Debug.NOTICE, null, "DataManager connected to GameServer" );
    }

 /*------------------------------------------------------------------------------------*/

  /** To wait (timeout max) for the connection to be established.
   */
   public void waitForConnection(long timeout) {

      long t0 = System.currentTimeMillis();

      synchronized( connectionLock ) {
        do{
         long now = System.currentTimeMillis();

           if( connection==null && timeout>(now-t0) )
              try{
                 connectionLock.wait(timeout-(now-t0));
              }catch(Exception e ) {}
           else
              return;
        }
        while( true );
      }
   }

 /*------------------------------------------------------------------------------------*/

  /** This method is called when the network connection of the client is closed
   *
   * @param connection the NetConnection object associated to this connection.
   */
  public void connectionClosed( NetConnection connection ) {
    synchronized( connectionLock ) {
      this.connection = null;
    }

    Debug.signal( Debug.NOTICE, null, "DataManager not connected anymore to GameServer" );

    pauseTickThread();

     if ( clientScreen!=null && clientScreen.isShowing() ) {

        if( !ClientDirector.getClientManager().getAutomaticLogin() ) {
           gDirector.removeAllDrawables();
           showWarningMessage("Connection to Server lost ! Re-connect to the game...");
        }

        Runnable runnable = new Runnable() {
           public void run() {
              ClientDirector.getClientManager().start(ClientManager.ACCOUNT_LOGIN_SCREEN);  // we restart the ClientManager
           }                                                                                // on the Login entry
        };

        SwingUtilities.invokeLater( runnable );
     }
     else if( clientScreen!=null ) {
        Runnable runnable = new Runnable() {
           public void run() {
              ClientDirector.getClientManager().start(ClientManager.MAIN_SCREEN);
           }
        };

        SwingUtilities.invokeLater( runnable );
     }
  }

 /*------------------------------------------------------------------------------------*/

  /** Use this method to send a NetMessage to the server.
   *
   * @param message message to send to the player.
   */
  public void sendMessage( NetMessage message ) {
    synchronized( connectionLock ) {
      if ( connection!=null ) {
        connection.queueMessage( message );
      }
    }
  }

 /*------------------------------------------------------------------------------------*/

  /** To close the network connection if any.
   */
  public void closeConnection() {
      synchronized( connectionLock ) {
           if ( connection!=null )
                connection.close();
      }
  }

 /*------------------------------------------------------------------------------------*/

  /** To show the client's interface.
   */
  public void showInterface() {

    // 0 - State analysis, progress monitor init...
       Debug.signal( Debug.NOTICE, null, "DataManager call to ShowInterface");

       if (imageLib !=null) {
          // All data have already been initialized
          // => there was a disconnection and player has resumed the game
          resumeInterface();
          return;
       }

       AProgressMonitor pMonitor = new AProgressMonitor( ClientDirector.getClientManager(), "Wotlas" );
       pMonitor.setProgress("Loading Shared Images...",0);

    // 1 - Create Image Library
       try {
          imageLib = new ImageLibrary( ClientDirector.getResourceManager() );
          imageLib.setLoadAllJITDirectoryImages(true); // images from JIT directories are loaded together.
       }
       catch( Exception ex ) {
          Debug.signal(Debug.FAILURE, this, ex );
          Debug.exit();
       }

       pMonitor.setProgress("Reading Preferences...",10);

    // 2 - Set Client Configuration Choices
       ClientConfiguration clientConfiguration = ClientDirector.getClientConfiguration();
    
       SoundLibrary.getMusicPlayer().setNoMusicState(clientConfiguration.getNoMusic());

       if( clientConfiguration.getMusicVolume()>0 )
          SoundLibrary.getMusicPlayer().setMusicVolume((short) clientConfiguration.getMusicVolume());

       SoundLibrary.getSoundPlayer().setNoSoundState(clientConfiguration.getNoSound());

       if(clientConfiguration.getSoundVolume()>0)
         SoundLibrary.getSoundPlayer().setSoundVolume((short) clientConfiguration.getSoundVolume());

       pMonitor.setProgress("Creating 2D Engine...",15);
    
    // 3 - Create Graphics Director
       WindowPolicy wPolicy = null;
    
       if( clientConfiguration.getCenterScreenPolicy() )
           wPolicy = new CenterWindowPolicy();
       else
           wPolicy = new LimitWindowPolicy();

       if( clientConfiguration.getUseHardwareAcceleration() )
           gDirector = new EnhancedGraphicsDirector( wPolicy, imageLib );
       else
           gDirector = new GraphicsDirector( wPolicy, imageLib );

       Debug.signal(Debug.NOTICE, null, "Graphics Engine is using hardware mode : "+
                                         clientConfiguration.getUseHardwareAcceleration() );

       pMonitor.setProgress("Creating GUI...",20);

    // 4 - Creation of the GUI components
       clientScreen = new JClientScreen(gDirector, this );

       if(SHOW_DEBUG)
          System.out.println("JClientScreen created");

       pMonitor.setProgress("Loading Player Data from Server...",30);

    // 5 - We retrieve our player's own data
       myPlayer = null;

       waitForConnection(30000); // 30s max...

       try {
         synchronized(startGameLock) {
            connection.queueMessage(new MyPlayerDataPleaseMessage());
            startGameLock.wait(CONNECTION_TIMEOUT);
         }
       } catch (InterruptedException ie) {
       }

       if(myPlayer==null) {
          pMonitor.close();
          showWarningMessage("Failed to retrieve your player data from the Game Server !\nPlease retry later...");
          imageLib = null;
          closeConnection();
          return;
       }

       myPlayer.setIsMaster( true );   // this player is controlled by the user.
       myPlayer.tick();                // we tick the player to validate data recreation
       addPlayer(myPlayer);

       if (SHOW_DEBUG)
          System.out.println("POSITION set to x:"+myPlayer.getX()+" y:"+myPlayer.getY()+" location is "+myPlayer.getLocation());

       pMonitor.setProgress("Setting Preferences...",80);

    // 6 - Final GUI inits
       connection.setPingListener( (NetPingListener) clientScreen.getPingPanel() );

       clientScreen.init();

       if ( (clientConfiguration.getClientWidth()>0) && (clientConfiguration.getClientHeight()>0) )
          clientScreen.setSize(clientConfiguration.getClientWidth(),clientConfiguration.getClientHeight());

       menuManager = new MenuManager( myPlayer, gDirector );
       menuManager.addMenu2DListener(this);       

       pMonitor.setProgress("Loading Map Data...",85);

    // 7 - Init the map display...
       changeMapData();

       if(SHOW_DEBUG)
         System.out.println("Changed map data !");

       pMonitor.setProgress("Starting Game...",95);

    // 8 - Start the tick thread.
       start();
       Debug.signal( Debug.NOTICE, null, "Started the tick thread..." );

       clientScreen.show();
       Debug.signal( Debug.NOTICE, null, "Show clientScreen..." );
       pMonitor.setProgress("Done...",100);
       pMonitor.close();

       if (SHOW_DEBUG)
           System.out.println("Frame displayed on screen...");

    // 9 - Welcome message
       sendMessage(new WelcomeMessage());

    // 10 - ask for server environment
    // diego : at least i'm trying to ask for it.....
       sendMessage(new TheServerEnvironmentPleaseMessage());
       
       if(SHOW_DEBUG)
          System.out.println("End of DataManager's showInterface !");
                 
    // 11 - Add extra plugin
          //clientScreen.getPlayerPanel().addPlugIn((JPanelPlugIn) new ChangeAspectPlugIn(), -1);

// Test Petrus
      String empty[] = { "head", "body", "left hand", "right hand" };
      SimpleMenu2D emptyMenu = new SimpleMenu2D("emptyMenu",empty);
      emptyMenu.setItemEnabled( "head", true );
      emptyMenu.setItemEnabled( "body", true );
      emptyMenu.setItemEnabled( "left hand", true );
      emptyMenu.setItemEnabled( "right hand", true );
      ((SimpleMenu2D) menuManager.getRootMenu()).addItemLink(MenuManager.OBJECT_ITEM_NAME, emptyMenu );
      
      /*SimpleMenu2D objectMenu = (SimpleMenu2D) menuManager.findByName(MenuManager.OBJECT_ITEM_NAME);
      objectMenu.addItem("head");
      */
// end Test Petrus
     
  }

 /*------------------------------------------------------------------------------------*/

  /** Resumes the game screen in case of server connection shut.
   */
    public void resumeInterface() {
       Debug.signal( Debug.NOTICE, null, "DataManager::ResumeInterface");

       AProgressMonitor pMonitor = new AProgressMonitor( ClientDirector.getClientManager(), "Wotlas" );
       pMonitor.setProgress("Creating 2D Engine...",15);

    // 1 - We recreate the graphics director...
       WindowPolicy wPolicy = null;
    
       if( ClientDirector.getClientConfiguration().getCenterScreenPolicy() )
           wPolicy = new CenterWindowPolicy();
       else
           wPolicy = new LimitWindowPolicy();

       if( ClientDirector.getClientConfiguration().getUseHardwareAcceleration() )
           gDirector = new EnhancedGraphicsDirector( wPolicy, imageLib );
       else
           gDirector = new GraphicsDirector( wPolicy, imageLib );

       Debug.signal(Debug.NOTICE, null, "Graphics Engine is using hardware mode : "+
                    ClientDirector.getClientConfiguration().getUseHardwareAcceleration() );

       clientScreen.getMapPanel().updateGraphicsDirector(gDirector);

       if(menuManager!=null)
          menuManager.clear();

    // 2 - Retrieve player's informations
       pMonitor.setProgress("Loading Player Data from Server...",30);
       myPlayer = null;

       waitForConnection(30000); // 30s max...

       try {
         synchronized(startGameLock) {
             connection.queueMessage(new MyPlayerDataPleaseMessage());
             startGameLock.wait(CONNECTION_TIMEOUT);
         }
       } catch (InterruptedException ie) {
       }

       if(myPlayer==null) {
          pMonitor.close();
          showWarningMessage("Failed to retrieve your player data from the Game Server !\nPlease retry later...");
          closeConnection();
          return;
       }

       myPlayer.setIsMaster( true );   // this player is controlled by the user.
       myPlayer.tick();
       addPlayer(myPlayer);

       if(SHOW_DEBUG)
          System.out.println("POSITION set to x:"+myPlayer.getX()+" y:"+myPlayer.getY()+" location is "+myPlayer.getLocation());

    // 3 - Reset previous the data
       pMonitor.setProgress("Setting Preferences...",80);

       clientScreen.getChatPanel().reset();
       clientScreen.getPlayerPanel().reset();
       players.clear();
       screenObjects.clear();
       connection.setPingListener( (NetPingListener) clientScreen.getPingPanel() );

       menuManager = new MenuManager( myPlayer, gDirector );
       menuManager.addMenu2DListener(this);

    // 4 - Init map display, resume tick thread & show screen...
       pMonitor.setProgress("Loading Map Data...",85);

⌨️ 快捷键说明

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