-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LaserBeam.gd
73 lines (52 loc) · 1.75 KB
/
LaserBeam.gd
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
extends RayCast2D
export var cast_speed := 7000.0
export var max_length := 1400
export var growth_time := 0.1
var is_casting := false setget set_is_casting
onready var fill := $FillLine2D
onready var tween := $Tween
onready var casting_particles := $CastingParticles2D
onready var collision_particles := $CollisionParticles2D
onready var beam_particles := $BeamParticles2D
onready var line_width: float = fill.width
func _ready() -> void:
set_physics_process(false)
fill.points[1] = Vector2.ZERO
func _physics_process(delta: float) -> void:
cast_to = (cast_to + Vector2.RIGHT * cast_speed * delta).clamped(max_length)
cast_beam()
func set_is_casting(cast: bool) -> void:
is_casting = cast
if is_casting:
cast_to = Vector2.ZERO
fill.points[1] = cast_to
appear()
else:
collision_particles.emitting = false
disappear()
set_physics_process(is_casting)
beam_particles.emitting = is_casting
casting_particles.emitting = is_casting
func cast_beam() -> void:
var cast_point := cast_to
force_raycast_update()
collision_particles.emitting = is_colliding()
if is_colliding():
cast_point = to_local(get_collision_point())
collision_particles.process_material.direction = Vector3(
get_collision_normal().x, get_collision_normal().y, 0
)
collision_particles.position = cast_point
fill.points[1] = cast_point
beam_particles.position = cast_point * 0.5
beam_particles.process_material.emission_box_extents.x = cast_point.length() * 0.5
func appear() -> void:
if tween.is_active():
tween.stop_all()
tween.interpolate_property(fill, "width", 0, line_width, growth_time * 2)
tween.start()
func disappear() -> void:
if tween.is_active():
tween.stop_all()
tween.interpolate_property(fill, "width", fill.width, 0, growth_time)
tween.start()