TouchSticks: a finger on the right half of the screen is a move stick where it lands (the move_* actions), one on the left half an attack stick (the aim_* actions, with fire held past the aim dead zone, so the weapon attacks that way each time it's ready). Buttons get their touches first; the sticks only act while the scarecrow can, and a pause or freeze lets them go. On phones a tap is also an emulated left click, so fire and offhand lose their mouse buttons and ScarecrowCombat ignores emulated mouse events for its aim device. A held finger still skips the intro, and a tap skips the farmers' return. Docs: the first device run's numbers (night 20 held 60 FPS) and the sticks. Graph update. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
148 lines
5 KiB
GDScript
148 lines
5 KiB
GDScript
class_name TouchSticks
|
|
extends Control
|
|
## The phone's controls (MobilePort; Main adds it over the HUD): two floating thumbsticks. A
|
|
## finger down on one half of the screen starts that half's stick where it lands; dragging tilts
|
|
## it (past the rim, the base follows the finger), and lifting the finger lets it go.
|
|
## * Move (right half): the move_* actions, as far as the stick is tilted. Screen up is
|
|
## forward, as with WASD.
|
|
## * Attack (left half): the aim_* actions, as a gamepad's right stick, and fire held while the
|
|
## stick is tilted past the aim dead zone, so the main weapon attacks that way each time it's
|
|
## ready.
|
|
## The sticks only take touches the HUD and the menus don't (their buttons get them first), and
|
|
## only while the scarecrow can act: not paused (menus, conversations), not frozen (the intro),
|
|
## not once the vacation is over. A pause or freeze lets both sticks go. Each stick is drawn only
|
|
## while a finger is on it.
|
|
## On phones a tap is also a left click (Godot's mouse emulation, which the buttons need), and
|
|
## the left click is fire: fire and offhand lose their mouse buttons here, so only the attack
|
|
## stick (or a paired gamepad) attacks.
|
|
|
|
enum Stick { MOVE, ATTACK }
|
|
|
|
const MOVE_ON_RIGHT := true # the move stick's half of the screen; the attack stick has the other
|
|
const RADIUS := 150.0 # base to rim, in canvas pixels (the 1920-wide canvas)
|
|
## Per stick: its left, right, up (forward), and down actions.
|
|
const ACTIONS: Array = [[&"move_left", &"move_right", &"move_forward", &"move_back"],
|
|
[&"aim_left", &"aim_right", &"aim_up", &"aim_down"]]
|
|
const COLORS: Array[Color] = [UiTheme.TEXT, Color(1.0, 0.5, 0.38)]
|
|
|
|
var sim: FarmSim
|
|
## Per stick (Stick): the finger on it (-1: none), its base, and its tilt (length 0-1).
|
|
var finger: Array[int] = [-1, -1]
|
|
var base: Array[Vector2] = [Vector2.ZERO, Vector2.ZERO]
|
|
var tilt: Array[Vector2] = [Vector2.ZERO, Vector2.ZERO]
|
|
|
|
|
|
func setup(p_sim: FarmSim) -> void:
|
|
sim = p_sim
|
|
|
|
|
|
func _ready() -> void:
|
|
set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
|
|
mouse_filter = Control.MOUSE_FILTER_IGNORE # never in the way of the HUD's own buttons
|
|
for action: StringName in [&"fire", &"offhand"]:
|
|
for e in InputMap.action_get_events(action):
|
|
if e is InputEventMouseButton:
|
|
InputMap.action_erase_event(action, e)
|
|
if sim != null:
|
|
sim.combat.device = ScarecrowCombat.Device.GAMEPAD # aim along the stick, not at the last touch
|
|
|
|
|
|
func _notification(what: int) -> void:
|
|
if what == NOTIFICATION_PAUSED or (what == NOTIFICATION_VISIBILITY_CHANGED and not is_visible_in_tree()):
|
|
let_go()
|
|
|
|
|
|
func _process(_delta: float) -> void:
|
|
if (finger[Stick.MOVE] != -1 or finger[Stick.ATTACK] != -1) and not can_act():
|
|
let_go()
|
|
|
|
|
|
func _unhandled_input(event: InputEvent) -> void:
|
|
var touch := event as InputEventScreenTouch
|
|
if touch != null:
|
|
if touch.pressed:
|
|
var s := side_of(touch.position)
|
|
if not can_act() or finger[s] != -1:
|
|
return
|
|
finger[s] = touch.index
|
|
base[s] = touch.position
|
|
tilt[s] = Vector2.ZERO
|
|
_apply(s)
|
|
get_viewport().set_input_as_handled()
|
|
else:
|
|
for s in 2:
|
|
if finger[s] == touch.index:
|
|
_release(s)
|
|
get_viewport().set_input_as_handled()
|
|
return
|
|
var drag := event as InputEventScreenDrag
|
|
if drag == null:
|
|
return
|
|
for s in 2:
|
|
if finger[s] != drag.index:
|
|
continue
|
|
var d := drag.position - base[s]
|
|
if d.length() > RADIUS:
|
|
base[s] += d.normalized() * (d.length() - RADIUS)
|
|
d = drag.position - base[s]
|
|
tilt[s] = d / RADIUS
|
|
_apply(s)
|
|
get_viewport().set_input_as_handled()
|
|
|
|
|
|
## Whether the scarecrow can take orders now (see the class notes).
|
|
func can_act() -> bool:
|
|
return sim != null and is_visible_in_tree() and not sim.local_frozen and not sim.campaign.is_over()
|
|
|
|
|
|
## Which stick a finger landing at this point (canvas coordinates) starts.
|
|
func side_of(at: Vector2) -> Stick:
|
|
var right := at.x >= size.x * 0.5
|
|
return Stick.MOVE if right == MOVE_ON_RIGHT else Stick.ATTACK
|
|
|
|
|
|
## Lets both sticks go: their actions and fire released.
|
|
func let_go() -> void:
|
|
for s in 2:
|
|
if finger[s] != -1:
|
|
_release(s)
|
|
|
|
|
|
func _release(s: int) -> void:
|
|
finger[s] = -1
|
|
tilt[s] = Vector2.ZERO
|
|
_apply(s)
|
|
|
|
|
|
## Presses the stick's actions by its tilt (released at zero), and for the attack stick, fire.
|
|
func _apply(s: int) -> void:
|
|
var v := tilt[s]
|
|
var names: Array = ACTIONS[s]
|
|
_press(names[0], -v.x)
|
|
_press(names[1], v.x)
|
|
_press(names[2], -v.y)
|
|
_press(names[3], v.y)
|
|
if s == Stick.ATTACK:
|
|
var dead := sim.config.aim_dead_zone if sim != null else 0.3
|
|
if v.length() > dead:
|
|
Input.action_press(&"fire") # aiming (combat's stick reads past the same dead zone)
|
|
else:
|
|
Input.action_release(&"fire")
|
|
queue_redraw()
|
|
|
|
|
|
func _press(action: StringName, strength: float) -> void:
|
|
if strength > 0.0:
|
|
Input.action_press(action, minf(strength, 1.0))
|
|
else:
|
|
Input.action_release(action)
|
|
|
|
|
|
func _draw() -> void:
|
|
for s in 2:
|
|
if finger[s] == -1:
|
|
continue
|
|
var c := COLORS[s]
|
|
draw_circle(base[s], RADIUS, Color(c, 0.1))
|
|
Sketch.circle(self, base[s], RADIUS, Color(c, 0.5), 5.0, 1.0 + s)
|
|
draw_circle(base[s] + tilt[s] * RADIUS, RADIUS * 0.38, Color(c, 0.45))
|