Added projectile deletion

This commit is contained in:
2025-11-03 20:20:29 +03:00
parent b22f6c44dd
commit 832db6f531
10 changed files with 116 additions and 15 deletions
@@ -2,6 +2,13 @@ class_name AbstractProjectile
extends CharacterBody2D
const PLAYER_LAYER = 2
const ENEMY_LAYER = 4
const PLAYER_PROJECTILE_LAYER = 8
const ENEMY_PROJECTILE_LAYER = 16
const PROJECTILE_BORDER_LAYER = 32
signal destroyed
@@ -12,6 +19,20 @@ signal destroyed
@export var max_distance : int
@export var max_livetime : int
@export var collide_player: bool:
set(value):
collide_player = value
_apply_collision_mask()
get:
return collide_player
@export var collide_enemies: bool:
set(value):
collide_enemies = value
_apply_collision_mask()
get:
return collide_enemies
var _traveled_distance: float
var _livetime: float
@@ -19,10 +40,14 @@ var _livetime: float
func _ready() -> void:
velocity = direction.normalized() * speed
_apply_collision_mask()
func move(delta: float) -> void:
position += velocity * delta
func _physics_process(_delta: float) -> void:
var was_collided := move_and_slide()
if was_collided:
destroyed.emit()
queue_free()
func process_acceleration(delta: float) -> void:
@@ -46,3 +71,21 @@ func process_livetime(delta: float) -> void:
_livetime += delta
if _livetime > max_livetime:
destroyed.emit()
func _apply_collision_mask() -> void:
collision_mask |= PROJECTILE_BORDER_LAYER
if collide_player:
collision_layer |= ENEMY_PROJECTILE_LAYER
collision_mask |= PLAYER_LAYER
else:
collision_layer &= ~ENEMY_PROJECTILE_LAYER
collision_mask &= ~PLAYER_LAYER
if collide_enemies:
collision_layer |= PLAYER_PROJECTILE_LAYER
collision_mask |= ENEMY_LAYER
else:
collision_layer &= ~PLAYER_PROJECTILE_LAYER
collision_mask &= ~ENEMY_LAYER