Code edit (1 edits merged)
Please save this source code
User prompt
Territory Tycoon
Initial prompt
bana tycoon tarzı oyun yap oyunun arka planı olsun ve karakterimiz olsun heryeri gezebilsin mauseyi takip etsin ve bir yer olsun o yer küçük bir daire hareket etmesin o nesne gibi bişey ve onun üstünde claim yazsın onun üstüne karakterimiz gelince onun olsun etrafa duvar gelsin
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 15;
self.targetX = 0;
self.targetY = 0;
self.update = function () {
var dx = self.targetX - self.x;
var dy = self.targetY - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 5) {
var moveX = dx / distance * self.speed;
var moveY = dy / distance * self.speed;
self.x += moveX;
self.y += moveY;
}
};
self.setTarget = function (x, y) {
self.targetX = x;
self.targetY = y;
};
return self;
});
var Character = Container.expand(function () {
var self = Container.call(this);
var characterGraphics = self.attachAsset('character', {
anchorX: 0.5,
anchorY: 0.5
});
self.targetX = 0;
self.targetY = 0;
self.moveSpeed = 8;
self.baseSpeed = 8;
self.carSpeed = 16;
self.inCar = false;
self.update = function () {
var dx = self.targetX - self.x;
var dy = self.targetY - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 5) {
var moveX = dx / distance * self.moveSpeed;
var moveY = dy / distance * self.moveSpeed;
var newX = self.x + moveX;
var newY = self.y + moveY;
// Keep character within bounds (accounting for character size)
if (newX >= 90 && newX <= 1958 && newY >= 90 && newY <= 2642) {
self.x = newX;
self.y = newY;
}
}
};
self.setTarget = function (x, y) {
self.targetX = x;
self.targetY = y;
};
self.applySpeedBoost = function () {
self.moveSpeed = self.carSpeed;
LK.setTimeout(function () {
self.moveSpeed = self.baseSpeed;
}, 5000); // Speed boost lasts 5 seconds
};
self.shoot = function (targetX, targetY) {
if (ammoCount > 0) {
var bullet = new Bullet();
bullet.x = self.x;
bullet.y = self.y;
bullet.setTarget(targetX, targetY);
bullets.push(bullet);
game.addChild(bullet);
ammoCount--;
updateAmmoDisplay();
}
};
return self;
});
var ClaimZone = Container.expand(function () {
var self = Container.call(this);
var zoneGraphics = self.attachAsset('claimZone', {
anchorX: 0.5,
anchorY: 0.5
});
var claimText = new Text2('CLAIM', {
size: 32,
fill: '#FFFFFF'
});
claimText.anchor.set(0.5, 1);
claimText.y = -45;
self.addChild(claimText);
// Add pulsing animation to make text more readable
function startClaimTextAnimation() {
tween(claimText, {
scaleX: 1.5,
scaleY: 1.5
}, {
duration: 800,
easing: tween.easeInOut,
onFinish: function onFinish() {
tween(claimText, {
scaleX: 1.0,
scaleY: 1.0
}, {
duration: 800,
easing: tween.easeInOut,
onFinish: startClaimTextAnimation
});
}
});
}
startClaimTextAnimation();
self.isClaimed = false;
self.hitsRequired = 2;
self.currentHits = 0;
self.hit = function () {
if (!self.isClaimed) {
self.currentHits++;
claimText.setText('CLAIM (' + self.currentHits + '/' + self.hitsRequired + ')');
if (self.currentHits >= self.hitsRequired) {
return self.claim();
}
}
return false;
};
self.claim = function () {
if (!self.isClaimed) {
self.isClaimed = true;
self.removeChild(zoneGraphics);
self.removeChild(claimText);
var claimedGraphics = self.attachAsset('claimedZone', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 1.0
});
var claimedText = new Text2('CLAIMED', {
size: 20,
fill: '#FFFFFF'
});
claimedText.anchor.set(0.5, 1);
claimedText.y = -40;
self.addChild(claimedText);
return true;
}
return false;
};
return self;
});
var Dog = Container.expand(function () {
var self = Container.call(this);
var dogGraphics = self.attachAsset('dog', {
anchorX: 0.5,
anchorY: 0.5
});
self.targetX = 0;
self.targetY = 0;
self.moveSpeed = 12;
self.isAttacking = false;
self.update = function () {
if (self.isAttacking && character) {
// Always chase the character's current position
self.targetX = character.x;
self.targetY = character.y;
var dx = self.targetX - self.x;
var dy = self.targetY - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 5) {
var moveX = dx / distance * self.moveSpeed;
var moveY = dy / distance * self.moveSpeed;
self.x += moveX;
self.y += moveY;
}
}
};
self.attackTarget = function (targetX, targetY) {
self.targetX = targetX;
self.targetY = targetY;
self.isAttacking = true;
};
return self;
});
var PowerUp = Container.expand(function () {
var self = Container.call(this);
var powerUpGraphics = self.attachAsset('powerUp', {
anchorX: 0.5,
anchorY: 0.5
});
var powerUpText = new Text2('SPEED', {
size: 24,
fill: '#FFFFFF'
});
powerUpText.anchor.set(0.5, 0.5);
self.addChild(powerUpText);
// Add pulsing animation to power up
function startPowerUpAnimation() {
tween(self, {
scaleX: 1.3,
scaleY: 1.3
}, {
duration: 600,
easing: tween.easeInOut,
onFinish: function onFinish() {
tween(self, {
scaleX: 1.0,
scaleY: 1.0
}, {
duration: 600,
easing: tween.easeInOut,
onFinish: startPowerUpAnimation
});
}
});
}
startPowerUpAnimation();
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x2E8B57
});
/****
* Game Code
****/
var character = new Character();
character.x = 1024;
character.y = 1366;
game.addChild(character);
var claimZones = [];
var totalZones = 5;
// Power up management
var powerUps = [];
// Game timer
var gameStartTime = Date.now();
var gameTimer = 0;
// Dog management
var dogs = [];
var isUnderAttack = false;
var attackOverlay = null;
var attackCountdown = 0;
var attackCountdownTimer = null;
// Weapon system
var bullets = [];
var ammoCount = 7;
var maxAmmo = 7;
// Ammo display
var ammoTxt = new Text2('Ammo: 7', {
size: 50,
fill: '#FFFFFF'
});
ammoTxt.anchor.set(1, 1);
LK.gui.bottomRight.addChild(ammoTxt);
function updateAmmoDisplay() {
ammoTxt.setText('Ammo: ' + ammoCount);
}
// Function to create power up at random location
function createPowerUp() {
var powerUp = new PowerUp();
var powerUpValidPosition = false;
var powerUpAttempts = 0;
while (!powerUpValidPosition && powerUpAttempts < 50) {
powerUp.x = 150 + Math.random() * 1748;
powerUp.y = 150 + Math.random() * 2432;
// Check distance from character
var powerUpDx = powerUp.x - character.x;
var powerUpDy = powerUp.y - character.y;
var distanceFromCharacter = Math.sqrt(powerUpDx * powerUpDx + powerUpDy * powerUpDy);
if (distanceFromCharacter > 200) {
powerUpValidPosition = true;
}
powerUpAttempts++;
}
powerUps.push(powerUp);
game.addChild(powerUp);
}
// Function to create attack screen
function createAttackScreen() {
if (attackOverlay) return;
isUnderAttack = true;
// Create semi-transparent overlay
attackOverlay = new Container();
var overlay = LK.getAsset('wall', {
anchorX: 0,
anchorY: 0,
x: 0,
y: 0,
width: 2048,
height: 2732,
alpha: 0.7,
tint: 0xFF0000
});
attackOverlay.addChild(overlay);
// Create tap text
var tapText = new Text2('TAP QUICKLY!', {
size: 120,
fill: '#FFFFFF'
});
tapText.anchor.set(0.5, 0.5);
tapText.x = 1024;
tapText.y = 1366;
attackOverlay.addChild(tapText);
// Create countdown text
var countdownText = new Text2('3', {
size: 150,
fill: '#FF0000'
});
countdownText.anchor.set(0.5, 0.5);
countdownText.x = 1024;
countdownText.y = 1500;
attackOverlay.addChild(countdownText);
attackOverlay.countdownText = countdownText;
// Update countdown display
var countdownUpdateTimer = LK.setInterval(function () {
if (attackOverlay && attackOverlay.countdownText) {
attackOverlay.countdownText.setText(attackCountdown.toString());
}
}, 100);
attackOverlay.countdownUpdateTimer = countdownUpdateTimer;
// Add pulsing animation
function startTapAnimation() {
tween(tapText, {
scaleX: 1.5,
scaleY: 1.5
}, {
duration: 300,
easing: tween.easeInOut,
onFinish: function onFinish() {
tween(tapText, {
scaleX: 1.0,
scaleY: 1.0
}, {
duration: 300,
easing: tween.easeInOut,
onFinish: startTapAnimation
});
}
});
}
startTapAnimation();
game.addChild(attackOverlay);
}
// Function to remove attack screen
function removeAttackScreen() {
if (attackOverlay) {
if (attackCountdownTimer) {
LK.clearInterval(attackCountdownTimer);
attackCountdownTimer = null;
}
if (attackOverlay.countdownUpdateTimer) {
LK.clearInterval(attackOverlay.countdownUpdateTimer);
}
attackOverlay.destroy();
attackOverlay = null;
isUnderAttack = false;
attackCountdown = 0;
}
}
// Function to create dog at random edge position
function createDog() {
var dog = new Dog();
// Spawn dog at random edge, far from character
var edge = Math.floor(Math.random() * 4); // 0=top, 1=right, 2=bottom, 3=left
switch (edge) {
case 0:
// top
dog.x = 150 + Math.random() * 1748;
dog.y = 150;
break;
case 1:
// right
dog.x = 1898;
dog.y = 150 + Math.random() * 2432;
break;
case 2:
// bottom
dog.x = 150 + Math.random() * 1748;
dog.y = 2582;
break;
case 3:
// left
dog.x = 150;
dog.y = 150 + Math.random() * 2432;
break;
}
dogs.push(dog);
game.addChild(dog);
}
// Spawn first power up immediately
createPowerUp();
// Set timer to spawn dogs every 10 seconds
LK.setInterval(function () {
if (dogs.length < 1) {
createDog();
}
}, 10000);
// Set timer to spawn power ups every 10 seconds
LK.setInterval(function () {
if (powerUps.length < 2) {
createPowerUp();
}
}, 10000);
// Set timer to spawn claim zones every 0.5 seconds
LK.setInterval(function () {
if (claimZones.length < 5) {
var zone = createClaimZone();
claimZones.push(zone);
game.addChild(zone);
}
}, 500);
// Create walls around the perimeter
var walls = [];
// Top wall
for (var i = 0; i < 42; i++) {
var topWall = LK.getAsset('wall', {
anchorX: 0.5,
anchorY: 0.5,
x: i * 50 + 25,
y: 25
});
game.addChild(topWall);
walls.push(topWall);
}
// Bottom wall
for (var i = 0; i < 42; i++) {
var bottomWall = LK.getAsset('wall', {
anchorX: 0.5,
anchorY: 0.5,
x: i * 50 + 25,
y: 2707
});
game.addChild(bottomWall);
walls.push(bottomWall);
}
// Left wall
for (var i = 1; i < 54; i++) {
var leftWall = LK.getAsset('wall', {
anchorX: 0.5,
anchorY: 0.5,
x: 25,
y: i * 50 + 25
});
game.addChild(leftWall);
walls.push(leftWall);
}
// Right wall
for (var i = 1; i < 54; i++) {
var rightWall = LK.getAsset('wall', {
anchorX: 0.5,
anchorY: 0.5,
x: 2023,
y: i * 50 + 25
});
game.addChild(rightWall);
walls.push(rightWall);
}
// Create initial claim zones
function createClaimZone() {
var zone = new ClaimZone();
var validPosition = false;
var attempts = 0;
while (!validPosition && attempts < 50) {
zone.x = 150 + Math.random() * 1748;
zone.y = 150 + Math.random() * 2432;
// Check distance from character
var dx = zone.x - character.x;
var dy = zone.y - character.y;
var distanceFromCharacter = Math.sqrt(dx * dx + dy * dy);
// Check distance from other zones
var tooClose = false;
for (var i = 0; i < claimZones.length; i++) {
var otherZone = claimZones[i];
var zdx = zone.x - otherZone.x;
var zdy = zone.y - otherZone.y;
var distanceFromZone = Math.sqrt(zdx * zdx + zdy * zdy);
if (distanceFromZone < 200) {
tooClose = true;
break;
}
}
if (distanceFromCharacter > 150 && !tooClose) {
validPosition = true;
}
attempts++;
}
return zone;
}
// Generate initial claim zones
for (var i = 0; i < totalZones; i++) {
var zone = createClaimZone();
claimZones.push(zone);
game.addChild(zone);
}
// Score display
var scoreTxt = new Text2('Territory: 0', {
size: 60,
fill: '#FFFFFF'
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Timer display
var timerTxt = new Text2('Time: 00:00', {
size: 50,
fill: '#FFFFFF'
});
timerTxt.anchor.set(0.5, 0);
timerTxt.y = 80;
LK.gui.top.addChild(timerTxt);
// Mouse/touch controls
game.move = function (x, y, obj) {
character.setTarget(x, y);
};
game.down = function (x, y, obj) {
if (isUnderAttack) {
// Handle attack screen tap
removeAttackScreen();
// Remove all dogs and give ammo reward
for (var d = dogs.length - 1; d >= 0; d--) {
dogs[d].destroy();
// Give 2 ammo clips (14 bullets) for killing dog
ammoCount = Math.min(ammoCount + 14, maxAmmo * 10); // Allow stacking up to 70 bullets
updateAmmoDisplay();
}
dogs = [];
} else {
// Shoot at target location
character.shoot(x, y);
}
};
// Main game loop
game.update = function () {
// Update timer
gameTimer = Math.floor((Date.now() - gameStartTime) / 1000);
var minutes = Math.floor(gameTimer / 60);
var seconds = gameTimer % 60;
var timeString = (minutes < 10 ? '0' : '') + minutes + ':' + (seconds < 10 ? '0' : '') + seconds;
timerTxt.setText('Time: ' + timeString);
// Check for power up collection
for (var p = powerUps.length - 1; p >= 0; p--) {
var powerUp = powerUps[p];
var powerUpDx = character.x - powerUp.x;
var powerUpDy = character.y - powerUp.y;
var powerUpDistance = Math.sqrt(powerUpDx * powerUpDx + powerUpDy * powerUpDy);
if (powerUpDistance < 75) {
character.applySpeedBoost();
powerUp.destroy();
powerUps.splice(p, 1);
}
}
// Handle dog attacks
for (var d = dogs.length - 1; d >= 0; d--) {
var dog = dogs[d];
if (!dog.isAttacking) {
dog.attackTarget(character.x, character.y);
}
// Check if dog reached character
var dogDx = character.x - dog.x;
var dogDy = character.y - dog.y;
var dogDistance = Math.sqrt(dogDx * dogDx + dogDy * dogDy);
if (dogDistance < 100 && !isUnderAttack) {
createAttackScreen();
// Start 3 second countdown
attackCountdown = 3;
attackCountdownTimer = LK.setInterval(function () {
attackCountdown--;
if (attackCountdown <= 0) {
LK.clearInterval(attackCountdownTimer);
// Game over if player doesn't tap in time
LK.showGameOver();
}
}, 1000);
}
}
// Handle attack screen taps
if (isUnderAttack) {
// This will be handled by the down event
}
// Handle bullet collisions and movement
for (var b = bullets.length - 1; b >= 0; b--) {
var bullet = bullets[b];
// Check bullet collision with claim zones
var bulletHitZone = false;
for (var z = 0; z < claimZones.length; z++) {
var zone = claimZones[z];
if (!zone.isClaimed) {
var bulletZoneDx = bullet.x - zone.x;
var bulletZoneDy = bullet.y - zone.y;
var bulletZoneDistance = Math.sqrt(bulletZoneDx * bulletZoneDx + bulletZoneDy * bulletZoneDy);
if (bulletZoneDistance < 60) {
if (zone.hit()) {
LK.setScore(LK.getScore() + 1);
if (LK.getScore() >= 15) {
scoreTxt.setText('Territory: 15 WIN');
} else {
scoreTxt.setText('Territory: ' + LK.getScore());
}
LK.getSound('claim').play();
// Remove claimed zone
claimZones.splice(z, 1);
zone.destroy();
// Check win condition
if (LK.getScore() >= 15) {
var currentTime = storage.bestTime || 999999;
if (gameTimer < currentTime) {
storage.bestTime = gameTimer;
}
var recordMinutes = Math.floor(storage.bestTime / 60);
var recordSeconds = storage.bestTime % 60;
var recordTimeString = (recordMinutes < 10 ? '0' : '') + recordMinutes + ':' + (recordSeconds < 10 ? '0' : '') + recordSeconds;
LK.showYouWin('Record: ' + recordTimeString);
}
}
bullet.destroy();
bullets.splice(b, 1);
bulletHitZone = true;
break;
}
}
}
// Remove bullets that go off screen
if (!bulletHitZone && (bullet.x < 0 || bullet.x > 2048 || bullet.y < 0 || bullet.y > 2732)) {
bullet.destroy();
bullets.splice(b, 1);
}
}
// Check for zone claims (keep original touch-based claiming as backup)
for (var i = claimZones.length - 1; i >= 0; i--) {
var zone = claimZones[i];
if (!zone.isClaimed) {
var dx = character.x - zone.x;
var dy = character.y - zone.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 75) {
if (zone.hit()) {
LK.setScore(LK.getScore() + 1);
if (LK.getScore() >= 15) {
scoreTxt.setText('Territory: 15 WIN');
} else {
scoreTxt.setText('Territory: ' + LK.getScore());
}
LK.getSound('claim').play();
// Remove claimed zone instead of creating new one
claimZones.splice(i, 1);
zone.destroy();
// Check win condition - game ends at 15 claims
if (LK.getScore() >= 15) {
// Store the record time
var currentTime = storage.bestTime || 999999;
if (gameTimer < currentTime) {
storage.bestTime = gameTimer;
}
// Format the record time for display
var recordMinutes = Math.floor(storage.bestTime / 60);
var recordSeconds = storage.bestTime % 60;
var recordTimeString = (recordMinutes < 10 ? '0' : '') + recordMinutes + ':' + (recordSeconds < 10 ? '0' : '') + recordSeconds;
LK.showYouWin('Record: ' + recordTimeString);
}
}
}
}
}
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 15;
self.targetX = 0;
self.targetY = 0;
self.update = function () {
var dx = self.targetX - self.x;
var dy = self.targetY - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 5) {
var moveX = dx / distance * self.speed;
var moveY = dy / distance * self.speed;
self.x += moveX;
self.y += moveY;
}
};
self.setTarget = function (x, y) {
self.targetX = x;
self.targetY = y;
};
return self;
});
var Character = Container.expand(function () {
var self = Container.call(this);
var characterGraphics = self.attachAsset('character', {
anchorX: 0.5,
anchorY: 0.5
});
self.targetX = 0;
self.targetY = 0;
self.moveSpeed = 8;
self.baseSpeed = 8;
self.carSpeed = 16;
self.inCar = false;
self.update = function () {
var dx = self.targetX - self.x;
var dy = self.targetY - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 5) {
var moveX = dx / distance * self.moveSpeed;
var moveY = dy / distance * self.moveSpeed;
var newX = self.x + moveX;
var newY = self.y + moveY;
// Keep character within bounds (accounting for character size)
if (newX >= 90 && newX <= 1958 && newY >= 90 && newY <= 2642) {
self.x = newX;
self.y = newY;
}
}
};
self.setTarget = function (x, y) {
self.targetX = x;
self.targetY = y;
};
self.applySpeedBoost = function () {
self.moveSpeed = self.carSpeed;
LK.setTimeout(function () {
self.moveSpeed = self.baseSpeed;
}, 5000); // Speed boost lasts 5 seconds
};
self.shoot = function (targetX, targetY) {
if (ammoCount > 0) {
var bullet = new Bullet();
bullet.x = self.x;
bullet.y = self.y;
bullet.setTarget(targetX, targetY);
bullets.push(bullet);
game.addChild(bullet);
ammoCount--;
updateAmmoDisplay();
}
};
return self;
});
var ClaimZone = Container.expand(function () {
var self = Container.call(this);
var zoneGraphics = self.attachAsset('claimZone', {
anchorX: 0.5,
anchorY: 0.5
});
var claimText = new Text2('CLAIM', {
size: 32,
fill: '#FFFFFF'
});
claimText.anchor.set(0.5, 1);
claimText.y = -45;
self.addChild(claimText);
// Add pulsing animation to make text more readable
function startClaimTextAnimation() {
tween(claimText, {
scaleX: 1.5,
scaleY: 1.5
}, {
duration: 800,
easing: tween.easeInOut,
onFinish: function onFinish() {
tween(claimText, {
scaleX: 1.0,
scaleY: 1.0
}, {
duration: 800,
easing: tween.easeInOut,
onFinish: startClaimTextAnimation
});
}
});
}
startClaimTextAnimation();
self.isClaimed = false;
self.hitsRequired = 2;
self.currentHits = 0;
self.hit = function () {
if (!self.isClaimed) {
self.currentHits++;
claimText.setText('CLAIM (' + self.currentHits + '/' + self.hitsRequired + ')');
if (self.currentHits >= self.hitsRequired) {
return self.claim();
}
}
return false;
};
self.claim = function () {
if (!self.isClaimed) {
self.isClaimed = true;
self.removeChild(zoneGraphics);
self.removeChild(claimText);
var claimedGraphics = self.attachAsset('claimedZone', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 1.0
});
var claimedText = new Text2('CLAIMED', {
size: 20,
fill: '#FFFFFF'
});
claimedText.anchor.set(0.5, 1);
claimedText.y = -40;
self.addChild(claimedText);
return true;
}
return false;
};
return self;
});
var Dog = Container.expand(function () {
var self = Container.call(this);
var dogGraphics = self.attachAsset('dog', {
anchorX: 0.5,
anchorY: 0.5
});
self.targetX = 0;
self.targetY = 0;
self.moveSpeed = 12;
self.isAttacking = false;
self.update = function () {
if (self.isAttacking && character) {
// Always chase the character's current position
self.targetX = character.x;
self.targetY = character.y;
var dx = self.targetX - self.x;
var dy = self.targetY - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 5) {
var moveX = dx / distance * self.moveSpeed;
var moveY = dy / distance * self.moveSpeed;
self.x += moveX;
self.y += moveY;
}
}
};
self.attackTarget = function (targetX, targetY) {
self.targetX = targetX;
self.targetY = targetY;
self.isAttacking = true;
};
return self;
});
var PowerUp = Container.expand(function () {
var self = Container.call(this);
var powerUpGraphics = self.attachAsset('powerUp', {
anchorX: 0.5,
anchorY: 0.5
});
var powerUpText = new Text2('SPEED', {
size: 24,
fill: '#FFFFFF'
});
powerUpText.anchor.set(0.5, 0.5);
self.addChild(powerUpText);
// Add pulsing animation to power up
function startPowerUpAnimation() {
tween(self, {
scaleX: 1.3,
scaleY: 1.3
}, {
duration: 600,
easing: tween.easeInOut,
onFinish: function onFinish() {
tween(self, {
scaleX: 1.0,
scaleY: 1.0
}, {
duration: 600,
easing: tween.easeInOut,
onFinish: startPowerUpAnimation
});
}
});
}
startPowerUpAnimation();
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x2E8B57
});
/****
* Game Code
****/
var character = new Character();
character.x = 1024;
character.y = 1366;
game.addChild(character);
var claimZones = [];
var totalZones = 5;
// Power up management
var powerUps = [];
// Game timer
var gameStartTime = Date.now();
var gameTimer = 0;
// Dog management
var dogs = [];
var isUnderAttack = false;
var attackOverlay = null;
var attackCountdown = 0;
var attackCountdownTimer = null;
// Weapon system
var bullets = [];
var ammoCount = 7;
var maxAmmo = 7;
// Ammo display
var ammoTxt = new Text2('Ammo: 7', {
size: 50,
fill: '#FFFFFF'
});
ammoTxt.anchor.set(1, 1);
LK.gui.bottomRight.addChild(ammoTxt);
function updateAmmoDisplay() {
ammoTxt.setText('Ammo: ' + ammoCount);
}
// Function to create power up at random location
function createPowerUp() {
var powerUp = new PowerUp();
var powerUpValidPosition = false;
var powerUpAttempts = 0;
while (!powerUpValidPosition && powerUpAttempts < 50) {
powerUp.x = 150 + Math.random() * 1748;
powerUp.y = 150 + Math.random() * 2432;
// Check distance from character
var powerUpDx = powerUp.x - character.x;
var powerUpDy = powerUp.y - character.y;
var distanceFromCharacter = Math.sqrt(powerUpDx * powerUpDx + powerUpDy * powerUpDy);
if (distanceFromCharacter > 200) {
powerUpValidPosition = true;
}
powerUpAttempts++;
}
powerUps.push(powerUp);
game.addChild(powerUp);
}
// Function to create attack screen
function createAttackScreen() {
if (attackOverlay) return;
isUnderAttack = true;
// Create semi-transparent overlay
attackOverlay = new Container();
var overlay = LK.getAsset('wall', {
anchorX: 0,
anchorY: 0,
x: 0,
y: 0,
width: 2048,
height: 2732,
alpha: 0.7,
tint: 0xFF0000
});
attackOverlay.addChild(overlay);
// Create tap text
var tapText = new Text2('TAP QUICKLY!', {
size: 120,
fill: '#FFFFFF'
});
tapText.anchor.set(0.5, 0.5);
tapText.x = 1024;
tapText.y = 1366;
attackOverlay.addChild(tapText);
// Create countdown text
var countdownText = new Text2('3', {
size: 150,
fill: '#FF0000'
});
countdownText.anchor.set(0.5, 0.5);
countdownText.x = 1024;
countdownText.y = 1500;
attackOverlay.addChild(countdownText);
attackOverlay.countdownText = countdownText;
// Update countdown display
var countdownUpdateTimer = LK.setInterval(function () {
if (attackOverlay && attackOverlay.countdownText) {
attackOverlay.countdownText.setText(attackCountdown.toString());
}
}, 100);
attackOverlay.countdownUpdateTimer = countdownUpdateTimer;
// Add pulsing animation
function startTapAnimation() {
tween(tapText, {
scaleX: 1.5,
scaleY: 1.5
}, {
duration: 300,
easing: tween.easeInOut,
onFinish: function onFinish() {
tween(tapText, {
scaleX: 1.0,
scaleY: 1.0
}, {
duration: 300,
easing: tween.easeInOut,
onFinish: startTapAnimation
});
}
});
}
startTapAnimation();
game.addChild(attackOverlay);
}
// Function to remove attack screen
function removeAttackScreen() {
if (attackOverlay) {
if (attackCountdownTimer) {
LK.clearInterval(attackCountdownTimer);
attackCountdownTimer = null;
}
if (attackOverlay.countdownUpdateTimer) {
LK.clearInterval(attackOverlay.countdownUpdateTimer);
}
attackOverlay.destroy();
attackOverlay = null;
isUnderAttack = false;
attackCountdown = 0;
}
}
// Function to create dog at random edge position
function createDog() {
var dog = new Dog();
// Spawn dog at random edge, far from character
var edge = Math.floor(Math.random() * 4); // 0=top, 1=right, 2=bottom, 3=left
switch (edge) {
case 0:
// top
dog.x = 150 + Math.random() * 1748;
dog.y = 150;
break;
case 1:
// right
dog.x = 1898;
dog.y = 150 + Math.random() * 2432;
break;
case 2:
// bottom
dog.x = 150 + Math.random() * 1748;
dog.y = 2582;
break;
case 3:
// left
dog.x = 150;
dog.y = 150 + Math.random() * 2432;
break;
}
dogs.push(dog);
game.addChild(dog);
}
// Spawn first power up immediately
createPowerUp();
// Set timer to spawn dogs every 10 seconds
LK.setInterval(function () {
if (dogs.length < 1) {
createDog();
}
}, 10000);
// Set timer to spawn power ups every 10 seconds
LK.setInterval(function () {
if (powerUps.length < 2) {
createPowerUp();
}
}, 10000);
// Set timer to spawn claim zones every 0.5 seconds
LK.setInterval(function () {
if (claimZones.length < 5) {
var zone = createClaimZone();
claimZones.push(zone);
game.addChild(zone);
}
}, 500);
// Create walls around the perimeter
var walls = [];
// Top wall
for (var i = 0; i < 42; i++) {
var topWall = LK.getAsset('wall', {
anchorX: 0.5,
anchorY: 0.5,
x: i * 50 + 25,
y: 25
});
game.addChild(topWall);
walls.push(topWall);
}
// Bottom wall
for (var i = 0; i < 42; i++) {
var bottomWall = LK.getAsset('wall', {
anchorX: 0.5,
anchorY: 0.5,
x: i * 50 + 25,
y: 2707
});
game.addChild(bottomWall);
walls.push(bottomWall);
}
// Left wall
for (var i = 1; i < 54; i++) {
var leftWall = LK.getAsset('wall', {
anchorX: 0.5,
anchorY: 0.5,
x: 25,
y: i * 50 + 25
});
game.addChild(leftWall);
walls.push(leftWall);
}
// Right wall
for (var i = 1; i < 54; i++) {
var rightWall = LK.getAsset('wall', {
anchorX: 0.5,
anchorY: 0.5,
x: 2023,
y: i * 50 + 25
});
game.addChild(rightWall);
walls.push(rightWall);
}
// Create initial claim zones
function createClaimZone() {
var zone = new ClaimZone();
var validPosition = false;
var attempts = 0;
while (!validPosition && attempts < 50) {
zone.x = 150 + Math.random() * 1748;
zone.y = 150 + Math.random() * 2432;
// Check distance from character
var dx = zone.x - character.x;
var dy = zone.y - character.y;
var distanceFromCharacter = Math.sqrt(dx * dx + dy * dy);
// Check distance from other zones
var tooClose = false;
for (var i = 0; i < claimZones.length; i++) {
var otherZone = claimZones[i];
var zdx = zone.x - otherZone.x;
var zdy = zone.y - otherZone.y;
var distanceFromZone = Math.sqrt(zdx * zdx + zdy * zdy);
if (distanceFromZone < 200) {
tooClose = true;
break;
}
}
if (distanceFromCharacter > 150 && !tooClose) {
validPosition = true;
}
attempts++;
}
return zone;
}
// Generate initial claim zones
for (var i = 0; i < totalZones; i++) {
var zone = createClaimZone();
claimZones.push(zone);
game.addChild(zone);
}
// Score display
var scoreTxt = new Text2('Territory: 0', {
size: 60,
fill: '#FFFFFF'
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Timer display
var timerTxt = new Text2('Time: 00:00', {
size: 50,
fill: '#FFFFFF'
});
timerTxt.anchor.set(0.5, 0);
timerTxt.y = 80;
LK.gui.top.addChild(timerTxt);
// Mouse/touch controls
game.move = function (x, y, obj) {
character.setTarget(x, y);
};
game.down = function (x, y, obj) {
if (isUnderAttack) {
// Handle attack screen tap
removeAttackScreen();
// Remove all dogs and give ammo reward
for (var d = dogs.length - 1; d >= 0; d--) {
dogs[d].destroy();
// Give 2 ammo clips (14 bullets) for killing dog
ammoCount = Math.min(ammoCount + 14, maxAmmo * 10); // Allow stacking up to 70 bullets
updateAmmoDisplay();
}
dogs = [];
} else {
// Shoot at target location
character.shoot(x, y);
}
};
// Main game loop
game.update = function () {
// Update timer
gameTimer = Math.floor((Date.now() - gameStartTime) / 1000);
var minutes = Math.floor(gameTimer / 60);
var seconds = gameTimer % 60;
var timeString = (minutes < 10 ? '0' : '') + minutes + ':' + (seconds < 10 ? '0' : '') + seconds;
timerTxt.setText('Time: ' + timeString);
// Check for power up collection
for (var p = powerUps.length - 1; p >= 0; p--) {
var powerUp = powerUps[p];
var powerUpDx = character.x - powerUp.x;
var powerUpDy = character.y - powerUp.y;
var powerUpDistance = Math.sqrt(powerUpDx * powerUpDx + powerUpDy * powerUpDy);
if (powerUpDistance < 75) {
character.applySpeedBoost();
powerUp.destroy();
powerUps.splice(p, 1);
}
}
// Handle dog attacks
for (var d = dogs.length - 1; d >= 0; d--) {
var dog = dogs[d];
if (!dog.isAttacking) {
dog.attackTarget(character.x, character.y);
}
// Check if dog reached character
var dogDx = character.x - dog.x;
var dogDy = character.y - dog.y;
var dogDistance = Math.sqrt(dogDx * dogDx + dogDy * dogDy);
if (dogDistance < 100 && !isUnderAttack) {
createAttackScreen();
// Start 3 second countdown
attackCountdown = 3;
attackCountdownTimer = LK.setInterval(function () {
attackCountdown--;
if (attackCountdown <= 0) {
LK.clearInterval(attackCountdownTimer);
// Game over if player doesn't tap in time
LK.showGameOver();
}
}, 1000);
}
}
// Handle attack screen taps
if (isUnderAttack) {
// This will be handled by the down event
}
// Handle bullet collisions and movement
for (var b = bullets.length - 1; b >= 0; b--) {
var bullet = bullets[b];
// Check bullet collision with claim zones
var bulletHitZone = false;
for (var z = 0; z < claimZones.length; z++) {
var zone = claimZones[z];
if (!zone.isClaimed) {
var bulletZoneDx = bullet.x - zone.x;
var bulletZoneDy = bullet.y - zone.y;
var bulletZoneDistance = Math.sqrt(bulletZoneDx * bulletZoneDx + bulletZoneDy * bulletZoneDy);
if (bulletZoneDistance < 60) {
if (zone.hit()) {
LK.setScore(LK.getScore() + 1);
if (LK.getScore() >= 15) {
scoreTxt.setText('Territory: 15 WIN');
} else {
scoreTxt.setText('Territory: ' + LK.getScore());
}
LK.getSound('claim').play();
// Remove claimed zone
claimZones.splice(z, 1);
zone.destroy();
// Check win condition
if (LK.getScore() >= 15) {
var currentTime = storage.bestTime || 999999;
if (gameTimer < currentTime) {
storage.bestTime = gameTimer;
}
var recordMinutes = Math.floor(storage.bestTime / 60);
var recordSeconds = storage.bestTime % 60;
var recordTimeString = (recordMinutes < 10 ? '0' : '') + recordMinutes + ':' + (recordSeconds < 10 ? '0' : '') + recordSeconds;
LK.showYouWin('Record: ' + recordTimeString);
}
}
bullet.destroy();
bullets.splice(b, 1);
bulletHitZone = true;
break;
}
}
}
// Remove bullets that go off screen
if (!bulletHitZone && (bullet.x < 0 || bullet.x > 2048 || bullet.y < 0 || bullet.y > 2732)) {
bullet.destroy();
bullets.splice(b, 1);
}
}
// Check for zone claims (keep original touch-based claiming as backup)
for (var i = claimZones.length - 1; i >= 0; i--) {
var zone = claimZones[i];
if (!zone.isClaimed) {
var dx = character.x - zone.x;
var dy = character.y - zone.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 75) {
if (zone.hit()) {
LK.setScore(LK.getScore() + 1);
if (LK.getScore() >= 15) {
scoreTxt.setText('Territory: 15 WIN');
} else {
scoreTxt.setText('Territory: ' + LK.getScore());
}
LK.getSound('claim').play();
// Remove claimed zone instead of creating new one
claimZones.splice(i, 1);
zone.destroy();
// Check win condition - game ends at 15 claims
if (LK.getScore() >= 15) {
// Store the record time
var currentTime = storage.bestTime || 999999;
if (gameTimer < currentTime) {
storage.bestTime = gameTimer;
}
// Format the record time for display
var recordMinutes = Math.floor(storage.bestTime / 60);
var recordSeconds = storage.bestTime % 60;
var recordTimeString = (recordMinutes < 10 ? '0' : '') + recordMinutes + ':' + (recordSeconds < 10 ? '0' : '') + recordSeconds;
LK.showYouWin('Record: ' + recordTimeString);
}
}
}
}
}
};