-- changeObjectByDistanceEvent.lua

ChangeObjectByDistanceEvent = {}
local ChangeObjectByDistanceEvent_mt = Class(ChangeObjectByDistanceEvent, Event)

InitEventClass(ChangeObjectByDistanceEvent, "ChangeObjectByDistanceEvent")

function ChangeObjectByDistanceEvent.emptyNew()
    local self = Event.new(ChangeObjectByDistanceEvent_mt)
    return self
end

function ChangeObjectByDistanceEvent.new(vehicle, distance)
    local self = ChangeObjectByDistanceEvent.emptyNew()
    self.vehicle = vehicle
    self.distance = distance
    return self
end

function ChangeObjectByDistanceEvent:readStream(streamId, connection)
    self.vehicle = NetworkUtil.readNodeObject(streamId)
    self.distance = streamReadFloat32(streamId)
    self:run(connection)
end

function ChangeObjectByDistanceEvent:writeStream(streamId, connection)
    NetworkUtil.writeNodeObject(streamId, self.vehicle)
    streamWriteFloat32(streamId, self.distance)
end

function ChangeObjectByDistanceEvent:run(connection)
    if not connection:getIsServer() then
        if self.vehicle ~= nil and self.vehicle:getIsSynchronized() then
            self.vehicle:setChangeObjectDistance(self.distance, true)
        end
    end
end

function ChangeObjectByDistanceEvent.sendEvent(vehicle, distance, noEventSend)
    if noEventSend == nil or noEventSend == false then
        if g_server ~= nil then
            g_server:broadcastEvent(ChangeObjectByDistanceEvent.new(vehicle, distance), nil, nil, vehicle)
        else
            g_client:getServerConnection():sendEvent(ChangeObjectByDistanceEvent.new(vehicle, distance))
        end
    end
end
