local module = {}

function module.new(this,rocket,context)
	this.scale = rocket:GetExtentsSize().Magnitude -- same as with the equivalent statement on the wing! BRRRRRRRRRRR
	this.binding = _G.GetInstance(rocket,"Binding")
	this.exhaust = _G.GetInstance(rocket,"Exhaust")
	this.sound = _G.GetInstance(this.exhaust,"Sound")
	this.force = _G.GetInstance(rocket,"Force")
	this.inverted = _G.GetInstance(rocket,"Inverted")
	return this
end

function module.idle(this,engine) return true end

local begin = tick()
module.run = _G.Protect(function(this,engine,context)
	local throttle = this.throttle or context.controls[_G.GetValue(engine,"Binding","throttle")] or this.Throttle or 0
	if this.inverted.Value then throttle = -throttle end
	context.replicate(this,engine,module,math.clamp(throttle,0,1))
	return true
end)

module.replication_delta = 0.25
function module.replicate(this,rocket,context,throttle)
	this.exhaust:ApplyImpulseAtPosition(this.exhaust.CFrame.RightVector * throttle * this.scale * this.force.Value * context.delta * -1000, this.exhaust.Position)
	this.exhaust.Flame.Rate = throttle * 1000
	this.sound.Volume = math.clamp(throttle,0,1)
	if _G.IsServer and this.sound.Volume > 0 then this.sound:Play() else this.sound:Pause() end
end

module.messages = {
	control = function(this,object,context,value)
		this.throttle = value
	end,
	starter = function(this,object,context,value)
		this.starter = value
	end,
}
module.messages.default = module.messages.control

module.manifest = {
	"Rocket",
	"BigRocket"
}

return module
