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

📄 updaterthread.java

📁 Massively Multiplayer Space Trading and Combat game. This is an online strategy game, not a 3D space
💻 JAVA
字号:
/*
 * UpdaterThread.java
 *
 * Copyright (C) 2000 Jason M. Hanley
 * Released under the GNU General Public License (GPL)
 * See license.txt for additional information.
 *
 * Created on August 31, 2000, 11:58 PM
 */
 
package fate.server;

import java.util.*;

import fate.*;
import fate.messages.*;
import fate.network.*;
import fate.util.*;
import fate.world.*;

/** 
 * Thread to send periodic updates to the client, based on the current
 * client status.
 *
 * @author  preylude@s3m.com
 * @version 0.1.0
 */
public class UpdaterThread extends Thread {

  static final int DELAY_TIME = 2000;
  ServerConnection connection;
  MapPlayerInfo mapPlayerInfo;

  /** Creates new MessageProcessor */
  public UpdaterThread( ServerConnection connection,
      MapPlayerInfo mapPlayerInfo ) {
    this.connection = connection;
    this.mapPlayerInfo = mapPlayerInfo;
  }

  /** Process incoming messages from the server */
  public void run() {
    
    Iterator iterPlayers;
    PlayerInfo playerInfo;
    Player player;
    
    final double PI_2 = Math.PI * 2;

    // On initialization, mark players as not connected
    iterPlayers = mapPlayerInfo.values().iterator();
    while( iterPlayers.hasNext() ) {
      playerInfo = (PlayerInfo) iterPlayers.next();
      playerInfo.state.bConnected = false;
    }
     
    try {
      Debug.trace( "UpdaterThread initialized and entering loop" );
      
      while( true ) {

        // Iterate through the universe
        Iterator iterGalaxies = FateServer.universe.mapGalaxies.values().iterator();
        while( iterGalaxies.hasNext() ) {
          Galaxy galaxy = (Galaxy) iterGalaxies.next();
          Iterator iterSolarSystems = galaxy.mapSolarSystems.values().iterator();
          while( iterSolarSystems.hasNext() ) {
            SolarSystem solarSystem = (SolarSystem) iterSolarSystems.next();
            Iterator iterPlanetoids = solarSystem.mapPlanetoids.values().iterator();
            while( iterPlanetoids.hasNext() ) {
              Planetoid planetoid = (Planetoid)iterPlanetoids.next();
              
              // Update planetoid's position
              planetoid.position += planetoid.velocity;
              if ( planetoid.position >= PI_2 )
                planetoid.position -= PI_2;
              
            }
          }
        } // (all galaxies)
        
        // Iterate through all connected players
        iterPlayers = mapPlayerInfo.values().iterator();
        while( iterPlayers.hasNext() ) {
          playerInfo = (PlayerInfo) iterPlayers.next();
          player = (Player)FateServer.universe.mapPlayers.get( playerInfo.idPlayer );

          if ( player != null && playerInfo != null && playerInfo.state.bConnected == true ) {
            Debug.trace( "Doing update for player " + player.name );

            Galaxy galaxy = (Galaxy) FateServer.universe.mapGalaxies.get( playerInfo.state.idGalaxy );
              
            if( playerInfo.state.viewType == PlayerState.MAIN_VIEW_SOLARSYSTEM ) {
              
              //@ here we need to ensure that sending information for this view
              //  is actually ok.. we don't want people to be able to set their
              //  view to any object in the database..
              
              Debug.trace( "Attempting to send solar system view" );
              
              if( galaxy != null ) {           
                SolarSystem solarSystem = (SolarSystem) galaxy.mapSolarSystems.get( playerInfo.state.idSolarSystem );
                if( solarSystem != null ) {
                  SolarSystemViewMessage msgToSend = new SolarSystemViewMessage( solarSystem );

                  OrbitingBody object = (OrbitingBody)
                    ((SolarSystemViewMessage)msgToSend).planetList.iterator().next();
                  Debug.trace( "Sending SSViewMsg - First planet: " + object.name + ", " + object.id +
                    ", pos:" + object.position );
                  
                  connection.sendMessage( playerInfo.id, msgToSend );
                }
                else
                  Debug.trace( "Error: solarSystem == null" );
              }
                else
                  Debug.trace( "Error: galaxy == null" );
            } // solar system view
            
          } // player != null
        } // all players
        
        sleep( DELAY_TIME );
      } // infinate loop
    } catch ( Exception e ) {
      Debug.trace( "UpdaterThread.run() Exception " + e.getMessage() );
      e.printStackTrace();
    }
  }
  
}

⌨️ 快捷键说明

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