/****
* Classes
****/
var Hand = Container.expand(function (player, isLeft, fingers) {
var self = Container.call(this);
self.player = player;
self.isLeft = isLeft;
self.fingers = fingers || 1;
self.isEliminated = false;
self.isSelected = false;
self.handGraphics = null;
self.selectedOutline = null;
self.eliminatedOverlay = null;
self.fingerDots = [];
self.updateDisplay = function () {
// Remove all existing hand graphics
for (var i = 0; i < self.fingerDots.length; i++) {
self.removeChild(self.fingerDots[i]);
}
self.fingerDots = [];
if (self.handGraphics) {
self.removeChild(self.handGraphics);
}
if (self.selectedOutline) {
self.removeChild(self.selectedOutline);
}
if (self.eliminatedOverlay) {
self.removeChild(self.eliminatedOverlay);
}
// Create hand palm
self.handGraphics = self.attachAsset('handPalm', {
anchorX: 0.5,
anchorY: 0.5
});
// Create fingers (5 total, positioned from left to right with better spacing)
var fingerPositions = [-60, -30, 0, 30, 60];
for (var i = 0; i < 5; i++) {
var isFingerUp = i < self.fingers && !self.isEliminated;
var fingerAsset = isFingerUp ? 'fingerUp' : 'fingerDown';
var fingerY, anchorY;
// Player 2 hands face down (flipped), Player 1 hands face up
if (self.player === 2) {
fingerY = isFingerUp ? 110 : 70;
anchorY = 0.0;
} else {
fingerY = isFingerUp ? -110 : -70;
anchorY = 1.0;
}
var finger = self.attachAsset(fingerAsset, {
anchorX: 0.5,
anchorY: anchorY,
x: fingerPositions[i],
y: fingerY
});
// Make down fingers more visually distinct
if (!isFingerUp) {
finger.alpha = 0.6;
}
self.fingerDots.push(finger);
}
// Add selection outline if selected
if (self.isSelected) {
self.selectedOutline = self.attachAsset('selectedHandOutline', {
anchorX: 0.5,
anchorY: 0.5
});
self.selectedOutline.alpha = 0.3;
}
// Add eliminated overlay if eliminated
if (self.isEliminated) {
self.eliminatedOverlay = self.attachAsset('eliminatedOverlay', {
anchorX: 0.5,
anchorY: 0.5
});
self.eliminatedOverlay.alpha = 0.7;
}
};
self.setFingers = function (count) {
self.fingers = count;
if (self.fingers >= 5) {
if (self.fingers === 5) {
self.isEliminated = true;
LK.getSound('eliminate').play();
} else {
self.fingers = self.fingers % 5;
}
}
self.updateDisplay();
};
self.revive = function (count) {
self.isEliminated = false;
self.fingers = count;
self.updateDisplay();
};
self.setSelected = function (selected) {
self.isSelected = selected;
self.updateDisplay();
};
self.down = function (x, y, obj) {
if (!self.isEliminated) {
if (currentPlayer === self.player && gamePhase === 'selectOwn') {
// Select own hand
selectedHand = self;
gamePhase = 'selectTarget';
clearSelection();
self.setSelected(true);
LK.getSound('select').play();
} else if (currentPlayer !== self.player && gamePhase === 'selectTarget') {
// Attack opponent's hand
var damage = selectedHand.fingers;
self.setFingers(self.fingers + damage);
LK.getSound('attack').play();
clearSelection();
checkWinCondition();
if (!gameEnded) {
switchPlayer();
}
}
}
};
self.updateDisplay();
return self;
});
var RedistributeButton = Container.expand(function () {
var self = Container.call(this);
self.buttonGraphics = self.attachAsset('redistributeButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.buttonText = new Text2('Redistribute', {
size: 40,
fill: '#ffffff'
});
self.buttonText.anchor.set(0.5, 0.5);
self.addChild(self.buttonText);
self.down = function (x, y, obj) {
if (canRedistribute()) {
redistribute();
LK.getSound('select').play();
switchPlayer();
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x2c3e50
});
/****
* Game Code
****/
// Game state variables
var currentPlayer = 1;
var gamePhase = 'selectOwn'; // 'selectOwn', 'selectTarget'
var selectedHand = null;
var gameEnded = false;
// Player hands
var player1LeftHand, player1RightHand;
var player2LeftHand, player2RightHand;
var redistributeButton;
// UI elements
var turnText, instructionText;
// Initialize hands
player1LeftHand = game.addChild(new Hand(1, true, 1));
player1RightHand = game.addChild(new Hand(1, false, 1));
player2LeftHand = game.addChild(new Hand(2, true, 1));
player2RightHand = game.addChild(new Hand(2, false, 1));
// Position hands
player1LeftHand.x = 2048 / 2 - 150;
player1LeftHand.y = 2732 - 200;
player1RightHand.x = 2048 / 2 + 150;
player1RightHand.y = 2732 - 200;
player2LeftHand.x = 2048 / 2 - 150;
player2LeftHand.y = 200;
player2RightHand.x = 2048 / 2 + 150;
player2RightHand.y = 200;
// Create redistribute button
redistributeButton = game.addChild(new RedistributeButton());
redistributeButton.x = 2048 / 2;
redistributeButton.y = 2732 / 2 + 100;
redistributeButton.visible = false;
// UI Text
turnText = new Text2('Player 1 Turn', {
size: 60,
fill: '#ffffff'
});
turnText.anchor.set(0.5, 0.5);
turnText.x = 2048 / 2;
turnText.y = 2732 / 2 - 100;
game.addChild(turnText);
instructionText = new Text2('Select your hand', {
size: 40,
fill: '#ffffff'
});
instructionText.anchor.set(0.5, 0.5);
instructionText.x = 2048 / 2;
instructionText.y = 2732 / 2 - 40;
game.addChild(instructionText);
function clearSelection() {
player1LeftHand.setSelected(false);
player1RightHand.setSelected(false);
player2LeftHand.setSelected(false);
player2RightHand.setSelected(false);
}
function switchPlayer() {
currentPlayer = currentPlayer === 1 ? 2 : 1;
gamePhase = 'selectOwn';
selectedHand = null;
turnText.setText('Player ' + currentPlayer + ' Turn');
instructionText.setText('Select your hand');
redistributeButton.visible = canRedistribute();
// Position redistribute button on current player's side
if (currentPlayer === 1) {
redistributeButton.y = 2732 - 350; // Near player 1
} else {
redistributeButton.y = 350; // Near player 2
}
}
function canRedistribute() {
var hands = getCurrentPlayerHands();
var activeHand = null;
var eliminatedCount = 0;
for (var i = 0; i < hands.length; i++) {
if (hands[i].isEliminated) {
eliminatedCount++;
} else {
activeHand = hands[i];
}
}
return eliminatedCount === 1 && activeHand && activeHand.fingers % 2 === 0 && activeHand.fingers > 0;
}
function redistribute() {
var hands = getCurrentPlayerHands();
var activeHand = null;
var eliminatedHand = null;
for (var i = 0; i < hands.length; i++) {
if (hands[i].isEliminated) {
eliminatedHand = hands[i];
} else {
activeHand = hands[i];
}
}
if (activeHand && eliminatedHand) {
var fingersToRedistribute = activeHand.fingers / 2;
activeHand.setFingers(fingersToRedistribute);
eliminatedHand.revive(fingersToRedistribute);
}
redistributeButton.visible = false;
}
// Set initial redistribute button position for player 1
redistributeButton.y = 2732 - 350;
function getCurrentPlayerHands() {
if (currentPlayer === 1) {
return [player1LeftHand, player1RightHand];
} else {
return [player2LeftHand, player2RightHand];
}
}
function checkWinCondition() {
// Check if player 1 lost
if (player1LeftHand.isEliminated && player1RightHand.isEliminated) {
gameEnded = true;
turnText.setText('Player 2 Wins!');
instructionText.setText('Game Over');
LK.getSound('win').play();
LK.setTimeout(function () {
LK.showYouWin();
}, 2000);
}
// Check if player 2 lost
else if (player2LeftHand.isEliminated && player2RightHand.isEliminated) {
gameEnded = true;
turnText.setText('Player 1 Wins!');
instructionText.setText('Game Over');
LK.getSound('win').play();
LK.setTimeout(function () {
LK.showYouWin();
}, 2000);
}
}
game.update = function () {
if (gameEnded) return;
// Update instruction text based on game phase
if (gamePhase === 'selectOwn') {
if (canRedistribute()) {
instructionText.setText('Select your hand or redistribute');
} else {
instructionText.setText('Select your hand');
}
} else if (gamePhase === 'selectTarget') {
instructionText.setText('Select opponent hand to attack');
}
// Update redistribute button visibility
redistributeButton.visible = gamePhase === 'selectOwn' && canRedistribute();
}; /****
* Classes
****/
var Hand = Container.expand(function (player, isLeft, fingers) {
var self = Container.call(this);
self.player = player;
self.isLeft = isLeft;
self.fingers = fingers || 1;
self.isEliminated = false;
self.isSelected = false;
self.handGraphics = null;
self.selectedOutline = null;
self.eliminatedOverlay = null;
self.fingerDots = [];
self.updateDisplay = function () {
// Remove all existing hand graphics
for (var i = 0; i < self.fingerDots.length; i++) {
self.removeChild(self.fingerDots[i]);
}
self.fingerDots = [];
if (self.handGraphics) {
self.removeChild(self.handGraphics);
}
if (self.selectedOutline) {
self.removeChild(self.selectedOutline);
}
if (self.eliminatedOverlay) {
self.removeChild(self.eliminatedOverlay);
}
// Create hand palm
self.handGraphics = self.attachAsset('handPalm', {
anchorX: 0.5,
anchorY: 0.5
});
// Create fingers (5 total, positioned from left to right with better spacing)
var fingerPositions = [-60, -30, 0, 30, 60];
for (var i = 0; i < 5; i++) {
var isFingerUp = i < self.fingers && !self.isEliminated;
var fingerAsset = isFingerUp ? 'fingerUp' : 'fingerDown';
var fingerY, anchorY;
// Player 2 hands face down (flipped), Player 1 hands face up
if (self.player === 2) {
fingerY = isFingerUp ? 110 : 70;
anchorY = 0.0;
} else {
fingerY = isFingerUp ? -110 : -70;
anchorY = 1.0;
}
var finger = self.attachAsset(fingerAsset, {
anchorX: 0.5,
anchorY: anchorY,
x: fingerPositions[i],
y: fingerY
});
// Make down fingers more visually distinct
if (!isFingerUp) {
finger.alpha = 0.6;
}
self.fingerDots.push(finger);
}
// Add selection outline if selected
if (self.isSelected) {
self.selectedOutline = self.attachAsset('selectedHandOutline', {
anchorX: 0.5,
anchorY: 0.5
});
self.selectedOutline.alpha = 0.3;
}
// Add eliminated overlay if eliminated
if (self.isEliminated) {
self.eliminatedOverlay = self.attachAsset('eliminatedOverlay', {
anchorX: 0.5,
anchorY: 0.5
});
self.eliminatedOverlay.alpha = 0.7;
}
};
self.setFingers = function (count) {
self.fingers = count;
if (self.fingers >= 5) {
if (self.fingers === 5) {
self.isEliminated = true;
LK.getSound('eliminate').play();
} else {
self.fingers = self.fingers % 5;
}
}
self.updateDisplay();
};
self.revive = function (count) {
self.isEliminated = false;
self.fingers = count;
self.updateDisplay();
};
self.setSelected = function (selected) {
self.isSelected = selected;
self.updateDisplay();
};
self.down = function (x, y, obj) {
if (!self.isEliminated) {
if (currentPlayer === self.player && gamePhase === 'selectOwn') {
// Select own hand
selectedHand = self;
gamePhase = 'selectTarget';
clearSelection();
self.setSelected(true);
LK.getSound('select').play();
} else if (currentPlayer !== self.player && gamePhase === 'selectTarget') {
// Attack opponent's hand
var damage = selectedHand.fingers;
self.setFingers(self.fingers + damage);
LK.getSound('attack').play();
clearSelection();
checkWinCondition();
if (!gameEnded) {
switchPlayer();
}
}
}
};
self.updateDisplay();
return self;
});
var RedistributeButton = Container.expand(function () {
var self = Container.call(this);
self.buttonGraphics = self.attachAsset('redistributeButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.buttonText = new Text2('Redistribute', {
size: 40,
fill: '#ffffff'
});
self.buttonText.anchor.set(0.5, 0.5);
self.addChild(self.buttonText);
self.down = function (x, y, obj) {
if (canRedistribute()) {
redistribute();
LK.getSound('select').play();
switchPlayer();
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x2c3e50
});
/****
* Game Code
****/
// Game state variables
var currentPlayer = 1;
var gamePhase = 'selectOwn'; // 'selectOwn', 'selectTarget'
var selectedHand = null;
var gameEnded = false;
// Player hands
var player1LeftHand, player1RightHand;
var player2LeftHand, player2RightHand;
var redistributeButton;
// UI elements
var turnText, instructionText;
// Initialize hands
player1LeftHand = game.addChild(new Hand(1, true, 1));
player1RightHand = game.addChild(new Hand(1, false, 1));
player2LeftHand = game.addChild(new Hand(2, true, 1));
player2RightHand = game.addChild(new Hand(2, false, 1));
// Position hands
player1LeftHand.x = 2048 / 2 - 150;
player1LeftHand.y = 2732 - 200;
player1RightHand.x = 2048 / 2 + 150;
player1RightHand.y = 2732 - 200;
player2LeftHand.x = 2048 / 2 - 150;
player2LeftHand.y = 200;
player2RightHand.x = 2048 / 2 + 150;
player2RightHand.y = 200;
// Create redistribute button
redistributeButton = game.addChild(new RedistributeButton());
redistributeButton.x = 2048 / 2;
redistributeButton.y = 2732 / 2 + 100;
redistributeButton.visible = false;
// UI Text
turnText = new Text2('Player 1 Turn', {
size: 60,
fill: '#ffffff'
});
turnText.anchor.set(0.5, 0.5);
turnText.x = 2048 / 2;
turnText.y = 2732 / 2 - 100;
game.addChild(turnText);
instructionText = new Text2('Select your hand', {
size: 40,
fill: '#ffffff'
});
instructionText.anchor.set(0.5, 0.5);
instructionText.x = 2048 / 2;
instructionText.y = 2732 / 2 - 40;
game.addChild(instructionText);
function clearSelection() {
player1LeftHand.setSelected(false);
player1RightHand.setSelected(false);
player2LeftHand.setSelected(false);
player2RightHand.setSelected(false);
}
function switchPlayer() {
currentPlayer = currentPlayer === 1 ? 2 : 1;
gamePhase = 'selectOwn';
selectedHand = null;
turnText.setText('Player ' + currentPlayer + ' Turn');
instructionText.setText('Select your hand');
redistributeButton.visible = canRedistribute();
// Position redistribute button on current player's side
if (currentPlayer === 1) {
redistributeButton.y = 2732 - 350; // Near player 1
} else {
redistributeButton.y = 350; // Near player 2
}
}
function canRedistribute() {
var hands = getCurrentPlayerHands();
var activeHand = null;
var eliminatedCount = 0;
for (var i = 0; i < hands.length; i++) {
if (hands[i].isEliminated) {
eliminatedCount++;
} else {
activeHand = hands[i];
}
}
return eliminatedCount === 1 && activeHand && activeHand.fingers % 2 === 0 && activeHand.fingers > 0;
}
function redistribute() {
var hands = getCurrentPlayerHands();
var activeHand = null;
var eliminatedHand = null;
for (var i = 0; i < hands.length; i++) {
if (hands[i].isEliminated) {
eliminatedHand = hands[i];
} else {
activeHand = hands[i];
}
}
if (activeHand && eliminatedHand) {
var fingersToRedistribute = activeHand.fingers / 2;
activeHand.setFingers(fingersToRedistribute);
eliminatedHand.revive(fingersToRedistribute);
}
redistributeButton.visible = false;
}
// Set initial redistribute button position for player 1
redistributeButton.y = 2732 - 350;
function getCurrentPlayerHands() {
if (currentPlayer === 1) {
return [player1LeftHand, player1RightHand];
} else {
return [player2LeftHand, player2RightHand];
}
}
function checkWinCondition() {
// Check if player 1 lost
if (player1LeftHand.isEliminated && player1RightHand.isEliminated) {
gameEnded = true;
turnText.setText('Player 2 Wins!');
instructionText.setText('Game Over');
LK.getSound('win').play();
LK.setTimeout(function () {
LK.showYouWin();
}, 2000);
}
// Check if player 2 lost
else if (player2LeftHand.isEliminated && player2RightHand.isEliminated) {
gameEnded = true;
turnText.setText('Player 1 Wins!');
instructionText.setText('Game Over');
LK.getSound('win').play();
LK.setTimeout(function () {
LK.showYouWin();
}, 2000);
}
}
game.update = function () {
if (gameEnded) return;
// Update instruction text based on game phase
if (gamePhase === 'selectOwn') {
if (canRedistribute()) {
instructionText.setText('Select your hand or redistribute');
} else {
instructionText.setText('Select your hand');
}
} else if (gamePhase === 'selectTarget') {
instructionText.setText('Select opponent hand to attack');
}
// Update redistribute button visibility
redistributeButton.visible = gamePhase === 'selectOwn' && canRedistribute();
};