/**** 
* Classes
****/
var Paddle = Container.expand(function () {
	var self = Container.call(this);
	var paddleGraphics = self.attachAsset('paddle', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 20;
	self.move = function (direction) {
		self.y += direction * self.speed;
	};
});
var Ball = Container.expand(function () {
	var self = Container.call(this);
	var ballGraphics = self.attachAsset('ball', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = {
		x: 10,
		y: 10
	};
	self.move = function () {
		self.x += self.speed.x;
		self.y += self.speed.y;
	};
});
/**** 
* Initialize Game
****/
var game = new LK.Game({
	backgroundColor: 0x000000
});
/**** 
* Game Code
****/
var playerPaddle = game.addChild(new Paddle());
var aiPaddle = game.addChild(new Paddle());
var ball = game.addChild(new Ball());
var paddles = [playerPaddle, aiPaddle];
var score = 0;
playerPaddle.x = 100;
playerPaddle.y = 2732 / 2;
aiPaddle.x = 2048 - 100;
aiPaddle.y = 2732 / 2;
ball.x = 2048 / 2;
ball.y = 2732 / 2;
var scoreTxt = new Text2('0', {
	size: 150,
	fill: "#ffffff"
});
scoreTxt.anchor.set(.5, 0);
LK.gui.topCenter.addChild(scoreTxt);
var targetY = playerPaddle.y;
game.on('down', function (obj) {
	var event = obj.event;
	var pos = event.getLocalPosition(game);
	targetY = pos.y;
});
game.on('move', function (obj) {
	var event = obj.event;
	var pos = event.getLocalPosition(game);
	targetY = pos.y;
});
LK.on('tick', function () {
	if (targetY < playerPaddle.y) {
		playerPaddle.move(-1);
	} else if (targetY > playerPaddle.y) {
		playerPaddle.move(1);
	}
});
LK.on('tick', function () {
	ball.move();
	if (ball.y < aiPaddle.y) {
		aiPaddle.move(-1);
	} else if (ball.y > aiPaddle.y) {
		aiPaddle.move(1);
	}
	for (var i = 0; i < paddles.length; i++) {
		if (ball.intersects(paddles[i])) {
			var hitPoint = ball.y - paddles[i].y;
			var hitRatio = hitPoint / paddles[i].height;
			ball.speed.y += hitRatio * 10;
			if (ball.speed.x > 0 && ball.x < paddles[i].x || ball.speed.x < 0 && ball.x > paddles[i].x) {
				ball.speed.x *= -1;
			}
		}
	}
	if (ball.y <= 0 || ball.y >= 2732) {
		ball.speed.y *= -1;
	}
	if (ball.x <= 0) {
		LK.showGameOver();
	}
	if (ball.x >= 2048) {
		score++;
		ball.x = 2048 / 2;
		ball.y = 2732 / 2;
		ball.speed.x = 10;
		ball.speed.y = 10;
	}
	LK.setScore(score);
	scoreTxt.setText(LK.getScore().toString());
}); /**** 
* Classes
****/
var Paddle = Container.expand(function () {
	var self = Container.call(this);
	var paddleGraphics = self.attachAsset('paddle', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 20;
	self.move = function (direction) {
		self.y += direction * self.speed;
	};
});
var Ball = Container.expand(function () {
	var self = Container.call(this);
	var ballGraphics = self.attachAsset('ball', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = {
		x: 10,
		y: 10
	};
	self.move = function () {
		self.x += self.speed.x;
		self.y += self.speed.y;
	};
});
/**** 
* Initialize Game
****/
var game = new LK.Game({
	backgroundColor: 0x000000
});
/**** 
* Game Code
****/
var playerPaddle = game.addChild(new Paddle());
var aiPaddle = game.addChild(new Paddle());
var ball = game.addChild(new Ball());
var paddles = [playerPaddle, aiPaddle];
var score = 0;
playerPaddle.x = 100;
playerPaddle.y = 2732 / 2;
aiPaddle.x = 2048 - 100;
aiPaddle.y = 2732 / 2;
ball.x = 2048 / 2;
ball.y = 2732 / 2;
var scoreTxt = new Text2('0', {
	size: 150,
	fill: "#ffffff"
});
scoreTxt.anchor.set(.5, 0);
LK.gui.topCenter.addChild(scoreTxt);
var targetY = playerPaddle.y;
game.on('down', function (obj) {
	var event = obj.event;
	var pos = event.getLocalPosition(game);
	targetY = pos.y;
});
game.on('move', function (obj) {
	var event = obj.event;
	var pos = event.getLocalPosition(game);
	targetY = pos.y;
});
LK.on('tick', function () {
	if (targetY < playerPaddle.y) {
		playerPaddle.move(-1);
	} else if (targetY > playerPaddle.y) {
		playerPaddle.move(1);
	}
});
LK.on('tick', function () {
	ball.move();
	if (ball.y < aiPaddle.y) {
		aiPaddle.move(-1);
	} else if (ball.y > aiPaddle.y) {
		aiPaddle.move(1);
	}
	for (var i = 0; i < paddles.length; i++) {
		if (ball.intersects(paddles[i])) {
			var hitPoint = ball.y - paddles[i].y;
			var hitRatio = hitPoint / paddles[i].height;
			ball.speed.y += hitRatio * 10;
			if (ball.speed.x > 0 && ball.x < paddles[i].x || ball.speed.x < 0 && ball.x > paddles[i].x) {
				ball.speed.x *= -1;
			}
		}
	}
	if (ball.y <= 0 || ball.y >= 2732) {
		ball.speed.y *= -1;
	}
	if (ball.x <= 0) {
		LK.showGameOver();
	}
	if (ball.x >= 2048) {
		score++;
		ball.x = 2048 / 2;
		ball.y = 2732 / 2;
		ball.speed.x = 10;
		ball.speed.y = 10;
	}
	LK.setScore(score);
	scoreTxt.setText(LK.getScore().toString());
});