2024-10-05 15:57:40 +00:00
|
|
|
extends StaticBody2D
|
2024-10-05 10:39:19 +00:00
|
|
|
|
|
|
|
@export var input_name = ""
|
|
|
|
const ENEMY = preload("res://enemy.tscn")
|
2024-10-05 15:57:40 +00:00
|
|
|
const DISTANCE = 40
|
2024-10-05 10:39:19 +00:00
|
|
|
func _unhandled_input(event: InputEvent) -> void:
|
|
|
|
if(event.is_action_pressed(input_name)):
|
|
|
|
print_debug(input_name)
|
|
|
|
$AnimatedSprite2D.play("pressed")
|
|
|
|
var children = get_children(false)
|
2024-10-05 15:57:40 +00:00
|
|
|
var was_valid_input = false
|
2024-10-05 10:39:19 +00:00
|
|
|
for a in children:
|
|
|
|
if(a.has_method("button_pressed")):
|
|
|
|
a.button_pressed()
|
2024-10-05 15:57:40 +00:00
|
|
|
was_valid_input = true
|
|
|
|
if(!was_valid_input):
|
|
|
|
if get_parent().has_method("bad_press"):
|
|
|
|
get_parent().bad_press()
|
2024-10-05 10:39:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
func _on_spawn_tick_timeout() -> void:
|
2024-10-05 15:57:40 +00:00
|
|
|
spawn_enemy()
|
|
|
|
|
|
|
|
func spawn_enemy():
|
|
|
|
if get_children().any(func (a:Node):return a.has_method("button_pressed")):
|
|
|
|
return
|
|
|
|
elif(randi_range(0,1)==1):
|
2024-10-05 10:39:19 +00:00
|
|
|
var enemy = ENEMY.instantiate()
|
2024-10-05 15:57:40 +00:00
|
|
|
print_debug(randf_range(0.0, 2.0) * PI)
|
2024-10-05 10:39:19 +00:00
|
|
|
add_child(enemy)
|
2024-10-05 15:57:40 +00:00
|
|
|
enemy.global_position = global_position
|
|
|
|
enemy.global_rotation=randf_range(0, 2 * PI)
|
|
|
|
enemy.look_at(global_position)
|
|
|
|
enemy.enemy_attacked.connect(enemy_attacked)
|
|
|
|
|
|
|
|
func enemy_attacked():
|
|
|
|
#TODO: check for collisions
|
|
|
|
global_position = get_random_position()
|
|
|
|
|
|
|
|
|
|
|
|
func get_random_position():
|
|
|
|
var area2d:CollisionShape2D = get_parent().get_node(^"SpawnArea/CollisionShape2D")
|
|
|
|
var xa = area2d.shape.get_rect().position.x
|
|
|
|
var ya = area2d.global_position.y
|
|
|
|
|
|
|
|
# collison shape position is in the center of the shape :(( it took me too long to figure that out
|
|
|
|
var x = randi_range(area2d.global_position.x + area2d.shape.get_rect().position.x, area2d.global_position.x + area2d.shape.get_rect().end.x)
|
|
|
|
var y = randi_range(area2d.global_position.y + area2d.shape.get_rect().position.y, area2d.global_position.y + area2d.shape.get_rect().end.y)
|
|
|
|
return Vector2(x,y)
|