summaryrefslogtreecommitdiff
path: root/Player/bow.gd
blob: aa3fafc54e0a75ce376733cd6bb2f012c4c8710a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
extends Node2D


var anchor := Vector2.ZERO
var anchor_mode := false
var drag_distance := Vector2.ZERO
var max_length := 75.0

var arrow_scene = preload("res://Player/arrow.tscn")
var arrow: Node2D

var process_touch := false


func _ready() -> void:
	if OS.has_feature("mobile"):
		scale = Vector2(2, 2)
	
	$DrawLine.visible = false
	$TargetLine.visible = false


func _input(event: InputEvent) -> void:
	if event is InputEventScreenTouch:
		if event.is_pressed():
			process_touch = true
		elif event.is_released():
			process_touch = false


func _process(_delta: float) -> void:
	if Input.is_action_just_pressed("click"):
		global_position = get_global_mouse_position()
		anchor = get_global_mouse_position()
		anchor_mode = true
		
		arrow = arrow_scene.instantiate()
		arrow.global_position = global_position
		
		get_tree().current_scene.add_child(arrow)
		$DrawLine.visible = true
		
		$TargetLine.visible = true
		$TargetLine.origin = anchor
		$TargetLine.queue_redraw()
	
	elif Input.is_action_just_released("click"):
		anchor_mode = false
		
		arrow.velocity = -drag_distance * 1.5
		arrow.flying = true
		arrow = null
		
		$DrawLine.visible = false
		$DrawLine.clear_points()
		
		$TargetLine.visible = false
		$TargetLine.queue_redraw()
	
	elif Input.is_action_pressed("click"):
		drag_distance = get_global_mouse_position() - anchor
		rotation = atan2(drag_distance.y, drag_distance.x)
		
		var max_distance = get_global_mouse_position()
		if drag_distance.length() > max_length:
			drag_distance = (max_length / drag_distance.length()) * drag_distance
			max_distance = anchor + drag_distance
		
		$DrawLine.clear_points()
		$DrawLine.add_point(Vector2.ZERO)
		$DrawLine.add_point((max_distance - global_position) / scale)
		$DrawLine.rotation = -rotation
		
		$TargetLine.direction = (max_distance - global_position).normalized() * -1
		$TargetLine.rotation = -rotation
		$TargetLine.queue_redraw()
	
	if not anchor_mode:
		if OS.has_feature("mobile"):
			if process_touch:
				global_position = get_global_mouse_position()
		else:
			global_position = get_global_mouse_position()
	
	if is_instance_valid(arrow):
		arrow.rotation = rotation