bbc123321 commited on
Commit
dd91f72
·
verified ·
1 Parent(s): 65bb0ca

Manual changes saved

Browse files
Files changed (1) hide show
  1. index.html +144 -83
index.html CHANGED
@@ -3,16 +3,13 @@
3
  <head>
4
  <meta charset="utf-8" />
5
  <meta name="viewport" content="width=device-width, initial-scale=1" />
6
- <title>BattleZone Royale - Enemies Gather First (Improved)</title>
7
  <script src="https://cdn.tailwindcss.com"></script>
8
  <script src="https://unpkg.com/feather-icons"></script>
9
  <style>
10
  html,body { height:100%; margin:0; background:#0b1220; color:#fff; font-family:monospace; }
11
  #canvasContainer { position:relative; flex:1; display:flex; justify-content:center; align-items:center; height:100vh; overflow:hidden; }
12
  #gameCanvas { display:block; user-select:none; cursor:crosshair; box-shadow:0 0 20px rgba(0,0,0,.5); width:100%; height:100%; }
13
- #stormWarning { z-index:10; }
14
- #deathScreen, #victoryScreen { z-index:20; }
15
-
16
  /* HUD */
17
  #hudHealth { position:absolute; left:12px; bottom:12px; background:rgba(0,0,0,0.55); padding:6px 8px; border-radius:8px; font-weight:700; font-size:13px; display:flex; align-items:center; gap:8px; z-index:30; }
18
  #hudGearWrap { position:absolute; right:12px; bottom:12px; display:flex; gap:8px; align-items:center; z-index:30; }
@@ -21,6 +18,7 @@
21
  .selected { outline: 2px solid rgba(255,215,0,0.9); box-shadow: 0 0 6px rgba(255,215,0,0.12); }
22
  .equipped { box-shadow: inset 0 -6px 14px rgba(255,255,255,0.03); border:1px solid rgba(255,255,255,0.04); }
23
  .medkit-count { position:absolute; right:4px; bottom:2px; font-size:10px; color:#ffd; }
 
24
  </style>
25
  </head>
26
  <body>
@@ -31,7 +29,7 @@
31
  </h1>
32
 
33
  <h2 class="text-2xl text-yellow-300 mb-4">Choose Landing Zone</h2>
34
- <div class="grid grid-cols-2 gap-6 max-w-xl w-full mb-6">
35
  <div class="biome-selector bg-gray-700 p-6 rounded-lg cursor-pointer hover:bg-yellow-800 flex flex-col items-center" data-biome="desert">
36
  <i data-feather="sun" class="text-yellow-300 mb-2"></i>
37
  <h3 class="font-bold text-lg">Golden Dunes</h3>
@@ -125,7 +123,7 @@
125
  </div>
126
 
127
  <script>
128
- // DOM
129
  const landingScreen = document.getElementById('landingScreen');
130
  const gameScreen = document.getElementById('gameScreen');
131
  const canvas = document.getElementById('gameCanvas');
@@ -140,6 +138,7 @@
140
  const victoryScreen = document.getElementById('victoryScreen');
141
  const goHomeBtn = document.getElementById('goHomeBtn');
142
  const continueBtn = document.getElementById('continueBtn');
 
143
 
144
  // World
145
  const WORLD = { width: 6000, height: 4000 };
@@ -246,7 +245,7 @@
246
  function generateLootForBiome(b){
247
  const roll = Math.random();
248
  if (roll < 0.35) return { type:'medkit', amount:1 };
249
- if (roll < 0.7) return { type:'materials', amount: 10 }; // always 10 per request
250
  const weapons = [
251
  { name:'Pistol', dmg:12, rate:320, color:'#ffd86b', magSize:12, startReserve:24 },
252
  { name:'SMG', dmg:6, rate:120, color:'#8ef0ff', magSize:30, startReserve:90 },
@@ -263,8 +262,7 @@
263
  const x = rand(150, WORLD.width-150);
264
  const y = rand(150, WORLD.height-150);
265
  const loot = generateLootForBiome(biomeAt(x,y));
266
- // ensure materials amount=10 (per request) if type materials
267
- if (loot.type === 'materials') loot.amount = 10;
268
  chests.push({ x,y, opened:false, loot });
269
  }
270
  for (let i=0;i<700;i++){
@@ -290,11 +288,12 @@
290
  materials: 0,
291
  lastShot: 0,
292
  reloadingUntil: 0,
 
293
  lastAttackedTime: 0,
294
  state: 'gather',
295
  gatherTimeLeft: rand(8,16),
296
  target: null,
297
- nextHealTime: 0 // only heal using medkit and with cooldown
298
  });
299
  }
300
  updatePlayerCount();
@@ -432,9 +431,7 @@
432
  if (loot.type === 'weapon') pickups.push({ x:px, y:py, type:'weapon', weapon: makeWeaponProto(loot.weapon), ammoInMag: loot.weapon.magSize || 12, ammoReserve: loot.weapon.startReserve || (loot.weapon.magSize*2 || 24) });
433
  else if (loot.type === 'medkit') pickups.push({ x:px, y:py, type:'medkit', amount: loot.amount || 1 });
434
  else if (loot.type === 'materials') pickups.push({ x:px, y:py, type:'materials', amount: loot.amount || 10 });
435
- if (Math.random() < 0.25){
436
- pickups.push({ x:px+8, y:py+8, type:'ammo', forWeapon: null, amount: randInt(6,30) });
437
- }
438
  updateHUD();
439
  return;
440
  }
@@ -480,7 +477,7 @@
480
  }
481
  }
482
 
483
- // Enemy helpers
484
  function enemyEquipBestWeapon(e){
485
  let bestIdx = -1;
486
  let bestScore = -Infinity;
@@ -646,7 +643,7 @@
646
  }
647
  }
648
 
649
- // Nearest helpers
650
  function findNearestChest(e, maxDist = 1200){
651
  let best = null; let bd = Infinity;
652
  for (const c of chests){ if (c.opened) continue; const d = Math.hypot(c.x - e.x, c.y - e.y); if (d < bd && d <= maxDist){ bd = d; best = c; } }
@@ -663,57 +660,51 @@
663
  return best;
664
  }
665
 
666
- // Enemy AI: gather-first state machine, heal only using medkits, visible weapon drawing logic supported in drawEnemies
667
  function updateEnemies(dt, now){
 
 
 
668
  for (const e of enemies){
669
  if (e.health <= 0) continue;
670
 
671
- // If recently attacked, go combat
672
- if (now - e.lastAttackedTime < 4000){
673
- e.state = 'combat';
674
- }
675
 
676
- if (e.state === 'gather'){
677
- e.gatherTimeLeft -= dt;
678
- }
679
 
680
- // Only heal using medkit: check cooldown and presence in inventory
681
  if (e.health < 60 && now >= (e.nextHealTime || 0)){
682
- // find medkit in inventory
683
  let medIdx = -1;
684
  for (let s=0;s<5;s++){
685
  const it = e.inventory[s];
686
  if (it && it.type === 'medkit' && it.amount > 0){ medIdx = s; break; }
687
  }
688
  if (medIdx !== -1){
689
- // use medkit (consume one), set cooldown so it's not instant recurring
690
  const kit = e.inventory[medIdx];
691
  kit.amount -= 1;
692
  e.health = Math.min(120, e.health + 50);
693
  if (kit.amount <= 0) e.inventory[medIdx] = null;
694
- e.nextHealTime = now + 6000; // 6s cooldown before next possible medkit use
695
- // do not switch state to combat just because healing occurred
696
  continue;
697
  }
698
  }
699
 
700
- // transition gather -> combat if conditions met
701
  const hasUsefulWeapon = e.inventory.some(it => it && it.type === 'weapon' && (it.ammoInMag > 0 || it.ammoReserve > 0));
702
- if ((e.gatherTimeLeft <= 0 || e.materials >= 20 || hasUsefulWeapon) && e.state === 'gather'){
703
- e.state = 'combat';
704
- }
705
 
706
  if (e.state === 'gather'){
707
- // prioritize nearby pickups, then chests, then harvest objects
708
- let targetPickup = findNearestPickup(e, 240);
709
- if (targetPickup){
710
- const angle = Math.atan2(targetPickup.y - e.y, targetPickup.x - e.x);
711
  e.angle = angle;
712
  e.x += Math.cos(e.angle) * e.speed * dt * 0.9;
713
  e.y += Math.sin(e.angle) * e.speed * dt * 0.9;
714
- if (Math.hypot(targetPickup.x - e.x, targetPickup.y - e.y) < 18){
715
- enemyPickupCollect(e, targetPickup);
716
- const idx = pickups.indexOf(targetPickup);
717
  if (idx >= 0) pickups.splice(idx,1);
718
  }
719
  continue;
@@ -729,13 +720,9 @@
729
  } else {
730
  chestTarget.opened = true;
731
  const loot = chestTarget.loot;
732
- if (loot.type === 'weapon'){
733
- enemyPickupCollect(e, { type:'weapon', weapon: makeWeaponProto(loot.weapon), ammoInMag:loot.weapon.magSize||12, ammoReserve:loot.weapon.startReserve||24 });
734
- } else if (loot.type === 'medkit'){
735
- enemyPickupCollect(e, { type:'medkit', amount: loot.amount || 1 });
736
- } else if (loot.type === 'materials'){
737
- enemyPickupCollect(e, { type:'materials', amount: loot.amount || 10 });
738
- }
739
  }
740
  continue;
741
  }
@@ -751,8 +738,7 @@
751
  objTarget.hp -= 40 * dt;
752
  if (objTarget.hp <= 0 && !objTarget.dead){
753
  objTarget.dead = true;
754
- const matGain = objTarget.type === 'wood' ? 3 : 6;
755
- e.materials += matGain;
756
  if (Math.random() < 0.08){
757
  const lootRoll = Math.random();
758
  if (lootRoll < 0.4) enemyPickupCollect(e, { type:'medkit', amount:1 });
@@ -769,18 +755,31 @@
769
  e.y += Math.sin(e.angle) * e.speed * dt * 0.25;
770
  if (Math.random() < 0.01) e.angle += (Math.random()-0.5)*2;
771
  continue;
772
- } // end gather
773
 
774
- // Combat behavior
775
  if (e.equippedIndex === -1) enemyEquipBestWeapon(e);
 
 
 
 
 
 
 
 
 
 
 
 
776
  if (e.equippedIndex >= 0){
777
  const eq = e.inventory[e.equippedIndex];
778
- if (eq && eq.type === 'weapon' && eq.ammoInMag <= 0 && eq.ammoReserve > 0){
779
- reloadItem(eq);
 
780
  }
781
  }
782
 
783
- // choose target (player preferred)
784
  let target = player;
785
  let bestDist = Math.hypot(player.x - e.x, player.y - e.y);
786
  for (const other of enemies){
@@ -789,6 +788,7 @@
789
  if (d < bestDist && Math.random() < 0.6){ bestDist = d; target = other; }
790
  }
791
 
 
792
  const distToPlayer = Math.hypot(player.x - e.x, player.y - e.y);
793
  if (distToPlayer < 160 && e.health < 35 && e.materials >= 10 && Math.random() < 0.5){
794
  enemyTryBuild(e);
@@ -805,7 +805,7 @@
805
  } else {
806
  if (e.equippedIndex >= 0){
807
  const eq = e.inventory[e.equippedIndex];
808
- if (eq && eq.type === 'weapon' && eq.ammoInMag > 0 && now - e.lastShot > (eq.weapon.rate || 300)){
809
  e.lastShot = now;
810
  eq.ammoInMag -= 1;
811
  const angle = Math.atan2(blocker.y - e.y, blocker.x - e.x);
@@ -821,7 +821,7 @@
821
  }
822
  }
823
 
824
- // Attack
825
  if (distToPlayer < 36 && now - e.lastMelee > e.meleeRate){
826
  e.lastMelee = now;
827
  const dmg = 10 + randInt(0,8);
@@ -830,9 +830,9 @@
830
  if (player.health <= 0) { player.health = 0; playerDeath(); }
831
  } else if (e.equippedIndex >= 0){
832
  const eq = e.inventory[e.equippedIndex];
833
- if (eq && eq.type === 'weapon' && now - (e.lastShot||0) > (eq.weapon.rate || 300)){
834
  if (eq.ammoInMag <= 0){
835
- if (eq.ammoReserve > 0) reloadItem(eq);
836
  } else {
837
  if (hasLineOfSight(e.x, e.y, target.x, target.y)){
838
  e.lastShot = now;
@@ -841,22 +841,60 @@
841
  shootBullet(e.x + Math.cos(angle)*12, e.y + Math.sin(angle)*12, target.x + (Math.random()-0.5)*6, target.y + (Math.random()-0.5)*6, eq, e.id);
842
  e.lastAttackedTime = now;
843
  } else {
 
844
  e.angle = Math.atan2(target.y - e.y, target.x - e.x);
845
  e.x += Math.cos(e.angle) * e.speed * dt * 0.6;
846
  e.y += Math.sin(e.angle) * e.speed * dt * 0.6;
847
  }
848
  }
849
  } else {
 
850
  e.angle = Math.atan2(target.y - e.y, target.x - e.x);
851
  e.x += Math.cos(e.angle) * e.speed * dt * 0.9;
852
  e.y += Math.sin(e.angle) * e.speed * dt * 0.9;
853
  }
854
  } else {
 
855
  e.angle = Math.atan2(target.y - e.y, target.x - e.x);
856
  e.x += Math.cos(e.angle) * e.speed * dt * 0.9;
857
  e.y += Math.sin(e.angle) * e.speed * dt * 0.9;
858
  }
859
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
860
  }
861
 
862
  // Storm
@@ -990,31 +1028,23 @@
990
  const s = worldToScreen(e.x,e.y);
991
  ctx.save();
992
  ctx.translate(s.x,s.y); ctx.rotate(e.angle);
993
-
994
- // shadow & body
995
  ctx.fillStyle='rgba(0,0,0,0.18)'; ctx.beginPath(); ctx.ellipse(0,12,14,6,0,0,Math.PI*2); ctx.fill();
996
  ctx.fillStyle='#ff6b6b'; ctx.beginPath(); ctx.moveTo(12,0); ctx.lineTo(-10,-8); ctx.lineTo(-10,8); ctx.closePath(); ctx.fill();
997
-
998
- // HP bar
999
  ctx.fillStyle='rgba(0,0,0,0.6)'; ctx.fillRect(-18,-22,36,6);
1000
  const hpPct = Math.max(0, Math.min(1, e.health/120));
1001
  ctx.fillStyle='#ff6b6b'; ctx.fillRect(-18,-22,36*hpPct,6);
1002
 
1003
- // If equipped with weapon, draw it in hand visibly
1004
  if (e.equippedIndex >= 0 && e.inventory[e.equippedIndex] && e.inventory[e.equippedIndex].type === 'weapon'){
1005
  const we = e.inventory[e.equippedIndex];
1006
  const color = we.weapon.color || '#ddd';
1007
  ctx.save();
1008
- // position weapon in right-hand area
1009
  ctx.translate(12, 2);
1010
  ctx.rotate(-0.08);
1011
- // body of weapon
1012
  ctx.fillStyle = color;
1013
  ctx.fillRect(0, -4, 24, 8);
1014
- // muzzle detail
1015
  ctx.fillStyle = '#222';
1016
  ctx.fillRect(18, -2, 6, 4);
1017
- // small magazine indicator
1018
  const magPct = Math.max(0, we.ammoInMag / we.weapon.magSize);
1019
  ctx.fillStyle = 'rgba(0,0,0,0.35)';
1020
  ctx.fillRect(4, 6, 18, 4);
@@ -1022,7 +1052,6 @@
1022
  ctx.fillRect(4, 6, 18 * magPct, 4);
1023
  ctx.restore();
1024
  } else {
1025
- // draw pickaxe if unarmed (small icon)
1026
  if (e.equippedIndex === -1){
1027
  ctx.save();
1028
  ctx.translate(12, 6);
@@ -1035,10 +1064,9 @@
1035
  }
1036
  }
1037
 
1038
- // draw state marker
1039
  ctx.fillStyle = e.state === 'gather' ? 'rgba(0,200,200,0.9)' : (e.state === 'combat' ? 'rgba(255,80,80,0.95)' : 'rgba(255,200,80,0.9)');
1040
  ctx.beginPath(); ctx.arc(18, -18, 5, 0, Math.PI*2); ctx.fill();
1041
-
1042
  ctx.restore();
1043
  }
1044
  }
@@ -1101,9 +1129,9 @@
1101
  ctx.restore();
1102
  }
1103
 
1104
- function drawCrosshair(){ }
1105
 
1106
- // Game loop
1107
  let lastTime = 0;
1108
  function gameLoop(ts){
1109
  if (!gameActive) return;
@@ -1125,7 +1153,6 @@
1125
  cameraUpdate();
1126
  mouse.worldX = mouse.canvasX + camera.x;
1127
  mouse.worldY = mouse.canvasY + camera.y;
1128
-
1129
  player.angle = Math.atan2(mouse.worldY - player.y, mouse.worldX - player.x);
1130
 
1131
  let activeWeaponItem = null;
@@ -1137,9 +1164,8 @@
1137
 
1138
  // player attack
1139
  if (mouse.down){
1140
- if (player.equippedIndex === -1){
1141
- playerMeleeHit();
1142
- } else if (activeWeaponItem && activeWeaponItem.type === 'weapon'){
1143
  const now = performance.now();
1144
  if (now - player.lastShot > (activeWeaponItem.weapon.rate || 300)){
1145
  if (activeWeaponItem.ammoInMag > 0){
@@ -1151,7 +1177,6 @@
1151
  }
1152
  }
1153
 
1154
- // reload/interact/build
1155
  if (keys.r) { reloadEquipped(); keys.r = false; }
1156
  if (keys.e){ interactNearby(); keys.e = false; }
1157
  if (keys.q){ tryBuild(); keys.q = false; }
@@ -1182,12 +1207,29 @@
1182
  requestAnimationFrame(gameLoop);
1183
  }
1184
 
 
 
 
 
 
 
 
 
 
 
 
 
1185
  // Start/end
1186
  let timerInterval = null;
1187
  let gameTime = 300;
1188
  let gameActive = false;
1189
 
1190
- function startGame(){
 
 
 
 
 
1191
  victoryScreen.classList.add('hidden');
1192
  deathScreen.classList.add('hidden');
1193
 
@@ -1196,8 +1238,8 @@
1196
  gameScreen.classList.remove('hidden');
1197
  resizeCanvas();
1198
 
1199
- player.x = WORLD.width/2 + (Math.random()-0.5)*400;
1200
- player.y = WORLD.height/2 + (Math.random()-0.5)*400;
1201
  player.health = 100; player.armor = 0; player.kills = 0; player.materials = 0;
1202
  player.inventory = [null,null,null,null,null];
1203
  player.selectedSlot = 0; player.equippedIndex = -1; player.lastShot = 0; player.lastMelee = 0;
@@ -1206,6 +1248,14 @@
1206
  initHUD();
1207
  cameraUpdate();
1208
 
 
 
 
 
 
 
 
 
1209
  gameTime = 300; storm.active = false; storm.radius = storm.maxRadius;
1210
  document.getElementById('gameTimer').textContent = '5:00';
1211
  timerInterval && clearInterval(timerInterval);
@@ -1234,11 +1284,22 @@
1234
 
1235
  document.getElementById('respawnBtn').addEventListener('click', ()=>{ deathScreen.classList.add('hidden'); landingScreen.classList.remove('hidden'); });
1236
  goHomeBtn.addEventListener('click', ()=>{ victoryScreen.classList.add('hidden'); gameScreen.classList.add('hidden'); landingScreen.classList.remove('hidden'); });
1237
- continueBtn.addEventListener('click', ()=>{ victoryScreen.classList.add('hidden'); startGame(); });
1238
-
1239
- document.querySelectorAll('.biome-selector').forEach(el => el.addEventListener('click', ()=> startGame()));
 
 
 
 
 
 
 
 
 
 
 
1240
 
1241
- // init
1242
  resizeCanvas();
1243
  populateWorld();
1244
  feather.replace();
 
3
  <head>
4
  <meta charset="utf-8" />
5
  <meta name="viewport" content="width=device-width, initial-scale=1" />
6
+ <title>BattleZone Royale - Spawn & AI Fixes</title>
7
  <script src="https://cdn.tailwindcss.com"></script>
8
  <script src="https://unpkg.com/feather-icons"></script>
9
  <style>
10
  html,body { height:100%; margin:0; background:#0b1220; color:#fff; font-family:monospace; }
11
  #canvasContainer { position:relative; flex:1; display:flex; justify-content:center; align-items:center; height:100vh; overflow:hidden; }
12
  #gameCanvas { display:block; user-select:none; cursor:crosshair; box-shadow:0 0 20px rgba(0,0,0,.5); width:100%; height:100%; }
 
 
 
13
  /* HUD */
14
  #hudHealth { position:absolute; left:12px; bottom:12px; background:rgba(0,0,0,0.55); padding:6px 8px; border-radius:8px; font-weight:700; font-size:13px; display:flex; align-items:center; gap:8px; z-index:30; }
15
  #hudGearWrap { position:absolute; right:12px; bottom:12px; display:flex; gap:8px; align-items:center; z-index:30; }
 
18
  .selected { outline: 2px solid rgba(255,215,0,0.9); box-shadow: 0 0 6px rgba(255,215,0,0.12); }
19
  .equipped { box-shadow: inset 0 -6px 14px rgba(255,255,255,0.03); border:1px solid rgba(255,255,255,0.04); }
20
  .medkit-count { position:absolute; right:4px; bottom:2px; font-size:10px; color:#ffd; }
21
+ .biome-selected { outline: 3px solid rgba(96,165,250,0.9); }
22
  </style>
23
  </head>
24
  <body>
 
29
  </h1>
30
 
31
  <h2 class="text-2xl text-yellow-300 mb-4">Choose Landing Zone</h2>
32
+ <div id="biomeGrid" class="grid grid-cols-2 gap-6 max-w-xl w-full mb-6">
33
  <div class="biome-selector bg-gray-700 p-6 rounded-lg cursor-pointer hover:bg-yellow-800 flex flex-col items-center" data-biome="desert">
34
  <i data-feather="sun" class="text-yellow-300 mb-2"></i>
35
  <h3 class="font-bold text-lg">Golden Dunes</h3>
 
123
  </div>
124
 
125
  <script>
126
+ // DOM references
127
  const landingScreen = document.getElementById('landingScreen');
128
  const gameScreen = document.getElementById('gameScreen');
129
  const canvas = document.getElementById('gameCanvas');
 
138
  const victoryScreen = document.getElementById('victoryScreen');
139
  const goHomeBtn = document.getElementById('goHomeBtn');
140
  const continueBtn = document.getElementById('continueBtn');
141
+ const biomeGrid = document.getElementById('biomeGrid');
142
 
143
  // World
144
  const WORLD = { width: 6000, height: 4000 };
 
245
  function generateLootForBiome(b){
246
  const roll = Math.random();
247
  if (roll < 0.35) return { type:'medkit', amount:1 };
248
+ if (roll < 0.7) return { type:'materials', amount: 10 }; // always 10
249
  const weapons = [
250
  { name:'Pistol', dmg:12, rate:320, color:'#ffd86b', magSize:12, startReserve:24 },
251
  { name:'SMG', dmg:6, rate:120, color:'#8ef0ff', magSize:30, startReserve:90 },
 
262
  const x = rand(150, WORLD.width-150);
263
  const y = rand(150, WORLD.height-150);
264
  const loot = generateLootForBiome(biomeAt(x,y));
265
+ if (loot.type === 'materials') loot.amount = 10; // ensure 10
 
266
  chests.push({ x,y, opened:false, loot });
267
  }
268
  for (let i=0;i<700;i++){
 
288
  materials: 0,
289
  lastShot: 0,
290
  reloadingUntil: 0,
291
+ reloadPending: false,
292
  lastAttackedTime: 0,
293
  state: 'gather',
294
  gatherTimeLeft: rand(8,16),
295
  target: null,
296
+ nextHealTime: 0
297
  });
298
  }
299
  updatePlayerCount();
 
431
  if (loot.type === 'weapon') pickups.push({ x:px, y:py, type:'weapon', weapon: makeWeaponProto(loot.weapon), ammoInMag: loot.weapon.magSize || 12, ammoReserve: loot.weapon.startReserve || (loot.weapon.magSize*2 || 24) });
432
  else if (loot.type === 'medkit') pickups.push({ x:px, y:py, type:'medkit', amount: loot.amount || 1 });
433
  else if (loot.type === 'materials') pickups.push({ x:px, y:py, type:'materials', amount: loot.amount || 10 });
434
+ if (Math.random() < 0.25) pickups.push({ x:px+8, y:py+8, type:'ammo', forWeapon: null, amount: randInt(6,30) });
 
 
435
  updateHUD();
436
  return;
437
  }
 
477
  }
478
  }
479
 
480
+ // Enemy helpers (equip / collect)
481
  function enemyEquipBestWeapon(e){
482
  let bestIdx = -1;
483
  let bestScore = -Infinity;
 
643
  }
644
  }
645
 
646
+ // Helpers to find nearest
647
  function findNearestChest(e, maxDist = 1200){
648
  let best = null; let bd = Infinity;
649
  for (const c of chests){ if (c.opened) continue; const d = Math.hypot(c.x - e.x, c.y - e.y); if (d < bd && d <= maxDist){ bd = d; best = c; } }
 
660
  return best;
661
  }
662
 
663
+ // Enemy AI with gather-first + separation + reload with delay
664
  function updateEnemies(dt, now){
665
+ // basic separation step variables
666
+ const minSeparation = 20; // minimal distance between enemies
667
+ // Update each enemy movement and actions
668
  for (const e of enemies){
669
  if (e.health <= 0) continue;
670
 
671
+ // if recently attacked go combat
672
+ if (now - e.lastAttackedTime < 4000) e.state = 'combat';
 
 
673
 
674
+ if (e.state === 'gather') e.gatherTimeLeft -= dt;
 
 
675
 
676
+ // Medkit-only heal (consume medkit, with cooldown)
677
  if (e.health < 60 && now >= (e.nextHealTime || 0)){
 
678
  let medIdx = -1;
679
  for (let s=0;s<5;s++){
680
  const it = e.inventory[s];
681
  if (it && it.type === 'medkit' && it.amount > 0){ medIdx = s; break; }
682
  }
683
  if (medIdx !== -1){
 
684
  const kit = e.inventory[medIdx];
685
  kit.amount -= 1;
686
  e.health = Math.min(120, e.health + 50);
687
  if (kit.amount <= 0) e.inventory[medIdx] = null;
688
+ e.nextHealTime = now + 6000;
 
689
  continue;
690
  }
691
  }
692
 
693
+ // Transition gather->combat
694
  const hasUsefulWeapon = e.inventory.some(it => it && it.type === 'weapon' && (it.ammoInMag > 0 || it.ammoReserve > 0));
695
+ if ((e.gatherTimeLeft <= 0 || e.materials >= 20 || hasUsefulWeapon) && e.state === 'gather') e.state = 'combat';
 
 
696
 
697
  if (e.state === 'gather'){
698
+ // Prioritize pickup -> chest -> harvest -> roam
699
+ let p = findNearestPickup(e, 240);
700
+ if (p){
701
+ const angle = Math.atan2(p.y - e.y, p.x - e.x);
702
  e.angle = angle;
703
  e.x += Math.cos(e.angle) * e.speed * dt * 0.9;
704
  e.y += Math.sin(e.angle) * e.speed * dt * 0.9;
705
+ if (Math.hypot(p.x - e.x, p.y - e.y) < 18){
706
+ enemyPickupCollect(e, p);
707
+ const idx = pickups.indexOf(p);
708
  if (idx >= 0) pickups.splice(idx,1);
709
  }
710
  continue;
 
720
  } else {
721
  chestTarget.opened = true;
722
  const loot = chestTarget.loot;
723
+ if (loot.type === 'weapon') enemyPickupCollect(e, { type:'weapon', weapon: makeWeaponProto(loot.weapon), ammoInMag:loot.weapon.magSize||12, ammoReserve:loot.weapon.startReserve||24 });
724
+ else if (loot.type === 'medkit') enemyPickupCollect(e, { type:'medkit', amount: loot.amount || 1 });
725
+ else if (loot.type === 'materials') enemyPickupCollect(e, { type:'materials', amount: loot.amount || 10 });
 
 
 
 
726
  }
727
  continue;
728
  }
 
738
  objTarget.hp -= 40 * dt;
739
  if (objTarget.hp <= 0 && !objTarget.dead){
740
  objTarget.dead = true;
741
+ e.materials += (objTarget.type === 'wood' ? 3 : 6);
 
742
  if (Math.random() < 0.08){
743
  const lootRoll = Math.random();
744
  if (lootRoll < 0.4) enemyPickupCollect(e, { type:'medkit', amount:1 });
 
755
  e.y += Math.sin(e.angle) * e.speed * dt * 0.25;
756
  if (Math.random() < 0.01) e.angle += (Math.random()-0.5)*2;
757
  continue;
758
+ }
759
 
760
+ // Combat state
761
  if (e.equippedIndex === -1) enemyEquipBestWeapon(e);
762
+
763
+ // Handle reload pending (delayed reload to simulate player reload)
764
+ if (e.reloadPending){
765
+ if (now >= e.reloadingUntil){
766
+ const eq = e.inventory[e.equippedIndex];
767
+ if (eq && eq.type === 'weapon') reloadItem(eq);
768
+ e.reloadPending = false;
769
+ e.reloadingUntil = 0;
770
+ }
771
+ }
772
+
773
+ // If equipped weapon empty and reserve present, start reload (delayed)
774
  if (e.equippedIndex >= 0){
775
  const eq = e.inventory[e.equippedIndex];
776
+ if (eq && eq.type === 'weapon' && eq.ammoInMag <= 0 && eq.ammoReserve > 0 && !e.reloadPending && now >= e.reloadingUntil){
777
+ e.reloadPending = true;
778
+ e.reloadingUntil = now + 600 + rand(-100,100); // ~600ms reload time
779
  }
780
  }
781
 
782
+ // choose target (player preference)
783
  let target = player;
784
  let bestDist = Math.hypot(player.x - e.x, player.y - e.y);
785
  for (const other of enemies){
 
788
  if (d < bestDist && Math.random() < 0.6){ bestDist = d; target = other; }
789
  }
790
 
791
+ // retreat/build if low HP and has materials
792
  const distToPlayer = Math.hypot(player.x - e.x, player.y - e.y);
793
  if (distToPlayer < 160 && e.health < 35 && e.materials >= 10 && Math.random() < 0.5){
794
  enemyTryBuild(e);
 
805
  } else {
806
  if (e.equippedIndex >= 0){
807
  const eq = e.inventory[e.equippedIndex];
808
+ if (eq && eq.type === 'weapon' && eq.ammoInMag > 0 && !e.reloadPending && now - e.lastShot > (eq.weapon.rate || 300)){
809
  e.lastShot = now;
810
  eq.ammoInMag -= 1;
811
  const angle = Math.atan2(blocker.y - e.y, blocker.x - e.x);
 
821
  }
822
  }
823
 
824
+ // Attack: if melee range
825
  if (distToPlayer < 36 && now - e.lastMelee > e.meleeRate){
826
  e.lastMelee = now;
827
  const dmg = 10 + randInt(0,8);
 
830
  if (player.health <= 0) { player.health = 0; playerDeath(); }
831
  } else if (e.equippedIndex >= 0){
832
  const eq = e.inventory[e.equippedIndex];
833
+ if (eq && eq.type === 'weapon' && !e.reloadPending && now - (e.lastShot||0) > (eq.weapon.rate || 300)){
834
  if (eq.ammoInMag <= 0){
835
+ // reload will be handled above
836
  } else {
837
  if (hasLineOfSight(e.x, e.y, target.x, target.y)){
838
  e.lastShot = now;
 
841
  shootBullet(e.x + Math.cos(angle)*12, e.y + Math.sin(angle)*12, target.x + (Math.random()-0.5)*6, target.y + (Math.random()-0.5)*6, eq, e.id);
842
  e.lastAttackedTime = now;
843
  } else {
844
+ // move to get LOS
845
  e.angle = Math.atan2(target.y - e.y, target.x - e.x);
846
  e.x += Math.cos(e.angle) * e.speed * dt * 0.6;
847
  e.y += Math.sin(e.angle) * e.speed * dt * 0.6;
848
  }
849
  }
850
  } else {
851
+ // unarmed behavior: rush to melee
852
  e.angle = Math.atan2(target.y - e.y, target.x - e.x);
853
  e.x += Math.cos(e.angle) * e.speed * dt * 0.9;
854
  e.y += Math.sin(e.angle) * e.speed * dt * 0.9;
855
  }
856
  } else {
857
+ // no weapon: rush in
858
  e.angle = Math.atan2(target.y - e.y, target.x - e.x);
859
  e.x += Math.cos(e.angle) * e.speed * dt * 0.9;
860
  e.y += Math.sin(e.angle) * e.speed * dt * 0.9;
861
  }
862
  }
863
+
864
+ // Separation pass to avoid clumping (simple repulsion)
865
+ for (let i = 0; i < enemies.length; i++){
866
+ const a = enemies[i];
867
+ if (!a || a.health <= 0) continue;
868
+ for (let j = i+1; j < enemies.length; j++){
869
+ const b = enemies[j];
870
+ if (!b || b.health <= 0) continue;
871
+ const dx = b.x - a.x, dy = b.y - a.y;
872
+ const d = Math.hypot(dx,dy) || 0.0001;
873
+ const minD = minSeparation;
874
+ if (d < minD){
875
+ const overlap = (minD - d) * 0.5;
876
+ const nx = dx / d, ny = dy / d;
877
+ // push both away proportional to overlap
878
+ b.x += nx * overlap;
879
+ b.y += ny * overlap;
880
+ a.x -= nx * overlap;
881
+ a.y -= ny * overlap;
882
+ }
883
+ }
884
+ // also avoid getting on top of player
885
+ const pdx = a.x - player.x, pdy = a.y - player.y;
886
+ const pd = Math.hypot(pdx,pdy) || 0.0001;
887
+ const avoidDist = 24;
888
+ if (pd < avoidDist){
889
+ const overlap = (avoidDist - pd);
890
+ const nx = pdx / pd, ny = pdy / pd;
891
+ a.x += nx * overlap;
892
+ a.y += ny * overlap;
893
+ }
894
+ // clamp to world
895
+ a.x = Math.max(12, Math.min(WORLD.width-12, a.x));
896
+ a.y = Math.max(12, Math.min(WORLD.height-12, a.y));
897
+ }
898
  }
899
 
900
  // Storm
 
1028
  const s = worldToScreen(e.x,e.y);
1029
  ctx.save();
1030
  ctx.translate(s.x,s.y); ctx.rotate(e.angle);
 
 
1031
  ctx.fillStyle='rgba(0,0,0,0.18)'; ctx.beginPath(); ctx.ellipse(0,12,14,6,0,0,Math.PI*2); ctx.fill();
1032
  ctx.fillStyle='#ff6b6b'; ctx.beginPath(); ctx.moveTo(12,0); ctx.lineTo(-10,-8); ctx.lineTo(-10,8); ctx.closePath(); ctx.fill();
 
 
1033
  ctx.fillStyle='rgba(0,0,0,0.6)'; ctx.fillRect(-18,-22,36,6);
1034
  const hpPct = Math.max(0, Math.min(1, e.health/120));
1035
  ctx.fillStyle='#ff6b6b'; ctx.fillRect(-18,-22,36*hpPct,6);
1036
 
1037
+ // visible equipped weapon
1038
  if (e.equippedIndex >= 0 && e.inventory[e.equippedIndex] && e.inventory[e.equippedIndex].type === 'weapon'){
1039
  const we = e.inventory[e.equippedIndex];
1040
  const color = we.weapon.color || '#ddd';
1041
  ctx.save();
 
1042
  ctx.translate(12, 2);
1043
  ctx.rotate(-0.08);
 
1044
  ctx.fillStyle = color;
1045
  ctx.fillRect(0, -4, 24, 8);
 
1046
  ctx.fillStyle = '#222';
1047
  ctx.fillRect(18, -2, 6, 4);
 
1048
  const magPct = Math.max(0, we.ammoInMag / we.weapon.magSize);
1049
  ctx.fillStyle = 'rgba(0,0,0,0.35)';
1050
  ctx.fillRect(4, 6, 18, 4);
 
1052
  ctx.fillRect(4, 6, 18 * magPct, 4);
1053
  ctx.restore();
1054
  } else {
 
1055
  if (e.equippedIndex === -1){
1056
  ctx.save();
1057
  ctx.translate(12, 6);
 
1064
  }
1065
  }
1066
 
1067
+ // state marker
1068
  ctx.fillStyle = e.state === 'gather' ? 'rgba(0,200,200,0.9)' : (e.state === 'combat' ? 'rgba(255,80,80,0.95)' : 'rgba(255,200,80,0.9)');
1069
  ctx.beginPath(); ctx.arc(18, -18, 5, 0, Math.PI*2); ctx.fill();
 
1070
  ctx.restore();
1071
  }
1072
  }
 
1129
  ctx.restore();
1130
  }
1131
 
1132
+ function drawCrosshair(){}
1133
 
1134
+ // Main loop
1135
  let lastTime = 0;
1136
  function gameLoop(ts){
1137
  if (!gameActive) return;
 
1153
  cameraUpdate();
1154
  mouse.worldX = mouse.canvasX + camera.x;
1155
  mouse.worldY = mouse.canvasY + camera.y;
 
1156
  player.angle = Math.atan2(mouse.worldY - player.y, mouse.worldX - player.x);
1157
 
1158
  let activeWeaponItem = null;
 
1164
 
1165
  // player attack
1166
  if (mouse.down){
1167
+ if (player.equippedIndex === -1) playerMeleeHit();
1168
+ else if (activeWeaponItem && activeWeaponItem.type === 'weapon'){
 
1169
  const now = performance.now();
1170
  if (now - player.lastShot > (activeWeaponItem.weapon.rate || 300)){
1171
  if (activeWeaponItem.ammoInMag > 0){
 
1177
  }
1178
  }
1179
 
 
1180
  if (keys.r) { reloadEquipped(); keys.r = false; }
1181
  if (keys.e){ interactNearby(); keys.e = false; }
1182
  if (keys.q){ tryBuild(); keys.q = false; }
 
1207
  requestAnimationFrame(gameLoop);
1208
  }
1209
 
1210
+ // Landing -> spawn selection
1211
+ let selectedBiome = null;
1212
+ function getSpawnForBiome(b){
1213
+ // choose a spawn area region for each biome
1214
+ if (!b) return { x: WORLD.width/2 + (Math.random()-0.5)*400, y: WORLD.height/2 + (Math.random()-0.5)*400 };
1215
+ if (b === 'desert') return { x: rand(200, WORLD.width*0.3), y: rand(200, WORLD.height*0.3) };
1216
+ if (b === 'forest') return { x: rand(WORLD.width*0.7, WORLD.width-200), y: rand(200, WORLD.height*0.3) };
1217
+ if (b === 'oasis') return { x: rand(200, WORLD.width*0.3), y: rand(WORLD.height*0.7, WORLD.height-200) };
1218
+ if (b === 'ruins') return { x: rand(WORLD.width*0.7, WORLD.width-200), y: rand(WORLD.height*0.7, WORLD.height-200) };
1219
+ return { x: WORLD.width/2, y: WORLD.height/2 };
1220
+ }
1221
+
1222
  // Start/end
1223
  let timerInterval = null;
1224
  let gameTime = 300;
1225
  let gameActive = false;
1226
 
1227
+ function startGame(biome){
1228
+ // set current biome & spawn where selected
1229
+ selectedBiome = biome || selectedBiome;
1230
+ const spawn = getSpawnForBiome(selectedBiome);
1231
+ document.getElementById('currentBiome').textContent = selectedBiome ? selectedBiome.toUpperCase() : 'CENTER';
1232
+
1233
  victoryScreen.classList.add('hidden');
1234
  deathScreen.classList.add('hidden');
1235
 
 
1238
  gameScreen.classList.remove('hidden');
1239
  resizeCanvas();
1240
 
1241
+ player.x = spawn.x;
1242
+ player.y = spawn.y;
1243
  player.health = 100; player.armor = 0; player.kills = 0; player.materials = 0;
1244
  player.inventory = [null,null,null,null,null];
1245
  player.selectedSlot = 0; player.equippedIndex = -1; player.lastShot = 0; player.lastMelee = 0;
 
1248
  initHUD();
1249
  cameraUpdate();
1250
 
1251
+ // small safety: ensure enemies don't spawn right on player
1252
+ for (const e of enemies){
1253
+ if (Math.hypot(e.x - player.x, e.y - player.y) < 180){
1254
+ e.x += (Math.random()<0.5? -1:1) * rand(160,260);
1255
+ e.y += (Math.random()<0.5? -1:1) * rand(160,260);
1256
+ }
1257
+ }
1258
+
1259
  gameTime = 300; storm.active = false; storm.radius = storm.maxRadius;
1260
  document.getElementById('gameTimer').textContent = '5:00';
1261
  timerInterval && clearInterval(timerInterval);
 
1284
 
1285
  document.getElementById('respawnBtn').addEventListener('click', ()=>{ deathScreen.classList.add('hidden'); landingScreen.classList.remove('hidden'); });
1286
  goHomeBtn.addEventListener('click', ()=>{ victoryScreen.classList.add('hidden'); gameScreen.classList.add('hidden'); landingScreen.classList.remove('hidden'); });
1287
+ continueBtn.addEventListener('click', ()=>{ victoryScreen.classList.add('hidden'); startGame(selectedBiome); });
1288
+
1289
+ // Biome click behavior: select & start spawn there
1290
+ document.querySelectorAll('.biome-selector').forEach(el => {
1291
+ el.addEventListener('click', (ev)=>{
1292
+ // visual selection feedback
1293
+ document.querySelectorAll('.biome-selector').forEach(x=>x.classList.remove('biome-selected'));
1294
+ el.classList.add('biome-selected');
1295
+ const biome = el.dataset.biome;
1296
+ selectedBiome = biome;
1297
+ // start game immediately with the selected biome spawn region
1298
+ startGame(biome);
1299
+ });
1300
+ });
1301
 
1302
+ // initialisation
1303
  resizeCanvas();
1304
  populateWorld();
1305
  feather.replace();