godot-shooter / meteor / files / Bullet.gd
Branch meteor · default branch · all branches
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 | |
236d1677 2023-11-01 Pavel Safronov | 4 | const Utils = preload("res://Utils.gd") |
| 5 | const SmallExplosion = preload("res://SmallExplosion.tscn") | |
f398dcb9 2023-11-02 Pavel Safronov | 6 | export var lifetime_seconds = .75 |
c727f3b0 2023-10-31 Pavel Safronov | 7 | export var speed = 300 |
614e7fb1 2023-10-31 Pavel Safronov | 8 | |
| 9 | func _ready() -> void: | |
| 10 | $Timer.wait_time = lifetime_seconds | |
| 11 | $Timer.start() | |
b856550a 2023-10-30 Pavel Safronov | 12 | |
| 13 | func _physics_process(delta: float) -> void: | |
| 14 | position += transform.y * speed * delta | |
614e7fb1 2023-10-31 Pavel Safronov | 15 | |
| 16 | func on_expiration(): | |
| 17 | queue_free() | |
b856550a 2023-10-30 Pavel Safronov | 18 | |
| 19 | func on_bullet_body_entered(body): | |
| 20 | var tree = get_tree() | |
| 21 | var players = tree.get_nodes_in_group("player") | |
| 22 | var player = players[0] | |
| 23 | if body.has_method("hurt") && body != player: | |
| 24 | body.hurt() | |
236d1677 2023-11-01 Pavel Safronov | 25 | var se = SmallExplosion.instance() |
| 26 | get_parent().add_child(se) | |
| 27 | se.transform = transform | |
b856550a 2023-10-30 Pavel Safronov | 28 | queue_free() |