-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
test.gd
134 lines (88 loc) · 2.64 KB
/
test.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
class_name Test
extends Node
signal wait_done()
export var _enable_debug_collision = true
var _timer
var _timer_started = false
var _wait_physics_ticks_counter = 0
var _drawn_nodes = []
func _enter_tree():
if not _enable_debug_collision:
get_tree().debug_collisions_hint = false
func _physics_process(_delta):
if _wait_physics_ticks_counter > 0:
_wait_physics_ticks_counter -= 1
if _wait_physics_ticks_counter == 0:
emit_signal("wait_done")
func add_sphere(pos, radius, color):
var sphere = MeshInstance.new()
var sphere_mesh = SphereMesh.new()
sphere_mesh.radius = radius
sphere_mesh.height = radius * 2.0
sphere.mesh = sphere_mesh
var material = SpatialMaterial.new()
material.flags_unshaded = true
material.albedo_color = color
sphere.material_override = material
_drawn_nodes.push_back(sphere)
add_child(sphere)
sphere.global_transform.origin = pos
func add_shape(shape, transform, color):
var body = StaticBody.new()
body.collision_layer = 0
body.collision_mask = 0
var collision = CollisionShape.new()
collision.transform = transform
collision.shape = shape
body.add_child(collision)
add_child(body)
_drawn_nodes.push_back(body)
call_deferred("initialize_shape_material", body, color)
func initialize_shape_material(body, color):
var mesh_instance = body.get_child(1)
var material = SpatialMaterial.new()
material.flags_unshaded = true
material.albedo_color = color
mesh_instance.material_override = material
func clear_drawn_nodes():
for node in _drawn_nodes:
remove_child(node)
node.queue_free()
_drawn_nodes.clear()
func create_rigidbody(shape, pickable = false, transform = Transform.IDENTITY):
var collision = CollisionShape.new()
collision.shape = shape
collision.transform = transform
var body = RigidBody.new()
body.add_child(collision)
if pickable:
var script = load("res://utils/rigidbody_pick.gd")
body.set_script(script)
return body
func create_rigidbody_box(size, pickable = false, transform = Transform.IDENTITY):
var shape = BoxShape.new()
shape.extents = 0.5 * size
return create_rigidbody(shape, pickable, transform)
func start_timer(timeout):
if _timer == null:
_timer = Timer.new()
_timer.one_shot = true
add_child(_timer)
_timer.connect("timeout", self, "_on_timer_done")
else:
cancel_timer()
_timer.start(timeout)
_timer_started = true
return _timer
func cancel_timer():
if _timer_started:
_timer.paused = true
_timer.emit_signal("timeout")
_timer.paused = false
func is_timer_canceled():
return _timer.paused
func wait_for_physics_ticks(tick_count):
_wait_physics_ticks_counter = tick_count
return self
func _on_timer_done():
_timer_started = false