extends Spatial


onready var exhaust_node = $Exhaust;

func _set_thruster_thrust(node: MeshInstance, power):
	#print("Node: ", node.name)
	var MAX_POWER = 10;
	node.get_surface_material(0).set_shader_param("power", clamp(power, .0, MAX_POWER));
		
	var particle_node: Particles = node.get_node("./Particles");
		
	if power < 2.0:
		if power == 0.0:
			node.set_visible(false);
		particle_node.set_emitting(false);
	else:
		node.set_visible(true);
		particle_node.set_emitting(true);

# Set the visual main engine flare from 0.0 to 10.0
# 0.0 turns off all engine effects
func set_main_engine_thrust(power):
	var exhausts = exhaust_node.get_node("Main").get_children();
	print("Changing main exhaust to: ", power);
	
	for node in exhausts:
		self._set_thruster_thrust(node, power);

# Set the visual rotational engine flare from -10.0 to 10.0
# Negative power visualizes downwards, Positive upwards and 0.0 turns all X thrusters off
func set_X_rotational_thrust(power):
	var MAX_POWER = 10;
	var up_rotation_thrusters = [exhaust_node.get_node("FrontThrusters/ThrusterBottom"), exhaust_node.get_node("RearThrusters/ThrusterTop")];
	var down_rotation_thrusters = [exhaust_node.get_node("FrontThrusters/ThrusterTop"), exhaust_node.get_node("RearThrusters/ThrusterBottom")];
	
	if power > 0.0:
		print("Changing rotation thrust upwards to: ", power);
		for node in up_rotation_thrusters:
			self._set_thruster_thrust(node, power);
		for node in down_rotation_thrusters:
			self._set_thruster_thrust(node, 0.0);
	elif power < 0.0:
		print("Changing rotation thrust downwards to: ", power);
		for node in down_rotation_thrusters:
			self._set_thruster_thrust(node, -1 * power);
		for node in up_rotation_thrusters:
			self._set_thruster_thrust(node, 0.0);
	else:
		print("Turning X rotation thrusters off");
		for node in up_rotation_thrusters+down_rotation_thrusters:
			self._set_thruster_thrust(node, 0.0);


# Set the visual rotational engine flare from -10.0 to 10.0
# Negative power visualizes left, Positive right and 0.0 turns all Y thrusters off
func set_Y_rotational_thrust(power):
	var MAX_POWER = 10;
	var left_rotation_thrusters = [exhaust_node.get_node("FrontThrusters/ThrusterLeft"), exhaust_node.get_node("RearThrusters/ThrusterRight")];
	var right_rotation_thrusters = [exhaust_node.get_node("FrontThrusters/ThrusterRight"), exhaust_node.get_node("RearThrusters/ThrusterLeft")];
	
	if power > 0.0:
		print("Changing rotation thrust right to: ", power);
		for node in right_rotation_thrusters:
			self._set_thruster_thrust(node, power);
		for node in left_rotation_thrusters:
			self._set_thruster_thrust(node, 0.0);
	elif power < 0.0:
		print("Changing rotation thrust left to: ", power);
		for node in left_rotation_thrusters:
			self._set_thruster_thrust(node, -1 * power);
		for node in right_rotation_thrusters:
			self._set_thruster_thrust(node, 0.0);
	else:
		print("Turning Y rotation thrusters off");
		for node in right_rotation_thrusters+left_rotation_thrusters:
			self._set_thruster_thrust(node, 0.0);


func _ready():
	set_main_engine_thrust(5.0);
	set_X_rotational_thrust(10.0);
	set_Y_rotational_thrust(10.0);

var time_passed = 0.0;
var curr_rot_power = 10.0;
var curr_main_power = 0.0;
func _physics_process(delta):
	time_passed += delta;
	
	if time_passed > 1:
		print("Setting rotation X to 5.0");
		set_X_rotational_thrust(curr_rot_power);
		set_Y_rotational_thrust(curr_rot_power);
		set_main_engine_thrust(curr_main_power);
		curr_rot_power *= -1;
		curr_main_power += 1;
		if curr_main_power > 10.0:
			curr_main_power = 0.0;
		time_passed = 0.0;
