|  | @@ -4,6 +4,8 @@ signal hit
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  export var speed = 400 # pixel / sec
 | 
	
		
			
				|  |  |  var screen
 | 
	
		
			
				|  |  | +var target = Vector2()
 | 
	
		
			
				|  |  | +var target_reached = true
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  func spawn(atPos):
 | 
	
		
			
				|  |  |  	position = atPos
 | 
	
	
		
			
				|  | @@ -18,16 +20,10 @@ func _ready():
 | 
	
		
			
				|  |  |  	screen = get_viewport_rect()
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  func _process(delta):
 | 
	
		
			
				|  |  | -	var velocity = Vector2()
 | 
	
		
			
				|  |  | -	if Input.is_action_pressed("ui_right"):
 | 
	
		
			
				|  |  | -		velocity.x += 1
 | 
	
		
			
				|  |  | -	if Input.is_action_pressed("ui_left"):
 | 
	
		
			
				|  |  | -		velocity.x -= 1
 | 
	
		
			
				|  |  | -	if Input.is_action_pressed("ui_down"):
 | 
	
		
			
				|  |  | -		velocity.y += 1
 | 
	
		
			
				|  |  | -	if Input.is_action_pressed("ui_up"):
 | 
	
		
			
				|  |  | -		velocity.y -= 1
 | 
	
		
			
				|  |  | -		
 | 
	
		
			
				|  |  | +	var velocity = get_velocity_from_action()
 | 
	
		
			
				|  |  | +	if velocity == Vector2.ZERO :
 | 
	
		
			
				|  |  | +		velocity = get_velocity_from_target(delta)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  	if velocity.length() > 0:
 | 
	
		
			
				|  |  |  		velocity = velocity.normalized() * speed
 | 
	
		
			
				|  |  |  		$AnimatedSprite.play()
 | 
	
	
		
			
				|  | @@ -48,7 +44,40 @@ func _process(delta):
 | 
	
		
			
				|  |  |  		$AnimatedSprite.flip_v = velocity.y > 0
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +func _input(event):
 | 
	
		
			
				|  |  | +	if event is InputEventMouseButton:
 | 
	
		
			
				|  |  | +		print("Mouse click : ", event.button_index, " at ", event.position)
 | 
	
		
			
				|  |  | +		target = event.position
 | 
	
		
			
				|  |  | +		target_reached = false
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  func _on_Player_body_entered(body):
 | 
	
		
			
				|  |  |  	die()
 | 
	
		
			
				|  |  |  	emit_signal("hit")
 | 
	
		
			
				|  |  |  	$CollisionShape2D.set_deferred("disabled", true)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +func get_velocity_from_action() -> Vector2:
 | 
	
		
			
				|  |  | +	var velocity = Vector2()
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	if Input.is_action_pressed("ui_right"):
 | 
	
		
			
				|  |  | +		velocity.x += 1
 | 
	
		
			
				|  |  | +	if Input.is_action_pressed("ui_left"):
 | 
	
		
			
				|  |  | +		velocity.x -= 1
 | 
	
		
			
				|  |  | +	if Input.is_action_pressed("ui_down"):
 | 
	
		
			
				|  |  | +		velocity.y += 1
 | 
	
		
			
				|  |  | +	if Input.is_action_pressed("ui_up"):
 | 
	
		
			
				|  |  | +		velocity.y -= 1
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	return velocity;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +func get_velocity_from_target(delta):
 | 
	
		
			
				|  |  | +	print("Try target")
 | 
	
		
			
				|  |  | +	if target_reached :
 | 
	
		
			
				|  |  | +		return Vector2.ZERO
 | 
	
		
			
				|  |  | +	var path = target - position
 | 
	
		
			
				|  |  | +	print("Go that way : ", path)
 | 
	
		
			
				|  |  | +	if path.length_squared() < speed * speed * delta * delta :
 | 
	
		
			
				|  |  | +		print("Reached")
 | 
	
		
			
				|  |  | +		target_reached = true
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	return path
 |