/****
* Classes
****/
var Dot = Container.expand(function () {
var self = Container.call(this);
self.handleCollisions = function () {
var items = dots;
for (var i = 0; i < items.length; i++) {
if (items[i] !== self) {
var dx = items[i].x - self.x;
var dy = items[i].y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < self.width / 2 + items[i].width / 2) {}
}
}
};
var dotGraphics = self.attachAsset('whiteDot', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocity = {
x: 0,
y: 0
};
self.mass = 1; // Assuming a unit mass for realistic physics simulation
self.applyForce = function (force) {
self.velocity.x += force.x / self.mass;
self.velocity.y += force.y / self.mass;
};
self.dragging = false;
self.color = Math.random() * 0xFFFFFF; // Assign a unique color to each dot
self.update = function () {
if (!self.dragging) {
self.x += self.velocity.x;
self.y += self.velocity.y;
// Apply friction to simulate realistic physics
var friction = 0.98;
self.velocity.x *= friction;
self.velocity.y *= friction;
// Keep the dot within the game boundaries
if (self.x < 0) {
self.x = 0;
self.velocity.x *= -1;
} else if (self.x > 2048) {
self.x = 2048;
self.velocity.x *= -1;
}
if (self.y < 0) {
self.y = 0;
self.velocity.y *= -1;
} else if (self.y > 2732) {
self.y = 2732;
self.velocity.y *= -1;
}
self.handleCollisions();
// Add a trail effect
var trail = LK.getAsset('whiteDot', {
anchorX: 0.5,
anchorY: 0.5,
tint: self.color,
// Use the dot's color for the trail
alpha: 0.5
});
trail.x = self.x;
trail.y = self.y;
if (self.parent) {
game.addChildAt(trail, game.getChildIndex(self));
}
LK.setTimeout(function () {
trail.destroy();
self.destroy();
}, 6000);
}
};
self.handleDrag = function (pos) {
self.x = pos.x;
self.y = pos.y;
self.handleCollisions();
};
});
/****
* Initialize Game
****/
var game = new LK.Game();
/****
* Game Code
****/
var dots = [];
var touchStartPos = null;
// Function to handle the start of a touch
function handleTouchStart(obj) {
var event = obj.event;
touchStartPos = event.getLocalPosition(game);
for (var i = 0; i < dots.length; i++) {
var dot = dots[i];
var dx = touchStartPos.x - dot.x;
var dy = touchStartPos.y - dot.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < dot.width / 2) {
dot.dragging = true;
break;
}
}
}
// Function to handle the end of a touch
function handleTouchEnd(obj) {
if (touchStartPos) {
var event = obj.event;
var touchEndPos = event.getLocalPosition(game);
var force = {
x: touchEndPos.x - touchStartPos.x,
y: touchEndPos.y - touchStartPos.y
};
for (var i = 0; i < dots.length; i++) {
var dot = dots[i];
if (dot.dragging) {
dot.dragging = false;
dot.applyForce(force);
break;
}
}
if (!dot || !dot.dragging) {
var newDot = new Dot();
newDot.x = touchStartPos.x;
newDot.y = touchStartPos.y;
newDot.applyForce(force);
newDot.trailColor = Math.random() * 0xFFFFFF;
dots.push(newDot);
game.addChild(newDot);
}
touchStartPos = null;
}
}
// Add event listeners for touch start and end
game.on('down', handleTouchStart);
game.on('up', handleTouchEnd);
// Update function for the game
LK.on('tick', function () {
for (var i = dots.length - 1; i >= 0; i--) {
dots[i].update();
if (dots[i].dragging) {
dots[i].handleDrag(touchStartPos);
}
}
});