Advertisement
lisekmiki

Moving.cs

Sep 16th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.70 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Networking;
  3. using System.Collections.Generic;
  4.  
  5. [NetworkSettings(channel=2)]
  6. public class Moving : NetworkBehaviour {
  7.  
  8.     [SyncVar] Color color;
  9.     [SyncVar] CubeState state;
  10.     Queue<KeyCode> pendingMoves;
  11.     CubeState predictedState;
  12.     [SyncVar(hook="OnServerStateChanged")] CubeState serverState;
  13.     [Command(channel=0)]
  14.     void CmdMoveOnServer(KeyCode arrowKey) {
  15.         serverState = Move (state, arrowKey);
  16.     }
  17.     struct CubeState
  18.     {
  19.         public int moveNum;
  20.         public float x;
  21.         public float y;
  22.         public float z;
  23.     }
  24.  
  25.     void Start() {
  26.         if (isLocalPlayer) {
  27.             pendingMoves = new Queue<KeyCode> ();
  28.  
  29.             predictedState = new CubeState {
  30.                 moveNum = 0,
  31.                 x = 0,
  32.                 y = 0,
  33.                 z = 0
  34.             };
  35.         }
  36.         SyncColor ();
  37.     }
  38.  
  39.     void Awake()
  40.     {
  41.         InitState ();
  42.     }
  43.     [Server]
  44.     void InitState ()
  45.     {
  46.         Color[] colors = { Color.blue, Color.cyan, Color.green, Color.magenta, Color.red,Color.yellow };
  47.         color = colors [Random.Range (0, colors.Length)];
  48.         serverState = new CubeState {
  49.             moveNum = 0,
  50.             x = 0,
  51.             y = 0,
  52.             z = 0
  53.         };
  54.     }
  55.  
  56.  
  57.     void Update () {
  58.         if (isLocalPlayer) {
  59.             KeyCode[] arrowKeys = { KeyCode.UpArrow, KeyCode.DownArrow, KeyCode.RightArrow, KeyCode.LeftArrow };
  60.             foreach (KeyCode arrowKey in arrowKeys) {
  61.                 if (!Input.GetKey (arrowKey))
  62.                 continue;
  63.                 pendingMoves.Enqueue (arrowKey);
  64.                 UpdatePredictedState ();
  65.                 CmdMoveOnServer (arrowKey);
  66.             }
  67.         }
  68.         SyncState();
  69.     }
  70.  
  71.     void SyncState () {
  72.         CubeState stateToRender = isLocalPlayer ? predictedState : serverState;
  73.         transform.position = new Vector3 (stateToRender.x, stateToRender.y, stateToRender.z);
  74.     }
  75.  
  76.     CubeState Move(CubeState previous, KeyCode arrowKey){
  77.         float dx = 0;
  78.         float dy = 0;
  79.         float dz = 0;
  80.    
  81.         switch (arrowKey) {
  82.         case KeyCode.UpArrow:
  83.             dz = Time.deltaTime;
  84.             break;
  85.         case KeyCode.DownArrow:
  86.             dz = Time.deltaTime;
  87.             break;
  88.         case KeyCode.RightArrow:
  89.             dx = Time.deltaTime;
  90.             break;
  91.         case KeyCode.LeftArrow:
  92.             dx = Time.deltaTime;
  93.             break;
  94.         }
  95.  
  96.         return new CubeState {
  97.             moveNum = 1 + previous.moveNum,
  98.             x = dx + previous.x,
  99.             y = dy + previous.y,
  100.             z = dz + previous.z
  101.         };
  102.     }
  103.     void OnServerStateChanged(CubeState newState) {
  104.         serverState = newState;
  105.         if (pendingMoves != null) {
  106.             while (pendingMoves.Count > (predictedState.moveNum - serverState.moveNum)) {
  107.                 pendingMoves.Dequeue ();
  108.             }
  109.             UpdatePredictedState ();
  110.         }
  111.     }
  112.     void UpdatePredictedState () {
  113.         predictedState = serverState;
  114.         foreach (KeyCode arrowKey in pendingMoves) {
  115.             predictedState = Move (predictedState, arrowKey);
  116.         }
  117.     }
  118.     void SyncColor () {
  119.         GetComponent<Renderer> ().material.color = (isLocalPlayer ? Color.white : Color.grey) * color;
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement