/****
* Classes
****/
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Fidget Spinner class
var FidgetSpinner = Container.expand(function () {
var self = Container.call(this);
var spinnerGraphics = self.attachAsset('spinner', {
anchorX: 0.5,
anchorY: 0.5
});
self.rotationSpeed = 0;
self.maxSpeed = 0.2;
self.friction = 0.99;
self.update = function () {
self.rotation += self.rotationSpeed;
self.rotationSpeed *= self.friction;
self.x += self.velocity.x;
self.y += self.velocity.y;
self.velocity.x *= 0.95;
self.velocity.y *= 0.95;
};
self.spin = function (force) {
self.rotationSpeed += force;
if (self.rotationSpeed > self.maxSpeed) {
self.rotationSpeed = self.maxSpeed;
}
};
self.velocity = {
x: 0,
y: 0
};
self.boosted = false;
self.moveTo = function (x, y) {
self.velocity.x = (x - self.x) * 0.01;
self.velocity.y = (y - self.y) * 0.01;
};
self.boost = function () {
self.boosted = true;
self.maxSpeed = 0.4;
LK.setTimeout(function () {
self.boosted = false;
self.maxSpeed = 0.2;
}, 3000);
};
});
// Opponent class
var Opponent = Container.expand(function () {
var self = Container.call(this);
var opponentGraphics = self.attachAsset('opponent', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2;
self.update = function () {
// Calculate the direction towards the spinner
var dx = spinner.x - self.x;
var dy = spinner.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
// Normalize the direction
if (distance > 0) {
dx /= distance;
dy /= distance;
}
// Move towards the spinner
self.x += dx * self.speed;
self.y += dy * self.speed;
// Make the opponent spin
self.rotation += 0.04;
if (self.intersects(spinner)) {
// Show game over. The game will be automatically paused while game over is showing.
LK.showGameOver(); // Calling this will destroy the 'Game' and reset entire game state.
self.destroy();
self.destroyed = true;
}
};
});
// Point class
var Point = Container.expand(function () {
var self = Container.call(this);
// Randomly choose between golden and green colors
var color = Math.random() < 0.5 ? 0xFFD700 : 0x008000;
self.attachAsset('point', {
width: 10,
height: 10,
color: color,
shape: 'ellipse'
});
self.update = function () {
// Check if the point intersects with the spinner
if (self.intersects(spinner)) {
// Increase the score
score++;
// Update the score text
scoreTxt.setText(score);
// Boost the spinner
spinner.boost();
// Remove the point from the game
self.destroy();
// Remove an opponent from the map
if (opponents.length > 0) {
var opponentToRemove = opponents.pop();
opponentToRemove.destroy();
}
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize spinner
var spinner = game.addChild(new FidgetSpinner());
spinner.x = 2048 / 2;
spinner.y = 2732 / 2;
// Score display
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Game variables
var score = 0;
var lastRotation = 0;
var lives = 3;
// Initialize opponents array
var opponents = [];
// Update score based on spinner speed
function updateScore() {
var rotationDelta = Math.abs(spinner.rotation - lastRotation);
score += Math.floor(rotationDelta * 3.125);
scoreTxt.setText(score);
lastRotation = spinner.rotation;
}
// Handle touch events to spin the spinner
game.down = function (x, y, obj) {
var force = 0.05; // Adjust this value to change the spin force
spinner.spin(force);
spinner.moveTo(x, y);
};
// Game update loop
game.update = function () {
updateScore();
// Add a new point every 100 ticks
if (LK.ticks % 100 == 0) {
var point = game.addChild(new Point());
// Position the point at a random location ensuring at least 100 units distance from the edge
point.x = Math.random() * (2048 - 200) + 100;
point.y = Math.random() * (2732 - 200) + 100;
}
// Add a new opponent every 200 ticks
if (LK.ticks % 200 == 0 && opponents.length < 3) {
var opponent = game.addChild(new Opponent());
// Position the opponent at a random location on the top of the screen
opponent.x = Math.random() * 2048;
opponent.y = 0;
opponents.push(opponent);
}
// Remove opponents that are destroyed
for (var i = opponents.length - 1; i >= 0; i--) {
if (opponents[i].destroyed) {
opponents.splice(i, 1);
}
}
};
// Reset game state
function resetGame() {
score = 0;
scoreTxt.setText(score);
spinner.rotationSpeed = 0;
lastRotation = 0;
}
// Automatically reset game when game over or win is triggered
LK.on('gameOver', resetGame);
LK.on('youWin', resetGame);