extends Node2D


export (PackedScene) var _scene = null
export var spawn_time = 0
export var random = false
export var _range = Vector2()
export var immediatly_spawn = false

var enabled = true

onready var timer = $Timer

func _on_spawn_time():
	if enabled:
		spawn()
		timer.start()
		if random:
			var new_spawn_time = rand_range(_range.x, _range.y)
			timer.wait_time = new_spawn_time
	else:
		timer.stop()
		
func spawn():
	var _object = _scene.instance()
	_object.global_position = global_position
	get_parent().add_child(_object)

func _on_notifier_enter():
	enabled = false

func _ready():
	timer.connect("timeout", self, "_on_spawn_time")
	$VisibilityNotifier2D.connect("screen_entered", self,"_on_notifier_enter")
	timer.wait_time = spawn_time
	timer.start()
	if immediatly_spawn:
		call_deferred("spawn")
