diff --git a/objects/entities/enemy.tscn b/objects/entities/enemy.tscn index 4f4d8c3..73eb1f9 100644 --- a/objects/entities/enemy.tscn +++ b/objects/entities/enemy.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=20 format=3 uid="uid://bwdlmualj6xbw"] +[gd_scene load_steps=21 format=3 uid="uid://bwdlmualj6xbw"] [ext_resource type="Shader" uid="uid://bs4xvm4qkurpr" path="res://shaders/hit_flash.tres" id="1_ep4yr"] [ext_resource type="Texture2D" uid="uid://cu72810eyk4dx" path="res://sprites/enemy-robot.png" id="2_hjtwe"] @@ -7,6 +7,7 @@ [ext_resource type="Script" uid="uid://dmqpif5qhvri3" path="res://scripts/components/side_to_side_movement.gd" id="4_gbsq8"] [ext_resource type="Script" uid="uid://b37gqnycj6rtk" path="res://scripts/components/periodic_shooting.gd" id="5_m03v0"] [ext_resource type="Script" uid="uid://dqm371fysuk7i" path="res://scripts/components/enemy_death.gd" id="6_6p3gr"] +[ext_resource type="PackedScene" uid="uid://jiv4e82ycwmq" path="res://objects/entities/homing_bullet.tscn" id="7_lvsna"] [ext_resource type="Script" uid="uid://dqmbvuutd5c3c" path="res://scripts/components/flashing_component.gd" id="7_xsaiy"] [ext_resource type="Script" uid="uid://ceq8n7yw7qxpi" path="res://scripts/components/hit_component.gd" id="9_0qjr4"] [ext_resource type="Script" uid="uid://c07d50s20rl8s" path="res://scripts/components/status_effect_component.gd" id="10_5i27q"] @@ -66,6 +67,7 @@ right_wall_ray = NodePath("../Right Wall Ray") [node name="PeriodicShootingComponent" type="Node" parent="." node_paths=PackedStringArray("side_to_side_movement", "root", "bullet_spawn_right", "bullet_spawn_left")] script = ExtResource("5_m03v0") +bullet_scene = ExtResource("7_lvsna") side_to_side_movement = NodePath("../SideToSideMovement") root = NodePath("..") bullet_spawn_right = NodePath("../Sprite2D/right bullet spawn") diff --git a/objects/entities/homing_bullet.tscn b/objects/entities/homing_bullet.tscn new file mode 100644 index 0000000..2e85f8b --- /dev/null +++ b/objects/entities/homing_bullet.tscn @@ -0,0 +1,80 @@ +[gd_scene load_steps=10 format=3 uid="uid://jiv4e82ycwmq"] + +[ext_resource type="Texture2D" uid="uid://djifxc5x0dyrw" path="res://sprites/ppc_tileset.png" id="1_bmfqy"] +[ext_resource type="Script" uid="uid://dkmxhjtmu5xlb" path="res://scripts/components/damage_component.gd" id="2_id4r8"] +[ext_resource type="Script" uid="uid://1tnr46o1ib4u" path="res://scripts/components/out_of_screen_component.gd" id="3_2uduj"] +[ext_resource type="Script" uid="uid://bvsgg8lu0a8m6" path="res://scripts/components/lifetime_component.gd" id="4_x32ji"] +[ext_resource type="Script" uid="uid://873un8agkyja" path="res://scripts/components/launch_component.gd" id="5_r3id6"] +[ext_resource type="Script" uid="uid://cdnwrn8v05qhi" path="res://scripts/components/bullet_component.gd" id="7_8gqaa"] +[ext_resource type="Script" uid="uid://be8jhvb8t3kif" path="res://scripts/components/homing_missile_motion.gd" id="7_bmfqy"] + +[sub_resource type="CircleShape2D" id="CircleShape2D_txsw8"] +radius = 4.0 + +[sub_resource type="CircleShape2D" id="CircleShape2D_bmfqy"] +radius = 128.0 + +[node name="Bullet" type="Area2D"] +collision_layer = 8 +collision_mask = 21 + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("CircleShape2D_txsw8") + +[node name="Sprite2D" type="Sprite2D" parent="."] +scale = Vector2(0.5, 0.5) +texture = ExtResource("1_bmfqy") +hframes = 12 +vframes = 12 +frame = 79 + +[node name="VisibleOnScreenNotifier2D" type="VisibleOnScreenNotifier2D" parent="."] +position = Vector2(2.38419e-07, 2.38419e-07) +scale = Vector2(0.4, 0.4) + +[node name="DamageComponent" type="Node" parent="." node_paths=PackedStringArray("area2d")] +script = ExtResource("2_id4r8") +area2d = NodePath("..") + +[node name="Timer" type="Timer" parent="."] +wait_time = 5.0 +autostart = true + +[node name="OutOfScreenComponent" type="Node" parent="." node_paths=PackedStringArray("visibility_notifier", "root")] +script = ExtResource("3_2uduj") +visibility_notifier = NodePath("../VisibleOnScreenNotifier2D") +root = NodePath("..") + +[node name="LifetimeComponent" type="Node" parent="." node_paths=PackedStringArray("root", "timer")] +script = ExtResource("4_x32ji") +root = NodePath("..") +life_time = 10.0 +timer = NodePath("../Timer") + +[node name="LaunchComponent" type="Node2D" parent="." node_paths=PackedStringArray("root")] +script = ExtResource("5_r3id6") +root = NodePath("..") +speed = 120.0 +metadata/_custom_type_script = "uid://873un8agkyja" + +[node name="BulletComponent" type="Node" parent="." node_paths=PackedStringArray("root", "area2d")] +script = ExtResource("7_8gqaa") +root = NodePath("..") +area2d = NodePath("..") +metadata/_custom_type_script = "uid://cdnwrn8v05qhi" + +[node name="HomingMissileMotion" type="Node" parent="." node_paths=PackedStringArray("launch_component", "root", "detection_area")] +script = ExtResource("7_bmfqy") +launch_component = NodePath("../LaunchComponent") +root = NodePath("..") +max_speed = 122.0 +acceleration = 4.0 +detection_area = NodePath("../DetectionArea") + +[node name="DetectionArea" type="Area2D" parent="."] +collision_layer = 0 +collision_mask = 4 + +[node name="CollisionShape2D" type="CollisionShape2D" parent="DetectionArea"] +shape = SubResource("CircleShape2D_bmfqy") +debug_color = Color(0.802086, 0.36581, 0.539812, 0.42) diff --git a/scripts/components/homing_missile_motion.gd b/scripts/components/homing_missile_motion.gd new file mode 100644 index 0000000..203cfb1 --- /dev/null +++ b/scripts/components/homing_missile_motion.gd @@ -0,0 +1,60 @@ +class_name HomingMissileMotion +extends Node + +@export var launch_component: LaunchComponent +@export var root: Node2D +@export var max_speed: float = 16.0 +@export var acceleration: float = 8.0 +@export var detection_area: Area2D +@export var max_turn_rate: float = 180.0 + +var target: Node2D = null +var velocity: Vector2 = Vector2.ZERO + + +func _ready() -> void: + if not detection_area: + printerr("No detection area assigned!") + return + + if not launch_component: + printerr("No launch component assigned!") + return + + detection_area.body_entered.connect(on_detection_area_body_entered) + velocity = launch_component.get_initial_velocity() + + +func _physics_process(delta: float) -> void: + if not launch_component or not root: + return + + if not target: + root.position += velocity * delta + return + + var to_target := (target.global_position - root.global_position).normalized() + var angle_to_target := velocity.angle_to(to_target) + var max_angle := deg_to_rad(max_turn_rate) * delta + var clamped_angle = clamp(angle_to_target, - max_angle, max_angle) + velocity = velocity.rotated(clamped_angle).normalized() * velocity.length() + + var desired_speed = min(max_speed, velocity.length() + acceleration * delta) + velocity = velocity.normalized() * desired_speed + + root.position += velocity * delta + root.rotation = velocity.angle() + + +func on_detection_area_body_entered(body: Node) -> void: + print("Body entered detection area: ", body.name) + if target != null: + return + + if body == null: + return + + target = body + + + \ No newline at end of file diff --git a/scripts/components/homing_missile_motion.gd.uid b/scripts/components/homing_missile_motion.gd.uid new file mode 100644 index 0000000..e23147f --- /dev/null +++ b/scripts/components/homing_missile_motion.gd.uid @@ -0,0 +1 @@ +uid://be8jhvb8t3kif diff --git a/scripts/components/projectile_init_component.gd b/scripts/components/projectile_init_component.gd index ed1a6e5..4c89794 100644 --- a/scripts/components/projectile_init_component.gd +++ b/scripts/components/projectile_init_component.gd @@ -17,4 +17,4 @@ func initialize(params: Dictionary) -> void: launch_component.initial_direction = direction launch_component.spawn_position = position launch_component.spawn_rotation = rotation - launch_component.speed *= power \ No newline at end of file + launch_component.speed *= power