var Block = Container.expand(function () {
var self = Container.call(this);
var blockGraphics = self.createAsset('block', 'Block Graphics', .5, .5);
self.speed = 5;
self.falling = false;
self.move = function (hero) {
if (!self.falling) {
self.y += self.speed;
self.rotation += 0.1;
if (hero.intersects(self)) {
self.falling = true;
hero.balance(self);
self.speed = 0;
}
} else {
self.y = 2732 - self.height;
self.x += self.speed;
if (self.speed < 5) self.speed += 0.1;
}
};
});
var Pizza = Container.expand(function () {
var self = Container.call(this);
var pizzaGraphics = self.createAsset('pizza', 'Pizza Graphics', .5, .5);
self.speed = 5;
self.move = function () {};
self.destroy = function () {};
});
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.createAsset('hero', 'Hero Graphics', .5, .5);
heroGraphics.scale.set(2);
self.speed = 5;
self.move = function (dx, dy) {
self.x += dx * self.speed;
self.y += dy * self.speed;
};
self.balance = function (block) {
block.y = self.y - self.height - block.height;
block.x = self.x;
block.speed = 0;
block.falling = false;
self.parent.score += 1;
var scoreTxt = LK.gui.topCenter.children[0];
scoreTxt.setText(self.parent.score.toString());
};
});
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.createAsset('enemy', 'Enemy Graphics', .5, .5);
self.speed = 3;
self.move = function () {};
self.destroy = function () {};
});
var Game = Container.expand(function () {
var self = Container.call(this);
LK.stageContainer.setBackgroundColor(0x000000);
var hero = self.addChild(new Hero());
hero.x = 2048 / 2;
hero.y = 2732 / 2;
var blocks = [];
self.score = 0;
var scoreTxt = new Text2(self.score.toString(), {
size: 150,
fill: '#ffffff',
name: 'scoreTxt'
});
scoreTxt.anchor.set(.5, 0);
LK.gui.topCenter.addChild(scoreTxt);
var spawnBlocks = function () {
var block = new Block();
block.x = Math.random() * 2048;
block.y = 0;
self.addChild(block);
blocks.push(block);
};
LK.setInterval(spawnBlocks, 1000);
stage.on('move', function (obj) {
var event = obj.event;
var pos = event.getLocalPosition(self);
hero.x = pos.x;
hero.y = pos.y - hero.height - 200;
});
LK.on('tick', function () {
hero.move();
for (var a = blocks.length - 1; a >= 0; a--) {
blocks[a].move(hero);
if (blocks[a].y < -50) {
blocks[a].destroy();
blocks.splice(a, 1);
}
}
});
});