From 97315f74e656ea217ce5bb199df9a117fdf96007 Mon Sep 17 00:00:00 2001 From: karl Date: Sun, 21 Nov 2021 13:24:03 +0100 Subject: [PATCH] Add swipe input --- GameBoard.gd | 6 ++++++ GameBoard.tscn | 6 +++++- SwipeHandler.gd | 42 ++++++++++++++++++++++++++++++++++++++++++ project.godot | 6 ++++++ 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 SwipeHandler.gd diff --git a/GameBoard.gd b/GameBoard.gd index 1223daa..9da58eb 100644 --- a/GameBoard.gd +++ b/GameBoard.gd @@ -21,6 +21,12 @@ const RIGHT = RASTER_SIZE * 5 func _ready(): + # Swipe input + $SwipeHandler.connect("swipe_up", self, "rotate_shape") + $SwipeHandler.connect("swipe_left", self, "move_left") + $SwipeHandler.connect("swipe_right", self, "move_right") + $SwipeHandler.connect("swipe_down", self, "drop") + $GravityTimer.connect("timeout", self, "update_board") active_shape = get_random_shape() diff --git a/GameBoard.tscn b/GameBoard.tscn index 8d02587..5fb8e4d 100644 --- a/GameBoard.tscn +++ b/GameBoard.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=3 format=2] +[gd_scene load_steps=4 format=2] [ext_resource path="res://background.png" type="Texture" id=1] [ext_resource path="res://GameBoard.gd" type="Script" id=2] +[ext_resource path="res://SwipeHandler.gd" type="Script" id=3] [node name="GameBoard" type="Node2D"] script = ExtResource( 2 ) @@ -16,3 +17,6 @@ wait_time = 0.5 autostart = true [node name="StaticBlocks" type="Node2D" parent="."] + +[node name="SwipeHandler" type="Node" parent="."] +script = ExtResource( 3 ) diff --git a/SwipeHandler.gd b/SwipeHandler.gd new file mode 100644 index 0000000..3495924 --- /dev/null +++ b/SwipeHandler.gd @@ -0,0 +1,42 @@ +extends Node + + +signal swipe_left +signal swipe_right +signal swipe_up +signal swipe_down + +var swipe_start: Vector2 +var swipe_done = false + +const MAX_SWIPE_DISTANCE = 80 + + +func _unhandled_input(event): + if event is InputEventScreenTouch: + if event.pressed: + swipe_done = false + swipe_start = event.get_position() + elif not swipe_done: + _interpret_swipe(event.get_position()) + elif event is InputEventScreenDrag: + if not swipe_done: + _interpret_swipe(event.position) + + +func _interpret_swipe(swipe_end: Vector2): + var diff = swipe_end - swipe_start + + if diff.length_squared() > MAX_SWIPE_DISTANCE * MAX_SWIPE_DISTANCE: + var normalized = diff.normalized() + + if normalized.x > 0.5: + emit_signal("swipe_right") + elif normalized.x < -0.5: + emit_signal("swipe_left") + elif normalized.y > 0.5: + emit_signal("swipe_down") + else: + emit_signal("swipe_up") + + swipe_done = true diff --git a/project.godot b/project.godot index 001b2bc..e620c0c 100644 --- a/project.godot +++ b/project.godot @@ -30,7 +30,9 @@ window/size/width=1080 window/size/height=1920 window/size/test_width=576 window/size/test_height=1024 +window/handheld/orientation="portrait" window/stretch/mode="2d" +window/stretch/aspect="expand" [input] @@ -55,6 +57,10 @@ drop={ ] } +[input_devices] + +pointing/emulate_touch_from_mouse=true + [physics] common/enable_pause_aware_picking=true