/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var CharacterSelectionButton = Container.expand(function (characterName, x, y) {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('selectionButton', {
anchorX: 0.5,
anchorY: 0.5
});
var buttonText = new Text2(characterName, {
size: 40,
fill: 0xFFFFFF
});
buttonText.anchor.set(0.5, 0.5);
self.addChild(buttonText);
self.x = x;
self.y = y;
self.characterName = characterName;
self.isSelected = false;
self.down = function (x, y, obj) {
if (!gameStarted) {
selectCharacter(self.characterName);
}
};
self.setSelected = function (selected) {
self.isSelected = selected;
buttonGraphics.tint = selected ? 0xFFFF00 : 0x4CAF50;
};
return self;
});
var SoccerBall = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('soccerBall', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocityX = 0;
self.velocityY = 0;
self.friction = 0.95;
self.lastX = 0;
self.lastY = 0;
self.update = function () {
self.lastX = self.x;
self.lastY = self.y;
self.x += self.velocityX;
self.y += self.velocityY;
self.velocityX *= self.friction;
self.velocityY *= self.friction;
// Stop very slow movement
if (Math.abs(self.velocityX) < 0.1) self.velocityX = 0;
if (Math.abs(self.velocityY) < 0.1) self.velocityY = 0;
// Field boundaries
if (self.x < 150) {
self.x = 150;
self.velocityX = 0;
}
if (self.x > 1900) {
self.x = 1900;
self.velocityX = 0;
}
if (self.y < 800) {
self.y = 800;
self.velocityY = 0;
}
if (self.y > 2000) {
self.y = 2000;
self.velocityY = 0;
}
};
self.applyForce = function (forceX, forceY) {
self.velocityX += forceX;
self.velocityY += forceY;
};
return self;
});
var SoccerPlayer = Container.expand(function (characterName, playerColor) {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
if (playerColor) {
playerGraphics.tint = playerColor;
}
self.characterName = characterName;
self.isSelected = false;
self.isDragging = false;
self.speed = 3;
self.kickPower = 15;
self.select = function () {
self.isSelected = true;
playerGraphics.tint = 0xFFFF00; // Yellow when selected
};
self.deselect = function () {
self.isSelected = false;
playerGraphics.tint = playerColor || 0x2196F3;
};
self.kick = function (ballX, ballY) {
var dx = ballX - self.x;
var dy = ballY - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 100) {
// Close enough to kick
var force = self.kickPower / distance;
return {
x: dx * force,
y: dy * force
};
}
return null;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x388E3C
});
/****
* Game Code
****/
var gameStarted = false;
var selectionPhase = true;
var selectedCharacters = [];
var characterNames = ['Pando', 'Sula', 'Coco', 'Blossom', 'Bubbles', 'Buttercup'];
var selectionButtons = [];
var players = [];
var opponents = [];
var ball;
var selectedPlayer = null;
var draggedPlayer = null;
var goals = [];
var playerGoal;
var opponentGoal;
// Score tracking
var playerScore = 0;
var opponentScore = 0;
// UI Elements
var titleText = new Text2('Toca World Cup 2016', {
size: 80,
fill: 0xFFFFFF
});
titleText.anchor.set(0.5, 0.5);
titleText.x = 1024;
titleText.y = 200;
LK.gui.addChild(titleText);
var instructionText = new Text2('Select 3 characters for your team', {
size: 50,
fill: 0xFFFFFF
});
instructionText.anchor.set(0.5, 0.5);
instructionText.x = 1024;
instructionText.y = 300;
LK.gui.addChild(instructionText);
var scoreText = new Text2('0 - 0', {
size: 60,
fill: 0xFFFFFF
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
// Create character selection buttons
for (var i = 0; i < characterNames.length; i++) {
var buttonX = 200 + i % 3 * 400;
var buttonY = 600 + Math.floor(i / 3) * 150;
var button = new CharacterSelectionButton(characterNames[i], buttonX, buttonY);
selectionButtons.push(button);
game.addChild(button);
}
var startButton = new CharacterSelectionButton('START GAME', 1024, 1000);
startButton.setSelected(false);
game.addChild(startButton);
function selectCharacter(characterName) {
if (selectedCharacters.length < 3 && selectedCharacters.indexOf(characterName) === -1) {
selectedCharacters.push(characterName);
// Update button appearance
for (var i = 0; i < selectionButtons.length; i++) {
if (selectionButtons[i].characterName === characterName) {
selectionButtons[i].setSelected(true);
break;
}
}
// Update instruction text
if (selectedCharacters.length === 3) {
instructionText.setText('Tap START GAME to begin!');
startButton.setSelected(true);
} else {
instructionText.setText('Select ' + (3 - selectedCharacters.length) + ' more characters');
}
}
}
function startGame() {
if (selectedCharacters.length !== 3) return;
gameStarted = true;
selectionPhase = false;
// Hide selection UI
for (var i = 0; i < selectionButtons.length; i++) {
selectionButtons[i].visible = false;
}
startButton.visible = false;
titleText.visible = false;
instructionText.visible = false;
// Create soccer field
var field = game.attachAsset('soccerField', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1366
});
// Create field boundaries
var topBoundary = game.attachAsset('fieldBoundary', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 766
});
var bottomBoundary = game.attachAsset('fieldBoundary', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1966
});
// Create goals
playerGoal = game.attachAsset('goalPost', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 2000
});
opponentGoal = game.attachAsset('goalPost', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 800
});
// Create ball
ball = new SoccerBall();
ball.x = 1024;
ball.y = 1366;
game.addChild(ball);
// Create player team
var playerColors = [0x2196F3, 0x1976D2, 0x0D47A1];
for (var i = 0; i < 3; i++) {
var player = new SoccerPlayer(selectedCharacters[i], playerColors[i]);
player.x = 700 + i * 200;
player.y = 1600;
players.push(player);
game.addChild(player);
}
// Create opponent team
for (var i = 0; i < 3; i++) {
var opponent = new SoccerPlayer('Opponent' + (i + 1), 0xF44336);
opponent.x = 700 + i * 200;
opponent.y = 1100;
opponents.push(opponent);
game.addChild(opponent);
}
// Update instruction text for gameplay
instructionText.setText('Tap player to select, drag to move, tap again to kick!');
instructionText.visible = true;
}
// Handle start button
startButton.down = function (x, y, obj) {
if (selectedCharacters.length === 3) {
startGame();
}
};
function handlePlayerSelection(player) {
// Deselect all players
for (var i = 0; i < players.length; i++) {
players[i].deselect();
}
// Select clicked player
selectedPlayer = player;
player.select();
}
function handlePlayerKick(player) {
if (player === selectedPlayer) {
var kickResult = player.kick(ball.x, ball.y);
if (kickResult) {
ball.applyForce(kickResult.x, kickResult.y);
LK.getSound('kick').play();
}
}
}
// Game event handlers
game.down = function (x, y, obj) {
if (!gameStarted || selectionPhase) return;
// Check if clicking on a player
for (var i = 0; i < players.length; i++) {
var player = players[i];
var dx = x - player.x;
var dy = y - player.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 50) {
if (selectedPlayer === player) {
// Already selected - kick the ball
handlePlayerKick(player);
} else {
// Select this player
handlePlayerSelection(player);
draggedPlayer = player;
}
return;
}
}
};
game.move = function (x, y, obj) {
if (!gameStarted || selectionPhase) return;
if (draggedPlayer) {
// Keep player within field bounds
draggedPlayer.x = Math.max(150, Math.min(1900, x));
draggedPlayer.y = Math.max(800, Math.min(2000, y));
}
};
game.up = function (x, y, obj) {
draggedPlayer = null;
};
// Simple AI for opponents
var aiTimer = 0;
function updateOpponentAI() {
aiTimer++;
if (aiTimer % 60 === 0) {
// Update AI every second
for (var i = 0; i < opponents.length; i++) {
var opponent = opponents[i];
var dx = ball.x - opponent.x;
var dy = ball.y - opponent.y;
var distance = Math.sqrt(dx * dx + dy * dy);
// Move towards ball if close enough
if (distance < 300) {
var moveSpeed = 2;
opponent.x += dx / distance * moveSpeed;
opponent.y += dy / distance * moveSpeed;
// Keep within bounds
opponent.x = Math.max(150, Math.min(1900, opponent.x));
opponent.y = Math.max(800, Math.min(1600, opponent.y));
// Kick ball towards player goal if close
if (distance < 100 && Math.random() < 0.3) {
var kickForceX = (playerGoal.x - ball.x) * 0.3;
var kickForceY = (playerGoal.y - ball.y) * 0.3;
ball.applyForce(kickForceX, kickForceY);
}
}
}
}
}
function checkGoals() {
if (!ball) return;
var ballInPlayerGoal = ball.intersects(playerGoal);
var ballInOpponentGoal = ball.intersects(opponentGoal);
if (ballInPlayerGoal && !ball.lastInPlayerGoal) {
// Opponent scored
opponentScore++;
LK.getSound('goal').play();
resetBallPosition();
updateScore();
}
if (ballInOpponentGoal && !ball.lastInOpponentGoal) {
// Player scored
playerScore++;
LK.setScore(playerScore);
LK.getSound('goal').play();
resetBallPosition();
updateScore();
if (playerScore >= 3) {
LK.showYouWin();
}
}
ball.lastInPlayerGoal = ballInPlayerGoal;
ball.lastInOpponentGoal = ballInOpponentGoal;
}
function resetBallPosition() {
ball.x = 1024;
ball.y = 1366;
ball.velocityX = 0;
ball.velocityY = 0;
}
function updateScore() {
scoreText.setText(playerScore + ' - ' + opponentScore);
}
game.update = function () {
if (!gameStarted || selectionPhase) return;
updateOpponentAI();
checkGoals();
if (opponentScore >= 3) {
LK.showGameOver();
}
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var CharacterSelectionButton = Container.expand(function (characterName, x, y) {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('selectionButton', {
anchorX: 0.5,
anchorY: 0.5
});
var buttonText = new Text2(characterName, {
size: 40,
fill: 0xFFFFFF
});
buttonText.anchor.set(0.5, 0.5);
self.addChild(buttonText);
self.x = x;
self.y = y;
self.characterName = characterName;
self.isSelected = false;
self.down = function (x, y, obj) {
if (!gameStarted) {
selectCharacter(self.characterName);
}
};
self.setSelected = function (selected) {
self.isSelected = selected;
buttonGraphics.tint = selected ? 0xFFFF00 : 0x4CAF50;
};
return self;
});
var SoccerBall = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('soccerBall', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocityX = 0;
self.velocityY = 0;
self.friction = 0.95;
self.lastX = 0;
self.lastY = 0;
self.update = function () {
self.lastX = self.x;
self.lastY = self.y;
self.x += self.velocityX;
self.y += self.velocityY;
self.velocityX *= self.friction;
self.velocityY *= self.friction;
// Stop very slow movement
if (Math.abs(self.velocityX) < 0.1) self.velocityX = 0;
if (Math.abs(self.velocityY) < 0.1) self.velocityY = 0;
// Field boundaries
if (self.x < 150) {
self.x = 150;
self.velocityX = 0;
}
if (self.x > 1900) {
self.x = 1900;
self.velocityX = 0;
}
if (self.y < 800) {
self.y = 800;
self.velocityY = 0;
}
if (self.y > 2000) {
self.y = 2000;
self.velocityY = 0;
}
};
self.applyForce = function (forceX, forceY) {
self.velocityX += forceX;
self.velocityY += forceY;
};
return self;
});
var SoccerPlayer = Container.expand(function (characterName, playerColor) {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
if (playerColor) {
playerGraphics.tint = playerColor;
}
self.characterName = characterName;
self.isSelected = false;
self.isDragging = false;
self.speed = 3;
self.kickPower = 15;
self.select = function () {
self.isSelected = true;
playerGraphics.tint = 0xFFFF00; // Yellow when selected
};
self.deselect = function () {
self.isSelected = false;
playerGraphics.tint = playerColor || 0x2196F3;
};
self.kick = function (ballX, ballY) {
var dx = ballX - self.x;
var dy = ballY - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 100) {
// Close enough to kick
var force = self.kickPower / distance;
return {
x: dx * force,
y: dy * force
};
}
return null;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x388E3C
});
/****
* Game Code
****/
var gameStarted = false;
var selectionPhase = true;
var selectedCharacters = [];
var characterNames = ['Pando', 'Sula', 'Coco', 'Blossom', 'Bubbles', 'Buttercup'];
var selectionButtons = [];
var players = [];
var opponents = [];
var ball;
var selectedPlayer = null;
var draggedPlayer = null;
var goals = [];
var playerGoal;
var opponentGoal;
// Score tracking
var playerScore = 0;
var opponentScore = 0;
// UI Elements
var titleText = new Text2('Toca World Cup 2016', {
size: 80,
fill: 0xFFFFFF
});
titleText.anchor.set(0.5, 0.5);
titleText.x = 1024;
titleText.y = 200;
LK.gui.addChild(titleText);
var instructionText = new Text2('Select 3 characters for your team', {
size: 50,
fill: 0xFFFFFF
});
instructionText.anchor.set(0.5, 0.5);
instructionText.x = 1024;
instructionText.y = 300;
LK.gui.addChild(instructionText);
var scoreText = new Text2('0 - 0', {
size: 60,
fill: 0xFFFFFF
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
// Create character selection buttons
for (var i = 0; i < characterNames.length; i++) {
var buttonX = 200 + i % 3 * 400;
var buttonY = 600 + Math.floor(i / 3) * 150;
var button = new CharacterSelectionButton(characterNames[i], buttonX, buttonY);
selectionButtons.push(button);
game.addChild(button);
}
var startButton = new CharacterSelectionButton('START GAME', 1024, 1000);
startButton.setSelected(false);
game.addChild(startButton);
function selectCharacter(characterName) {
if (selectedCharacters.length < 3 && selectedCharacters.indexOf(characterName) === -1) {
selectedCharacters.push(characterName);
// Update button appearance
for (var i = 0; i < selectionButtons.length; i++) {
if (selectionButtons[i].characterName === characterName) {
selectionButtons[i].setSelected(true);
break;
}
}
// Update instruction text
if (selectedCharacters.length === 3) {
instructionText.setText('Tap START GAME to begin!');
startButton.setSelected(true);
} else {
instructionText.setText('Select ' + (3 - selectedCharacters.length) + ' more characters');
}
}
}
function startGame() {
if (selectedCharacters.length !== 3) return;
gameStarted = true;
selectionPhase = false;
// Hide selection UI
for (var i = 0; i < selectionButtons.length; i++) {
selectionButtons[i].visible = false;
}
startButton.visible = false;
titleText.visible = false;
instructionText.visible = false;
// Create soccer field
var field = game.attachAsset('soccerField', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1366
});
// Create field boundaries
var topBoundary = game.attachAsset('fieldBoundary', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 766
});
var bottomBoundary = game.attachAsset('fieldBoundary', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1966
});
// Create goals
playerGoal = game.attachAsset('goalPost', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 2000
});
opponentGoal = game.attachAsset('goalPost', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 800
});
// Create ball
ball = new SoccerBall();
ball.x = 1024;
ball.y = 1366;
game.addChild(ball);
// Create player team
var playerColors = [0x2196F3, 0x1976D2, 0x0D47A1];
for (var i = 0; i < 3; i++) {
var player = new SoccerPlayer(selectedCharacters[i], playerColors[i]);
player.x = 700 + i * 200;
player.y = 1600;
players.push(player);
game.addChild(player);
}
// Create opponent team
for (var i = 0; i < 3; i++) {
var opponent = new SoccerPlayer('Opponent' + (i + 1), 0xF44336);
opponent.x = 700 + i * 200;
opponent.y = 1100;
opponents.push(opponent);
game.addChild(opponent);
}
// Update instruction text for gameplay
instructionText.setText('Tap player to select, drag to move, tap again to kick!');
instructionText.visible = true;
}
// Handle start button
startButton.down = function (x, y, obj) {
if (selectedCharacters.length === 3) {
startGame();
}
};
function handlePlayerSelection(player) {
// Deselect all players
for (var i = 0; i < players.length; i++) {
players[i].deselect();
}
// Select clicked player
selectedPlayer = player;
player.select();
}
function handlePlayerKick(player) {
if (player === selectedPlayer) {
var kickResult = player.kick(ball.x, ball.y);
if (kickResult) {
ball.applyForce(kickResult.x, kickResult.y);
LK.getSound('kick').play();
}
}
}
// Game event handlers
game.down = function (x, y, obj) {
if (!gameStarted || selectionPhase) return;
// Check if clicking on a player
for (var i = 0; i < players.length; i++) {
var player = players[i];
var dx = x - player.x;
var dy = y - player.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 50) {
if (selectedPlayer === player) {
// Already selected - kick the ball
handlePlayerKick(player);
} else {
// Select this player
handlePlayerSelection(player);
draggedPlayer = player;
}
return;
}
}
};
game.move = function (x, y, obj) {
if (!gameStarted || selectionPhase) return;
if (draggedPlayer) {
// Keep player within field bounds
draggedPlayer.x = Math.max(150, Math.min(1900, x));
draggedPlayer.y = Math.max(800, Math.min(2000, y));
}
};
game.up = function (x, y, obj) {
draggedPlayer = null;
};
// Simple AI for opponents
var aiTimer = 0;
function updateOpponentAI() {
aiTimer++;
if (aiTimer % 60 === 0) {
// Update AI every second
for (var i = 0; i < opponents.length; i++) {
var opponent = opponents[i];
var dx = ball.x - opponent.x;
var dy = ball.y - opponent.y;
var distance = Math.sqrt(dx * dx + dy * dy);
// Move towards ball if close enough
if (distance < 300) {
var moveSpeed = 2;
opponent.x += dx / distance * moveSpeed;
opponent.y += dy / distance * moveSpeed;
// Keep within bounds
opponent.x = Math.max(150, Math.min(1900, opponent.x));
opponent.y = Math.max(800, Math.min(1600, opponent.y));
// Kick ball towards player goal if close
if (distance < 100 && Math.random() < 0.3) {
var kickForceX = (playerGoal.x - ball.x) * 0.3;
var kickForceY = (playerGoal.y - ball.y) * 0.3;
ball.applyForce(kickForceX, kickForceY);
}
}
}
}
}
function checkGoals() {
if (!ball) return;
var ballInPlayerGoal = ball.intersects(playerGoal);
var ballInOpponentGoal = ball.intersects(opponentGoal);
if (ballInPlayerGoal && !ball.lastInPlayerGoal) {
// Opponent scored
opponentScore++;
LK.getSound('goal').play();
resetBallPosition();
updateScore();
}
if (ballInOpponentGoal && !ball.lastInOpponentGoal) {
// Player scored
playerScore++;
LK.setScore(playerScore);
LK.getSound('goal').play();
resetBallPosition();
updateScore();
if (playerScore >= 3) {
LK.showYouWin();
}
}
ball.lastInPlayerGoal = ballInPlayerGoal;
ball.lastInOpponentGoal = ballInOpponentGoal;
}
function resetBallPosition() {
ball.x = 1024;
ball.y = 1366;
ball.velocityX = 0;
ball.velocityY = 0;
}
function updateScore() {
scoreText.setText(playerScore + ' - ' + opponentScore);
}
game.update = function () {
if (!gameStarted || selectionPhase) return;
updateOpponentAI();
checkGoals();
if (opponentScore >= 3) {
LK.showGameOver();
}
};