extends Node2D

var pattern = [2,3,4,5,3,1,2]
var patternIndex = 0

var opened = false
var fullyMoved = false

var moveSpeed = 25

var amountMoved = 0
var amountToMove = 250

func _ready():
	GameVariables.globalMessage.connect(global_message_recieved)

func _process(delta):
	if opened and !fullyMoved:
		var amountToMoved = moveSpeed*delta
		position.x += amountToMoved
		amountMoved += abs(amountToMoved)
		
		if amountMoved >= amountToMove:
			fullyMoved = true

func global_message_recieved(message:String):
	if message.substr(0,4) == "doot":
		var dootNumber = message.substr(4,5)
		if pattern[patternIndex] == int(dootNumber):
			patternIndex += 1
			if patternIndex >= pattern.size():
				open()
				patternIndex = 0
		else:
			patternIndex = 0
			if pattern[patternIndex] == int(dootNumber):
				patternIndex += 1

func open():
	opened = true
