Skip to content

Commit

Permalink
feat: support input gestures in Trolls
Browse files Browse the repository at this point in the history
Not too bad! just need to pass the event in a few places. Sometimes 2
moves fire quickly (e.g. down is interpreted as left then down) - i
think this can happen with the joystick as well - maybe a stronger delay
while moving would solve it?
  • Loading branch information
russmatney committed Jun 17, 2024
1 parent b2cf65c commit 755bfa4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
35 changes: 26 additions & 9 deletions addons/core/Trolls.gd
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,45 @@ static func is_released(event, event_name):
return event.is_action_released(event_name)
return false

static func is_screen_drag_event(event):
if event is InputEventScreenDrag:
return (
(abs(event.relative.x) > 4 or abs(event.relative.y) > 4)
and
(abs(event.velocity.x) > 100 or abs(event.velocity.y) > 100))

# returns a normalized Vector2 based checked the controller's movement
static func move_vector():
static func move_vector(event=null):
if event:
if Trolls.is_screen_drag_event(event):
var dir = to_cardinal_direction(event.velocity, 100)
Log.pr("drag gesture dir", dir)
return dir
if Dino.focused:
return Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
return Vector2.ZERO

static func grid_move_vector(thresh=0.6):
var move = move_vector()
if move.x > thresh:
static func to_cardinal_direction(vec, thresh=0.6):
if vec.x > thresh:
return Vector2.RIGHT
elif move.x < -1*thresh:
elif vec.x < -1*thresh:
return Vector2.LEFT
elif move.y < -1*thresh:
elif vec.y < -1*thresh:
return Vector2.UP
elif move.y > thresh:
elif vec.y > thresh:
return Vector2.DOWN
return Vector2.ZERO

static func grid_move_vector(event=null, thresh=0.6):
var move = move_vector(event)
return Trolls.to_cardinal_direction(move, thresh)

static func is_move(event):
return is_event(event, "ui_left") or is_event(event, "ui_right") or \
is_event(event, "ui_up") or is_event(event, "ui_down")
return (is_event(event, "ui_left") or
is_event(event, "ui_right") or
is_event(event, "ui_up") or
is_event(event, "ui_down") or
is_screen_drag_event(event))

static func is_move_released(event):
return is_released(event, "ui_left") or is_released(event, "ui_right") or \
Expand Down
6 changes: 3 additions & 3 deletions src/puzzle/PuzzleScene.gd
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func _unhandled_input(event):
if state == null:
Log.warn("No state, ignoring move input")
return
check_move_input()
check_move_input(event)
elif Trolls.is_undo(event) and not block_move:
if state == null:
Log.warn("No state, ignoring undo input")
Expand Down Expand Up @@ -236,8 +236,8 @@ func cancel_reset_puzzle():
var block_move
var last_move

func check_move_input():
var move_vec = Trolls.grid_move_vector()
func check_move_input(event=null):
var move_vec = Trolls.grid_move_vector(event)

if move_vec != last_move:
# allow moving in a new direction
Expand Down

0 comments on commit 755bfa4

Please sign in to comment.