/**** 
* Classes
****/ 
// Class for Connection
var Connection = Container.expand(function () {
	var self = Container.call(this);
	var connectionGraphics = self.attachAsset('connection', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.updatePosition = function (startX, startY, endX, endY) {
		self.x = (startX + endX) / 2;
		self.y = (startY + endY) / 2;
		// Calculate rotation and length based on start and end points
		var dx = endX - startX;
		var dy = endY - startY;
		self.rotation = Math.atan2(dy, dx);
		connectionGraphics.width = Math.sqrt(dx * dx + dy * dy);
	};
});
// Class for Letter N
var LetterN = Container.expand(function () {
	var self = Container.call(this);
	var letterNGraphics = self.attachAsset('dot', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.isConnected = false;
	self.connect = function () {
		self.isConnected = true;
		letterNGraphics.tint = 0x00ff00; // Change color to green when connected
	};
	self.disconnect = function () {
		self.isConnected = false;
		letterNGraphics.tint = 0xff0000; // Change color to red when disconnected
	};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Class for Machine
var Machine = Container.expand(function () {
	var self = Container.call(this);
	var machineGraphics = self.attachAsset('dot', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.isConnected = false;
	self.connect = function () {
		self.isConnected = true;
		machineGraphics.tint = 0x00ff00; // Change color to green when connected
	};
	self.disconnect = function () {
		self.isConnected = false;
		machineGraphics.tint = 0xff0000; // Change color to red when disconnected
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000 //Init game with black background 
});
/**** 
* Game Code
****/ 
// Initialize machines and connections
var machines = [];
var connections = [];
var selectedMachine = null;
// Create machines and letter N
for (var i = 0; i < 3; i++) {
	for (var j = 0; j < 3; j++) {
		var dot = new Machine();
		dot.x = 250 + i * 250 + 520;
		dot.y = 250 + j * 250 + 1500;
		machines.push(dot);
		game.addChild(dot);
		if (i == 1 && j == 1) {
			var letterN = new LetterN();
			letterN.x = dot.x;
			letterN.y = dot.y;
			machines.push(letterN);
			game.addChild(letterN);
		}
	}
}
// Handle touch events for machines
machines.forEach(function (machine) {
	machine.down = function (x, y, obj) {
		if (!selectedMachine) {
			selectedMachine = machine;
		} else {
			if (selectedMachine !== machine) {
				var connection = new Connection();
				connection.updatePosition(selectedMachine.x, selectedMachine.y, machine.x, machine.y);
				connections.push(connection);
				game.addChild(connection);
				selectedMachine.connect();
				machine.connect();
				selectedMachine = null;
			}
		}
	};
});
// Add phone asset to the center bottom of the map
var M1 = LK.getAsset('M1', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 2048 / 2,
	y: 2732 / 2
});
game.addChildAt(M1, 0);
var phone = LK.getAsset('phone', {
	anchorX: 0.5,
	anchorY: 1.0,
	x: 2048 / 2,
	y: 2732
});
game.addChildAt(phone, 1);
// Create a Text2 object to display the current time
var timeText = new Text2('', {
	size: 40,
	fill: 0xFFFFFF
});
timeText.anchor.set(0.5, 0.5);
timeText.x = phone.x;
timeText.y = phone.y - phone.height / 2 + timeText.height / 2 - 680; // Position the time text at the top of the phone screen
game.addChild(timeText);
// Update the time text every second
var updateTime = function updateTime() {
	var date = new Date();
	var hours = date.getHours();
	var minutes = date.getMinutes();
	var seconds = date.getSeconds();
	// Format the time as HH:MM:SS
	var timeString = (hours < 10 ? '0' : '') + hours + ':' + (minutes < 10 ? '0' : '') + minutes + ':' + (seconds < 10 ? '0' : '') + seconds;
	timeText.setText(timeString);
};
updateTime();
LK.setInterval(updateTime, 1000); // Update the time every second
// Update game logic
game.update = function () {
	// Check if all machines and the letter N are connected
	var allConnected = machines.every(function (machine) {
		return machine.isConnected;
	});
	var letterNConnected = machines.some(function (machine) {
		return machine instanceof LetterN && machine.isConnected;
	});
	if (allConnected && letterNConnected) {
		// Display success message
		var successTxt = new Text2('Unlocked', {
			size: 100,
			fill: 0x00FF00
		});
		successTxt.anchor.set(0.5, 0.5);
		LK.gui.center.addChild(successTxt);
	}
}; /**** 
* Classes
****/ 
// Class for Connection
var Connection = Container.expand(function () {
	var self = Container.call(this);
	var connectionGraphics = self.attachAsset('connection', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.updatePosition = function (startX, startY, endX, endY) {
		self.x = (startX + endX) / 2;
		self.y = (startY + endY) / 2;
		// Calculate rotation and length based on start and end points
		var dx = endX - startX;
		var dy = endY - startY;
		self.rotation = Math.atan2(dy, dx);
		connectionGraphics.width = Math.sqrt(dx * dx + dy * dy);
	};
});
// Class for Letter N
var LetterN = Container.expand(function () {
	var self = Container.call(this);
	var letterNGraphics = self.attachAsset('dot', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.isConnected = false;
	self.connect = function () {
		self.isConnected = true;
		letterNGraphics.tint = 0x00ff00; // Change color to green when connected
	};
	self.disconnect = function () {
		self.isConnected = false;
		letterNGraphics.tint = 0xff0000; // Change color to red when disconnected
	};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Class for Machine
var Machine = Container.expand(function () {
	var self = Container.call(this);
	var machineGraphics = self.attachAsset('dot', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.isConnected = false;
	self.connect = function () {
		self.isConnected = true;
		machineGraphics.tint = 0x00ff00; // Change color to green when connected
	};
	self.disconnect = function () {
		self.isConnected = false;
		machineGraphics.tint = 0xff0000; // Change color to red when disconnected
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000 //Init game with black background 
});
/**** 
* Game Code
****/ 
// Initialize machines and connections
var machines = [];
var connections = [];
var selectedMachine = null;
// Create machines and letter N
for (var i = 0; i < 3; i++) {
	for (var j = 0; j < 3; j++) {
		var dot = new Machine();
		dot.x = 250 + i * 250 + 520;
		dot.y = 250 + j * 250 + 1500;
		machines.push(dot);
		game.addChild(dot);
		if (i == 1 && j == 1) {
			var letterN = new LetterN();
			letterN.x = dot.x;
			letterN.y = dot.y;
			machines.push(letterN);
			game.addChild(letterN);
		}
	}
}
// Handle touch events for machines
machines.forEach(function (machine) {
	machine.down = function (x, y, obj) {
		if (!selectedMachine) {
			selectedMachine = machine;
		} else {
			if (selectedMachine !== machine) {
				var connection = new Connection();
				connection.updatePosition(selectedMachine.x, selectedMachine.y, machine.x, machine.y);
				connections.push(connection);
				game.addChild(connection);
				selectedMachine.connect();
				machine.connect();
				selectedMachine = null;
			}
		}
	};
});
// Add phone asset to the center bottom of the map
var M1 = LK.getAsset('M1', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 2048 / 2,
	y: 2732 / 2
});
game.addChildAt(M1, 0);
var phone = LK.getAsset('phone', {
	anchorX: 0.5,
	anchorY: 1.0,
	x: 2048 / 2,
	y: 2732
});
game.addChildAt(phone, 1);
// Create a Text2 object to display the current time
var timeText = new Text2('', {
	size: 40,
	fill: 0xFFFFFF
});
timeText.anchor.set(0.5, 0.5);
timeText.x = phone.x;
timeText.y = phone.y - phone.height / 2 + timeText.height / 2 - 680; // Position the time text at the top of the phone screen
game.addChild(timeText);
// Update the time text every second
var updateTime = function updateTime() {
	var date = new Date();
	var hours = date.getHours();
	var minutes = date.getMinutes();
	var seconds = date.getSeconds();
	// Format the time as HH:MM:SS
	var timeString = (hours < 10 ? '0' : '') + hours + ':' + (minutes < 10 ? '0' : '') + minutes + ':' + (seconds < 10 ? '0' : '') + seconds;
	timeText.setText(timeString);
};
updateTime();
LK.setInterval(updateTime, 1000); // Update the time every second
// Update game logic
game.update = function () {
	// Check if all machines and the letter N are connected
	var allConnected = machines.every(function (machine) {
		return machine.isConnected;
	});
	var letterNConnected = machines.some(function (machine) {
		return machine instanceof LetterN && machine.isConnected;
	});
	if (allConnected && letterNConnected) {
		// Display success message
		var successTxt = new Text2('Unlocked', {
			size: 100,
			fill: 0x00FF00
		});
		successTxt.anchor.set(0.5, 0.5);
		LK.gui.center.addChild(successTxt);
	}
};