/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.vx = 1; // Slowed down enemy speed
self.patrolStart = 0;
self.patrolEnd = 500;
self.width = enemyGraphics.width;
self.height = enemyGraphics.height;
self.update = function () {
self.x += self.vx;
// Add jumping capability
if (self.grounded && Math.random() < 0.01) {
// Random chance to jump
self.vy = -15; // Jump power
self.grounded = false;
}
self.vy += self.gravity; // Apply gravity
self.y += self.vy; // Apply vertical velocity
// Check if enemy lands on a platform
self.grounded = false;
for (var i = 0; i < platforms.length; i++) {
var platform = platforms[i];
if (self.y + self.height / 2 > platform.y - platform.height / 2 && self.y - self.height / 2 < platform.y + platform.height / 2 && self.x + self.width / 2 > platform.x - platform.width / 2 && self.x - self.width / 2 < platform.x + platform.width / 2) {
if (self.vy > 0) {
self.y = platform.y - platform.height / 2 - self.height / 2;
self.vy = 0;
self.grounded = true;
}
}
}
// Patrol back and forth
if (self.x > self.patrolEnd || self.x < self.patrolStart) {
self.vx *= -1;
}
// Animate based on direction
if (self.vx > 0) {
enemyGraphics.scaleX = 1;
} else {
enemyGraphics.scaleX = -1;
}
};
return self;
});
var Platform = Container.expand(function () {
var self = Container.call(this);
var platformGraphics = self.attachAsset('platform', {
anchorX: 0.5,
anchorY: 0.5
});
self.width = platformGraphics.width;
self.height = platformGraphics.height;
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.vx = 0;
self.vy = 0;
self.speed = 7;
self.jumpPower = -20;
self.gravity = 1;
self.grounded = false;
self.facingRight = true;
self.pushCooldown = 0;
self.maxPushCooldown = 30;
self.width = playerGraphics.width;
self.height = playerGraphics.height;
self.jumpCount = 0;
self.maxJumps = 4;
self.jump = function () {
if (self.grounded || self.jumpCount < self.maxJumps) {
self.vy = self.jumpPower;
self.grounded = false;
self.jumpCount++;
LK.getSound('jump').play();
}
};
self.pushAttack = function () {
if (self.pushCooldown <= 0) {
var attack = new PushAttack();
attack.x = self.x + (self.facingRight ? 70 : -70);
attack.y = self.y;
attack.direction = self.facingRight ? 1 : -1;
game.addChild(attack);
pushAttacks.push(attack);
self.pushCooldown = self.maxPushCooldown;
LK.getSound('push').play();
}
};
self.update = function () {
// Apply gravity
self.vy += self.gravity;
// Apply velocity
self.x += self.vx;
self.y += self.vy;
// Handle push cooldown
if (self.pushCooldown > 0) {
self.pushCooldown--;
}
// Check platform collisions
self.grounded = false;
for (var i = 0; i < platforms.length; i++) {
var platform = platforms[i];
if (self.y + self.height / 2 > platform.y - platform.height / 2 && self.y - self.height / 2 < platform.y + platform.height / 2 && self.x + self.width / 2 > platform.x - platform.width / 2 && self.x - self.width / 2 < platform.x + platform.width / 2) {
// Only handle collision if falling
if (self.vy > 0) {
self.y = platform.y - platform.height / 2 - self.height / 2;
self.vy = 0;
self.grounded = true;
self.jumpCount = 0;
}
}
}
// Screen boundaries
if (self.x - self.width / 2 < 0) {
self.x = self.width / 2;
} else if (self.x + self.width / 2 > 2048) {
self.x = 2048 - self.width / 2;
}
// Check for collisions with enemies
for (var i = 0; i < enemies.length; i++) {
var enemy = enemies[i];
if (self.intersects(enemy)) {
// Player is defeated
LK.getSound('defeat').play();
LK.effects.flashScreen(0xFF0000, 500);
resetPlayer();
break;
}
}
// If falling off screen
if (self.y > 2732 + 100) {
LK.getSound('fall').play();
LK.effects.flashScreen(0xFF0000, 500);
resetPlayer();
}
// Update facing direction based on movement
if (self.vx > 0) {
self.facingRight = true;
} else if (self.vx < 0) {
self.facingRight = false;
}
// Animate player based on facing direction
if (self.facingRight) {
playerGraphics.scaleX = 1;
} else {
playerGraphics.scaleX = -1;
}
};
return self;
});
var PushAttack = Container.expand(function () {
var self = Container.call(this);
var attackGraphics = self.attachAsset('pushAttack', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.7
});
self.active = true;
self.lifespan = 0;
self.maxLife = 15;
self.direction = 1; // 1 for right, -1 for left
self.speed = 15;
self.update = function () {
if (self.active) {
self.x += self.speed * self.direction;
self.lifespan++;
if (self.lifespan >= self.maxLife) {
self.active = false;
self.visible = false;
}
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Black background
});
/****
* Game Code
****/
// Game variables
var player;
var platforms = [];
var enemies = [];
var pushAttacks = [];
var level = 1;
var score = 0;
var scoreText;
var levelText;
var leftButton;
var rightButton;
var jumpButton;
var pushButton;
var controlsVisible = true;
// Create UI elements
function createUI() {
// Score display
scoreText = new Text2('Score: 0', {
size: 70,
fill: 0xFFFFFF
});
scoreText.anchor.set(1, 0);
LK.gui.topRight.addChild(scoreText);
// Level display
levelText = new Text2('Level: 1', {
size: 70,
fill: 0xFFFFFF
});
levelText.anchor.set(0, 0);
LK.gui.top.addChild(levelText);
// Mobile controls (only shown on touch devices)
createMobileControls();
}
function createMobileControls() {
// Left movement button
leftButton = LK.getAsset('platform', {
anchorX: 0.5,
anchorY: 0.5,
width: 200,
height: 200,
alpha: 0.5,
tint: 0x0000FF
});
leftButton.x = 200;
LK.gui.bottomLeft.addChild(leftButton);
// Right movement button
rightButton = LK.getAsset('platform', {
anchorX: 0.5,
anchorY: 0.5,
width: 200,
height: 200,
alpha: 0.5,
tint: 0x0000FF
});
rightButton.x = 400;
LK.gui.bottomLeft.addChild(rightButton);
// Jump button
jumpButton = LK.getAsset('platform', {
anchorX: 0.5,
anchorY: 0.5,
width: 200,
height: 200,
alpha: 0.5,
tint: 0x0000FF
});
jumpButton.x = -400;
LK.gui.bottomRight.addChild(jumpButton);
// Push attack button
pushButton = LK.getAsset('platform', {
anchorX: 0.5,
anchorY: 0.5,
width: 200,
height: 200,
alpha: 0.5,
tint: 0x0000FF
});
pushButton.x = -200;
LK.gui.bottomRight.addChild(pushButton);
// Add touch event listeners to buttons
leftButton.down = function () {
leftButtonPressed = true;
};
leftButton.up = function () {
leftButtonPressed = false;
};
rightButton.down = function () {
rightButtonPressed = true;
};
rightButton.up = function () {
rightButtonPressed = false;
};
jumpButton.down = function () {
player.jump();
};
pushButton.down = function () {
player.pushAttack();
};
}
// Generate a level
function generateLevel() {
// Clear existing level elements
clearLevel();
// Create platforms
var platformCount = 5 + Math.min(5, Math.floor(level / 2));
var minHeight = 2000; // Start platforms from this height upward
var maxHeight = 800; // Don't go higher than this
for (var i = 0; i < platformCount; i++) {
var platform = new Platform();
// Distribute platforms evenly across width
platform.x = i * (2048 / (platformCount - 1));
// Randomize height between minHeight and maxHeight
var height = minHeight - (minHeight - maxHeight) * (i / (platformCount - 1));
height += Math.random() * 200 - 100; // Add some variation
platform.y = height;
// Ensure ground platform is always at the bottom
if (i === 0) {
platform.x = 1024; // Center
platform.y = 2500; // Bottom
platform.width = 2048; // Full width
}
game.addChild(platform);
platforms.push(platform);
// Add enemies to platforms (except the ground platform)
if (i > 0 && Math.random() < 0.7 + level * 0.05) {
var enemy = new Enemy();
enemy.x = platform.x;
enemy.y = platform.y - platform.height / 2 - enemy.height / 2;
enemy.patrolStart = platform.x - platform.width / 2 + enemy.width / 2;
enemy.patrolEnd = platform.x + platform.width / 2 - enemy.width / 2;
// Increase enemy speed based on level
enemy.vx = 2 + level * 0.5;
if (Math.random() > 0.5) {
enemy.vx *= -1;
}
game.addChild(enemy);
enemies.push(enemy);
}
}
// Update level text
levelText.setText('Level: ' + level);
}
// Clear all level elements
function clearLevel() {
// Remove all platforms
for (var i = 0; i < platforms.length; i++) {
platforms[i].destroy();
}
platforms = [];
// Remove all enemies
for (var i = 0; i < enemies.length; i++) {
enemies[i].destroy();
}
enemies = [];
// Remove all push attacks
for (var i = 0; i < pushAttacks.length; i++) {
pushAttacks[i].destroy();
}
pushAttacks = [];
}
// Create the player
function createPlayer() {
player = new Player();
player.x = 1024; // Center of screen
player.y = 2300; // Above the ground platform
game.addChild(player);
}
// Reset player position
function resetPlayer() {
player.x = 1024;
player.y = 2300;
player.vx = 0;
player.vy = 0;
}
// Initialize the game
function initGame() {
// Add background image
var background = LK.getAsset('background', {
anchorX: 0.5,
anchorY: 0.5
});
background.x = 1024; // Center of screen
background.y = 1366; // Center of screen
game.addChild(background);
// Reset game state
level = 1;
score = 0;
LK.setScore(0);
// Create UI
createUI();
// Generate first level
generateLevel();
// Create player
createPlayer();
// Play background music
LK.playMusic('gameMusic', {
fade: {
start: 0,
end: 0.7,
duration: 1000
}
});
}
// Check if all enemies are defeated
function checkLevelComplete() {
if (enemies.length === 0) {
// Level complete
level++;
// Show level up message
var levelUpText = new Text2('Level ' + level + '!', {
size: 150,
fill: 0xFFFF00
});
levelUpText.anchor.set(0.5, 0.5);
levelUpText.x = 1024;
levelUpText.y = 1366;
game.addChild(levelUpText);
// Remove the text after a delay
LK.setTimeout(function () {
levelUpText.destroy();
generateLevel();
}, 2000);
}
}
// Control the player with touch/mouse
function handlePlayerMovement() {
// Reset horizontal velocity
player.vx = 0;
// Check for active touch on left/right buttons
if (leftButtonPressed) {
player.vx = -player.speed;
}
if (rightButtonPressed) {
player.vx = player.speed;
}
}
// Handle touch/mouse inputs
var leftButtonPressed = false;
var rightButtonPressed = false;
var jumpButtonPressed = false;
var pushButtonPressed = false;
var touchPositions = {};
game.down = function (x, y, obj) {
var touchId = Date.now(); // Generate unique ID for this touch
touchPositions[touchId] = {
x: x,
y: y,
target: null
};
// Check which button was pressed
if (leftButton.getBounds().contains(x, y)) {
leftButtonPressed = true;
touchPositions[touchId].target = 'left';
} else if (rightButton.getBounds().contains(x, y)) {
rightButtonPressed = true;
touchPositions[touchId].target = 'right';
} else if (jumpButton.getBounds().contains(x, y)) {
player.jump();
touchPositions[touchId].target = 'jump';
} else if (pushButton.getBounds().contains(x, y)) {
player.pushAttack();
touchPositions[touchId].target = 'push';
}
};
game.up = function (x, y, obj) {
// Find which touch this corresponds to
for (var id in touchPositions) {
var touch = touchPositions[id];
var target = touch.target;
// Reset the appropriate state based on which button was released
if (target === 'left') {
leftButtonPressed = false;
} else if (target === 'right') {
rightButtonPressed = false;
}
delete touchPositions[id];
break;
}
};
game.move = function (x, y, obj) {
// This method intentionally left empty - we use down/up for controls
};
// Update game state each frame
game.update = function () {
// Handle player movement
handlePlayerMovement();
// Update player
if (player) {
player.update();
}
// Update enemies
for (var i = 0; i < enemies.length; i++) {
enemies[i].update();
}
// Update push attacks and check for collisions
for (var i = pushAttacks.length - 1; i >= 0; i--) {
var attack = pushAttacks[i];
if (!attack.active) {
attack.destroy();
pushAttacks.splice(i, 1);
continue;
}
attack.update();
// Check for collisions with enemies
for (var j = enemies.length - 1; j >= 0; j--) {
var enemy = enemies[j];
if (attack.intersects(enemy)) {
// Defeated enemy
LK.getSound('defeat').play();
// Add score
score += 100;
LK.setScore(score);
scoreText.setText('Score: ' + score);
// Effect
LK.effects.flashObject(enemy, 0xFF0000, 500);
// Remove enemy
enemy.destroy();
enemies.splice(j, 1);
// Remove attack
attack.active = false;
// Check if level is complete
checkLevelComplete();
}
}
}
};
// Initialize the game
initGame(); /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.vx = 1; // Slowed down enemy speed
self.patrolStart = 0;
self.patrolEnd = 500;
self.width = enemyGraphics.width;
self.height = enemyGraphics.height;
self.update = function () {
self.x += self.vx;
// Add jumping capability
if (self.grounded && Math.random() < 0.01) {
// Random chance to jump
self.vy = -15; // Jump power
self.grounded = false;
}
self.vy += self.gravity; // Apply gravity
self.y += self.vy; // Apply vertical velocity
// Check if enemy lands on a platform
self.grounded = false;
for (var i = 0; i < platforms.length; i++) {
var platform = platforms[i];
if (self.y + self.height / 2 > platform.y - platform.height / 2 && self.y - self.height / 2 < platform.y + platform.height / 2 && self.x + self.width / 2 > platform.x - platform.width / 2 && self.x - self.width / 2 < platform.x + platform.width / 2) {
if (self.vy > 0) {
self.y = platform.y - platform.height / 2 - self.height / 2;
self.vy = 0;
self.grounded = true;
}
}
}
// Patrol back and forth
if (self.x > self.patrolEnd || self.x < self.patrolStart) {
self.vx *= -1;
}
// Animate based on direction
if (self.vx > 0) {
enemyGraphics.scaleX = 1;
} else {
enemyGraphics.scaleX = -1;
}
};
return self;
});
var Platform = Container.expand(function () {
var self = Container.call(this);
var platformGraphics = self.attachAsset('platform', {
anchorX: 0.5,
anchorY: 0.5
});
self.width = platformGraphics.width;
self.height = platformGraphics.height;
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.vx = 0;
self.vy = 0;
self.speed = 7;
self.jumpPower = -20;
self.gravity = 1;
self.grounded = false;
self.facingRight = true;
self.pushCooldown = 0;
self.maxPushCooldown = 30;
self.width = playerGraphics.width;
self.height = playerGraphics.height;
self.jumpCount = 0;
self.maxJumps = 4;
self.jump = function () {
if (self.grounded || self.jumpCount < self.maxJumps) {
self.vy = self.jumpPower;
self.grounded = false;
self.jumpCount++;
LK.getSound('jump').play();
}
};
self.pushAttack = function () {
if (self.pushCooldown <= 0) {
var attack = new PushAttack();
attack.x = self.x + (self.facingRight ? 70 : -70);
attack.y = self.y;
attack.direction = self.facingRight ? 1 : -1;
game.addChild(attack);
pushAttacks.push(attack);
self.pushCooldown = self.maxPushCooldown;
LK.getSound('push').play();
}
};
self.update = function () {
// Apply gravity
self.vy += self.gravity;
// Apply velocity
self.x += self.vx;
self.y += self.vy;
// Handle push cooldown
if (self.pushCooldown > 0) {
self.pushCooldown--;
}
// Check platform collisions
self.grounded = false;
for (var i = 0; i < platforms.length; i++) {
var platform = platforms[i];
if (self.y + self.height / 2 > platform.y - platform.height / 2 && self.y - self.height / 2 < platform.y + platform.height / 2 && self.x + self.width / 2 > platform.x - platform.width / 2 && self.x - self.width / 2 < platform.x + platform.width / 2) {
// Only handle collision if falling
if (self.vy > 0) {
self.y = platform.y - platform.height / 2 - self.height / 2;
self.vy = 0;
self.grounded = true;
self.jumpCount = 0;
}
}
}
// Screen boundaries
if (self.x - self.width / 2 < 0) {
self.x = self.width / 2;
} else if (self.x + self.width / 2 > 2048) {
self.x = 2048 - self.width / 2;
}
// Check for collisions with enemies
for (var i = 0; i < enemies.length; i++) {
var enemy = enemies[i];
if (self.intersects(enemy)) {
// Player is defeated
LK.getSound('defeat').play();
LK.effects.flashScreen(0xFF0000, 500);
resetPlayer();
break;
}
}
// If falling off screen
if (self.y > 2732 + 100) {
LK.getSound('fall').play();
LK.effects.flashScreen(0xFF0000, 500);
resetPlayer();
}
// Update facing direction based on movement
if (self.vx > 0) {
self.facingRight = true;
} else if (self.vx < 0) {
self.facingRight = false;
}
// Animate player based on facing direction
if (self.facingRight) {
playerGraphics.scaleX = 1;
} else {
playerGraphics.scaleX = -1;
}
};
return self;
});
var PushAttack = Container.expand(function () {
var self = Container.call(this);
var attackGraphics = self.attachAsset('pushAttack', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.7
});
self.active = true;
self.lifespan = 0;
self.maxLife = 15;
self.direction = 1; // 1 for right, -1 for left
self.speed = 15;
self.update = function () {
if (self.active) {
self.x += self.speed * self.direction;
self.lifespan++;
if (self.lifespan >= self.maxLife) {
self.active = false;
self.visible = false;
}
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Black background
});
/****
* Game Code
****/
// Game variables
var player;
var platforms = [];
var enemies = [];
var pushAttacks = [];
var level = 1;
var score = 0;
var scoreText;
var levelText;
var leftButton;
var rightButton;
var jumpButton;
var pushButton;
var controlsVisible = true;
// Create UI elements
function createUI() {
// Score display
scoreText = new Text2('Score: 0', {
size: 70,
fill: 0xFFFFFF
});
scoreText.anchor.set(1, 0);
LK.gui.topRight.addChild(scoreText);
// Level display
levelText = new Text2('Level: 1', {
size: 70,
fill: 0xFFFFFF
});
levelText.anchor.set(0, 0);
LK.gui.top.addChild(levelText);
// Mobile controls (only shown on touch devices)
createMobileControls();
}
function createMobileControls() {
// Left movement button
leftButton = LK.getAsset('platform', {
anchorX: 0.5,
anchorY: 0.5,
width: 200,
height: 200,
alpha: 0.5,
tint: 0x0000FF
});
leftButton.x = 200;
LK.gui.bottomLeft.addChild(leftButton);
// Right movement button
rightButton = LK.getAsset('platform', {
anchorX: 0.5,
anchorY: 0.5,
width: 200,
height: 200,
alpha: 0.5,
tint: 0x0000FF
});
rightButton.x = 400;
LK.gui.bottomLeft.addChild(rightButton);
// Jump button
jumpButton = LK.getAsset('platform', {
anchorX: 0.5,
anchorY: 0.5,
width: 200,
height: 200,
alpha: 0.5,
tint: 0x0000FF
});
jumpButton.x = -400;
LK.gui.bottomRight.addChild(jumpButton);
// Push attack button
pushButton = LK.getAsset('platform', {
anchorX: 0.5,
anchorY: 0.5,
width: 200,
height: 200,
alpha: 0.5,
tint: 0x0000FF
});
pushButton.x = -200;
LK.gui.bottomRight.addChild(pushButton);
// Add touch event listeners to buttons
leftButton.down = function () {
leftButtonPressed = true;
};
leftButton.up = function () {
leftButtonPressed = false;
};
rightButton.down = function () {
rightButtonPressed = true;
};
rightButton.up = function () {
rightButtonPressed = false;
};
jumpButton.down = function () {
player.jump();
};
pushButton.down = function () {
player.pushAttack();
};
}
// Generate a level
function generateLevel() {
// Clear existing level elements
clearLevel();
// Create platforms
var platformCount = 5 + Math.min(5, Math.floor(level / 2));
var minHeight = 2000; // Start platforms from this height upward
var maxHeight = 800; // Don't go higher than this
for (var i = 0; i < platformCount; i++) {
var platform = new Platform();
// Distribute platforms evenly across width
platform.x = i * (2048 / (platformCount - 1));
// Randomize height between minHeight and maxHeight
var height = minHeight - (minHeight - maxHeight) * (i / (platformCount - 1));
height += Math.random() * 200 - 100; // Add some variation
platform.y = height;
// Ensure ground platform is always at the bottom
if (i === 0) {
platform.x = 1024; // Center
platform.y = 2500; // Bottom
platform.width = 2048; // Full width
}
game.addChild(platform);
platforms.push(platform);
// Add enemies to platforms (except the ground platform)
if (i > 0 && Math.random() < 0.7 + level * 0.05) {
var enemy = new Enemy();
enemy.x = platform.x;
enemy.y = platform.y - platform.height / 2 - enemy.height / 2;
enemy.patrolStart = platform.x - platform.width / 2 + enemy.width / 2;
enemy.patrolEnd = platform.x + platform.width / 2 - enemy.width / 2;
// Increase enemy speed based on level
enemy.vx = 2 + level * 0.5;
if (Math.random() > 0.5) {
enemy.vx *= -1;
}
game.addChild(enemy);
enemies.push(enemy);
}
}
// Update level text
levelText.setText('Level: ' + level);
}
// Clear all level elements
function clearLevel() {
// Remove all platforms
for (var i = 0; i < platforms.length; i++) {
platforms[i].destroy();
}
platforms = [];
// Remove all enemies
for (var i = 0; i < enemies.length; i++) {
enemies[i].destroy();
}
enemies = [];
// Remove all push attacks
for (var i = 0; i < pushAttacks.length; i++) {
pushAttacks[i].destroy();
}
pushAttacks = [];
}
// Create the player
function createPlayer() {
player = new Player();
player.x = 1024; // Center of screen
player.y = 2300; // Above the ground platform
game.addChild(player);
}
// Reset player position
function resetPlayer() {
player.x = 1024;
player.y = 2300;
player.vx = 0;
player.vy = 0;
}
// Initialize the game
function initGame() {
// Add background image
var background = LK.getAsset('background', {
anchorX: 0.5,
anchorY: 0.5
});
background.x = 1024; // Center of screen
background.y = 1366; // Center of screen
game.addChild(background);
// Reset game state
level = 1;
score = 0;
LK.setScore(0);
// Create UI
createUI();
// Generate first level
generateLevel();
// Create player
createPlayer();
// Play background music
LK.playMusic('gameMusic', {
fade: {
start: 0,
end: 0.7,
duration: 1000
}
});
}
// Check if all enemies are defeated
function checkLevelComplete() {
if (enemies.length === 0) {
// Level complete
level++;
// Show level up message
var levelUpText = new Text2('Level ' + level + '!', {
size: 150,
fill: 0xFFFF00
});
levelUpText.anchor.set(0.5, 0.5);
levelUpText.x = 1024;
levelUpText.y = 1366;
game.addChild(levelUpText);
// Remove the text after a delay
LK.setTimeout(function () {
levelUpText.destroy();
generateLevel();
}, 2000);
}
}
// Control the player with touch/mouse
function handlePlayerMovement() {
// Reset horizontal velocity
player.vx = 0;
// Check for active touch on left/right buttons
if (leftButtonPressed) {
player.vx = -player.speed;
}
if (rightButtonPressed) {
player.vx = player.speed;
}
}
// Handle touch/mouse inputs
var leftButtonPressed = false;
var rightButtonPressed = false;
var jumpButtonPressed = false;
var pushButtonPressed = false;
var touchPositions = {};
game.down = function (x, y, obj) {
var touchId = Date.now(); // Generate unique ID for this touch
touchPositions[touchId] = {
x: x,
y: y,
target: null
};
// Check which button was pressed
if (leftButton.getBounds().contains(x, y)) {
leftButtonPressed = true;
touchPositions[touchId].target = 'left';
} else if (rightButton.getBounds().contains(x, y)) {
rightButtonPressed = true;
touchPositions[touchId].target = 'right';
} else if (jumpButton.getBounds().contains(x, y)) {
player.jump();
touchPositions[touchId].target = 'jump';
} else if (pushButton.getBounds().contains(x, y)) {
player.pushAttack();
touchPositions[touchId].target = 'push';
}
};
game.up = function (x, y, obj) {
// Find which touch this corresponds to
for (var id in touchPositions) {
var touch = touchPositions[id];
var target = touch.target;
// Reset the appropriate state based on which button was released
if (target === 'left') {
leftButtonPressed = false;
} else if (target === 'right') {
rightButtonPressed = false;
}
delete touchPositions[id];
break;
}
};
game.move = function (x, y, obj) {
// This method intentionally left empty - we use down/up for controls
};
// Update game state each frame
game.update = function () {
// Handle player movement
handlePlayerMovement();
// Update player
if (player) {
player.update();
}
// Update enemies
for (var i = 0; i < enemies.length; i++) {
enemies[i].update();
}
// Update push attacks and check for collisions
for (var i = pushAttacks.length - 1; i >= 0; i--) {
var attack = pushAttacks[i];
if (!attack.active) {
attack.destroy();
pushAttacks.splice(i, 1);
continue;
}
attack.update();
// Check for collisions with enemies
for (var j = enemies.length - 1; j >= 0; j--) {
var enemy = enemies[j];
if (attack.intersects(enemy)) {
// Defeated enemy
LK.getSound('defeat').play();
// Add score
score += 100;
LK.setScore(score);
scoreText.setText('Score: ' + score);
// Effect
LK.effects.flashObject(enemy, 0xFF0000, 500);
// Remove enemy
enemy.destroy();
enemies.splice(j, 1);
// Remove attack
attack.active = false;
// Check if level is complete
checkLevelComplete();
}
}
}
};
// Initialize the game
initGame();