fuzzyworld

godot-shooter / commit

Simplified the destination-setting code.

97a9ab3d03d0842ae232d050cb1538fe073aeaa9 · Pavel Safronov · 2023-11-07 16:57:47 -0800 · parents 11ed9f7abc79

Simplified the destination-setting code.
 World.gd | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/World.gd b/World.gd
index 3e94a61..af99a2c 100644
--- a/World.gd
+++ b/World.gd
@@ -3,6 +3,9 @@ extends Node2D
 const Utils = preload("res://Utils.gd")
 const Enemy = preload("res://Enemy.tscn")
 const PowerUp = preload("res://PowerUp.tscn")
+var width = ProjectSettings.get("display/window/size/width")
+var height = ProjectSettings.get("display/window/size/height")
+
 var destination: Vector2
 var is_destination_set = false
 var min_items_dict = {
@@ -83,18 +86,12 @@ func get_new_item_pos(dist_ratio: float = 1.0) -> Vector2:
 func _input(event: InputEvent) -> void:
 	if $Player.dead:
 		return
-	var width = ProjectSettings.get("display/window/size/width")
-	var height = ProjectSettings.get("display/window/size/height")
-	var mid = Vector2(width / 2.0, height / 2.0)
 	if event is InputEventScreenTouch && event.pressed:
-		var result = event.position + $Player.position - mid
-		set_destination(result)
+		set_destination(event.position)
 	if event is InputEventScreenDrag:
-		var result = event.position + $Player.position - mid
-		set_destination(result)
+		set_destination(event.position)
 	if event is InputEventMouseMotion:
-		var result = event.position + $Player.position - mid
-		set_destination(result)
+		set_destination(event.position)
 
 func _process(_delta: float) -> void:
 	if Input.is_action_just_pressed("pause"):
@@ -125,7 +122,10 @@ func _process(_delta: float) -> void:
 		if is_too_far:
 			item.queue_free()
 
-func set_destination(pos: Vector2) -> void:
+func set_destination(event_pos: Vector2) -> void:
+	var mid = Vector2(width / 2.0, height / 2.0)
+	var pos = event_pos + $Player.position - mid
+	
 	destination = pos
 	is_destination_set = true
 	$Destination.global_position = pos