Package edu.ups.gamedev.examples.one

Source Code of edu.ups.gamedev.examples.one.ExampleClient

package edu.ups.gamedev.examples.one;

import java.lang.reflect.Field;
import java.net.InetAddress;
import java.net.InetSocketAddress;

import com.captiveimagination.jgn.JGN;
import com.captiveimagination.jgn.clientserver.JGNClient;
import com.captiveimagination.jgn.event.MessageListener;
import com.captiveimagination.jgn.message.Message;
import com.captiveimagination.jgn.synchronization.SynchronizationManager;
import com.captiveimagination.jgn.synchronization.message.SynchronizeCreateMessage;
import com.captiveimagination.jmenet.JMEGraphicalController;
import com.jme.scene.Node;

import edu.ups.gamedev.player.PlayerSphere;

public class ExampleClient extends Example {
 
  public static void main(String[] args) throws Exception{
    new ExampleClient();
  }
 
  public ExampleClient() throws Exception{
    final ExampleGame app = new ExampleGame();
    app.setDialogBehaviour(2); //2 = always show properties
    new Thread() {
      public void run() {
        app.start(); //start the application
      }
    }.start(); //run this in its own thread
   
    //initialize networking
    //server addresses
    InetSocketAddress serverReliable = new InetSocketAddress(InetAddress.getLocalHost(), 1500);
        InetSocketAddress serverFast = new InetSocketAddress(InetAddress.getLocalHost(), 1501);
        //client addresses
        InetSocketAddress clientReliable = new InetSocketAddress(InetAddress.getLocalHost(), 0); //let Java choose a port for us
        InetSocketAddress clientFast = new InetSocketAddress(InetAddress.getLocalHost(), 0); //let Java choose a port for us
        //create the client and start it in its own thread
    JGNClient client = new JGNClient(clientReliable, clientFast); //create the networking client
    JGN.createThread(client).start(); //create a new thread for the client and start it
   
    JMEGraphicalController controller = new JMEGraphicalController(); //in charge of generating and applying sync messages
   
    //create the sync manager, register ourselves with it, and start its thread
    SynchronizationManager syncManager = new SynchronizationManager(client, controller);
    syncManager.addSyncObjectManager(this);
    JGN.createThread(syncManager).start();
   
    //get localSphere from the game
    Field field = ExampleGame.class.getDeclaredField("localSphere");
    field.setAccessible(true);
    PlayerSphere player = null;
    while((player = (PlayerSphere)field.get(app)) == null) {
      try {
        Thread.sleep(100);
      }catch(Exception e) {
        System.err.println("Error while thread sleeping to get field:");
        e.printStackTrace();
      }
    }
   
    //get rootNode from the game
    field = ExampleGame.class.getDeclaredField("scene");
    field.setAccessible(true);
    Node scene = (Node)field.get(app);
    setScene(scene);
   
    //connect to the server
    System.out.println("Connecting...");
    client.connectAndWait(serverReliable, serverFast, 5000);
    System.out.println("Connected!");
   
     client.getServerConnection().getReliableClient().addMessageListener(new MessageListener() {
              public void messageCertified(Message message) {
                  System.out.println("Message Certified: " + message);
              }
  
              public void messageFailed(Message message) {
                  System.out.println("Message Failed: " + message);
              }
  
              public void messageReceived(Message message) {
                  System.out.println("Message Received: " + message);
              }

              public void messageSent(Message message) {
                  System.out.println("Message Sent: " + message);
              }
          });
   
    //register our sphere
    syncManager.register(player, new SynchronizeCreateMessage(), 50);
  }
}
TOP

Related Classes of edu.ups.gamedev.examples.one.ExampleClient

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.