Exemplo n.º 1
0
    private void RelayTowedVehicleChanged(Vehicle sender, ElementChangedEventArgs <Vehicle, Vehicle?> args)
    {
        if (args.Source.IsSync)
        {
            return;
        }

        if (args.NewValue == null)
        {
            if (args.OldValue != null)
            {
                this.server.BroadcastPacket(VehiclePacketFactory.CreateTrailerDetachPacket(sender, args.OldValue));
            }
            return;
        }

        this.server.BroadcastPacket(new VehicleTrailerSyncPacket()
        {
            VehicleId         = sender.Id,
            AttachedVehicleId = args.NewValue.Id,
            IsAttached        = true,
            Position          = args.NewValue.Position,
            Rotation          = args.NewValue.Rotation,
            TurnVelocity      = args.NewValue.TurnVelocity,
        });
    }
Exemplo n.º 2
0
    public void HandlePacket(Client client, VehicleTrailerSyncPacket packet)
    {
        var otherPlayers = this.middleware.GetPlayersToSyncTo(client.Player, packet);

        var player = client.Player;

        var vehicle         = this.elementRepository.Get(packet.VehicleId) as Elements.Vehicle;
        var attachedVehicle = this.elementRepository.Get(packet.VehicleId) as Elements.Vehicle;

        if (vehicle != null && attachedVehicle != null)
        {
            vehicle.RunAsSync(() =>
            {
                if (packet.IsAttached)
                {
                    if (vehicle.TowedVehicle != null)
                    {
                        VehiclePacketFactory.CreateTrailerDetachPacket(vehicle, vehicle.TowedVehicle)
                        .SendTo(otherPlayers);
                    }
                    if (attachedVehicle.TowingVehicle != null)
                    {
                        VehiclePacketFactory.CreateTrailerDetachPacket(attachedVehicle.TowingVehicle, attachedVehicle)
                        .SendTo(otherPlayers);
                    }

                    attachedVehicle.RunAsSync(() => attachedVehicle.AttachToTower(vehicle, true));
                }
                else
                {
                    attachedVehicle.RunAsSync(() => attachedVehicle.AttachToTower(null, true));
                }

                packet.SendTo(otherPlayers);
            });
        }
    }