User prompt
Make alien warships work as AI
User prompt
Make alien warships work as AI
User prompt
Decrease the bullet speed of the alien warships. Every 10 seconds, a bomb will come from where the alien warships are coming from. When the bomb is detonated, the alien warships in the 0.5 cm area will be destroyed. ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Slow down the tracking system of incoming fighter jets against alien ships a little more. Increase the bullet speed of alien warships more.
User prompt
Increase the movement of enemy warships according to the movement of the fighter plane, increase the speed of the projectiles of the war alien warships by 5 times
User prompt
Increase the precision of spaceships and our fighter jets and increase the speed of their projectiles
Code edit (1 edits merged)
Please save this source code
User prompt
Space Fighter Squadron
Initial prompt
There should be a war with alien warships in space with fighter jets that we can control up to half of the screen. Each alien warship we destroy should have a size scale between 1 and 5, and we should get 5 points per size. There should be a reward system that gives faster fire and more weapons when we blow up some ships that are on the 5 size scale.
/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ var Alien = Container.expand(function (size) { var self = Container.call(this); self.size = size || 1; self.health = size; self.maxHealth = size; var assetName = 'alien' + size; var alienGraphics = self.attachAsset(assetName, { anchorX: 0.5, anchorY: 0.5 }); self.speed = Math.random() * 3 + 2; self.lastShot = 0; self.shootDelay = 60 + Math.random() * 120; // AI behavior properties self.aiType = Math.floor(Math.random() * 4); // 0: aggressive, 1: evasive, 2: patrol, 3: formation self.baseX = self.x; self.patrolDirection = Math.random() < 0.5 ? -1 : 1; self.evasionTimer = 0; self.formationOffset = Math.random() * Math.PI * 2; self.aggressionLevel = Math.random() * 0.5 + 0.5; // 0.5 to 1.0 self.update = function () { self.y += self.speed; // AI Movement Behaviors if (self.aiType === 0) { // Aggressive: Move directly toward player with increasing speed var playerDistance = player.x - self.x; var horizontalSpeed = Math.min(Math.abs(playerDistance) * 0.02 * self.aggressionLevel, 2.5); if (playerDistance > 0) { self.x += horizontalSpeed; } else if (playerDistance < 0) { self.x -= horizontalSpeed; } // Increase speed when close to player if (Math.abs(playerDistance) < 200) { self.speed += 0.1; } } else if (self.aiType === 1) { // Evasive: Move away from player bullets and use unpredictable movement self.evasionTimer++; var nearbyBullet = false; for (var b = 0; b < playerBullets.length; b++) { var bullet = playerBullets[b]; var bulletDistance = Math.sqrt(Math.pow(bullet.x - self.x, 2) + Math.pow(bullet.y - self.y, 2)); if (bulletDistance < 150) { nearbyBullet = true; // Evade away from bullet if (bullet.x < self.x) { self.x += 3; } else { self.x -= 3; } break; } } // Random evasive movement when no immediate threat if (!nearbyBullet && self.evasionTimer % 60 === 0) { self.x += (Math.random() - 0.5) * 100; } } else if (self.aiType === 2) { // Patrol: Move in horizontal patterns while slowly tracking player var patrolSpeed = 1.5; self.x += self.patrolDirection * patrolSpeed; if (self.x < 200 || self.x > 1848) { self.patrolDirection *= -1; } // Slight player tracking var playerDistance = player.x - self.x; var trackingSpeed = Math.min(Math.abs(playerDistance) * 0.005, 0.5); if (playerDistance > 0) { self.x += trackingSpeed; } else if (playerDistance < 0) { self.x -= trackingSpeed; } } else if (self.aiType === 3) { // Formation: Move in coordinated patterns with other aliens var formationSpeed = Math.sin((LK.ticks + self.formationOffset) * 0.05) * 2; self.x += formationSpeed; // Maintain formation spacing for (var a = 0; a < aliens.length; a++) { var otherAlien = aliens[a]; if (otherAlien !== self && otherAlien.aiType === 3) { var distance = Math.abs(otherAlien.x - self.x); if (distance < 120 && distance > 0) { if (otherAlien.x > self.x) { self.x -= 1; } else { self.x += 1; } } } } } // Keep aliens within screen bounds self.x = Math.max(50, Math.min(1998, self.x)); // AI Shooting Behavior self.lastShot++; var shootChance = 1.0; var playerDistance = Math.sqrt(Math.pow(player.x - self.x, 2) + Math.pow(player.y - self.y, 2)); // Adjust shooting based on AI type and player proximity if (self.aiType === 0) { // Aggressive aliens shoot more frequently when close to player if (playerDistance < 300) { shootChance = 1.5; self.shootDelay = 40 + Math.random() * 60; } } else if (self.aiType === 1) { // Evasive aliens shoot less frequently but more accurately shootChance = 0.7; self.shootDelay = 80 + Math.random() * 100; } else if (self.aiType === 2) { // Patrol aliens have steady firing rate shootChance = 0.9; } else if (self.aiType === 3) { // Formation aliens coordinate volleys if (LK.ticks % 120 < 20) { shootChance = 2.0; // Synchronized bursts } else { shootChance = 0.3; } } if (self.lastShot >= self.shootDelay * (1 / shootChance) && self.y > 100 && self.y < 2000) { self.lastShot = 0; self.shootDelay = 60 + Math.random() * 120; // Smart targeting for larger aliens if (self.size >= 3) { // Predictive shooting - aim where player will be var bulletSpeed = 20; var timeToReach = Math.abs(self.y - player.y) / bulletSpeed; var predictedPlayerX = player.x; if (dragNode) { // If player is moving, predict movement var playerVelocity = player.x - (player.lastX || player.x) || 0; predictedPlayerX = player.x + playerVelocity * timeToReach; } var bullet = new AlienBullet(); bullet.x = self.x; bullet.y = self.y + 30; // Add slight targeting adjustment var targetOffset = (predictedPlayerX - self.x) * 0.1; bullet.targetOffsetX = Math.max(-2, Math.min(2, targetOffset)); alienBullets.push(bullet); game.addChild(bullet); } else { // Regular shooting for smaller aliens var bullet = new AlienBullet(); bullet.x = self.x; bullet.y = self.y + 30; alienBullets.push(bullet); game.addChild(bullet); } } // Store last position for movement prediction self.lastX = self.x; }; self.takeDamage = function () { self.health--; if (self.health <= 0) { return true; // Destroyed } return false; }; return self; }); var AlienBullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('alienBullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 20; self.targetOffsetX = 0; // For smart targeting self.homingStrength = 0; // For advanced bullets self.update = function () { self.y += self.speed; // Apply horizontal targeting offset for smart bullets if (self.targetOffsetX !== 0) { self.x += self.targetOffsetX; self.targetOffsetX *= 0.95; // Gradually reduce offset } // Light homing behavior for bullets from larger aliens if (self.homingStrength > 0) { var playerDistance = player.x - self.x; var homingForce = Math.sign(playerDistance) * Math.min(Math.abs(playerDistance) * 0.01, 1) * self.homingStrength; self.x += homingForce; self.homingStrength *= 0.98; // Gradually reduce homing } }; return self; }); var Bomb = Container.expand(function () { var self = Container.call(this); var bombGraphics = self.attachAsset('bomb', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 8; self.explosionRadius = 250; // 0.5cm area converted to pixels self.fuseTime = 180; // 3 seconds at 60fps self.currentFuse = 0; self.hasExploded = false; self.update = function () { self.y += self.speed; self.currentFuse++; // Pulsing animation as fuse burns down var pulseScale = 1 + Math.sin(self.currentFuse * 0.3) * 0.2; bombGraphics.scaleX = pulseScale; bombGraphics.scaleY = pulseScale; // Explode after fuse time if (self.currentFuse >= self.fuseTime && !self.hasExploded) { self.explode(); } }; self.explode = function () { self.hasExploded = true; // Flash explosion effect LK.effects.flashScreen(0xffa500, 300); // Destroy all aliens within explosion radius for (var i = aliens.length - 1; i >= 0; i--) { var alien = aliens[i]; var distance = Math.sqrt(Math.pow(alien.x - self.x, 2) + Math.pow(alien.y - self.y, 2)); if (distance <= self.explosionRadius) { LK.setScore(LK.getScore() + alien.size * 5); alien.destroy(); aliens.splice(i, 1); } } // Create explosion visual effect using tween tween(bombGraphics, { scaleX: 5, scaleY: 5, alpha: 0 }, { duration: 500, easing: tween.easeOut, onFinish: function onFinish() { // Remove bomb after explosion animation for (var i = bombs.length - 1; i >= 0; i--) { if (bombs[i] === self) { self.destroy(); bombs.splice(i, 1); break; } } } }); }; return self; }); var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5 }); self.fireRate = 15; self.weaponLevel = 1; self.lastShot = 0; return self; }); var PlayerBullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('playerBullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -18; self.update = function () { self.y += self.speed; }; return self; }); var PowerUp = Container.expand(function () { var self = Container.call(this); var powerupGraphics = self.attachAsset('powerup', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 3; self.bobOffset = Math.random() * Math.PI * 2; self.update = function () { self.y += self.speed; self.x += Math.sin((LK.ticks + self.bobOffset) * 0.1) * 2; powerupGraphics.rotation += 0.05; }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000033 }); /**** * Game Code ****/ var player = game.addChild(new Player()); player.x = 2048 / 2; player.y = 2400; player.lastX = player.x; var playerBullets = []; var aliens = []; var alienBullets = []; var powerups = []; var bombs = []; var waveTimer = 0; var difficultyLevel = 1; var alienSpawnRate = 180; var bombTimer = 0; var bombSpawnRate = 600; // 10 seconds at 60fps // UI Elements var scoreTxt = new Text2('0', { size: 80, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); scoreTxt.y = 100; var weaponTxt = new Text2('Weapon Level: 1', { size: 50, fill: 0x00FF00 }); weaponTxt.anchor.set(0, 0); LK.gui.topRight.addChild(weaponTxt); weaponTxt.x = -350; weaponTxt.y = 50; // Player dragging var dragNode = null; function handleMove(x, y, obj) { if (dragNode) { dragNode.lastX = dragNode.x; // Track previous position dragNode.x = Math.max(40, Math.min(2008, x)); dragNode.y = Math.max(1366, Math.min(2600, y)); } } game.move = handleMove; game.down = function (x, y, obj) { dragNode = player; handleMove(x, y, obj); }; game.up = function (x, y, obj) { dragNode = null; }; // Spawn alien function function spawnAlien() { var size = 1; var rand = Math.random(); if (difficultyLevel >= 3 && rand < 0.1) size = 5;else if (difficultyLevel >= 2 && rand < 0.2) size = 4;else if (difficultyLevel >= 2 && rand < 0.35) size = 3;else if (rand < 0.5) size = 2; var alien = new Alien(size); alien.x = Math.random() * 1800 + 124; alien.y = -100; alien.baseX = alien.x; // Set initial base position for AI // Set AI type based on difficulty and alien size if (difficultyLevel >= 4) { // Higher difficulty: more aggressive and formation AI if (size >= 4) { alien.aiType = Math.random() < 0.4 ? 0 : 3; // Aggressive or Formation } else { alien.aiType = Math.floor(Math.random() * 4); // All types available } } else if (difficultyLevel >= 2) { // Medium difficulty: introduce evasive and patrol alien.aiType = Math.floor(Math.random() * 3); // No formation yet } else { // Low difficulty: mostly aggressive and patrol alien.aiType = Math.random() < 0.7 ? 0 : 2; // Aggressive or Patrol } // Larger aliens get enhanced capabilities if (size >= 3) { alien.aggressionLevel = Math.min(1.0, alien.aggressionLevel + 0.3); } aliens.push(alien); game.addChild(alien); } // Player shooting function playerShoot() { if (LK.ticks - player.lastShot >= player.fireRate) { player.lastShot = LK.ticks; if (player.weaponLevel === 1) { var bullet = new PlayerBullet(); bullet.x = player.x; bullet.y = player.y - 60; playerBullets.push(bullet); game.addChild(bullet); } else if (player.weaponLevel === 2) { // Double shot var bullet1 = new PlayerBullet(); bullet1.x = player.x - 20; bullet1.y = player.y - 60; playerBullets.push(bullet1); game.addChild(bullet1); var bullet2 = new PlayerBullet(); bullet2.x = player.x + 20; bullet2.y = player.y - 60; playerBullets.push(bullet2); game.addChild(bullet2); } else if (player.weaponLevel >= 3) { // Triple shot var bullet1 = new PlayerBullet(); bullet1.x = player.x - 30; bullet1.y = player.y - 60; playerBullets.push(bullet1); game.addChild(bullet1); var bullet2 = new PlayerBullet(); bullet2.x = player.x; bullet2.y = player.y - 60; playerBullets.push(bullet2); game.addChild(bullet2); var bullet3 = new PlayerBullet(); bullet3.x = player.x + 30; bullet3.y = player.y - 60; playerBullets.push(bullet3); game.addChild(bullet3); } LK.getSound('shoot').play(); } } game.update = function () { scoreTxt.setText(LK.getScore()); weaponTxt.setText('Weapon Level: ' + player.weaponLevel); // Player auto-shooting playerShoot(); // Wave management waveTimer++; if (waveTimer >= alienSpawnRate) { waveTimer = 0; spawnAlien(); // Increase difficulty over time if (LK.getScore() > difficultyLevel * 100) { difficultyLevel++; alienSpawnRate = Math.max(60, alienSpawnRate - 10); } } // Bomb spawning every 10 seconds bombTimer++; if (bombTimer >= bombSpawnRate) { bombTimer = 0; var bomb = new Bomb(); bomb.x = Math.random() * 1800 + 124; bomb.y = -100; bombs.push(bomb); game.addChild(bomb); } // Update and cleanup bombs for (var i = bombs.length - 1; i >= 0; i--) { var bomb = bombs[i]; if (bomb.y > 2800 && !bomb.hasExploded) { bomb.destroy(); bombs.splice(i, 1); continue; } } // Update and cleanup player bullets for (var i = playerBullets.length - 1; i >= 0; i--) { var bullet = playerBullets[i]; if (bullet.y < -50) { bullet.destroy(); playerBullets.splice(i, 1); continue; } // Check collision with aliens for (var j = aliens.length - 1; j >= 0; j--) { var alien = aliens[j]; if (bullet.intersects(alien)) { if (alien.takeDamage()) { // Alien destroyed LK.setScore(LK.getScore() + alien.size * 5); // Drop powerup from size 5 aliens if (alien.size === 5 && Math.random() < 0.7) { var powerup = new PowerUp(); powerup.x = alien.x; powerup.y = alien.y; powerups.push(powerup); game.addChild(powerup); } LK.getSound('enemyHit').play(); alien.destroy(); aliens.splice(j, 1); } bullet.destroy(); playerBullets.splice(i, 1); break; } } } // Update and cleanup aliens for (var i = aliens.length - 1; i >= 0; i--) { var alien = aliens[i]; if (alien.y > 2800) { alien.destroy(); aliens.splice(i, 1); continue; } // Check collision with player if (alien.intersects(player)) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); return; } } // Update and cleanup alien bullets for (var i = alienBullets.length - 1; i >= 0; i--) { var bullet = alienBullets[i]; if (bullet.y > 2800) { bullet.destroy(); alienBullets.splice(i, 1); continue; } // Check collision with player if (bullet.intersects(player)) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); return; } } // Update and cleanup powerups for (var i = powerups.length - 1; i >= 0; i--) { var powerup = powerups[i]; if (powerup.y > 2800) { powerup.destroy(); powerups.splice(i, 1); continue; } // Check collision with player if (powerup.intersects(player)) { player.weaponLevel = Math.min(3, player.weaponLevel + 1); player.fireRate = Math.max(8, player.fireRate - 3); LK.getSound('powerupCollect').play(); LK.effects.flashObject(player, 0x00ffff, 500); powerup.destroy(); powerups.splice(i, 1); } } };
===================================================================
--- original.js
+++ change.js
@@ -18,105 +18,156 @@
});
self.speed = Math.random() * 3 + 2;
self.lastShot = 0;
self.shootDelay = 60 + Math.random() * 120;
- // AI behavior variables
- self.aiPattern = Math.floor(Math.random() * 4); // 0: direct, 1: zigzag, 2: circle, 3: aggressive
- self.aiTimer = 0;
- self.targetX = self.x;
- self.baseSpeed = self.speed;
- self.aggressionLevel = self.size / 5; // Larger ships are more aggressive
+ // AI behavior properties
+ self.aiType = Math.floor(Math.random() * 4); // 0: aggressive, 1: evasive, 2: patrol, 3: formation
+ self.baseX = self.x;
+ self.patrolDirection = Math.random() < 0.5 ? -1 : 1;
+ self.evasionTimer = 0;
self.formationOffset = Math.random() * Math.PI * 2;
- self.evasionCooldown = 0;
+ self.aggressionLevel = Math.random() * 0.5 + 0.5; // 0.5 to 1.0
self.update = function () {
- self.aiTimer++;
- self.evasionCooldown = Math.max(0, self.evasionCooldown - 1);
- // Execute AI movement pattern
- self.executeAIPattern();
- // Alien shooting
+ self.y += self.speed;
+ // AI Movement Behaviors
+ if (self.aiType === 0) {
+ // Aggressive: Move directly toward player with increasing speed
+ var playerDistance = player.x - self.x;
+ var horizontalSpeed = Math.min(Math.abs(playerDistance) * 0.02 * self.aggressionLevel, 2.5);
+ if (playerDistance > 0) {
+ self.x += horizontalSpeed;
+ } else if (playerDistance < 0) {
+ self.x -= horizontalSpeed;
+ }
+ // Increase speed when close to player
+ if (Math.abs(playerDistance) < 200) {
+ self.speed += 0.1;
+ }
+ } else if (self.aiType === 1) {
+ // Evasive: Move away from player bullets and use unpredictable movement
+ self.evasionTimer++;
+ var nearbyBullet = false;
+ for (var b = 0; b < playerBullets.length; b++) {
+ var bullet = playerBullets[b];
+ var bulletDistance = Math.sqrt(Math.pow(bullet.x - self.x, 2) + Math.pow(bullet.y - self.y, 2));
+ if (bulletDistance < 150) {
+ nearbyBullet = true;
+ // Evade away from bullet
+ if (bullet.x < self.x) {
+ self.x += 3;
+ } else {
+ self.x -= 3;
+ }
+ break;
+ }
+ }
+ // Random evasive movement when no immediate threat
+ if (!nearbyBullet && self.evasionTimer % 60 === 0) {
+ self.x += (Math.random() - 0.5) * 100;
+ }
+ } else if (self.aiType === 2) {
+ // Patrol: Move in horizontal patterns while slowly tracking player
+ var patrolSpeed = 1.5;
+ self.x += self.patrolDirection * patrolSpeed;
+ if (self.x < 200 || self.x > 1848) {
+ self.patrolDirection *= -1;
+ }
+ // Slight player tracking
+ var playerDistance = player.x - self.x;
+ var trackingSpeed = Math.min(Math.abs(playerDistance) * 0.005, 0.5);
+ if (playerDistance > 0) {
+ self.x += trackingSpeed;
+ } else if (playerDistance < 0) {
+ self.x -= trackingSpeed;
+ }
+ } else if (self.aiType === 3) {
+ // Formation: Move in coordinated patterns with other aliens
+ var formationSpeed = Math.sin((LK.ticks + self.formationOffset) * 0.05) * 2;
+ self.x += formationSpeed;
+ // Maintain formation spacing
+ for (var a = 0; a < aliens.length; a++) {
+ var otherAlien = aliens[a];
+ if (otherAlien !== self && otherAlien.aiType === 3) {
+ var distance = Math.abs(otherAlien.x - self.x);
+ if (distance < 120 && distance > 0) {
+ if (otherAlien.x > self.x) {
+ self.x -= 1;
+ } else {
+ self.x += 1;
+ }
+ }
+ }
+ }
+ }
+ // Keep aliens within screen bounds
+ self.x = Math.max(50, Math.min(1998, self.x));
+ // AI Shooting Behavior
self.lastShot++;
- if (self.lastShot >= self.shootDelay && self.y > 100 && self.y < 2000) {
+ var shootChance = 1.0;
+ var playerDistance = Math.sqrt(Math.pow(player.x - self.x, 2) + Math.pow(player.y - self.y, 2));
+ // Adjust shooting based on AI type and player proximity
+ if (self.aiType === 0) {
+ // Aggressive aliens shoot more frequently when close to player
+ if (playerDistance < 300) {
+ shootChance = 1.5;
+ self.shootDelay = 40 + Math.random() * 60;
+ }
+ } else if (self.aiType === 1) {
+ // Evasive aliens shoot less frequently but more accurately
+ shootChance = 0.7;
+ self.shootDelay = 80 + Math.random() * 100;
+ } else if (self.aiType === 2) {
+ // Patrol aliens have steady firing rate
+ shootChance = 0.9;
+ } else if (self.aiType === 3) {
+ // Formation aliens coordinate volleys
+ if (LK.ticks % 120 < 20) {
+ shootChance = 2.0; // Synchronized bursts
+ } else {
+ shootChance = 0.3;
+ }
+ }
+ if (self.lastShot >= self.shootDelay * (1 / shootChance) && self.y > 100 && self.y < 2000) {
self.lastShot = 0;
self.shootDelay = 60 + Math.random() * 120;
- var bullet = new AlienBullet();
- bullet.x = self.x;
- bullet.y = self.y + 30;
- alienBullets.push(bullet);
- game.addChild(bullet);
+ // Smart targeting for larger aliens
+ if (self.size >= 3) {
+ // Predictive shooting - aim where player will be
+ var bulletSpeed = 20;
+ var timeToReach = Math.abs(self.y - player.y) / bulletSpeed;
+ var predictedPlayerX = player.x;
+ if (dragNode) {
+ // If player is moving, predict movement
+ var playerVelocity = player.x - (player.lastX || player.x) || 0;
+ predictedPlayerX = player.x + playerVelocity * timeToReach;
+ }
+ var bullet = new AlienBullet();
+ bullet.x = self.x;
+ bullet.y = self.y + 30;
+ // Add slight targeting adjustment
+ var targetOffset = (predictedPlayerX - self.x) * 0.1;
+ bullet.targetOffsetX = Math.max(-2, Math.min(2, targetOffset));
+ alienBullets.push(bullet);
+ game.addChild(bullet);
+ } else {
+ // Regular shooting for smaller aliens
+ var bullet = new AlienBullet();
+ bullet.x = self.x;
+ bullet.y = self.y + 30;
+ alienBullets.push(bullet);
+ game.addChild(bullet);
+ }
}
+ // Store last position for movement prediction
+ self.lastX = self.x;
};
self.takeDamage = function () {
self.health--;
if (self.health <= 0) {
return true; // Destroyed
}
return false;
};
- self.executeAIPattern = function () {
- var playerDistance = player.x - self.x;
- var distanceToPlayer = Math.sqrt(Math.pow(player.x - self.x, 2) + Math.pow(player.y - self.y, 2));
- // Check for incoming player bullets for evasion
- if (self.evasionCooldown === 0) {
- self.checkForEvasion();
- }
- switch (self.aiPattern) {
- case 0:
- // Direct assault
- self.y += self.speed;
- if (Math.abs(playerDistance) > 50) {
- var trackingSpeed = Math.min(Math.abs(playerDistance) * 0.02, 2);
- self.x += playerDistance > 0 ? trackingSpeed : -trackingSpeed;
- }
- break;
- case 1:
- // Zigzag pattern
- self.y += self.speed * 0.8;
- self.x += Math.sin(self.aiTimer * 0.1) * 3;
- if (distanceToPlayer < 400) {
- var trackingSpeed = Math.min(Math.abs(playerDistance) * 0.015, 1.5);
- self.x += playerDistance > 0 ? trackingSpeed : -trackingSpeed;
- }
- break;
- case 2:
- // Circular/orbital pattern
- self.y += self.speed * 0.6;
- var circleRadius = 100 + self.size * 20;
- var centerX = player.x;
- self.x += Math.cos(self.aiTimer * 0.05 + self.formationOffset) * 2;
- if (Math.abs(self.x - centerX) > circleRadius) {
- self.x += (centerX - self.x) * 0.02;
- }
- break;
- case 3:
- // Aggressive hunter
- self.speed = self.baseSpeed * (1 + self.aggressionLevel * 0.5);
- self.y += self.speed;
- if (distanceToPlayer < 600) {
- var aggressiveSpeed = Math.min(Math.abs(playerDistance) * 0.03, 3);
- self.x += playerDistance > 0 ? aggressiveSpeed : -aggressiveSpeed;
- // Increase fire rate when close
- self.shootDelay = Math.max(30, self.shootDelay * 0.99);
- }
- break;
- }
- // Keep aliens within screen bounds
- if (self.x < 50) self.x = 50;
- if (self.x > 1998) self.x = 1998;
- };
- self.checkForEvasion = function () {
- // Check for nearby player bullets and attempt evasion
- for (var i = 0; i < playerBullets.length; i++) {
- var bullet = playerBullets[i];
- var bulletDistance = Math.sqrt(Math.pow(bullet.x - self.x, 2) + Math.pow(bullet.y - self.y, 2));
- if (bulletDistance < 150 && bullet.y < self.y) {
- // Bullet approaching, attempt evasion
- var evasionDirection = bullet.x > self.x ? -1 : 1;
- self.x += evasionDirection * (4 + self.aggressionLevel);
- self.evasionCooldown = 30; // Cooldown to prevent constant evasion
- break;
- }
- }
- };
return self;
});
var AlienBullet = Container.expand(function () {
var self = Container.call(this);
@@ -124,10 +175,24 @@
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 20;
+ self.targetOffsetX = 0; // For smart targeting
+ self.homingStrength = 0; // For advanced bullets
self.update = function () {
self.y += self.speed;
+ // Apply horizontal targeting offset for smart bullets
+ if (self.targetOffsetX !== 0) {
+ self.x += self.targetOffsetX;
+ self.targetOffsetX *= 0.95; // Gradually reduce offset
+ }
+ // Light homing behavior for bullets from larger aliens
+ if (self.homingStrength > 0) {
+ var playerDistance = player.x - self.x;
+ var homingForce = Math.sign(playerDistance) * Math.min(Math.abs(playerDistance) * 0.01, 1) * self.homingStrength;
+ self.x += homingForce;
+ self.homingStrength *= 0.98; // Gradually reduce homing
+ }
};
return self;
});
var Bomb = Container.expand(function () {
@@ -240,8 +305,9 @@
****/
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2400;
+player.lastX = player.x;
var playerBullets = [];
var aliens = [];
var alienBullets = [];
var powerups = [];
@@ -250,11 +316,8 @@
var difficultyLevel = 1;
var alienSpawnRate = 180;
var bombTimer = 0;
var bombSpawnRate = 600; // 10 seconds at 60fps
-var formationSpawning = false;
-var formationTimer = 0;
-var lastFormationSpawn = 0;
// UI Elements
var scoreTxt = new Text2('0', {
size: 80,
fill: 0xFFFFFF
@@ -273,8 +336,9 @@
// Player dragging
var dragNode = null;
function handleMove(x, y, obj) {
if (dragNode) {
+ dragNode.lastX = dragNode.x; // Track previous position
dragNode.x = Math.max(40, Math.min(2008, x));
dragNode.y = Math.max(1366, Math.min(2600, y));
}
}
@@ -293,46 +357,31 @@
if (difficultyLevel >= 3 && rand < 0.1) size = 5;else if (difficultyLevel >= 2 && rand < 0.2) size = 4;else if (difficultyLevel >= 2 && rand < 0.35) size = 3;else if (rand < 0.5) size = 2;
var alien = new Alien(size);
alien.x = Math.random() * 1800 + 124;
alien.y = -100;
+ alien.baseX = alien.x; // Set initial base position for AI
+ // Set AI type based on difficulty and alien size
+ if (difficultyLevel >= 4) {
+ // Higher difficulty: more aggressive and formation AI
+ if (size >= 4) {
+ alien.aiType = Math.random() < 0.4 ? 0 : 3; // Aggressive or Formation
+ } else {
+ alien.aiType = Math.floor(Math.random() * 4); // All types available
+ }
+ } else if (difficultyLevel >= 2) {
+ // Medium difficulty: introduce evasive and patrol
+ alien.aiType = Math.floor(Math.random() * 3); // No formation yet
+ } else {
+ // Low difficulty: mostly aggressive and patrol
+ alien.aiType = Math.random() < 0.7 ? 0 : 2; // Aggressive or Patrol
+ }
+ // Larger aliens get enhanced capabilities
+ if (size >= 3) {
+ alien.aggressionLevel = Math.min(1.0, alien.aggressionLevel + 0.3);
+ }
aliens.push(alien);
game.addChild(alien);
}
-// Spawn formation of aliens with coordinated AI
-function spawnFormation() {
- var formationType = Math.floor(Math.random() * 3); // 0: line, 1: V-formation, 2: circle
- var formationSize = 3 + Math.floor(difficultyLevel / 2);
- var centerX = 1024;
- var startY = -150;
- for (var i = 0; i < formationSize; i++) {
- var alien = new Alien(Math.min(3, 1 + Math.floor(i / 2)));
- switch (formationType) {
- case 0:
- // Line formation
- alien.x = centerX + (i - formationSize / 2) * 120;
- alien.y = startY;
- alien.aiPattern = 0; // Direct assault for line formation
- break;
- case 1:
- // V-formation
- var offset = Math.abs(i - formationSize / 2) * 80;
- alien.x = centerX + (i - formationSize / 2) * 100;
- alien.y = startY - offset;
- alien.aiPattern = 1; // Zigzag for V-formation
- break;
- case 2:
- // Circle formation
- var angle = i / formationSize * Math.PI * 2;
- alien.x = centerX + Math.cos(angle) * 150;
- alien.y = startY + Math.sin(angle) * 50;
- alien.aiPattern = 2; // Circular pattern
- alien.formationOffset = angle;
- break;
- }
- aliens.push(alien);
- game.addChild(alien);
- }
-}
// Player shooting
function playerShoot() {
if (LK.ticks - player.lastShot >= player.fireRate) {
player.lastShot = LK.ticks;
@@ -381,18 +430,11 @@
// Player auto-shooting
playerShoot();
// Wave management
waveTimer++;
- formationTimer++;
if (waveTimer >= alienSpawnRate) {
waveTimer = 0;
- // Chance to spawn formation instead of single alien
- if (formationTimer - lastFormationSpawn > 300 && Math.random() < 0.3 && difficultyLevel >= 2) {
- spawnFormation();
- lastFormationSpawn = formationTimer;
- } else {
- spawnAlien();
- }
+ spawnAlien();
// Increase difficulty over time
if (LK.getScore() > difficultyLevel * 100) {
difficultyLevel++;
alienSpawnRate = Math.max(60, alienSpawnRate - 10);