public void Connect(TcpClient _socket) { socket = _socket; socket.ReceiveBufferSize = BufferSize; socket.SendBufferSize = BufferSize; stream = socket.GetStream(); receiveData = new Packet(); receiveBuffer = new byte[BufferSize]; stream.BeginRead(receiveBuffer, 0, BufferSize, ReceiveCallback, null); ServerSend.Welcome(id, "来自服务器的欢迎~"); Server.UpdatePlayerCount(1); ThreadManager.ExecuteOnMainThread(() => Actions.ClientTCPConnectedAction?.Invoke(id)); }
protected void OnTriggerEnter(Collider other) { if (other.CompareTag("Ball")) { Ball ball = other.GetComponent <Ball>(); if (ball.lastPlayer) { int score = shootScore; if (ball.lastPlayer.team.id == goal.TeamID) { score = -score; } ball.lastPlayer.AddSocre(score); goal.AddSocre(score); ServerSend.PlayerScored(ball.lastPlayer, goal, score); } ball.ResetLocationInfo(); } }
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 WelcomeReceived(int clientID, Packet packet) { int idcheck = packet.ReadInt(); string username = packet.ReadString(); Debug.Log($"{Server.clients[clientID].tcp.socket.Client.RemoteEndPoint} 连接成功,成为玩家{clientID}"); if (clientID != idcheck) { Debug.Log($"玩家ID检查出错!玩家{clientID}-{username}拥有了错误的客户端ID:{idcheck}"); } else { if (Server.onlineClients.ContainsKey(username)) { Debug.Log($"新加入的 玩家{clientID} 与 玩家{Server.onlineClients[username]} 昵称相同:{username},已拒绝新加入的玩家进入游戏"); ServerSend.ClientConnectionRefused(clientID, "李重名"); //Server.clients[clientID].Disconnect(); return; } Server.clients[clientID].InitUser(username); } //Server.clients[clientID].SendIntoGame(username); }
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); } }
public static void InitClientPlayer(int clientID, string prefabName) { int teamID = TeamManager.instance.DistributeOneTeam(); Team team = TeamManager.instance.teams[teamID]; TeamDescribe desc = team.GetDescribe(); if (team.HasNoGoal) { Goal goal = TeamManager.instance.DistributeOneGoal(); GoalBuildInfo goalInfo = new GoalBuildInfo { id = goal.id > 0 ? goal.id : StageManager.instance.GetMaxID(StageObjectCategory.Goal) + 1, teamID = team.id, goalName = goal.name, score = 0 }; GameManager.instance.SpawnStageObject(goalInfo, BuildType.Server); ServerSend.StageObjectSpawned(goalInfo); } (Vector3 playerPos, Quaternion playerRot) = Player.GetSpawnInfo(team); PlayerBuildInfo info = new PlayerBuildInfo { id = clientID, prefabName = prefabName, playerType = "Demo", playerName = Server.clients[clientID].username, firstBar = StageManager.instance.GetMaxID(StageObjectCategory.Dynamic) + 1, teamDescribe = desc, score = 0, position = playerPos, rotation = playerRot }; GameManager.instance.SpawnStageObject(info, BuildType.Server); ServerSend.StageObjectSpawned(info); }
private void FixedUpdate() { //移动 float h = buffer[InputType.rotate]; float accelerator = buffer[InputType.move]; //Debug.Log(buffer[InputType.move]); rd.AddForce(accelerator * transform.forward * force); if (rd.velocity.magnitude >= currentMaxSpeed) { rd.velocity = rd.velocity.normalized * currentMaxSpeed; } transform.Rotate(0, h * rotateSpeedFactor, 0, Space.Self); //鼠标 //旧 //Vector3 rDir = new Vector3(buffer[InputType.mouseX], 0, buffer[InputType.mouseY]); //if(rDir != Vector3.zero) //{ // Vector3 nDir = rDir.normalized; // transform.forward = nDir; //} //新(平滑旋转) Rotate(transform, buffer[InputType.mouseX], buffer[InputType.mouseY], 0.2f); //转bar if (!isBarRotate && buffer[InputType.barRotate] > 0) { isBarRotate = true; } if (isBarRotate) { for (int i = 0; i < player.barList.Count; i++) { Bar current = player.barList[i]; step += barSpeedFactor; current.transform.localPosition = Vector3.Lerp(current.original, current.Next.original, step); } if (step > 1) { step = 0; Vector3 firstOriginal = player.barList[0].original; for (int i = 0; i < player.barList.Count - 1; i++) { Bar current = player.barList[i]; current.original = current.Next.original; } player.barList[0].Previous.original = firstOriginal; isBarRotate = false; } } if (buffer[InputType.charge] > 0) { player.AddPower(player.powerPerSecond * Time.fixedDeltaTime); ServerSend.StageObjectInfo(new Pair { category = player.category, id = player.id }, nameof(player.Power), player.Power); if (player.IsMaxPower) { Debug.Log($"玩家{player.id} 洪荒之力已满 大招充能完毕"); } currentMaxSpeed = chargeMaxSpeed; } else { if (currentMaxSpeed != originMaxSpeed) { currentMaxSpeed = originMaxSpeed; } } //大招 if (player.IsMaxPower && buffer[InputType.ultimate] > 0) { if (ultimate != null && !ultimate.IsOn) { ultimate.Enter(); } player.ResetPower(); ServerSend.StageObjectMethod(new Pair { category = player.category, id = player.id }, nameof(player.ResetPower)); } if (ultimate != null && ultimate.IsOn) { ultimate.FixedUpdate(); } RefreshBuffer(); }