godot-shooter / files / weapons / Bullet.gd
Bullet.gd
| 1 | extends Area2D |
| 2 | class_name Bullet |
| 3 | |
| 4 | const Utils = preload("res://utils/Utils.gd") |
| 5 | const SmallExplosion = preload("res://effects/SmallExplosion.tscn") |
| 6 | const Constants = preload("res://utils/Constants.gd") |
| 7 | |
| 8 | export var lifetime_seconds = 1.5 |
| 9 | export var speed = Constants.SPEED * 5 |
| 10 | export var damage_amount = 1 |
| 11 | export var shooter_rid = 0 |
| 12 | |
| 13 | func _ready() -> void: |
| 14 | $Timer.wait_time = lifetime_seconds |
| 15 | $Timer.start() |
| 16 | |
| 17 | func _physics_process(delta: float) -> void: |
| 18 | position += transform.y * speed * delta |
| 19 | |
| 20 | func on_expiration(): |
| 21 | queue_free() |
| 22 | |
| 23 | func 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() |