87 lines
2.0 KiB
GDScript
87 lines
2.0 KiB
GDScript
extends Area2D
|
|
|
|
signal enemy_died()
|
|
|
|
@export var speed : int = 150
|
|
|
|
#position where the thing shoots from
|
|
@onready var marker = $Marker2D
|
|
|
|
#sensor location, central just for testing
|
|
@onready var ray1 = $RayCast2D
|
|
|
|
signal missile_spawn()
|
|
|
|
#health, duh.
|
|
var health = 3
|
|
|
|
#had to add for things that require sensors, going to require moving one for turrets maybe?
|
|
#for tutorial https://www.youtube.com/watch?v=V_gaCLbWqzk&ab_channel=KaanAlpar
|
|
func _ready():
|
|
ray1.exclude_parent = true
|
|
ray1.collide_with_areas = true
|
|
|
|
|
|
#setting variable if it can shoot used later.
|
|
var can_shoot = true
|
|
#testing for the raycast feature.
|
|
#func _process(delta):
|
|
#print(ray1.is_colliding())
|
|
#print(ray1.get_collider())
|
|
|
|
func _physics_process(delta):
|
|
#movement
|
|
global_position.y += speed * delta
|
|
#detecting player in area.
|
|
if ray1.is_colliding and ray1.get_collider() is player:
|
|
if can_shoot == true:
|
|
#shooting
|
|
fire_enemy_laser()
|
|
#starting firing timer so it cant just spam.
|
|
$Firing_timer.start()
|
|
can_shoot = false
|
|
|
|
func take_damage(damage):
|
|
health -= damage
|
|
if health <= 0:
|
|
$death_timer.start()
|
|
#$AnimatedSprite2D.play("death")
|
|
emit_signal("enemy_died")
|
|
get_tree().current_scene.scored()
|
|
power_up_check()
|
|
#print(str(get_tree().current_scene.player.global_position))
|
|
|
|
|
|
|
|
func fire_enemy_laser():
|
|
get_tree().current_scene.fire_enemy_laser(marker.global_position)
|
|
|
|
func power_up_check():
|
|
var index = randi() % 51
|
|
|
|
# 1 - 40 does nothing
|
|
# 40-48 spawns missiles
|
|
# 49 spawns nothing
|
|
# 50 spawns nothing
|
|
|
|
#1-39+ 40 does nothing.
|
|
if index <= 39:
|
|
pass
|
|
# if greater than or equal to 40, and less than or equal to 48 it spawns stuff
|
|
elif index >= 40 and index <= 48:
|
|
get_tree().current_scene.missile_spawn(marker.global_position)
|
|
|
|
#get_tree().current_scene.missile_spawn(marker.global_position)
|
|
|
|
func _on_area_entered(area):
|
|
if area is player:
|
|
area.take_damage(1)
|
|
|
|
|
|
func _on_death_timer_timeout():
|
|
queue_free() # Replace with function body.
|
|
|
|
|
|
func _on_firing_timer_timeout():
|
|
can_shoot = true
|