fuzzyworld

godot-shooter / commit

Added "tactical mode": dragging with touch or dragging the mouse while clicking, will now pause the game, until the user lets go. The destination moves around while the game is paused.

641b63b5c341d65b209854bd981a4561b959f27c · Pavel Safronov · 2023-11-10 15:48:03 -0800 · parents 3fcb817e31e6

Added "tactical mode": dragging with touch or dragging the mouse while clicking, will now pause the game, until the user lets go. The destination moves around while the game is paused.
 HUD.gd     | 19 +++++++++++++++++++
 World.gd   | 12 +++---------
 World.tscn |  1 +
 3 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/HUD.gd b/HUD.gd
index c259042..c9b750a 100644
--- a/HUD.gd
+++ b/HUD.gd
@@ -3,8 +3,10 @@ extends Control
 const Utils = preload("res://Utils.gd")
 export var score = 0
 export var objective = "1/17"
+var is_dragging = false
 signal paused
 signal debug_signal
+signal position_set(position)
 
 onready var label = $CanvasLayer/MessageLabel
 onready var animation_player = $AnimationPlayer
@@ -13,6 +15,23 @@ onready var objective_label = $MarginContainer/HBoxContainer2/Objective
 onready var weapon_label = $MarginContainer2/HBoxContainer/Weapon
 onready var ammo_label = $MarginContainer2/HBoxContainer/Ammo
 
+func _input(event: InputEvent) -> void:
+	if event is InputEventScreenTouch and event.pressed:
+		emit_signal("position_set", event.position)
+	if event is InputEventScreenDrag:
+		emit_signal("position_set", event.position)
+	if event is InputEventMouseButton:
+		if not is_dragging and event.pressed:
+			is_dragging = true
+			get_tree().paused = true
+			emit_signal("position_set", event.position)
+		if is_dragging and not event.pressed:
+			is_dragging = false
+			get_tree().paused = false
+	if event is InputEventMouseMotion and is_dragging:
+		emit_signal("position_set", event.position)
+
+
 func _process(_delta: float) -> void:
 	score_label.text = "%s" % score
 	objective_label.text = "%s" % objective
diff --git a/World.gd b/World.gd
index 9febfbf..1b29624 100644
--- a/World.gd
+++ b/World.gd
@@ -21,6 +21,7 @@ var generators = [
 var completed_objectives = 0
 var max_objectives = 5
 var item_replacement_distance = 2000
+var is_dragging = false
 
 func enemy_killed(_position: Vector2):
 	$UI/HUD.score += 1
@@ -109,15 +110,9 @@ func get_new_environment_position(dist_ratio: float = 1.0) -> Vector2:
 			break
 	return potential_pos
 
-func _input(event: InputEvent) -> void:
+func _input(_event: InputEvent) -> void:
 	if $Player.dead:
 		return
-	if event is InputEventScreenTouch && event.pressed:
-		set_destination(event.position)
-	if event is InputEventScreenDrag:
-		set_destination(event.position)
-	if event is InputEventMouseMotion:
-		set_destination(event.position)
 
 func _process(_delta: float) -> void:
 	if Input.is_action_just_pressed("pause"):
@@ -169,8 +164,7 @@ func pause():
 
 func set_destination(event_pos: Vector2) -> void:
 	var mid = Vector2(width / 2.0, height / 2.0)
-	var pos = event_pos + $Player.position - mid
-	
+	var pos = event_pos + $Player.position - mid	
 	destination = pos
 	is_destination_set = true
 	$Destination.global_position = pos
diff --git a/World.tscn b/World.tscn
index aafb9df..8c8bff5 100644
--- a/World.tscn
+++ b/World.tscn
@@ -159,5 +159,6 @@ antialiased = true
 [connection signal="body_entered" from="Destination" to="." method="_on_Destination_body_entered"]
 [connection signal="debug_signal" from="UI/HUD" to="." method="_on_HUD_debug_signal"]
 [connection signal="paused" from="UI/HUD" to="." method="_on_HUD_paused"]
+[connection signal="position_set" from="UI/HUD" to="." method="set_destination"]
 [connection signal="pressed" from="UI/PauseMenu/CenterContainer/PanelContainer/VBoxContainer/Button" to="." method="_on_Button_pressed"]
 [connection signal="timeout" from="ShootTimer" to="." method="_on_ShootTimer_timeout"]