Browse Source

Add swipe input

main
karl 1 year ago
parent
commit
97315f74e6
  1. 6
      GameBoard.gd
  2. 6
      GameBoard.tscn
  3. 42
      SwipeHandler.gd
  4. 6
      project.godot

6
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()

6
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 )

42
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

6
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

Loading…
Cancel
Save