91 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			GDScript
		
	
	
	
	
	
			
		
		
	
	
			91 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			GDScript
		
	
	
	
	
	
| extends Node2D
 | |
| 
 | |
| @export var human: Human
 | |
| @export var ray : ShapeCast2D
 | |
| @export var pathFinder : HumanPathFinder
 | |
| @onready var world: World = human.get_parent();
 | |
| 
 | |
| # Stores the nearest Node2D we can interact with, then we can start interacting with it if the player
 | |
| # press grab
 | |
| var can_interact_with : Node2D
 | |
| 
 | |
| func _unhandled_input(event: InputEvent) -> void:
 | |
| 	human.stop_interaction()
 | |
| 	human.velocityVector = Vector2(0, 0)
 | |
| 	human.wants_to_grab = false
 | |
| 	human.wants_to_interact_with = null
 | |
| 	human.velocityVector = Input.get_vector("move_left", "move_right", "move_up", "move_down")
 | |
| 	if human.velocityVector != Vector2(0, 0):
 | |
| 		GameState.isUsingTouch = false
 | |
| 
 | |
| 	if event.is_action_pressed("grab"):
 | |
| 		if can_interact_with:
 | |
| 			human.wants_to_interact_with = can_interact_with
 | |
| 		else:
 | |
| 			human.wants_to_grab = true
 | |
| 
 | |
| 	# moving using either touch or mouse button
 | |
| 	if event.is_action_pressed("touch") or event is InputEventScreenTouch:
 | |
| 		GameState.isUsingTouch = true
 | |
| 		var tile_pos = world.local_to_map(world.to_local(get_global_mouse_position()))
 | |
| 		if event.is_pressed():
 | |
| 			pathFinder.destination = get_global_mouse_position()
 | |
| 			
 | |
| func update_game_state():
 | |
| 	GameState.player_positions = human.get_feet_global_position()
 | |
| 
 | |
| func _process(delta) -> void:
 | |
| 	update_game_state()
 | |
| 	ray.target_position = human.last_facing_direction * 48
 | |
| 
 | |
| 	if human.last_facing_direction.y > 0 :
 | |
| 		ray.target_position = Vector2(0, 48)
 | |
| 		(ray.shape as RectangleShape2D).size = Vector2(50, 20)
 | |
| 	elif human.last_facing_direction.y < 0:
 | |
| 		ray.target_position = Vector2(0, -48)
 | |
| 		(ray.shape as RectangleShape2D).size = Vector2(50, 20)
 | |
| 	elif human.last_facing_direction.x > 0 :
 | |
| 		ray.target_position = Vector2(48, 0)
 | |
| 		(ray.shape as RectangleShape2D).size = Vector2(20, 50)
 | |
| 	else:
 | |
| 		ray.target_position = Vector2(-48, 0)
 | |
| 		(ray.shape as RectangleShape2D).size = Vector2(20, 50)
 | |
| 
 | |
| 	# find all the possible interactables
 | |
| 	var possible_interactables = []
 | |
| 	if ray.is_colliding():
 | |
| 		var nbCollisions = ray.get_collision_count()
 | |
| 		for n in range(nbCollisions):
 | |
| 			var colider = ray.get_collider(n) as Node2D
 | |
| 			if colider != null and colider != get_parent():
 | |
| 				if (
 | |
| 						colider and colider.get_parent().has_method("start_interaction") and
 | |
| 						colider.get_parent().has_method("enable_interaction_clue") and
 | |
| 						colider.get_parent().has_method("disable_interaction_clue")
 | |
| 					):
 | |
| 					possible_interactables.append(colider.get_parent())
 | |
| 	
 | |
| 	# find the closest interactable
 | |
| 	var distance_to_closest = Vector2(0, 0) 
 | |
| 	var closest: Node2D = null
 | |
| 	for potential_closest in possible_interactables:
 | |
| 		var distance_to_human = abs(potential_closest.global_position - human.global_position)
 | |
| 		
 | |
| 		if distance_to_closest == Vector2(0, 0):
 | |
| 			distance_to_closest = distance_to_human
 | |
| 			closest = potential_closest
 | |
| 		if distance_to_human < distance_to_closest:
 | |
| 			closest = potential_closest
 | |
| 			distance_to_closest = distance_to_human
 | |
| 	
 | |
| 	# if the new closest is different from the one we had before, notify the one we had before that it shouldn't keep its bubble visible
 | |
| 	if can_interact_with != null:
 | |
| 		if can_interact_with != closest:
 | |
| 			can_interact_with.get_children()
 | |
| 			can_interact_with.disable_interaction_clue()
 | |
| 	# don't retrigger enabling the clue if it was already enabled
 | |
| 	if can_interact_with != closest:
 | |
| 		can_interact_with = closest
 | |
| 		if can_interact_with:
 | |
| 			can_interact_with.enable_interaction_clue(human)
 | 
