LaserWeapon reworked to hitscan

This commit is contained in:
2025-12-22 16:45:22 +03:00
parent 77981194dd
commit e5155584dd
23 changed files with 190 additions and 174 deletions
@@ -5,3 +5,4 @@
[node name="DirectHitProjectile" instance=ExtResource("1_klynw")]
script = ExtResource("2_0vgnq")
damage = null
+1 -1
View File
@@ -47,7 +47,7 @@ func _on_out_of_screen_timer_timeout() -> void:
delete()
func _try_to_damage(body: Node2D, damage: AbstractDamage) -> bool:
static func _try_to_damage(body: Node2D, damage: AbstractDamage) -> bool:
var health_component : Health = body.find_child("Health")
if health_component and health_component.has_method("apply_damage"):
health_component.apply_damage(damage)
@@ -1,19 +0,0 @@
class_name AbstractLaserProjectile
extends AbstractDirectHitProjectile
@onready var particles : GPUParticles2D = $GPUParticles2D
func _ready() -> void:
super._ready()
_update_sprite(_velocity)
func _update_sprite(velocity: Vector2) -> void:
var angle := posmod(floor(rad_to_deg(velocity.angle())), 360)
if angle > 90 and angle < 270:
particles.process_material.direction = Vector3.RIGHT
else:
particles.process_material.direction = Vector3.LEFT
@@ -1 +0,0 @@
uid://bdxq4aflhc8vd
@@ -1,43 +0,0 @@
[gd_scene load_steps=8 format=3 uid="uid://cmni0xrbbfcy5"]
[ext_resource type="PackedScene" uid="uid://cdv5n4t47hr8i" path="res://game/entities/weapons/abstract_direct_hit_projectile.tscn" id="1_3a8fg"]
[ext_resource type="Script" uid="uid://bdxq4aflhc8vd" path="res://game/entities/weapons/laser/abstract_laser_projectile.gd" id="2_je1a2"]
[ext_resource type="Script" uid="uid://c27v705giygv4" path="res://game/data/damage/energy_damage.gd" id="3_ylokk"]
[ext_resource type="Texture2D" uid="uid://dk3t14mrgjmma" path="res://particle_textures/energy_medium.tres" id="4_bytws"]
[sub_resource type="Resource" id="Resource_bytws"]
script = ExtResource("3_ylokk")
value = 2
metadata/_custom_type_script = "uid://c27v705giygv4"
[sub_resource type="CircleShape2D" id="CircleShape2D_ylokk"]
radius = 1.0
[sub_resource type="ParticleProcessMaterial" id="ParticleProcessMaterial_7dd03"]
resource_local_to_scene = true
particle_flag_disable_z = true
emission_shape = 3
emission_box_extents = Vector3(8, 0, 1)
angle_min = -179.99998
angle_max = 180.00002
inherit_velocity_ratio = 1.0
direction = Vector3(0, 0, 0)
spread = 0.0
gravity = Vector3(0, 0, 0)
[node name="AbstractLaserProjectile" instance=ExtResource("1_3a8fg")]
collision_layer = 0
collision_mask = 0
script = ExtResource("2_je1a2")
damage = SubResource("Resource_bytws")
speed = 500
[node name="CollisionShape2D" parent="." index="0"]
shape = SubResource("CircleShape2D_ylokk")
[node name="GPUParticles2D" type="GPUParticles2D" parent="." index="2"]
amount = 16
texture = ExtResource("4_bytws")
lifetime = 0.09999999999999999
fixed_fps = 20
process_material = SubResource("ParticleProcessMaterial_7dd03")
@@ -2,21 +2,46 @@ class_name AbstractLaserWeapon
extends AbstractWeapon
@export var damage : AbstractDamage
@export_range(-100.0, 100.0) var line_texture_speed := 7.0
@onready var sprite : AnimatedSprite2D = $AnimatedSprite2D
@onready var cooldown_timer : Timer = $CooldownTimer
@onready var ray_cast : RayCast2D = $RayCast2D
@onready var line : Line2D = $Line2D
@onready var hit_particles : GPUParticles2D = $HitParticles
func _ready() -> void:
sprite.play(SHOT_ANIMATION)
func shoot(ship_velocity: Vector2) -> bool:
var is_shot := super.shoot(ship_velocity)
if is_shot:
_can_shoot = false
cooldown_timer.start()
func _process(_delta: float) -> void:
var collision_point: Vector2
ray_cast.force_raycast_update()
if ray_cast.is_colliding():
collision_point = ray_cast.get_collision_point() - global_position
hit_particles.position = collision_point
hit_particles.show()
else:
collision_point = muzzle.position + ray_cast.target_position
hit_particles.hide()
return is_shot
line.clear_points()
line.add_point(muzzle.position)
line.add_point(collision_point)
func shoot(_ship_velocity: Vector2) -> bool:
if not _can_shoot: return false
if ray_cast.is_colliding():
AbstractProjectile._try_to_damage(ray_cast.get_collider(), damage)
_can_shoot = false
cooldown_timer.start()
return true
func _on_cooldown_timer_timeout() -> void:
@@ -1,21 +1,57 @@
[gd_scene load_steps=4 format=3 uid="uid://def1alrel4ioo"]
[gd_scene load_steps=11 format=3 uid="uid://def1alrel4ioo"]
[ext_resource type="PackedScene" uid="uid://1o2ta17yc5bp" path="res://game/entities/weapons/abstract_weapon.tscn" id="1_pki4x"]
[ext_resource type="Script" uid="uid://bxr150at8ul2a" path="res://game/entities/weapons/laser/abstract_laser_weapon.gd" id="2_lbdvb"]
[ext_resource type="Script" uid="uid://c27v705giygv4" path="res://game/data/damage/energy_damage.gd" id="3_2cxn5"]
[ext_resource type="Shader" uid="uid://bndty4otyxb5t" path="res://game/shaders/laser.gdshader" id="4_dsgqp"]
[ext_resource type="Texture2D" uid="uid://b13pk3rgmggc3" path="res://images/projectiles/laser.png" id="4_sanrs"]
[ext_resource type="Texture2D" uid="uid://dk3t14mrgjmma" path="res://particle_textures/energy_medium.tres" id="6_70bnd"]
[sub_resource type="Resource" id="Resource_sanrs"]
script = ExtResource("3_2cxn5")
value = 2
metadata/_custom_type_script = "uid://c27v705giygv4"
[sub_resource type="ShaderMaterial" id="ShaderMaterial_70bnd"]
shader = ExtResource("4_dsgqp")
shader_parameter/speed = 10.0
[sub_resource type="SpriteFrames" id="SpriteFrames_btyhf"]
[sub_resource type="ParticleProcessMaterial" id="ParticleProcessMaterial_12kuh"]
particle_flag_disable_z = true
spread = 180.0
initial_velocity_min = 5.0
initial_velocity_max = 15.0
gravity = Vector3(0, 0, 0)
[node name="AbstractLaserWeapon" instance=ExtResource("1_pki4x")]
script = ExtResource("2_lbdvb")
damage = SubResource("Resource_sanrs")
line_texture_speed = 7.0
bullet_per_shot = 2
type = 2
[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="." index="0"]
[node name="Line2D" type="Line2D" parent="." index="0"]
material = SubResource("ShaderMaterial_70bnd")
width = 4.0
texture = ExtResource("4_sanrs")
texture_mode = 1
[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="." index="1"]
sprite_frames = SubResource("SpriteFrames_btyhf")
[node name="CooldownTimer" type="Timer" parent="." index="1"]
[node name="CooldownTimer" type="Timer" parent="." index="2"]
process_callback = 0
wait_time = 0.05
one_shot = true
[node name="RayCast2D" type="RayCast2D" parent="." index="4"]
collision_mask = 0
[node name="HitParticles" type="GPUParticles2D" parent="." index="5"]
texture = ExtResource("6_70bnd")
lifetime = 0.2
process_material = SubResource("ParticleProcessMaterial_12kuh")
[connection signal="timeout" from="CooldownTimer" to="." method="_on_cooldown_timer_timeout"]
@@ -1,2 +0,0 @@
class_name EnemyLaserProjectile
extends AbstractLaserProjectile
@@ -1 +0,0 @@
uid://1tw0n0y7c3n4
@@ -1,25 +0,0 @@
[gd_scene load_steps=4 format=3 uid="uid://cq8vnjrk70iby"]
[ext_resource type="PackedScene" uid="uid://cmni0xrbbfcy5" path="res://game/entities/weapons/laser/abstract_laser_projectile.tscn" id="1_6hudn"]
[ext_resource type="Script" uid="uid://1tw0n0y7c3n4" path="res://game/entities/weapons/laser/enemy_laser_projectile.gd" id="2_2l4gg"]
[sub_resource type="ParticleProcessMaterial" id="ParticleProcessMaterial_2l4gg"]
resource_local_to_scene = true
particle_flag_disable_z = true
emission_shape = 3
emission_box_extents = Vector3(16, 0, 1)
angle_min = -179.99998
angle_max = 180.00002
inherit_velocity_ratio = 1.0
direction = Vector3(-1, 0, 0)
spread = 0.0
gravity = Vector3(0, 0, 0)
[node name="EnemyLaserProjectile" instance=ExtResource("1_6hudn")]
collision_layer = 16
collision_mask = 2
script = ExtResource("2_2l4gg")
direction = Vector2(-1, 0)
[node name="GPUParticles2D" parent="." index="2"]
process_material = SubResource("ParticleProcessMaterial_2l4gg")
@@ -1,8 +1,7 @@
[gd_scene load_steps=10 format=3 uid="uid://mjt8xj8k28vi"]
[gd_scene load_steps=9 format=3 uid="uid://mjt8xj8k28vi"]
[ext_resource type="PackedScene" uid="uid://def1alrel4ioo" path="res://game/entities/weapons/laser/abstract_laser_weapon.tscn" id="1_wwqbk"]
[ext_resource type="Script" uid="uid://bckua73myf5v7" path="res://game/entities/weapons/laser/enemy_laser_weapon.gd" id="2_preya"]
[ext_resource type="PackedScene" uid="uid://cq8vnjrk70iby" path="res://game/entities/weapons/laser/enemy_laser_projectile.tscn" id="3_yr2si"]
[ext_resource type="Texture2D" uid="uid://6hh66k8s4a1e" path="res://images/ships/weapons.png" id="4_a3m6t"]
[sub_resource type="AtlasTexture" id="AtlasTexture_dggsl"]
@@ -43,12 +42,15 @@ animations = [{
[node name="EnemyLaserWeapon" instance=ExtResource("1_wwqbk")]
script = ExtResource("2_preya")
projectile_scene = ExtResource("3_yr2si")
[node name="AnimatedSprite2D" parent="." index="0"]
[node name="AnimatedSprite2D" parent="." index="1"]
position = Vector2(-3, 0)
sprite_frames = SubResource("SpriteFrames_ga5wd")
animation = &"shot"
[node name="Muzzle" parent="." index="2"]
[node name="Muzzle" parent="." index="3"]
position = Vector2(-5, 0)
[node name="RayCast2D" parent="." index="4"]
target_position = Vector2(-1000, 0)
collision_mask = 2
@@ -1,2 +0,0 @@
class_name PlayerLaserProjectile
extends AbstractLaserProjectile
@@ -1 +0,0 @@
uid://v7pcdd8yxdse
@@ -1,24 +0,0 @@
[gd_scene load_steps=4 format=3 uid="uid://cyskk1kc88lgf"]
[ext_resource type="PackedScene" uid="uid://cmni0xrbbfcy5" path="res://game/entities/weapons/laser/abstract_laser_projectile.tscn" id="1_q67uk"]
[ext_resource type="Script" uid="uid://v7pcdd8yxdse" path="res://game/entities/weapons/laser/player_laser_projectile.gd" id="2_truhp"]
[sub_resource type="ParticleProcessMaterial" id="ParticleProcessMaterial_truhp"]
resource_local_to_scene = true
particle_flag_disable_z = true
emission_shape = 3
emission_box_extents = Vector3(16, 0, 1)
angle_min = -179.99998
angle_max = 180.00002
inherit_velocity_ratio = 1.0
spread = 0.0
gravity = Vector3(0, 0, 0)
[node name="PlayerLaserProjectile" instance=ExtResource("1_q67uk")]
collision_layer = 8
collision_mask = 4
script = ExtResource("2_truhp")
direction = Vector2(1, 0)
[node name="GPUParticles2D" parent="." index="2"]
process_material = SubResource("ParticleProcessMaterial_truhp")
@@ -1,8 +1,7 @@
[gd_scene load_steps=10 format=3 uid="uid://cargptjtnale2"]
[gd_scene load_steps=9 format=3 uid="uid://cargptjtnale2"]
[ext_resource type="PackedScene" uid="uid://def1alrel4ioo" path="res://game/entities/weapons/laser/abstract_laser_weapon.tscn" id="1_nky8c"]
[ext_resource type="Script" uid="uid://824ta0lbinkb" path="res://game/entities/weapons/laser/player_laser_weapon.gd" id="2_oqbns"]
[ext_resource type="PackedScene" uid="uid://cyskk1kc88lgf" path="res://game/entities/weapons/laser/player_laser_projectile.tscn" id="3_62i0g"]
[ext_resource type="Texture2D" uid="uid://6hh66k8s4a1e" path="res://images/ships/weapons.png" id="4_oaprh"]
[sub_resource type="AtlasTexture" id="AtlasTexture_5c4fv"]
@@ -43,12 +42,15 @@ animations = [{
[node name="PlayerLaserWeapon" instance=ExtResource("1_nky8c")]
script = ExtResource("2_oqbns")
projectile_scene = ExtResource("3_62i0g")
[node name="AnimatedSprite2D" parent="." index="0"]
[node name="AnimatedSprite2D" parent="." index="1"]
position = Vector2(3, 0)
sprite_frames = SubResource("SpriteFrames_geipx")
animation = &"shot"
[node name="Muzzle" parent="." index="2"]
[node name="Muzzle" parent="." index="3"]
position = Vector2(5, 0)
[node name="RayCast2D" parent="." index="4"]
target_position = Vector2(1000, 0)
collision_mask = 4