diff --git a/Scenes/enemy_ywing.tscn b/Scenes/enemy_ywing.tscn index 54a3680..5bd1e3f 100644 --- a/Scenes/enemy_ywing.tscn +++ b/Scenes/enemy_ywing.tscn @@ -1,6 +1,7 @@ -[gd_scene load_steps=5 format=3 uid="uid://6xeat2a1yr6k"] +[gd_scene load_steps=6 format=3 uid="uid://6xeat2a1yr6k"] [ext_resource type="Texture2D" uid="uid://bayc56dplo4ub" path="res://Assets/Y-Wing_Draft.png" id="1_i65b5"] +[ext_resource type="Script" path="res://Scripts/enemy_ywing.gd" id="1_ox3kv"] [ext_resource type="Texture2D" uid="uid://drtiwwj2pa73x" path="res://Assets/Y-Wing_Turret.png" id="2_wt4e7"] [sub_resource type="AtlasTexture" id="AtlasTexture_7hq4j"] @@ -18,7 +19,10 @@ animations = [{ "speed": 5.0 }] -[node name="enemy_ywing" type="Area2D"] +[node name="enemy_ywing" type="Area2D" groups=["enemies"]] +collision_layer = 4 +collision_mask = 11 +script = ExtResource("1_ox3kv") [node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."] texture_filter = 1 @@ -30,6 +34,11 @@ polygon = PackedVector2Array(13, 140, 25, 122, 28, 111, 28, 66, 23, 61, 18, 61, [node name="Muzzle1" type="Marker2D" parent="."] position = Vector2(0, 146) +[node name="RayCast2D" type="RayCast2D" parent="."] +position = Vector2(0, 141) +target_position = Vector2(1, 821) +collision_mask = 11 + [node name="Turret" type="Sprite2D" parent="."] texture_filter = 1 position = Vector2(1, 10) @@ -42,7 +51,9 @@ offset = Vector2(0, 5.2) position = Vector2(0, 21) [node name="target_confirm" type="RayCast2D" parent="Turret"] +position = Vector2(0, -1) target_position = Vector2(-1, 873) +collision_mask = 11 [node name="refire_timer1" type="Timer" parent="."] @@ -50,3 +61,7 @@ target_position = Vector2(-1, 873) [node name="death_timer" type="Timer" parent="."] wait_time = 0.5 + +[connection signal="area_entered" from="." to="." method="_on_area_entered"] +[connection signal="timeout" from="refire_timer1" to="." method="_on_refire_timer_1_timeout"] +[connection signal="timeout" from="death_timer" to="." method="_on_death_timer_timeout"] diff --git a/Scripts/enemy_spawner.gd b/Scripts/enemy_spawner.gd index db5b43e..5b2d014 100644 --- a/Scripts/enemy_spawner.gd +++ b/Scripts/enemy_spawner.gd @@ -5,6 +5,7 @@ signal scored() var spawn_positions = null var enemy = preload("res://Scenes/enemy_xwing.tscn") +var enemy2 = preload("res://Scenes/enemy_ywing.tscn") func _ready(): randomize() @@ -13,11 +14,11 @@ func _ready(): func spawn_enemy(): var index = randi() % spawn_positions.size() - var enemy = enemy.instantiate() - enemy.global_position = spawn_positions[index].global_position + var enemy2 = enemy2.instantiate() + enemy2.global_position = spawn_positions[index].global_position #connect error. unsure why. #enemy.connect("enemy_died", get_tree().current_scene, "scored") - add_child(enemy) + add_child(enemy2) #AI Generated code, doesn't work. connect error #func spawn_enemy(): diff --git a/Scripts/enemy_ywing.gd b/Scripts/enemy_ywing.gd new file mode 100644 index 0000000..a1876c2 --- /dev/null +++ b/Scripts/enemy_ywing.gd @@ -0,0 +1,129 @@ +extends Area2D + +#preload muzzle positions +@onready var muzzle = $Muzzle1 +@onready var mainray = $RayCast2D +@onready var turret_center = $Turret/turret_Center +@onready var turret_muzzle = $Turret/turret_muzzle +@onready var target_ray = $Turret/target_confirm +@onready var turret = $Turret +@onready var mainCannonFire = $refire_timer1 +@onready var turret_timer = $refire_timer2 + +#signal dont expect it to work +signal enemy_died() +signal missile_spawn() + +#set speed to an int +var speed : int = 20 + +#set health +var health : int = 3 + +#can the main gun fire? (rate of fire control) +var main_cannon = true + +#can turret fire? (rate of fire control) +var turret_cannon = true +var firing_solution = null + + +#reset raycast, setup settings +func _ready(): + target_ray.exclude_parent = true + target_ray.collide_with_areas = true + mainray.exclude_parent = true + mainray.collide_with_areas = true + +#main loop +func _physics_process(delta): + + #movement + global_position.y += speed * delta + + #main cannon firing + if mainray.is_colliding and mainray.get_collider is player: + if main_cannon == true: + fire_main_cannon() + main_cannon = false + mainCannonFire.start() + aim_turret(delta) + + if target_ray.is_colliding() and target_ray.get_colldier() is player: + if turret_cannon == true: + fire_turret(firing_solution) + turret_timer.start() + +#func aim_turret(delta): + #var target : Vector2 = player.global_position + #var turret_pos = turret.global_position + #var target_rot = atan2(target.x - turret_pos.x, target.y - turret_pos.y) + #var current_rotation = turret.rotation + #current_rotation += current_rotation * 0.90 + target_rot * 0.10 * 5 * (3.149 / 180) + #turret.rotate(current_rotation) + #turret.rotation(atan2(target.x - turret_pos.x,target.y - turret_pos.y)) + + +#figured out with loki, holy shit this was annoying +func aim_turret(delta): + var target : Vector2 = get_tree().current_scene.player.global_position + var turret_pos = turret.global_position + var target_rot = atan2(target.x - turret_pos.x, target.y - turret_pos.y) + var target_pos = get_tree().current_scene.player.global_position + var current_rotation = turret.rotation + + var speed = 5 #in degrees, can adjust + var direction = 1 + if target_rot < current_rotation: #may need to be "<" + direction = -1 + + #current_rotation += direction * speed * (PI/180) * delta + #turret.look_at(target_pos) + #turret.rotation(current_rotation) + #turret.rotation = current_rotation + #current_rotation += current_rotation * 0.90 + target_rot * 0.10 + #turret.set_rotation(atan2(target.x - turret_pos.x,target.y - turret_pos.y)) + #turret.set_rotation(atan2(turret_pos.x - target.x, turret_pos.y - target_pos.y)) + turret.set_rotation(atan2(target.y - turret_pos.y, target.x - turret_pos.x)-90*(PI/180)) + firing_solution = atan2(target.y - turret_pos.y, target.x - turret_pos.x)-90*(PI/180) + +func fire_turret(rotation): + pass + + + + +func take_damage(damage): + health -= damage + if health <= 0: + $death_timer.start() + get_tree().current_scene.scored() + power_up_check() + +#firing the lazors! +func fire_main_cannon(): + get_tree().current_scene.fire_y_wing_main(muzzle.global_position) + +func power_up_check(): + var index = randi() % 51 + + if index <= 39: + pass + elif index >= 40 and index <= 48: + get_tree().current_scene.missile_spawn(muzzle.global_position) + + + + +func _on_refire_timer_1_timeout(): + main_cannon = true + + +func _on_death_timer_timeout(): + emit_signal("enemy_died") + queue_free() + + +func _on_area_entered(area): + if area is player: + area.take_damage(1)