/****
* Classes
****/
// Class for the Ball
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocity = {
x: 0,
y: 0
};
self.update = function () {
self.x += self.velocity.x;
self.y += self.velocity.y;
self.velocity.x *= 0.98; // Friction
self.velocity.y *= 0.98; // Friction
};
});
// Class for the Control Strip
var ControlStrip = Container.expand(function () {
var self = Container.call(this);
var stripGraphics = self.attachAsset('controlStrip', {
anchorX: 0.5,
anchorY: 0.5
});
self.isSelected = false;
self.startDragPosition = null;
self.down = function (x, y, obj) {
self.isSelected = true;
self.startDragPosition = {
x: x,
y: y
};
};
self.up = function (x, y, obj) {
if (self.isSelected) {
var dy = self.startDragPosition.y - y;
self.height = dy;
self.isSelected = false;
}
};
});
// Class for the Goal
var Goal = Container.expand(function () {
var self = Container.call(this);
var goalGraphics = self.attachAsset('goal', {
anchorX: 0.5,
anchorY: 0.5
});
});
// Class for the Opponent Button
var OpponentButton = Container.expand(function () {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('opponentButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Simple AI logic to move towards the ball
var dx = ball.x - self.x;
var dy = ball.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 0) {
self.x += dx / distance * 2;
self.y += dy / distance * 2;
}
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Class for a Player Button
var PlayerButton = Container.expand(function () {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('playerButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.isSelected = false;
self.startDragPosition = null;
self.down = function (x, y, obj) {
self.isSelected = true;
self.startDragPosition = {
x: x,
y: y
};
};
self.up = function (x, y, obj) {
if (self.isSelected) {
var dx = x - self.startDragPosition.x;
var dy = y - self.startDragPosition.y;
self.x += dx;
self.y += dy;
self.isSelected = false;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x008000 // Green background for the pitch
});
/****
* Game Code
****/
// Initialize players, ball and goals
var playerButtons = [];
var opponentButtons = [];
var ball = game.addChild(new Ball());
var playerGoal = game.addChild(new Goal());
var opponentGoal = game.addChild(new Goal());
// Create player buttons
for (var i = 0; i < 11; i++) {
var playerButton = new PlayerButton();
if (i < 4) {
playerButton.x = 200 + i * 150;
playerButton.y = 1366;
} else if (i < 7) {
playerButton.x = 350 + (i - 4) * 150;
playerButton.y = 1366 + 150;
} else {
playerButton.x = 200 + (i - 7) * 150;
playerButton.y = 1366 + 300;
}
playerButtons.push(playerButton);
game.addChild(playerButton);
}
// Create opponent buttons
for (var i = 0; i < 11; i++) {
var opponentButton = new OpponentButton();
if (i == 0) {
// Goalkeeper
opponentButton.x = 2048 / 2;
opponentButton.y = 1000 - 100;
} else {
opponentButton.x = 200 + (i - 1) % 4 * 150;
opponentButton.y = 1000 + Math.floor((i - 1) / 4) * 150;
}
opponentButtons.push(opponentButton);
game.addChild(opponentButton);
}
// Position the ball and the goals
ball.x = 2048 / 2;
ball.y = 2732 / 2;
playerGoal.x = 2048 / 2;
playerGoal.y = 2732 - 100; // 100 pixels from the bottom
opponentGoal.x = 2048 / 2;
opponentGoal.y = 100; // 100 pixels from the top
var controlStrip = game.addChild(new ControlStrip());
controlStrip.x = 2048 - 50;
controlStrip.y = 0;
// Game update loop
game.update = function () {
ball.update();
opponentButtons.forEach(function (button) {
button.update();
});
// Check if the ball has entered a goal
if (ball.intersects(playerGoal)) {
// The opponent has scored a goal
LK.setScore(LK.getScore() - 1);
} else if (ball.intersects(opponentGoal)) {
// The player has scored a goal
LK.setScore(LK.getScore() + 1);
}
if (controlStrip.isSelected) {
var dy = controlStrip.startDragPosition.y - controlStrip.y;
playerButtons.forEach(function (button) {
if (button.isSelected) {
button.y -= dy;
}
});
}
// Add frame to the edges of the map
playerButtons.forEach(function (button) {
if (button.x < 50) {
button.x = 50;
}
if (button.y < 50) {
button.y = 50;
}
if (button.x > 2048 - 50) {
button.x = 2048 - 50;
}
if (button.y > 2732 - 50) {
button.y = 2732 - 50;
}
});
opponentButtons.forEach(function (button) {
if (button.x < 50) {
button.x = 50;
}
if (button.y < 50) {
button.y = 50;
}
if (button.x > 2048 - 50) {
button.x = 2048 - 50;
}
if (button.y > 2732 - 50) {
button.y = 2732 - 50;
}
});
};
// Handle player button selection
game.down = function (x, y, obj) {
playerButtons.forEach(function (button) {
if (button.intersects(obj)) {
button.isSelected = true;
button.startDragPosition = {
x: x,
y: y
};
}
});
if (controlStrip.intersects(obj)) {
controlStrip.isSelected = true;
controlStrip.startDragPosition = {
x: x,
y: y
};
}
};
// Handle player button release
game.up = function (x, y, obj) {
playerButtons.forEach(function (button) {
if (button.isSelected) {
button.isSelected = false;
}
});
if (controlStrip.isSelected) {
controlStrip.isSelected = false;
}
};
game.move = function (x, y, obj) {
playerButtons.forEach(function (button) {
if (button.isSelected) {
var dx = (x - button.startDragPosition.x) / 4;
var dy = (y - button.startDragPosition.y) / 4;
button.x += dx;
button.y += dy;
button.startDragPosition = {
x: x,
y: y
};
}
});
if (controlStrip.isSelected) {
var dy = controlStrip.startDragPosition.y - y;
controlStrip.height = dy;
}
};
game.down = function (x, y, obj) {
if (controlStrip.intersects(obj)) {
controlStrip.isSelected = true;
controlStrip.startDragPosition = {
x: x,
y: y
};
controlStrip.height = 1;
}
};
game.up = function (x, y, obj) {
if (controlStrip.isSelected) {
controlStrip.isSelected = false;
var shotStrength = Math.min(controlStrip.height, 500); // Limit the shotStrength to a maximum of 500
controlStrip.height = 2732;
playerButtons.forEach(function (button) {
if (button.isSelected) {
button.y -= shotStrength;
}
});
}
};