/// <summary> /// Updates the objects position when the server says so. :D /// </summary> /// <param name="proto"></param> public void UpdateServerObject(Protocol.BaseProtocol proto) { Protocol.ServerObject obj = proto.AsType <Protocol.ServerObject>(); if (obj.Type != serverObjectType || obj.object_id != serverObjectId) { return; } print(obj.Action + "==" + Protocol.ServerObject.ObjectAction.Destroy + " && " + obj.Type + "==" + serverObjectType + " && " + obj.object_id + "==" + serverObjectId); if (obj.Action == Protocol.ServerObject.ObjectAction.Destroy) { Destroy(gameObject); } if (obj.Action != Protocol.ServerObject.ObjectAction.Defualt) { return; } // stop the nav agent if present. ClientAgent agent = GetComponent <ClientAgent>(); if (agent != null && !agent.FindingPath && agent.Naving) { print("Stop Agent."); agent?.CancelAction(); } print("TAG " + gameObject.tag + " transform.position " + transform.position); // update the position of the object. transform.position = lastPosition = obj.Position; transform.eulerAngles = lastRotation = obj.Rotation; }
/// <summary> /// Sends the ServerObject Protocol for this object to the server :) /// </summary> public virtual void Send(bool newObject = false) { if (!newObject && transform.position == lastPosition && transform.eulerAngles == lastRotation) { return; } Protocol.ServerObject.ObjectAction soACtion = newObject ? Protocol.ServerObject.ObjectAction.Add : Protocol.ServerObject.ObjectAction.Defualt; Protocol.ServerObject obj = new Protocol.ServerObject(transform.position, transform.eulerAngles, serverObjectType, serverObjectId, soACtion); obj.Send(); lastPosition = transform.position; lastRotation = transform.eulerAngles; }
private void SpawnObject(Protocol.BaseProtocol proto) { Protocol.ServerObject servObj = proto.AsType <Protocol.ServerObject>(); if (servObj.Action != Protocol.ServerObject.ObjectAction.Add) { return; } // check that the object does not not exist. SelectedObject = null; ServerObject.InvokeSelectObject(servObj.object_id, this); if (SelectedObject != null) { Debug.LogError("Unable to spwan object, already exist :( "); return; } // find the object to spawn and spawn it :D ServerObject spawnObj = null; switch (servObj.Type) { case Protocol.ServerObject.ObjectType.Block: spawnObj = blockPrefab; break; default: Debug.LogErrorFormat("Unable to spwan server object of type {0}", servObj.Type); return; } if (spawnObj != null) { Quaternion quat = Quaternion.identity; quat.eulerAngles = servObj.Rotation; ServerObject so = Instantiate(spawnObj, servObj.Position, quat); so.serverObjectId = servObj.object_id; } }