-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClock.gd
More file actions
42 lines (33 loc) · 1.12 KB
/
Clock.gd
File metadata and controls
42 lines (33 loc) · 1.12 KB
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
extends CharacterBody2D
@export var speed = 250.0
func _ready():
velocity = Vector2(speed, speed)
func _physics_process(delta):
if get_tree().paused:
return
if get_last_slide_collision():
var collision: KinematicCollision2D = get_last_slide_collision()
if collision.get_collider() and collision.get_collider().is_in_group("bullet"):
collision.get_collider().queue_free()
elif collision.get_collider():
#print(collision.get_collider().name)
hit(collision.get_collider())
velocity = velocity.bounce(clamp_to_1bit(collision.get_normal()))
$CollisionFX.play()
move_and_slide()
func clamp_to_1bit(normal: Vector2):
var normals = [Vector2(1, 0), Vector2(0, 1), Vector2(-1, 0), Vector2(0, -1)]
var closest_normal = normals[0]
var max_dot_product = normal.dot(closest_normal)
for i in range(1, normals.size()):
var dot_product = normal.dot(normals[i])
if dot_product > max_dot_product:
max_dot_product = dot_product
closest_normal = normals[i]
return closest_normal
func hit(body):
if body.is_in_group("destructible"):
if body.has_method("destroy"):
body.destroy()
else:
body.queue_free()