godot-shooter / files / weapons / Bullet.gd
Bullet.gd
b856550a 2023-10-30 Pavel Safronov | 1 | extends Area2D |
f398dcb9 2023-11-02 Pavel Safronov | 2 | class_name Bullet |
b856550a 2023-10-30 Pavel Safronov | 3 | |
16938031 2023-11-17 Pavel Safronov | 4 | const Utils = preload("res://utils/Utils.gd") |
| 5 | const SmallExplosion = preload("res://effects/SmallExplosion.tscn") | |
ee6296b3 2023-11-17 Pavel Safronov | 6 | const Constants = preload("res://utils/Constants.gd") |
| 7 | ||
969fd5af 2023-11-06 Pavel Safronov | 8 | export var lifetime_seconds = 1.5 |
ee6296b3 2023-11-17 Pavel Safronov | 9 | export var speed = Constants.SPEED * 5 |
3fcb817e 2023-11-10 Pavel Safronov | 10 | export var damage_amount = 1 |
636e5224 2023-12-01 Pavel Safronov | 11 | export var shooter_rid = 0 |
614e7fb1 2023-10-31 Pavel Safronov | 12 | |
| 13 | func _ready() -> void: | |
| 14 | $Timer.wait_time = lifetime_seconds | |
| 15 | $Timer.start() | |
b856550a 2023-10-30 Pavel Safronov | 16 | |
| 17 | func _physics_process(delta: float) -> void: | |
| 18 | position += transform.y * speed * delta | |
614e7fb1 2023-10-31 Pavel Safronov | 19 | |
| 20 | func on_expiration(): | |
| 21 | queue_free() | |
b856550a 2023-10-30 Pavel Safronov | 22 | |
| 23 | func on_bullet_body_entered(body): | |
636e5224 2023-12-01 Pavel Safronov | 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: | |
0f8974b8 2023-12-01 Pavel Safronov | 27 | return |
| 28 | if body.has_method("hurt"): | |
3fcb817e 2023-11-10 Pavel Safronov | 29 | body.hurt(damage_amount) |
236d1677 2023-11-01 Pavel Safronov | 30 | var se = SmallExplosion.instance() |
| 31 | get_parent().add_child(se) | |
| 32 | se.transform = transform | |
b856550a 2023-10-30 Pavel Safronov | 33 | queue_free() |