fuzzyworld

godot-shooter / asteroids / files / weapons / Bullet.gd

Branch asteroids · default branch · all branches

Bullet.gd

881 B · history · blame

1extends Area2D
2class_name Bullet
3
4const Utils = preload("res://utils/Utils.gd")
5const SmallExplosion = preload("res://effects/SmallExplosion.tscn")
6const Constants = preload("res://utils/Constants.gd")
7
8export var lifetime_seconds = 1.5
9export var speed = Constants.SPEED * 5
10export var damage_amount = 1
11export var shooter_rid = 0
12
13func _ready() -> void:
14 $Timer.wait_time = lifetime_seconds
15 $Timer.start()
16
17func _physics_process(delta: float) -> void:
18 position += transform.y * speed * delta
19
20func on_expiration():
21 queue_free()
22
23func on_bullet_body_entered(body):
24 var body_id = (body as Node2D).get_instance_id()
25 var body_is_shooter = shooter_rid != 0 and shooter_rid == body_id
26 if body_is_shooter:
27 return
28 if body.has_method("hurt"):
29 body.hurt(damage_amount)
30 var se = SmallExplosion.instance()
31 get_parent().add_child(se)
32 se.transform = transform
33 queue_free()