var SoundEngine = Container.expand(function () {
var self = Container.call(this);
self.sounds = {};
self.loadSound = function (id, path) {
self.sounds[id] = new Audio(path);
};
self.playSound = function (id) {
if (self.sounds[id]) {
self.sounds[id].play();
}
};
self.saveSounds = function () {
console.log('Sounds saved');
};
self.readSounds = function () {
console.log('Sounds read');
};
self.writeSounds = function () {
console.log('Sounds written');
};
});
var Paddle = Container.expand(function () {
var self = Container.call(this);
var paddleGraphics = self.createAsset('paddle', 'Paddle Graphics', 0.5, 0.5);
self.speed = 10;
});
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.createAsset('ball', 'Ball Graphics', 0.5, 0.5);
self.speed = {
x: 5,
y: 5
};
self.move = function () {
self.x += self.speed.x;
self.y += self.speed.y;
};
self.increaseSpeed = function () {
self.speed.x *= 1.1;
self.speed.y *= 1.1;
};
self.resetSpeed = function () {
self.speed.x = 5 * (self.speed.x < 0 ? -1 : 1);
self.speed.y = 5 * (self.speed.y < 0 ? -1 : 1);
};
});
var Game = Container.expand(function () {
var self = Container.call(this);
var soundEngine = self.addChild(new SoundEngine());
var playerPaddle = self.addChild(new Paddle());
var aiPaddle = self.addChild(new Paddle());
var ball = self.addChild(new Ball());
var paddles = [playerPaddle, aiPaddle];
var score = {
player: 0,
ai: 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 - 0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(.5, 0);
LK.gui.topCenter.addChild(scoreTxt);
var moveDirection = 0;
LK.on('tick', function () {
ball.move();
stage.on('move', function (obj) {
var event = obj.event;
var pos = event.getLocalPosition(self);
playerPaddle.y = pos.y - playerPaddle.height / 2;
});
aiPaddle.y = ball.y - aiPaddle.height / 2;
for (var i = 0; i < paddles.length; i++) {
if (ball.intersects(paddles[i])) {
if (soundEngine) soundEngine.playSound('beep');
ball.speed.x *= -1;
ball.increaseSpeed();
}
}
if (ball.y <= 0 || ball.y >= 2732) {
if (soundEngine) soundEngine.playSound('beep');
ball.speed.y *= -1;
}
if (ball.x <= 0) {
if (LK.sfx) LK.sfx.play('beep');
score.ai += 1;
checkScoreAndReset();
}
if (ball.x >= 2048) {
if (LK.sfx) LK.sfx.play('score_point');
score.player += 1;
checkScoreAndReset();
}
scoreTxt.setText(score.player + ' - ' + score.ai);
});
function checkScoreAndReset() {
if (score.player >= 5 || score.ai >= 5) {
score.player = 0;
score.ai = 0;
LK.showGameOver();
}
resetBall();
}
function resetBall() {
ball.x = 2048 / 2;
ball.y = 2732 / 2;
ball.resetSpeed();
}
});
var SoundEngine = Container.expand(function () {
var self = Container.call(this);
self.sounds = {};
self.loadSound = function (id, path) {
self.sounds[id] = new Audio(path);
};
self.playSound = function (id) {
if (self.sounds[id]) {
self.sounds[id].play();
}
};
self.saveSounds = function () {
console.log('Sounds saved');
};
self.readSounds = function () {
console.log('Sounds read');
};
self.writeSounds = function () {
console.log('Sounds written');
};
});
var Paddle = Container.expand(function () {
var self = Container.call(this);
var paddleGraphics = self.createAsset('paddle', 'Paddle Graphics', 0.5, 0.5);
self.speed = 10;
});
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.createAsset('ball', 'Ball Graphics', 0.5, 0.5);
self.speed = {
x: 5,
y: 5
};
self.move = function () {
self.x += self.speed.x;
self.y += self.speed.y;
};
self.increaseSpeed = function () {
self.speed.x *= 1.1;
self.speed.y *= 1.1;
};
self.resetSpeed = function () {
self.speed.x = 5 * (self.speed.x < 0 ? -1 : 1);
self.speed.y = 5 * (self.speed.y < 0 ? -1 : 1);
};
});
var Game = Container.expand(function () {
var self = Container.call(this);
var soundEngine = self.addChild(new SoundEngine());
var playerPaddle = self.addChild(new Paddle());
var aiPaddle = self.addChild(new Paddle());
var ball = self.addChild(new Ball());
var paddles = [playerPaddle, aiPaddle];
var score = {
player: 0,
ai: 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 - 0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(.5, 0);
LK.gui.topCenter.addChild(scoreTxt);
var moveDirection = 0;
LK.on('tick', function () {
ball.move();
stage.on('move', function (obj) {
var event = obj.event;
var pos = event.getLocalPosition(self);
playerPaddle.y = pos.y - playerPaddle.height / 2;
});
aiPaddle.y = ball.y - aiPaddle.height / 2;
for (var i = 0; i < paddles.length; i++) {
if (ball.intersects(paddles[i])) {
if (soundEngine) soundEngine.playSound('beep');
ball.speed.x *= -1;
ball.increaseSpeed();
}
}
if (ball.y <= 0 || ball.y >= 2732) {
if (soundEngine) soundEngine.playSound('beep');
ball.speed.y *= -1;
}
if (ball.x <= 0) {
if (LK.sfx) LK.sfx.play('beep');
score.ai += 1;
checkScoreAndReset();
}
if (ball.x >= 2048) {
if (LK.sfx) LK.sfx.play('score_point');
score.player += 1;
checkScoreAndReset();
}
scoreTxt.setText(score.player + ' - ' + score.ai);
});
function checkScoreAndReset() {
if (score.player >= 5 || score.ai >= 5) {
score.player = 0;
score.ai = 0;
LK.showGameOver();
}
resetBall();
}
function resetBall() {
ball.x = 2048 / 2;
ball.y = 2732 / 2;
ball.resetSpeed();
}
});