topical media & game development

talk show tell print

lib-unity-tutorial-network-m2h-Assets-Example4-GameAssets-Scripts-GameSetup.js / js



  /* 
  *  This file is part of the Unity networking tutorial by M2H (http://www.M2H.nl)
  *  The original author of this code is Mike Hergaarden, even though some small parts 
  *  are copied from the Unity tutorials/manuals.
  *  Feel free to use this code for your own projects, drop us a line if you made something exciting! 
  */
  #pragma strict
  
  var playerPref : Transform;
  var gameName : String = "Example4";
  var chatScript : FPSChat;
  var playerName : String = "";
  
  //Server-only playerlist
  public var playerList = new ArrayList();
  class FPSPlayerNode {
          var playerName : String;
          var networkPlayer : NetworkPlayer;
          
          var kills : int =0;
          var deaths : int =0;        
  }
  
  function Awake() 
  {
          playerName = PlayerPrefs.GetString("playerName");
          
          chatScript = GetComponent(FPSChat);
          Network.isMessageQueueRunning = true;
          Screen.lockCursor=true;        
          
          if(Network.isServer){
                  
          
                  chatScript.ShowChatWindow();
                  
                  networkView.RPC ("TellOurName", RPCMode.AllBuffered, playerName);
                  
                  for (var go : GameObject in FindObjectsOfType(GameObject)){
                          go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);        
                  }                
                  MasterServer.RegisterHost(gameName, PlayerPrefs.GetString("playerName")+"'s game");
                          
          }else if(Network.isClient){
                  
                  networkView.RPC ("TellOurName", RPCMode.AllBuffered, playerName);
                  chatScript.ShowChatWindow();
                  
                  for (var go : GameObject in FindObjectsOfType(GameObject)){
                          go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);        
                  }        
                  
                  
                  
          }else{
                  //How did we even get here without connection?
                  Screen.lockCursor=false;        
                  Application.LoadLevel((Application.loadedLevel-1));                
          }
  }
  
  //Server function
  function OnPlayerDisconnected(player: NetworkPlayer) {
          Network.RemoveRPCs(player, 0);
          Network.DestroyPlayerObjects(player);
          
          //Remove player from the server list
          for(var entry : FPSPlayerNode in  playerList){
                  if(entry.networkPlayer==player){
                          chatScript.addGameChatMessage(entry.playerName+" disconnected from: " + player.ipAddress+":" + player.port);
                          playerList.Remove(entry);
                          break;
                  }
          }
  }
  
  //Server function
  function OnPlayerConnected(player: NetworkPlayer) {
          chatScript.addGameChatMessage("Player connected from: " + player.ipAddress +":" + player.port);
  }
  
  @RPC
  //Sent by newly connected clients, recieved by server
  function TellOurName(name : String, info : NetworkMessageInfo){
          var netPlayer : NetworkPlayer = info.sender;
          if(netPlayer+""=="-1"){
                  //This hack is required to fix the local players networkplayer when the RPC is sent to itself.
                  netPlayer=Network.player;
          }
          
          var newEntry : FPSPlayerNode = new FPSPlayerNode();
          newEntry.playerName=name;
          newEntry.networkPlayer=netPlayer;
          playerList.Add(newEntry);
          
          if(Network.isServer){
                  chatScript.addGameChatMessage(name+" joined the game");
          }
  }
  
  //Called via Awake()
  function OnNetworkLoadedLevel()
  {
          // Randomize starting location
          var spawnpoints : GameObject[] = GameObject.FindGameObjectsWithTag ("Spawnpoint");
          Debug.Log("spawns: "+spawnpoints.length);
          
          var spawnpoint : Transform = spawnpoints[Random.Range(0, spawnpoints.length)].transform;
          var newTrans : Transform = Network.Instantiate(playerPref,spawnpoint.position, spawnpoint.rotation, 0);
  }
  
  function OnDisconnectedFromServer () {
          //Load main menu
          Screen.lockCursor=false;
          Application.LoadLevel((Application.loadedLevel-1));
  }
  
  


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.