123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class ServerSend
- {
- private static void SendTCPData(int _toClient, Packet _packet)
- {
- _packet.WriteLength();
- Server.clients[_toClient].tcp.SendData(_packet);
- }
-
- private static void SendUDPData(int _toClient, Packet _packet)
- {
- _packet.WriteLength();
- Server.clients[_toClient].udp.SendData(_packet);
- }
-
- private static void SendTCPDataToAll(Packet _packet)
- {
- _packet.WriteLength();
- for (int i = 1; i <= Server.MaxPlayers; i++)
- {
- Server.clients[i].tcp.SendData(_packet);
- }
- }
-
- private static void SendTCPDataToAll(int _exceptClient, Packet _packet)
- {
- _packet.WriteLength();
- for (int i = 1; i <= Server.MaxPlayers; i++)
- {
- if (i != _exceptClient)
- {
- Server.clients[i].tcp.SendData(_packet);
- }
- }
- }
-
- private static void SendUDPDataToAll(Packet _packet)
- {
- _packet.WriteLength();
- for (int i = 1; i <= Server.MaxPlayers; i++)
- {
- Server.clients[i].udp.SendData(_packet);
- }
- }
-
- private static void SendUDPDataToAll(int _exceptClient, Packet _packet)
- {
- _packet.WriteLength();
- for (int i = 1; i <= Server.MaxPlayers; i++)
- {
- if (i != _exceptClient)
- {
- Server.clients[i].udp.SendData(_packet);
- }
- }
- }
-
- #region packets
- public static void Welcome(int _toClient, string _msg)
- {
- using (Packet _packet = new Packet((int)ServerPackets.welcome))
- {
- _packet.Write(_msg);
- _packet.Write(_toClient);
-
- SendTCPData(_toClient, _packet);
- }
- }
-
- public static void SpawnPlayer(int _toClient, Player _player)
- {
- using (Packet _packet = new Packet((int)ServerPackets.spawnPlayer))
- {
- _packet.Write(_player.id);
- _packet.Write(_player.username);
- _packet.Write(_player.transform.position);
- _packet.Write(_player.transform.rotation);
-
- SendTCPData(_toClient, _packet);
- }
- }
-
- public static void PlayerPosition(Player _player)
- {
- using (Packet _packet = new Packet((int)ServerPackets.playerPosition))
- {
- _packet.Write(_player.id);
- _packet.Write(_player.transform.position);
-
- SendUDPDataToAll(_packet);
- }
- }
-
- public static void PlayerRotation(Player _player)
- {
- using (Packet _packet = new Packet((int)ServerPackets.playerRotation))
- {
- _packet.Write(_player.id);
- _packet.Write(_player.transform.rotation);
-
- SendUDPDataToAll(_player.id, _packet);
- }
- }
-
- public static void PlayerDisconnected(int _playerId)
- {
- using (Packet _packet = new Packet((int)ServerPackets.playerDisconnected))
- {
- _packet.Write(_playerId);
-
- SendTCPDataToAll(_packet);
- }
- }
-
- public static void PlayerHealth(Player _player)
- {
- using (Packet _packet = new Packet((int)ServerPackets.playerHealth))
- {
- _packet.Write(_player.id);
- _packet.Write(_player.health);
-
- SendTCPDataToAll(_packet);
- }
- }
-
- public static void PlayerRespawned(Player _player)
- {
- using (Packet _packet = new Packet((int)ServerPackets.playerRespawned))
- {
- _packet.Write(_player.id);
-
- SendTCPDataToAll(_packet);
- }
- }
- #endregion
- }
|