Skip to content

Commit

Permalink
click to move
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbrain committed Jan 27, 2024
1 parent 9a18023 commit 8ddb93b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 4 deletions.
4 changes: 2 additions & 2 deletions godot/input/player_controller.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ extends Node2D
var input_vector:Vector2 = Vector2(0, 0)

func _physics_process(delta: float) -> void:
input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
input_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
input_vector.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
input_vector.y = Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
player.move(input_vector)
4 changes: 4 additions & 0 deletions godot/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -66,27 +66,31 @@ move_left={
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":65,"key_label":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194319,"key_label":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":0,"axis_value":-1.0,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":65,"key_label":0,"unicode":97,"echo":false,"script":null)
]
}
move_up={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":87,"key_label":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194320,"key_label":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":1,"axis_value":-1.0,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":87,"key_label":0,"unicode":119,"echo":false,"script":null)
]
}
move_right={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":68,"key_label":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194321,"key_label":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":0,"axis_value":1.0,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":68,"key_label":0,"unicode":100,"echo":false,"script":null)
]
}
move_down={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194322,"key_label":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":1,"axis_value":1.0,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":115,"echo":false,"script":null)
]
}
interact={
Expand Down
2 changes: 1 addition & 1 deletion godot/world/emotion/emotion.gd
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func _ready() -> void:
self.emotion_type = emotion_type
navigation_agent_2d.path_desired_distance = 8.0
navigation_agent_2d.target_desired_distance = 8.0
navigation_agent_2d.debug_enabled = true
navigation_agent_2d.debug_enabled = false


func _update_navigation() -> void:
Expand Down
1 change: 0 additions & 1 deletion godot/world/emotion/emotion.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ animation_strength = 1
[node name="NavigationAgent2D" type="NavigationAgent2D" parent="."]
path_desired_distance = 8.0
target_desired_distance = 8.0
debug_enabled = true

[node name="NavigationUpdateTimer" type="Timer" parent="."]
wait_time = 0.25
Expand Down
22 changes: 22 additions & 0 deletions godot/world/player/player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extends CharacterBody2D
@onready var emotion_detector: Area2D = $EmotionDetector
@onready var receptor_detector: Area2D = $ReceptorDetector
@onready var collectors: Array[Node] = $Collectors.get_children()
@onready var navigation_agent_2d: NavigationAgent2D = $NavigationAgent2D


var input_vector = Vector2.ZERO
Expand All @@ -18,6 +19,20 @@ var emotions:Array[int]= []
func _ready() -> void:
emotion_detector.body_entered.connect(_on_emotion_collected)
receptor_detector.body_entered.connect(_on_receptor_touched)

navigation_agent_2d.path_desired_distance = 8.0
navigation_agent_2d.target_desired_distance = 8.0
navigation_agent_2d.debug_enabled = false


func _unhandled_input(event):
if not event.is_action_pressed("click"):
return
set_target_position(get_global_mouse_position())


func set_target_position(target_position:Vector2) -> void:
navigation_agent_2d.target_position = target_position


func _on_receptor_touched(receptor:Receptor) -> void:
Expand Down Expand Up @@ -45,7 +60,14 @@ func move(input_vector:Vector2) -> void:

func _physics_process(delta: float) -> void:
if input_vector != Vector2.ZERO:
navigation_agent_2d.target_position = global_position
navigation_agent_2d.get_next_path_position()
velocity = velocity.move_toward(input_vector * MAX_SPEED, ACCELERATION * delta)
elif not navigation_agent_2d.is_navigation_finished():
var current_agent_position: Vector2 = global_position
var next_path_position: Vector2 = navigation_agent_2d.get_next_path_position()
velocity = current_agent_position.direction_to(next_path_position) * MAX_SPEED
else:
velocity = velocity.move_toward(Vector2.ZERO, FRICTION * delta)
move_and_slide()

2 changes: 2 additions & 0 deletions godot/world/player/player.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,5 @@ shape = SubResource("CircleShape2D_ytl16")
position = Vector2(7.25407, -14.181)
energy = 0.5
animation_strength = 3

[node name="NavigationAgent2D" type="NavigationAgent2D" parent="."]

0 comments on commit 8ddb93b

Please # to comment.