Skip to content

Commit

Permalink
Add Joystick
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon32 committed Aug 20, 2024
1 parent fc0af74 commit f3cc4a7
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Characters/Hero/hero.gd
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ func get_input():
#var run := Input.is_action_pressed("run")
#var attack := Input.is_action_pressed("attack")
var input_direction := Input.get_vector("left", "right", "up", "down")
if GameManager.game_state.has("joystick"):
input_direction = GameManager.game_state["joystick"]
var rest = input_direction == Vector2.ZERO
var interact := Input.is_action_pressed("interact")
var interact2 := Input.is_action_pressed("interact2")
Expand Down
2 changes: 1 addition & 1 deletion Menus/MainMenu.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ scene_path = "res://Menus/TestsMenu.tscn"
layout_mode = 2
text = "Quit"

[node name="Gmtk2024-logo" type="Sprite2D" parent="."]
[node name="Gmtk2024-logo" type="Sprite2D" parent="Panel"]
position = Vector2(1016.4, 567.125)
scale = Vector2(0.127493, 0.127493)
texture = ExtResource("4_xf6fx")
Expand Down
49 changes: 49 additions & 0 deletions UI/Joystick.tscn
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"]
6 changes: 5 additions & 1 deletion UI/UI.tscn
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[gd_scene load_steps=4 format=3 uid="uid://c4f4xoibdq0s3"]
[gd_scene load_steps=5 format=3 uid="uid://c4f4xoibdq0s3"]

[ext_resource type="Script" path="res://UI/progress_bar.gd" id="1_0ykeg"]
[ext_resource type="Theme" uid="uid://bewgrgj1ylev2" path="res://Menus/MenuTheme.tres" id="1_1l0h6"]
[ext_resource type="Script" path="res://Scripts/Debug.gd" id="2_i4ly0"]
[ext_resource type="PackedScene" uid="uid://crd0d1dxajbfi" path="res://UI/Joystick.tscn" id="4_cyln3"]

[node name="UI" type="CanvasLayer"]

Expand Down Expand Up @@ -50,3 +51,6 @@ text = "Debug"
horizontal_alignment = 2
script = ExtResource("2_i4ly0")
metadata/_edit_use_anchors_ = true

[node name="Joystick" parent="." instance=ExtResource("4_cyln3")]
position = Vector2(1028, 525)
55 changes: 55 additions & 0 deletions UI/joystick.gd
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")

0 comments on commit f3cc4a7

Please sign in to comment.