CZY W TYM KODZIE COŚ ODPOWIADA ZA SIALDY POZOSAWIANE NA ZIEMI PRZY HAMOWANIU I PRZYSPIESZANIU?

extends Control

@export_enum("ft⋅lb", "nm", "kg/m") var Torque_Unit = 1
@export_enum("hp", "bhp", "ps", "kW") var Power_Unit = 0

# Engine
@export var RevSpeed: float = 2.0
@export var EngineFriction: float = 18000.0
@export var EngineDrag: float = 0.006
@export var ThrottleResponse: float = 0.5

# ECU
@export var IdleRPM: float = 800.0
@export var RPMLimit: float = 7000.0
@export var VVTRPM: float = 4500.0

# Torque normal state
@export var BuildUpTorque: float = 0.0035
@export var TorqueRise: float = 30.0
@export var RiseRPM: float = 1000.0
@export var OffsetTorque: float = 110.0
@export var FloatRate: float = 0.1
@export var DeclineRate: float = 1.5
@export var DeclineRPM: float = 3500.0
@export var DeclineSharpness: float = 1.0

# Torque VVT state
@export var VVT_BuildUpTorque: float = 0.0
@export var VVT_TorqueRise: float = 60.0
@export var VVT_RiseRPM: float = 1000.0
@export var VVT_OffsetTorque: float = 70.0
@export var VVT_FloatRate: float = 0.1
@export var VVT_DeclineRate: float = 2.0
@export var VVT_DeclineRPM: float = 5000.0
@export var VVT_DeclineSharpness: float = 1.0

# Forced induction
@export var TurboEnabled: bool = false
@export var MaxPSI: float = 8.0
@export var TurboAmount: int = 1
@export var EngineCompressionRatio: float = 8.0
@export var SuperchargerEnabled: bool = false
@export var SCRPMInfluence: float = 1.0
@export var BlowRate: float = 35.0
@export var SCThreshold: float = 6.0

# Graph
@export var graph_scale: float = 0.005
@export var Generation_Range: float = 7000.0
@export var Draw_RPM: float = 800.0

var peakhp: Array = [0.0, 0.0]
var peaktq: Array = [0.0, 0.0]

func _ready():
	peakhp = [0.0, 0.0]
	peaktq = [0.0, 0.0]
	$torque.clear_points()
	$power.clear_points()

	var torque_points := PackedVector2Array()
	var power_points := PackedVector2Array()
	var skip := 0

	for i in range(int(Generation_Range)):
		if i > Draw_RPM:
			var tr: float = VitaVehicleSimulation.multivariate(
				RiseRPM, TorqueRise, BuildUpTorque, EngineFriction, EngineDrag, OffsetTorque, i,
				DeclineRPM, DeclineRate, FloatRate, MaxPSI, TurboAmount, EngineCompressionRatio, TurboEnabled,
				VVTRPM, VVT_BuildUpTorque, VVT_TorqueRise, VVT_RiseRPM, VVT_OffsetTorque, VVT_FloatRate,
				VVT_DeclineRPM, VVT_DeclineRate, SuperchargerEnabled, SCRPMInfluence, BlowRate, SCThreshold,
				DeclineSharpness, VVT_DeclineSharpness
			)

			var hp: float = (i / 5252.0) * tr

			# Konwersja jednostek momentu
			if Torque_Unit == 1:
				tr *= 1.3558179483
			elif Torque_Unit == 2:
				tr *= 0.138255

			# Konwersja jednostek mocy
			if Power_Unit == 1:
				hp *= 0.986
			elif Power_Unit == 2:
				hp *= 1.01387
			elif Power_Unit == 3:
				hp *= 0.7457

			# Przekształcenie punktów na współrzędne wykresu
			var tr_p = Vector2((i / Generation_Range) * size.x, size.y - (tr * size.y) * graph_scale)
			var hp_p = Vector2((i / Generation_Range) * size.x, size.y - (hp * size.y) * graph_scale)

			# Aktualizacja pozycji szczytowych punktów
			if hp > peakhp[0]:
				peakhp = [hp, i]
				$"power/peak".position = hp_p

			if tr > peaktq[0]:
				peaktq = [tr, i]
				$"torque/peak".position = tr_p

			# Dodawanie punktów co N kroków
			skip -= 1
			if skip <= 0:
				torque_points.append(tr_p)
				power_points.append(hp_p)
				skip = 100

	# Ustawienie punktów tylko jeśli są co najmniej 3
	if torque_points.size() >= 3:
		$torque.points = torque_points
	else:
		print("Za mało punktów do narysowania wykresu momentu.")

	if power_points.size() >= 3:
		$power.points = power_points
	else:
		print("Za mało punktów do narysowania wykresu mocy.")