-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
112 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
[gd_scene load_steps=6 format=3 uid="uid://crd0d1dxajbfi"] | ||
|
||
[ext_resource type="Script" path="res://UI/joystick.gd" id="1_0lls8"] | ||
|
||
[sub_resource type="Gradient" id="Gradient_l1esd"] | ||
offsets = PackedFloat32Array(0.543544, 0.54955) | ||
colors = PackedColorArray(1, 1, 1, 1, 0, 0, 0, 0) | ||
|
||
[sub_resource type="GradientTexture2D" id="GradientTexture2D_beaca"] | ||
gradient = SubResource("Gradient_l1esd") | ||
fill = 1 | ||
fill_from = Vector2(0.5, 0.5) | ||
|
||
[sub_resource type="Gradient" id="Gradient_04c1n"] | ||
offsets = PackedFloat32Array(0.552553, 0.555556, 0.606607, 0.612613) | ||
colors = PackedColorArray(0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0) | ||
|
||
[sub_resource type="GradientTexture2D" id="GradientTexture2D_6y6lu"] | ||
gradient = SubResource("Gradient_04c1n") | ||
width = 256 | ||
height = 256 | ||
fill = 1 | ||
fill_from = Vector2(0.5, 0.5) | ||
|
||
[node name="Joystick" type="Node2D"] | ||
script = ExtResource("1_0lls8") | ||
|
||
[node name="Knob" type="Sprite2D" parent="."] | ||
texture = SubResource("GradientTexture2D_beaca") | ||
|
||
[node name="Ring" type="Sprite2D" parent="."] | ||
texture = SubResource("GradientTexture2D_6y6lu") | ||
|
||
[node name="Button" type="Button" parent="."] | ||
modulate = Color(1, 1, 1, 0) | ||
anchors_preset = 8 | ||
anchor_left = 0.5 | ||
anchor_top = 0.5 | ||
anchor_right = 0.5 | ||
anchor_bottom = 0.5 | ||
offset_left = -128.0 | ||
offset_top = -128.0 | ||
offset_right = 128.0 | ||
offset_bottom = 128.0 | ||
grow_horizontal = 2 | ||
grow_vertical = 2 | ||
|
||
[connection signal="button_down" from="Button" to="." method="_on_button_button_down"] | ||
[connection signal="button_up" from="Button" to="." method="_on_button_button_up"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
extends Node2D | ||
|
||
@onready var knob = $Knob | ||
|
||
var pressing := false | ||
var posVector : Vector2 | ||
var show_debug := false | ||
|
||
@export var maxLength = 64 | ||
@export var deadzone = 5 | ||
|
||
# Called when the node enters the scene tree for the first time. | ||
func _ready(): | ||
maxLength *= scale.x | ||
visible = OS.has_feature("mobile") or show_debug | ||
set_process(visible) | ||
|
||
# Called every frame. 'delta' is the elapsed time since the previous frame. | ||
func _process(delta): | ||
|
||
if pressing: | ||
if get_global_mouse_position().distance_to(global_position) <= maxLength: | ||
knob.global_position = get_global_mouse_position() | ||
else: | ||
var angle = global_position.angle_to_point(get_global_mouse_position()) | ||
knob.global_position.x = global_position.x + cos(angle)*maxLength | ||
knob.global_position.y = global_position.y + sin(angle)*maxLength | ||
calculateVector() | ||
else: | ||
knob.global_position = lerp(knob.global_position, global_position, delta*10) | ||
posVector = Vector2.ZERO | ||
GameManager.game_state["joystick"] = posVector | ||
|
||
func calculateVector(): | ||
if abs((knob.global_position.x - global_position.x)) - deadzone: | ||
posVector.x = (knob.global_position.x - global_position.x)/maxLength | ||
if abs((knob.global_position.y - global_position.y)) - deadzone: | ||
posVector.y = (knob.global_position.y - global_position.y)/maxLength | ||
|
||
func _on_button_button_down(): | ||
pressing = true | ||
|
||
func _on_button_button_up(): | ||
pressing = false | ||
|
||
|
||
func _input(event): | ||
if event.is_action_pressed("terminal"): | ||
show_debug = not show_debug; | ||
if show_debug: | ||
visible = true | ||
set_process(true) | ||
else: | ||
posVector = Vector2.ZERO | ||
GameManager.game_state.erase("joystick") |