public virtual void DespawnFrom(Player player) { if (this.viewers.Contains(player)) { RemoveEntityPacket pk = new RemoveEntityPacket(); pk.EntityUniqueId = this.EntityID; player.SendPacket(pk); this.viewers.Remove(player); } }
public static RemoveEntityPacket CreateRemoveEntityPacket(IEnumerable <Element> elements) { var packet = new RemoveEntityPacket(); foreach (var element in elements) { packet.AddEntity(element.Id); } return(packet); }
private void RelayElementDestroy(Element element) { if (!(element is Player)) { var packet = new RemoveEntityPacket(); packet.AddEntity(element.Id); var players = this.elementCollection .GetByType <Player>(ElementType.Player) .Where(p => p != element); packet.SendTo(players); } }
/// <summary> /// この <see cref="Entity"/> を指定した <see cref="Player"/> から表示されなくします /// </summary> /// <param name="player"></param> public virtual void DespawnFrom(Player player) { RemoveEntityPacket pk = new RemoveEntityPacket { EntityUniqueId = this.EntityID }; player.SendPacket(pk); if (this._hasSpawned.ContainsKey(player.EntityID)) { this._hasSpawned.Remove(player.EntityID); } }
private void ReceiveDeleteObject(IRecvPacket packet) { RemoveEntityPacket p = (RemoveEntityPacket)packet; EntityManager.RemoveObject(p.Serial); }
private void SetupTestEntities(Client client) { var entitypacket = new AddEntityPacket(); entitypacket.AddWater(667, (byte)ElementType.Water, null, 0, 0, null, true, true, new CustomData(), "Test water", 0, new Vector3[] { new Vector3(-6, 0, 4), new Vector3(-3, 0, 4), new Vector3(-6, 3, 4), new Vector3(-3, 3, 4) }, false); entitypacket.AddObject( 668, (byte)ElementType.Object, null, 0, 0, null, true, false, new CustomData(), "Test object", 0, new Vector3(0, -5, 3), Vector3.Zero, 321, 255, false, null, true, true, null, Vector3.One * 3, false, 1000f ); entitypacket.AddBlip(669, (byte)ElementType.Blip, null, 0, 0, null, true, true, new CustomData(), "Test blip", 0, new Vector3(20, 0, 0), 0, 2500, 56, 1, Color.White); entitypacket.AddRadarArea(670, (byte)ElementType.RadarArea, null, 0, 0, null, true, true, new CustomData(), "Test radar area", 0, new Vector2(0, 0), new Vector2(250, 250), Color.FromArgb(100, Color.DarkGoldenrod), true); entitypacket.AddMarker(671, (byte)ElementType.Marker, null, 0, 0, null, true, true, new CustomData(), "Test marker", 0, new Vector3(5, 0, 2), (byte)MarkerType.Cylinder, 2, Color.FromArgb(100, Color.DarkCyan), null); entitypacket.AddPickup(672, (byte)ElementType.Pickup, null, 0, 0, null, true, true, new CustomData(), "Test pickup", 0, new Vector3(0, 5, 3), 349, true, (byte)PickupType.Weapon, null, null, 25, 0); entitypacket.AddPed(673, (byte)ElementType.Ped, null, 0, 0, null, true, true, new CustomData(), "Test ped", 0, new Vector3(10, 0, 3), 181, 45, 100, 50, null, null, true, true, true, false, 200, 0, new PedClothing[0], new PedWeapon[0], 0); entitypacket.AddWeapon(674, (byte)ElementType.Weapon, null, 0, 0, null, true, true, new CustomData(), "Test weapon", 0, new Vector3(5, 5, 5), Vector3.Zero, 355, 255, false, null, true, true, null, Vector3.One, false, 100, (byte)WeaponTargetType.Vector, null, null, null, new Vector3(10, 10, 5), true, 10, 1, 100, 200, false, false, true, true, true, true, true, true, true, true, true, true, true, (byte)WeaponState.Ready, 1000, 50, 666); entitypacket.AddVehicle(675, (byte)ElementType.Vehicle, null, 0, 0, null, true, true, new CustomData(), "Test vehicle", 0, new Vector3(-10, 5, 3), Vector3.Zero, 602, 1000, new Color[] { Color.Red, Color.Blue }, 0, new VehicleDamage() { Doors = new byte[] { 0, 0, 0, 0, 0, 0 }, Wheels = new byte[] { 0, 0, 0, 0 }, Panels = new byte[] { 0, 0, 0, 0, 0, 0, 0 }, Lights = new byte[] { 0, 0, 0, 0 } }, 0, 0, null, null, new float[] { 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f }, new byte[0], "SLIPE", 0, true, false, false, false, false, false, false, false, false, false, false, true, 200, Color.MediumPurple, null, null); entitypacket.AddObject( 676, (byte)ElementType.Object, null, 0, 0, null, true, false, new CustomData(), "Moving test object", 0, new Vector3(0, -5, 3), Vector3.Zero, 321, 255, false, null, true, true, new PositionRotationAnimation() { SourcePosition = new Vector3(0, -5, 3), SourceRotation = new Vector3(0, 0, 0), TargetPosition = new Vector3(0, 45, 3), TargetRotation = new Vector3(0, 0, 360), DeltaRotationMode = true, DeltaRotation = new Vector3(0, 0, 360), StartTime = DateTime.UtcNow, EndTime = DateTime.UtcNow + TimeSpan.FromSeconds(10), EasingType = "Linear" }, Vector3.One * 3, false, 1000f ); client.SendPacket(entitypacket); _ = Task.Run(async() => { await Task.Delay(10000); var packet = new RemoveEntityPacket(); packet.AddEntity(676); client.SendPacket(packet); }); }
// Update is called once per frame void Update() { if (spawnQueue.Count > 0) { lock (spawnQueueLock) { SpawnEntityPacket packet = spawnQueue.Dequeue(); GameObject entity = Instantiate(entityPrefab, new Vector3(packet.X, packet.Y), Quaternion.identity); entity.GetComponent <NetworkEntity>().Setup(packet.EntityId, packet.IsSelf, lobbyClients.Where(x => x.Name == packet.Owner).SingleOrDefault()); entityMap.Add(packet.EntityId, entity); Debug.Log($"Spawned entity: {packet.EntityId} - Self: {packet.IsSelf}"); GameObject.Find("RoomManager").GetComponent <RoomManager>().players++; } } if (relocationQueue.Count > 0) { lock (relocLock) { RelocationPacket packet = relocationQueue.Dequeue(); entityMap[packet.EntityId].transform.position = new Vector3(packet.X, packet.Y); } } if (dropQueue.Count > 0) { lock (dropLock) { RemoveEntityPacket packet = dropQueue.Dequeue(); Destroy(entityMap[packet.EntityId]); entityMap.Remove(packet.EntityId); } } if (visibilityQueue.Count > 0) { lock (visibilityLock) { VisibilityPacket packet = visibilityQueue.Dequeue(); entityMap[packet.EntityId].GetComponent <SpriteRenderer>().enabled = packet.Visible; } } if (!loadingScene && currentScene.name != requestedScene) { loadingScene = true; Debug.Log($"Loading '{requestedScene}' from '{currentScene.name}'"); SceneManager.LoadScene(requestedScene); currentScene = SceneManager.GetSceneByName(requestedScene); loadingScene = false; } if (allLoaded) { allLoaded = false; GameObject.Find("RoomManager").GetComponent <RoomManager>().AllLoaded(); } if (allFinished) { allFinished = false; GameObject.Find("RoomManager").GetComponent <RoomManager>().AllFinished(); } }
void HandleServerComm(object server) { TcpClient tcpClient = (TcpClient)server; serverStream = tcpClient.GetStream(); connected = true; Debug.Log($"Connected to server on {tcpClient.Client.RemoteEndPoint}"); byte[] message = new byte[4096]; int bytesRead; while (true) { try { Debug.Log("Waiting for server"); //blocks until the server sends a message bytesRead = serverStream.Read(message, 0, 4096); } catch (Exception e) { Debug.LogError($"ERROR: {e.Message}"); break; } if (bytesRead == 0) { Debug.LogWarning("Lost connection to server"); break; } try { string formattedData = FormatJsonString(Encoding.UTF8.GetString(message)); Debug.Log(formattedData); JObject json = JObject.Parse(formattedData); if (!Enum.TryParse(json["type"].ToString(), out PacketType type)) { Console.WriteLine("Invalid message received"); continue; } switch (type) { case PacketType.Disconnect: Debug.LogWarning($"Disconnected from host with message: {json["reason"].ToString()}"); break; case PacketType.LobbyState: requestedScene = "Lobby"; lobbyClients = JsonConvert.DeserializeObject <LobbyClient[]>(json["players"].ToString()); break; case PacketType.Start: requestedScene = "Game 1"; break; case PacketType.SpawnEntity: SpawnEntityPacket SpawnPacket = new SpawnEntityPacket(int.Parse(json["entityId"].ToString()), json["owner"].ToString(), float.Parse(json["x"].ToString()), float.Parse(json["y"].ToString()), bool.Parse(json["isSelf"].ToString())); lock (spawnQueueLock) spawnQueue.Enqueue(SpawnPacket); break; case PacketType.AllLoaded: allLoaded = true; break; case PacketType.RemoveEntity: RemoveEntityPacket removePacket = new RemoveEntityPacket(int.Parse(json["entityId"].ToString())); lock (dropLock) dropQueue.Enqueue(removePacket); break; case PacketType.Relocation: Vector3 newPosition = new Vector3(float.Parse(json["x"].ToString()), float.Parse(json["y"].ToString())); RelocationPacket relocPacket = new RelocationPacket(int.Parse(json["entityId"].ToString()), newPosition); lock (relocLock) relocationQueue.Enqueue(relocPacket); break; case PacketType.Visibility: VisibilityPacket visibilityPacket = new VisibilityPacket(int.Parse(json["entityId"].ToString()), bool.Parse(json["visible"].ToString())); lock (visibilityLock) visibilityQueue.Enqueue(visibilityPacket); break; case PacketType.AllFinished: scores = json["scores"].ToObject <Dictionary <string, TimeStamp> >(); allFinished = true; break; default: Debug.LogWarning("Invalid packet type received"); continue; } } catch (Exception e) { Debug.LogException(e); continue; } } Disconnect(); }