/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Collectible = Container.expand(function (isLightWorld) {
var self = Container.call(this);
self.isLightWorld = isLightWorld;
var collectibleGraphics = self.attachAsset(isLightWorld ? 'lightCollectible' : 'darkCollectible', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -8;
self.collected = false;
self.update = function () {
self.x += self.speed;
collectibleGraphics.rotation += 0.1;
};
return self;
});
var Obstacle = Container.expand(function (isLightWorld) {
var self = Container.call(this);
self.isLightWorld = isLightWorld;
var obstacleGraphics = self.attachAsset(isLightWorld ? 'lightObstacle' : 'darkObstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -8;
self.update = function () {
self.x += self.speed;
};
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.verticalSpeed = 0;
self.gravity = 0.5;
self.jumpPower = -12;
self.isGrounded = false;
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var isLightWorld = true;
var player = null;
var obstacles = [];
var collectibles = [];
var gameSpeed = 8;
var spawnTimer = 0;
var groundY = 2200;
var lastSpawnX = 2200;
var scoreTxt = new Text2('Score: 0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var worldIndicator = new Text2('Light World', {
size: 60,
fill: 0xFFD700
});
worldIndicator.anchor.set(0.5, 1);
LK.gui.bottom.addChild(worldIndicator);
function updateWorldVisuals() {
if (isLightWorld) {
game.setBackgroundColor(0x87CEEB);
worldIndicator.setText('Light World');
worldIndicator.style.fill = "#FFD700";
} else {
game.setBackgroundColor(0x2C1810);
worldIndicator.setText('Dark World');
worldIndicator.style.fill = "#9C27B0";
}
tween(game, {}, {
duration: 200,
onFinish: function onFinish() {
for (var i = 0; i < obstacles.length; i++) {
var obstacle = obstacles[i];
obstacle.alpha = obstacle.isLightWorld === isLightWorld ? 1 : 0.3;
}
for (var j = 0; j < collectibles.length; j++) {
var collectible = collectibles[j];
collectible.alpha = collectible.isLightWorld === isLightWorld ? 1 : 0.3;
}
}
});
}
function spawnObstacle() {
var worldType = Math.random() < 0.5;
var obstacle = new Obstacle(worldType);
obstacle.x = 2200;
obstacle.y = groundY - 50;
obstacle.alpha = obstacle.isLightWorld === isLightWorld ? 1 : 0.3;
obstacles.push(obstacle);
game.addChild(obstacle);
}
function spawnCollectible() {
var worldType = Math.random() < 0.5;
var collectible = new Collectible(worldType);
collectible.x = 2200;
collectible.y = groundY - Math.random() * 300 - 100;
collectible.alpha = collectible.isLightWorld === isLightWorld ? 1 : 0.3;
collectibles.push(collectible);
game.addChild(collectible);
}
player = game.addChild(new Player());
player.x = 400;
player.y = groundY - 40;
updateWorldVisuals();
game.down = function (x, y, obj) {
isLightWorld = !isLightWorld;
updateWorldVisuals();
LK.getSound('flip').play();
if (player.isGrounded) {
player.verticalSpeed = player.jumpPower;
player.isGrounded = false;
}
};
game.update = function () {
if (!player) return;
player.verticalSpeed += player.gravity;
player.y += player.verticalSpeed;
if (player.y >= groundY - 40) {
player.y = groundY - 40;
player.verticalSpeed = 0;
player.isGrounded = true;
}
spawnTimer += 1;
gameSpeed = Math.min(12, 8 + LK.getScore() / 100);
if (spawnTimer % Math.max(60, 120 - LK.getScore()) === 0) {
if (Math.random() < 0.7) {
spawnObstacle();
} else {
spawnCollectible();
}
}
for (var i = obstacles.length - 1; i >= 0; i--) {
var obstacle = obstacles[i];
obstacle.speed = -gameSpeed;
if (obstacle.x < -100) {
obstacle.destroy();
obstacles.splice(i, 1);
continue;
}
if (obstacle.isLightWorld === isLightWorld && player.intersects(obstacle)) {
LK.getSound('hit').play();
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
return;
}
}
for (var j = collectibles.length - 1; j >= 0; j--) {
var collectible = collectibles[j];
collectible.speed = -gameSpeed;
if (collectible.x < -100) {
collectible.destroy();
collectibles.splice(j, 1);
continue;
}
if (!collectible.collected && collectible.isLightWorld === isLightWorld && player.intersects(collectible)) {
collectible.collected = true;
LK.setScore(LK.getScore() + 10);
scoreTxt.setText('Score: ' + LK.getScore());
LK.getSound('collect').play();
tween(collectible, {
alpha: 0,
scaleX: 2,
scaleY: 2
}, {
duration: 300,
onFinish: function onFinish() {
collectible.destroy();
}
});
collectibles.splice(j, 1);
}
}
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Collectible = Container.expand(function (isLightWorld) {
var self = Container.call(this);
self.isLightWorld = isLightWorld;
var collectibleGraphics = self.attachAsset(isLightWorld ? 'lightCollectible' : 'darkCollectible', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -8;
self.collected = false;
self.update = function () {
self.x += self.speed;
collectibleGraphics.rotation += 0.1;
};
return self;
});
var Obstacle = Container.expand(function (isLightWorld) {
var self = Container.call(this);
self.isLightWorld = isLightWorld;
var obstacleGraphics = self.attachAsset(isLightWorld ? 'lightObstacle' : 'darkObstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -8;
self.update = function () {
self.x += self.speed;
};
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.verticalSpeed = 0;
self.gravity = 0.5;
self.jumpPower = -12;
self.isGrounded = false;
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var isLightWorld = true;
var player = null;
var obstacles = [];
var collectibles = [];
var gameSpeed = 8;
var spawnTimer = 0;
var groundY = 2200;
var lastSpawnX = 2200;
var scoreTxt = new Text2('Score: 0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var worldIndicator = new Text2('Light World', {
size: 60,
fill: 0xFFD700
});
worldIndicator.anchor.set(0.5, 1);
LK.gui.bottom.addChild(worldIndicator);
function updateWorldVisuals() {
if (isLightWorld) {
game.setBackgroundColor(0x87CEEB);
worldIndicator.setText('Light World');
worldIndicator.style.fill = "#FFD700";
} else {
game.setBackgroundColor(0x2C1810);
worldIndicator.setText('Dark World');
worldIndicator.style.fill = "#9C27B0";
}
tween(game, {}, {
duration: 200,
onFinish: function onFinish() {
for (var i = 0; i < obstacles.length; i++) {
var obstacle = obstacles[i];
obstacle.alpha = obstacle.isLightWorld === isLightWorld ? 1 : 0.3;
}
for (var j = 0; j < collectibles.length; j++) {
var collectible = collectibles[j];
collectible.alpha = collectible.isLightWorld === isLightWorld ? 1 : 0.3;
}
}
});
}
function spawnObstacle() {
var worldType = Math.random() < 0.5;
var obstacle = new Obstacle(worldType);
obstacle.x = 2200;
obstacle.y = groundY - 50;
obstacle.alpha = obstacle.isLightWorld === isLightWorld ? 1 : 0.3;
obstacles.push(obstacle);
game.addChild(obstacle);
}
function spawnCollectible() {
var worldType = Math.random() < 0.5;
var collectible = new Collectible(worldType);
collectible.x = 2200;
collectible.y = groundY - Math.random() * 300 - 100;
collectible.alpha = collectible.isLightWorld === isLightWorld ? 1 : 0.3;
collectibles.push(collectible);
game.addChild(collectible);
}
player = game.addChild(new Player());
player.x = 400;
player.y = groundY - 40;
updateWorldVisuals();
game.down = function (x, y, obj) {
isLightWorld = !isLightWorld;
updateWorldVisuals();
LK.getSound('flip').play();
if (player.isGrounded) {
player.verticalSpeed = player.jumpPower;
player.isGrounded = false;
}
};
game.update = function () {
if (!player) return;
player.verticalSpeed += player.gravity;
player.y += player.verticalSpeed;
if (player.y >= groundY - 40) {
player.y = groundY - 40;
player.verticalSpeed = 0;
player.isGrounded = true;
}
spawnTimer += 1;
gameSpeed = Math.min(12, 8 + LK.getScore() / 100);
if (spawnTimer % Math.max(60, 120 - LK.getScore()) === 0) {
if (Math.random() < 0.7) {
spawnObstacle();
} else {
spawnCollectible();
}
}
for (var i = obstacles.length - 1; i >= 0; i--) {
var obstacle = obstacles[i];
obstacle.speed = -gameSpeed;
if (obstacle.x < -100) {
obstacle.destroy();
obstacles.splice(i, 1);
continue;
}
if (obstacle.isLightWorld === isLightWorld && player.intersects(obstacle)) {
LK.getSound('hit').play();
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
return;
}
}
for (var j = collectibles.length - 1; j >= 0; j--) {
var collectible = collectibles[j];
collectible.speed = -gameSpeed;
if (collectible.x < -100) {
collectible.destroy();
collectibles.splice(j, 1);
continue;
}
if (!collectible.collected && collectible.isLightWorld === isLightWorld && player.intersects(collectible)) {
collectible.collected = true;
LK.setScore(LK.getScore() + 10);
scoreTxt.setText('Score: ' + LK.getScore());
LK.getSound('collect').play();
tween(collectible, {
alpha: 0,
scaleX: 2,
scaleY: 2
}, {
duration: 300,
onFinish: function onFinish() {
collectible.destroy();
}
});
collectibles.splice(j, 1);
}
}
};