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 left with the Swap sticks setting, move_on_right): the move_* ## actions, as far as the stick is tilted. Screen up is forward, as with WASD. ## * Attack (the other 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, as do ## TouchButtons'), and only while the scarecrow can act: not paused (menus, conversations), not ## frozen (the intro), not once the vacation is over. A finger on a stick keeps it wherever it ## slides, over a button too (its drags and its lift are read before the GUI's). A pause or freeze ## lets both sticks go. Each stick is drawn only while a finger is on it. ## Settings: in group &"touch_controls" with TouchButtons; apply_settings() takes the Swap sticks ## setting (GameSettings.swap_sticks), and move_on_right_now() is the side in play (Prompts' tips). ## 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 by default (the user's call); the attack stick has the other ## The touch controls' group: GameSettings.apply calls apply_settings on each (#41). const GROUP := &"touch_controls" 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 ## The move stick's half: the right (MOVE_ON_RIGHT), or the left with Swap sticks (apply_settings). var move_on_right := MOVE_ON_RIGHT ## 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 ## The move stick's side in play: the move_on_right of the TouchSticks in the tree, else ## MOVE_ON_RIGHT (desktop, or before Main has added them). static func move_on_right_now() -> bool: var tree := Engine.get_main_loop() as SceneTree if tree != null: for n in tree.get_nodes_in_group(GROUP): if n is TouchSticks: return (n as TouchSticks).move_on_right return MOVE_ON_RIGHT ## The settings contract (group &"touch_controls"): Swap sticks puts the move stick on the left. ## A stick held while it changes lets go. func apply_settings(s: GameSettings) -> void: var right := MOVE_ON_RIGHT != s.swap_sticks if right != move_on_right: let_go() move_on_right = right func _ready() -> void: add_to_group(GROUP) 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() ## A finger already on a stick: its drags and its lift, before the GUI sees them (so a button it ## slides over neither takes them nor stops the stick). func _input(event: InputEvent) -> void: var drag := event as InputEventScreenDrag if drag != null: for s in 2: if finger[s] == drag.index: _drag(s, drag.position) get_viewport().set_input_as_handled() return var touch := event as InputEventScreenTouch if touch != null and not touch.pressed: for s in 2: if finger[s] == touch.index: _release(s) get_viewport().set_input_as_handled() ## A finger landing where nothing else took it starts its half's stick. func _unhandled_input(event: InputEvent) -> void: var touch := event as InputEventScreenTouch if touch == null or not touch.pressed: return 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() func _drag(s: int, at: Vector2) -> void: var d := at - base[s] if d.length() > RADIUS: base[s] += d.normalized() * (d.length() - RADIUS) d = at - base[s] tilt[s] = d / RADIUS _apply(s) ## 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))