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
@@ -3,5 +3,7 @@
[ext_resource type="Script" uid="uid://ctmjb3nkxrepu" path="res://game/entities/weapons/projectiles/abstract_projectile.gd" id="1_4tgfk"]
[node name="AbstractProjectile" type="CharacterBody2D"]
collision_layer = 0
collision_mask = 0
motion_mode = 1
script = ExtResource("1_4tgfk")
@@ -7,7 +7,3 @@ func _ready() -> void:
$Sprite2D.texture = texture
super._ready()
func _physics_process(delta: float) -> void:
move(delta)
@@ -1,11 +1,16 @@
[gd_scene load_steps=3 format=3 uid="uid://cnoiv8hdgossf"]
[gd_scene load_steps=4 format=3 uid="uid://cnoiv8hdgossf"]
[ext_resource type="Script" uid="uid://rtsf1n0djorp" path="res://game/entities/weapons/projectiles/gatling_projectile.gd" id="1_xq7oi"]
[sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_xq7oi"]
size = Vector2(4, 4)
[sub_resource type="CircleShape2D" id="CircleShape2D_xq7oi"]
radius = 1.0
[node name="GatlingProjectile" type="CharacterBody2D"]
collision_layer = 0
collision_mask = 0
motion_mode = 1
script = ExtResource("1_xq7oi")
damage = 6
@@ -16,3 +21,4 @@ metadata/_custom_type_script = "uid://ctmjb3nkxrepu"
texture = SubResource("PlaceholderTexture2D_xq7oi")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("CircleShape2D_xq7oi")