extends Node

onready var audioPlayer = $AudioPlayer

var fastFactor
var lifetime = 5
var elapsed = 0
var sprite
var players

onready var mask = $Mask

func slow_player(victim):
	if victim.is_in_group("Player"):
		players.append(victim)
		audioPlayer.play()
	
func unslow_player(victim):
	players.remove(players.find(victim))

# Called when the node enters the scene tree for the first time.
func _ready():
	players = []
	sprite = get_node("Sprite")
	get_node("Sprite/Area2D").connect("body_entered", self, "slow_player")
	get_node("Sprite/Area2D").connect("body_exited", self, "unslow_player")

func _process(delta):
	elapsed += delta
	if mask != null:
		mask.offset.x = lerp(88, 270, elapsed / 0.8)
		if mask.offset.x >= 269:
			mask.queue_free()
			mask = null
	for each in players:
		each.SPEED_FACTORS.append(2)
	if elapsed >= lifetime * 0.7:
		sprite.self_modulate = Color(1,1,1,lerp(1, 0, (elapsed - lifetime * 0.7) / (lifetime * 0.3)))		
	if elapsed >= lifetime:
		queue_free()
