extends RigidBody2D

var initialpos
var initialrot
var maxrot = 1
var is_active = 0 # active while a key is pressed
var rotating = 0 # 1 when rotating right, -1 when rotating left
var rotspeed = 20

func _ready():
	set_process_input(true)
	initialpos = position
	initialrot = rotation
	pass

func _process(delta):
	if Input.is_action_just_pressed("ui_right"):
		is_active = 1
		rotating = 1
		apply_impulse(Vector2(), Vector2(0.001, 0.001))
	if Input.is_action_just_released("ui_right"):
		is_active = 0
		apply_impulse(Vector2(), Vector2(-0.001, -0.001))
	if Input.is_action_just_pressed("ui_left"):
		is_active = 1
		rotating = -1;
		apply_impulse(Vector2(), Vector2(0.001, 0.001))
	if Input.is_action_just_released("ui_left"):
		is_active = 0
		apply_impulse(Vector2(), Vector2(-0.001, -0.001))


func _integrate_forces(state):
	if is_active:
		if (rotation > initialrot + maxrot) or (rotation < initialrot - maxrot):
			angular_velocity = 0
		else:
			angular_velocity = rotating * rotspeed
	else:
		var rot_diff = initialrot - rotation
		angular_velocity = rot_diff * 30
