baflink

ForgeExample_Move.cs

Jul 7th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.43 KB | None | 0 0
  1. /*-----------------------------+------------------------------\
  2. |                                                             |
  3. |                        !!!NOTICE!!!                         |
  4. |                                                             |
  5. |  These libraries are under heavy development so they are    |
  6. |  subject to make many changes as development continues.     |
  7. |  For this reason, the libraries may not be well commented.  |
  8. |  THANK YOU for supporting forge with all your feedback      |
  9. |  suggestions, bug reports and comments!                     |
  10. |                                                             |
  11. |                               - The Forge Team              |
  12. |                                 Bearded Man Studios, Inc.   |
  13. |                                                             |
  14. |  This source code, project files, and associated files are  |
  15. |  copyrighted by Bearded Man Studios, Inc. (2012-2015) and   |
  16. |  may not be redistributed without written permission.       |
  17. |                                                             |
  18. \------------------------------+-----------------------------*/
  19.  
  20.  
  21.  
  22. using UnityEngine;
  23.  
  24. using BeardedManStudios.Network;
  25. using System.Collections.Generic;
  26.  
  27. namespace BeardedManStudios.Forge.Examples
  28. {
  29.     public class ForgeExample_Move : NetworkedMonoBehavior
  30.     {
  31.         [NetSync("Print", NetworkCallers.Everyone)]
  32.         public int number = 0;
  33.  
  34.         public TextMesh nameMesh = null;
  35.         private BMSByte rawMessageCache = new BMSByte();
  36.  
  37.         public void Print()
  38.         {
  39.             Debug.Log("The number has been updated to " + number);
  40.         }
  41.  
  42.         protected override void NetworkStart()
  43.         {
  44.             // Always call the base Start method
  45.             base.NetworkStart();
  46.  
  47.             if (!IsOwner)
  48.                 return;
  49.  
  50.             // Read raw data from the network
  51.             OwningNetWorker.rawDataRead += OwningNetWorker_rawDataRead;
  52.         }
  53.  
  54.         private void OwningNetWorker_rawDataRead(NetworkingPlayer sender, BMSByte data)
  55.         {
  56.             // In this test we are just writing a string across the network
  57.             string message = System.Text.Encoding.UTF8.GetString(data.byteArr, data.StartIndex(), data.Size);
  58.  
  59.             Debug.Log("Hello " + message);
  60.         }
  61.  
  62.         protected override void OwnerUpdate()
  63.         {
  64.             base.OwnerUpdate();
  65.  
  66.             if (Input.GetKeyDown(KeyCode.B))
  67.                 AssignName("Brent");
  68.             else if (Input.GetKeyDown(KeyCode.F))
  69.                 AssignName("Farris");
  70.  
  71.             if (Input.GetKey(KeyCode.UpArrow))
  72.                 transform.position += Vector3.up * 5.0f * Time.deltaTime;
  73.  
  74.             if (Input.GetKey(KeyCode.DownArrow))
  75.                 transform.position += Vector3.down * 5.0f * Time.deltaTime;
  76.  
  77.             if (Input.GetKey(KeyCode.LeftArrow))
  78.                 transform.position += Vector3.left * 5.0f * Time.deltaTime;
  79.  
  80.             if (Input.GetKey(KeyCode.RightArrow))
  81.                 transform.position += Vector3.right * 5.0f * Time.deltaTime;
  82.  
  83.             // Send a raw message across the network (total of 13 bytes)
  84.             if (Input.GetKeyDown(KeyCode.E))
  85.             {
  86.                 rawMessageCache.Clone(System.Text.Encoding.UTF8.GetBytes("Brent Farris"));
  87.                 Networking.WriteRaw(OwningNetWorker, rawMessageCache);
  88.             }
  89.  
  90.             if (Input.GetKeyDown(KeyCode.Space))
  91.                 number++;
  92.         }
  93.  
  94.         private void AssignName(string newName)
  95.         {
  96.             NetworkingManager.Instance.SetName(newName);
  97.             RPC("UpdatePlayers");
  98.         }
  99.  
  100.         [BRPC]
  101.         private void UpdatePlayers()
  102.         {
  103.             NetworkingManager.Instance.PollPlayerList(GetPlayers);
  104.         }
  105.  
  106.         private void GetPlayers(List<NetworkingPlayer> players)
  107.         {
  108.             CallOnMainThread((object[] args) =>
  109.             {
  110.                 // Note:  This is not optimal, it is just for the idea
  111.                 foreach (KeyValuePair<ulong, SimpleNetworkedMonoBehavior> kv in networkedBehaviors)
  112.                 {
  113.                     if (kv.Value is ForgeExample_Move)
  114.                     {
  115.                         foreach (NetworkingPlayer player in players)
  116.                         {
  117.                             if (kv.Value.OwnerId == player.NetworkId)
  118.                             {
  119.                                 ((ForgeExample_Move)kv.Value).nameMesh.text = !string.IsNullOrEmpty(player.Name) ? player.Name : "Client";
  120.                                 break;
  121.                             }
  122.                         }
  123.                     }
  124.                 }
  125.             }, null);
  126.         }
  127.  
  128.         private void OnApplicationQuit()
  129.         {
  130.             Networking.Destroy(this);
  131.         }
  132.  
  133.         private void OnGUI()
  134.         {
  135.             if (!IsOwner)
  136.                 return;
  137.  
  138.             GUILayout.Space(25);
  139.  
  140.             // The server NetworkingManager object controls how fast the client's times are updated
  141.             GUILayout.Label("The current server time is: " + NetworkingManager.Instance.ServerTime);
  142.             GUILayout.Label("Press B or F to assign your name across the network.");
  143.             GUILayout.Label("This will also updated get the latest list of players");
  144.         }
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment