// Start is called before the first frame update void Start() { lastPosition = transform.position; lastRotation = transform.rotation; lastScale = transform.localScale; target = GetComponent <BaseStageObject>(); }
public override void Enter() { base.Enter(); //playerPos = cubePlayer.transform.position; //Vector3 InitPos = playerPos + new Vector3(1, 0, 1); RigidBuildInfo info = new RigidBuildInfo { category = StageObjectCategory.Dynamic, id = StageManager.instance.GetMaxID(StageObjectCategory.Dynamic) + 1, mass = 50, useGravity = false, drag = 0f, angularDrag = 0f, prefabName = "RoadBlock", constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ, sendFlags = SendFlag.Position | SendFlag.Rotation | SendFlag.LocalScale, //position = InitPos, //rotation = Quaternion.identity, //initForce = Vector3.zero, }; Vector3 playerPos = cubePlayer.transform.position; List <BaseStageObject> objList = new List <BaseStageObject>(); for (int i = 0; i < 8; i++) { RigidBuildInfo newInfo = (RigidBuildInfo)info.Clone(); newInfo.position = playerPos + initPosList[i]; newInfo.initForce = initPosList[i] * 5; newInfo.initForceMode = ForceMode.VelocityChange; newInfo.id += i; BaseStageObject obj = GameManager.instance.SpawnStageObject(newInfo, BuildType.Server); Player.AddPlayerPrefix(obj.gameObject, cubePlayer); //只有服务端名字会改掉,用于标识这是player的所有物 //obj.transform.parent = cubePlayer.transform; //不能设为player的子物体,不然player转的时候会一起转 //obj.transform.forward = initPosList[i].normalized; objList.Add(obj); ServerSend.StageObjectSpawned(newInfo); } //Rigidbody rig = obj.GetComponent<Rigidbody>(); GameManager.instance.StartCoroutine(BlockMove(playerPos, objList)); //不这么写的话关掉游戏携程跑不完,那些块块就不会销毁,所以得挂在一个场景中不会被销毁的东西上 Exit(); }
public static void StageSynchronize(int clientID, List <StageObjectPair> clientObjs) { Debug.Log($"收到了 客户端{clientID} 发来的场景同步请求"); List <StageObjectPair> serverObjs = StageManager.instance.GetStageObjectPairs(new List <StageObjectPair> { new StageObjectPair { category = StageObjectCategory.Player, id = clientID } }); //得到除去该客户端Player以外的元素 IEnumerable <StageObjectPair> intersection = serverObjs.Intersect(clientObjs); IEnumerable <StageObjectPair> serverOnly = serverObjs.Except(intersection); IEnumerable <StageObjectPair> clientOnly = clientObjs.Except(intersection); Debug.Log($"当前:{serverObjs.Count},客户端:{clientObjs.Count},交集:{intersection.Count()},服务器独有:{serverOnly.Count()},客户端独有:{clientOnly.Count()}"); List <Vector3Holder> posList = new List <Vector3Holder>(); List <QuaternionHolder> rotList = new List <QuaternionHolder>(); foreach (StageObjectPair pair in intersection) { BaseStageObject obj = StageManager.instance.GetStageObject(pair.category, pair.id); posList.Add(new Vector3Holder { category = pair.category, id = pair.id, vect = obj.transform.position }); rotList.Add(new QuaternionHolder { category = pair.category, id = pair.id, quat = obj.transform.rotation }); } ServerSend.StageObjectPositions(clientID, posList); ServerSend.StageObjectRotations(clientID, rotList); ServerSend.StageObjectDespawned(clientID, clientOnly.ToList()); foreach (StageObjectPair pair in serverOnly) { BaseStageObject obj = StageManager.instance.GetStageObject(pair.category, pair.id); if (obj.builder != null) { BaseBuildInfo info = obj.builder.GenerateBuildInfo(obj); if (info != null) { ServerSend.StageObjectSpawned(clientID, info); } } } }
public static void PlayerDisconnected(int playerID) { BaseStageObject obj = StageManager.instance.GetStageObject(StageObjectCategory.Player, playerID); if (obj) { Player player = (Player)obj; int score = player.Score; TeamManager.instance.RemoveFromTeam(player); ServerSend.TeamLeft(playerID, score); List <StageObjectPair> bars = new List <StageObjectPair>(); player.barList.ForEach((Bar bar) => { bars.Add(new StageObjectPair { category = bar.category, id = bar.id }); StageManager.instance.RemoveStageObject(bar.category, bar.id); }); ServerSend.StageObjectRemoved(bars); GameManager.instance.DespawnStageObject(StageObjectCategory.Player, playerID); ServerSend.StageObjectDespawned(StageObjectCategory.Player, playerID); } }