/****
* Classes
****/
var Bed = Container.expand(function () {
var self = Container.call(this);
var bedGraphics = self.attachAsset('bed', {
anchorX: 0.5,
anchorY: 0.5
});
});
// Class for the enemy creatures
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 30;
self.update = function () {
// Calculate the direction vector between the enemy and the player
var dx = player.x - self.x;
var dy = player.y - self.y;
var magnitude = Math.sqrt(dx * dx + dy * dy);
// Normalize the direction vector
dx /= magnitude;
dy /= magnitude;
// Move the enemy towards the player
self.x += dx * self.speed;
self.y += dy * self.speed;
};
});
// Assets will be automatically created and loaded by the LK engine based on their usage in the code.
// Class for the player character
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
// Player update logic
};
});
// Class for the maze walls
var Wall = Container.expand(function () {
var self = Container.call(this);
var wallGraphics = self.attachAsset('wall', {
anchorX: 0.5,
anchorY: 0.5
});
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Initialize game variables
var player;
var enemies = [];
var walls = [];
var score = 0;
var scoreTxt;
var bed;
// Function to initialize the game elements
function initGame() {
// Create player
player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 - 200;
// Create enemies
for (var i = 0; i < 1; i++) {
var enemy = new Enemy();
enemy.x = 2048 / 2;
enemy.y = 0;
enemies.push(enemy);
game.addChild(enemy);
}
// Create maze wall with two doors
for (var i = 0; i < 2048; i += 100) {
// Skip creating wall blocks at the door positions
if (i == 800 || i == 1200) {
continue;
}
var wall = new Wall();
wall.x = i;
wall.y = 100;
walls.push(wall);
game.addChild(wall);
// Remove the green flash from the walls
LK.effects.flashObject(wall, 0x000000, 0);
}
// Initialize score text
scoreTxt = new Text2('Score: 0', {
size: 100,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Create bed
bed = game.addChild(new Bed());
bed.x = 2048 / 2;
bed.y = 2732 - 50;
}
// Function to update the game state
game.update = function () {
// Check if any enemy is touching the player
for (var i = 0; i < enemies.length; i++) {
if (enemies[i].visible && player.intersects(enemies[i])) {
// Flash screen red for 1 second (1000ms) to show player is dead.
LK.effects.flashScreen(0xff0000, 1000);
// Show game over. The game will be automatically paused while game over is showing.
LK.showGameOver();
}
}
// Check if the player is touching the bed
if (player.intersects(bed)) {
// Make the enemy disappear
for (var i = 0; i < enemies.length; i++) {
enemies[i].visible = false;
}
// Make the enemy reappear after 10 seconds
LK.setTimeout(function () {
for (var i = 0; i < enemies.length; i++) {
enemies[i].visible = true;
}
}, 10000);
}
};
// Initialize the game elements
initGame();
// Event listeners for player movement
game.down = function (x, y, obj) {
player.x = x;
player.y = y;
};
game.move = function (x, y, obj) {
player.x = x;
player.y = y;
};
game.up = function (x, y, obj) {
// Stop player movement
};