/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
kills: 0,
bestKills: 0
});
/****
* Classes
****/
var Checkpoint = Container.expand(function () {
var self = Container.call(this);
var checkpointGraphics = self.attachAsset('checkpoint', {
anchorX: 0.5,
anchorY: 0.5
});
self.isCheckpoint = true;
self.lastIntersecting = false;
return self;
});
var Hazard = Container.expand(function () {
var self = Container.call(this);
var hazardGraphics = self.attachAsset('hazard', {
anchorX: 0.5,
anchorY: 0.5
});
self.isDangerous = true;
self.lastIntersecting = false;
return self;
});
var Platform = Container.expand(function () {
var self = Container.call(this);
var platformGraphics = self.attachAsset('platform', {
anchorX: 0.5,
anchorY: 0.5
});
self.isSolid = true;
self.lastIntersecting = false;
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1a1a1a
});
/****
* Game Code
****/
var player = game.addChild(new Container());
var playerGraphics = player.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
player.x = 100;
player.y = 2600;
player.velocityX = 0;
player.velocityY = 0;
player.isJumping = false;
player.isGrounded = false;
player.canWallRun = false;
player.wallRunDirection = 0; // -1 left, 1 right
player.moveLeft = false;
player.moveRight = false;
player.jumpPressed = false;
var platforms = [];
var hazards = [];
var checkpoints = [];
var currentCheckpoint = 0;
var gameTime = 0;
var levelStartTime = 0;
var levelComplete = false;
var statusText = new Text2('Reach the finish!', {
size: 80,
fill: '#ffffff'
});
statusText.anchor.set(0.5, 0);
LK.gui.top.addChild(statusText);
var timerText = new Text2('0.00s', {
size: 100,
fill: '#ffff00'
});
timerText.anchor.set(0.5, 0);
LK.gui.top.addChild(timerText);
function updateUI() {
var elapsedTime = (gameTime - levelStartTime) / 1000;
timerText.setText(elapsedTime.toFixed(2) + 's');
if (levelComplete) {
statusText.setText('Level Complete!');
}
}
function createLevel() {
// Starting platform
var startPlatform = game.addChild(new Platform());
startPlatform.x = 200;
startPlatform.y = 2650;
platforms.push(startPlatform);
// Platform sequence
var platformSequence = [{
x: 500,
y: 2450,
w: 250,
h: 40
}, {
x: 800,
y: 2200,
w: 250,
h: 40
}, {
x: 1100,
y: 2000,
w: 250,
h: 40
}, {
x: 1400,
y: 1750,
w: 250,
h: 40
}, {
x: 1600,
y: 1500,
w: 200,
h: 40
}, {
x: 1300,
y: 1300,
w: 200,
h: 40
}, {
x: 900,
y: 1100,
w: 200,
h: 40
}, {
x: 600,
y: 900,
w: 250,
h: 40
}, {
x: 800,
y: 650,
w: 250,
h: 40
}, {
x: 1200,
y: 400,
w: 300,
h: 40
}];
for (var i = 0; i < platformSequence.length; i++) {
var p = platformSequence[i];
var platform = game.addChild(new Platform());
platform.x = p.x;
platform.y = p.y;
platform.width = p.w;
platform.height = p.h;
platforms.push(platform);
}
// Add hazards
var hazardSequence = [{
x: 900,
y: 1600
}, {
x: 1500,
y: 900
}, {
x: 400,
y: 500
}];
for (var h = 0; h < hazardSequence.length; h++) {
var hazard = game.addChild(new Hazard());
hazard.x = hazardSequence[h].x;
hazard.y = hazardSequence[h].y;
hazards.push(hazard);
}
// Add checkpoints
var checkpoint1 = game.addChild(new Checkpoint());
checkpoint1.x = 1100;
checkpoint1.y = 2000;
checkpoints.push(checkpoint1);
var checkpoint2 = game.addChild(new Checkpoint());
checkpoint2.x = 600;
checkpoint2.y = 900;
checkpoints.push(checkpoint2);
var finishCheckpoint = game.addChild(new Checkpoint());
finishCheckpoint.x = 1200;
finishCheckpoint.y = 300;
checkpoints.push(finishCheckpoint);
}
var GRAVITY = 0.5;
var JUMP_FORCE = -12;
var WALL_JUMP_FORCE = 8;
var MOVE_SPEED = 5;
var MAX_FALL_SPEED = 15;
function applyGravity() {
if (!player.isGrounded && !player.canWallRun) {
player.velocityY += GRAVITY;
if (player.velocityY > MAX_FALL_SPEED) {
player.velocityY = MAX_FALL_SPEED;
}
}
player.y += player.velocityY;
}
function checkCollisions() {
player.isGrounded = false;
player.canWallRun = false;
// Check platform collisions
for (var i = 0; i < platforms.length; i++) {
var platform = platforms[i];
if (player.intersects(platform)) {
// Landing on top of platform
if (player.velocityY >= 0 && player.lastY + 30 <= platform.y) {
player.y = platform.y - 30;
player.velocityY = 0;
player.isGrounded = true;
} else if (player.velocityY < 0) {
// Hit bottom of platform
player.y = platform.y + 20;
player.velocityY = 0;
}
}
}
// Check hazard collisions
for (var h = 0; h < hazards.length; h++) {
var hazard = hazards[h];
if (!hazard.lastIntersecting && player.intersects(hazard)) {
LK.effects.flashObject(player, 0xff0000, 300);
LK.getSound('hit').play();
// Reset to last checkpoint
player.x = checkpoints[Math.max(0, currentCheckpoint - 1)].x;
player.y = checkpoints[Math.max(0, currentCheckpoint - 1)].y + 100;
player.velocityY = 0;
}
hazard.lastIntersecting = player.intersects(hazard);
}
// Check checkpoint collisions
for (var c = 0; c < checkpoints.length; c++) {
var checkpoint = checkpoints[c];
if (!checkpoint.lastIntersecting && player.intersects(checkpoint)) {
currentCheckpoint = c;
LK.getSound('checkpoint').play();
if (c === checkpoints.length - 1) {
levelComplete = true;
}
}
checkpoint.lastIntersecting = player.intersects(checkpoint);
}
}
function handleJump() {
if (player.jumpPressed) {
if (player.isGrounded) {
player.velocityY = JUMP_FORCE;
player.isJumping = true;
LK.getSound('jump').play();
} else if (player.canWallRun) {
player.velocityY = JUMP_FORCE;
player.velocityX = WALL_JUMP_FORCE * -player.wallRunDirection;
LK.getSound('jump').play();
}
player.jumpPressed = false;
}
}
function endGame() {
var finalTime = (gameTime - levelStartTime) / 1000;
storage.bestTime = storage.bestTime || finalTime;
if (finalTime < storage.bestTime) {
storage.bestTime = finalTime;
}
LK.showGameOver();
}
game.down = function (x, y, obj) {
var centerX = 2048 / 2;
if (x < centerX) {
player.moveLeft = true;
} else {
player.moveRight = true;
}
player.jumpPressed = true;
};
game.up = function (x, y, obj) {
player.moveLeft = false;
player.moveRight = false;
};
game.move = function (x, y, obj) {
var centerX = 2048 / 2;
if (x < centerX) {
player.moveLeft = true;
player.moveRight = false;
} else {
player.moveRight = true;
player.moveLeft = false;
}
};
game.update = function () {
if (levelStartTime === 0) {
levelStartTime = gameTime;
}
gameTime += 16.67;
// Player horizontal movement
if (player.moveLeft) {
player.x = Math.max(0, player.x - MOVE_SPEED);
}
if (player.moveRight) {
player.x = Math.min(2048, player.x + MOVE_SPEED);
}
// Apply gravity and check collisions
player.lastY = player.y;
applyGravity();
checkCollisions();
// Handle jump input
handleJump();
// Fall off screen
if (player.y > 2732) {
LK.effects.flashObject(player, 0xff0000, 300);
LK.getSound('hit').play();
player.x = 100;
player.y = 2600;
player.velocityY = 0;
currentCheckpoint = 0;
levelStartTime = 0;
}
// Update UI
if (LK.ticks % 10 === 0) {
updateUI();
}
// End game condition
if (levelComplete) {
endGame();
}
};
createLevel();
LK.playMusic('bgmusic', {
loop: true
}); /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
kills: 0,
bestKills: 0
});
/****
* Classes
****/
var Checkpoint = Container.expand(function () {
var self = Container.call(this);
var checkpointGraphics = self.attachAsset('checkpoint', {
anchorX: 0.5,
anchorY: 0.5
});
self.isCheckpoint = true;
self.lastIntersecting = false;
return self;
});
var Hazard = Container.expand(function () {
var self = Container.call(this);
var hazardGraphics = self.attachAsset('hazard', {
anchorX: 0.5,
anchorY: 0.5
});
self.isDangerous = true;
self.lastIntersecting = false;
return self;
});
var Platform = Container.expand(function () {
var self = Container.call(this);
var platformGraphics = self.attachAsset('platform', {
anchorX: 0.5,
anchorY: 0.5
});
self.isSolid = true;
self.lastIntersecting = false;
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1a1a1a
});
/****
* Game Code
****/
var player = game.addChild(new Container());
var playerGraphics = player.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
player.x = 100;
player.y = 2600;
player.velocityX = 0;
player.velocityY = 0;
player.isJumping = false;
player.isGrounded = false;
player.canWallRun = false;
player.wallRunDirection = 0; // -1 left, 1 right
player.moveLeft = false;
player.moveRight = false;
player.jumpPressed = false;
var platforms = [];
var hazards = [];
var checkpoints = [];
var currentCheckpoint = 0;
var gameTime = 0;
var levelStartTime = 0;
var levelComplete = false;
var statusText = new Text2('Reach the finish!', {
size: 80,
fill: '#ffffff'
});
statusText.anchor.set(0.5, 0);
LK.gui.top.addChild(statusText);
var timerText = new Text2('0.00s', {
size: 100,
fill: '#ffff00'
});
timerText.anchor.set(0.5, 0);
LK.gui.top.addChild(timerText);
function updateUI() {
var elapsedTime = (gameTime - levelStartTime) / 1000;
timerText.setText(elapsedTime.toFixed(2) + 's');
if (levelComplete) {
statusText.setText('Level Complete!');
}
}
function createLevel() {
// Starting platform
var startPlatform = game.addChild(new Platform());
startPlatform.x = 200;
startPlatform.y = 2650;
platforms.push(startPlatform);
// Platform sequence
var platformSequence = [{
x: 500,
y: 2450,
w: 250,
h: 40
}, {
x: 800,
y: 2200,
w: 250,
h: 40
}, {
x: 1100,
y: 2000,
w: 250,
h: 40
}, {
x: 1400,
y: 1750,
w: 250,
h: 40
}, {
x: 1600,
y: 1500,
w: 200,
h: 40
}, {
x: 1300,
y: 1300,
w: 200,
h: 40
}, {
x: 900,
y: 1100,
w: 200,
h: 40
}, {
x: 600,
y: 900,
w: 250,
h: 40
}, {
x: 800,
y: 650,
w: 250,
h: 40
}, {
x: 1200,
y: 400,
w: 300,
h: 40
}];
for (var i = 0; i < platformSequence.length; i++) {
var p = platformSequence[i];
var platform = game.addChild(new Platform());
platform.x = p.x;
platform.y = p.y;
platform.width = p.w;
platform.height = p.h;
platforms.push(platform);
}
// Add hazards
var hazardSequence = [{
x: 900,
y: 1600
}, {
x: 1500,
y: 900
}, {
x: 400,
y: 500
}];
for (var h = 0; h < hazardSequence.length; h++) {
var hazard = game.addChild(new Hazard());
hazard.x = hazardSequence[h].x;
hazard.y = hazardSequence[h].y;
hazards.push(hazard);
}
// Add checkpoints
var checkpoint1 = game.addChild(new Checkpoint());
checkpoint1.x = 1100;
checkpoint1.y = 2000;
checkpoints.push(checkpoint1);
var checkpoint2 = game.addChild(new Checkpoint());
checkpoint2.x = 600;
checkpoint2.y = 900;
checkpoints.push(checkpoint2);
var finishCheckpoint = game.addChild(new Checkpoint());
finishCheckpoint.x = 1200;
finishCheckpoint.y = 300;
checkpoints.push(finishCheckpoint);
}
var GRAVITY = 0.5;
var JUMP_FORCE = -12;
var WALL_JUMP_FORCE = 8;
var MOVE_SPEED = 5;
var MAX_FALL_SPEED = 15;
function applyGravity() {
if (!player.isGrounded && !player.canWallRun) {
player.velocityY += GRAVITY;
if (player.velocityY > MAX_FALL_SPEED) {
player.velocityY = MAX_FALL_SPEED;
}
}
player.y += player.velocityY;
}
function checkCollisions() {
player.isGrounded = false;
player.canWallRun = false;
// Check platform collisions
for (var i = 0; i < platforms.length; i++) {
var platform = platforms[i];
if (player.intersects(platform)) {
// Landing on top of platform
if (player.velocityY >= 0 && player.lastY + 30 <= platform.y) {
player.y = platform.y - 30;
player.velocityY = 0;
player.isGrounded = true;
} else if (player.velocityY < 0) {
// Hit bottom of platform
player.y = platform.y + 20;
player.velocityY = 0;
}
}
}
// Check hazard collisions
for (var h = 0; h < hazards.length; h++) {
var hazard = hazards[h];
if (!hazard.lastIntersecting && player.intersects(hazard)) {
LK.effects.flashObject(player, 0xff0000, 300);
LK.getSound('hit').play();
// Reset to last checkpoint
player.x = checkpoints[Math.max(0, currentCheckpoint - 1)].x;
player.y = checkpoints[Math.max(0, currentCheckpoint - 1)].y + 100;
player.velocityY = 0;
}
hazard.lastIntersecting = player.intersects(hazard);
}
// Check checkpoint collisions
for (var c = 0; c < checkpoints.length; c++) {
var checkpoint = checkpoints[c];
if (!checkpoint.lastIntersecting && player.intersects(checkpoint)) {
currentCheckpoint = c;
LK.getSound('checkpoint').play();
if (c === checkpoints.length - 1) {
levelComplete = true;
}
}
checkpoint.lastIntersecting = player.intersects(checkpoint);
}
}
function handleJump() {
if (player.jumpPressed) {
if (player.isGrounded) {
player.velocityY = JUMP_FORCE;
player.isJumping = true;
LK.getSound('jump').play();
} else if (player.canWallRun) {
player.velocityY = JUMP_FORCE;
player.velocityX = WALL_JUMP_FORCE * -player.wallRunDirection;
LK.getSound('jump').play();
}
player.jumpPressed = false;
}
}
function endGame() {
var finalTime = (gameTime - levelStartTime) / 1000;
storage.bestTime = storage.bestTime || finalTime;
if (finalTime < storage.bestTime) {
storage.bestTime = finalTime;
}
LK.showGameOver();
}
game.down = function (x, y, obj) {
var centerX = 2048 / 2;
if (x < centerX) {
player.moveLeft = true;
} else {
player.moveRight = true;
}
player.jumpPressed = true;
};
game.up = function (x, y, obj) {
player.moveLeft = false;
player.moveRight = false;
};
game.move = function (x, y, obj) {
var centerX = 2048 / 2;
if (x < centerX) {
player.moveLeft = true;
player.moveRight = false;
} else {
player.moveRight = true;
player.moveLeft = false;
}
};
game.update = function () {
if (levelStartTime === 0) {
levelStartTime = gameTime;
}
gameTime += 16.67;
// Player horizontal movement
if (player.moveLeft) {
player.x = Math.max(0, player.x - MOVE_SPEED);
}
if (player.moveRight) {
player.x = Math.min(2048, player.x + MOVE_SPEED);
}
// Apply gravity and check collisions
player.lastY = player.y;
applyGravity();
checkCollisions();
// Handle jump input
handleJump();
// Fall off screen
if (player.y > 2732) {
LK.effects.flashObject(player, 0xff0000, 300);
LK.getSound('hit').play();
player.x = 100;
player.y = 2600;
player.velocityY = 0;
currentCheckpoint = 0;
levelStartTime = 0;
}
// Update UI
if (LK.ticks % 10 === 0) {
updateUI();
}
// End game condition
if (levelComplete) {
endGame();
}
};
createLevel();
LK.playMusic('bgmusic', {
loop: true
});