-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfish.html
More file actions
777 lines (663 loc) · 30.1 KB
/
Copy pathfish.html
File metadata and controls
777 lines (663 loc) · 30.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Evolutionary Fish Simulation</title>
<style>
body { margin: 0; overflow: hidden; background: #000033; }
canvas { display: block; }
#controls {
position: absolute;
top: 10px;
left: 10px;
z-index: 10;
background: rgba(255,255,255,0.8);
padding: 10px;
border-radius: 5px;
}
#info {
position: absolute;
top: 10px;
right: 10px;
z-index: 10;
background: rgba(0,0,0,0.7);
color: white;
padding: 10px;
border-radius: 5px;
font-family: Arial, sans-serif;
font-size: 14px;
min-width: 250px;
}
#pauseControl {
position: absolute;
bottom: 10px;
left: 10px;
z-index: 10;
background: rgba(255,255,255,0.8);
padding: 10px;
border-radius: 5px;
}
button { padding: 5px 10px; margin: 2px; }
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<div id="controls">
<button id="resetBtn">Reset Simulation</button>
</div>
<div id="info"></div>
<div id="pauseControl">
<button id="pauseBtn">Pause</button>
</div>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
let width = canvas.width = window.innerWidth;
let height = canvas.height = window.innerHeight;
// Simulation variables
const FOOD_PER_GENERATION = 72; // Reduced by 20% from 90
let fishes = [];
let eggs = [];
let foodPellets = [];
let generation = 0;
let totalEggsThisGeneration = 0;
let totalFoodThisGeneration = 0;
let isPaused = false;
class Fish {
constructor(parents = null) {
// Physical traits - wider ranges for more visible evolution
this.bodyWidth = 0.6 + Math.random() * 0.8;
this.bodyHeight = 0.6 + Math.random() * 0.8;
this.eyeSize = 0.05 + Math.random() * 0.3;
this.finSize = 0.2 + Math.random() * 0.6;
this.symmetry = 0.5 + Math.random() * 0.7;
// Color traits
this.bodyColor = [Math.random(), Math.random(), Math.random()];
this.eyeColor = [Math.random(), Math.random(), Math.random()];
this.finColor = [Math.random(), Math.random(), Math.random()];
// Position & movement
this.x = Math.random() * width;
this.y = Math.random() * height;
this.angle = Math.random() * Math.PI * 2;
this.speed = 1 + Math.random() * 0.5;
// Beauty preferences
this.beautyWeights = {
symmetry: 0.8 + Math.random() * 0.4,
eyeSize: 0.8 + Math.random() * 0.4,
finSize: 0.8 + Math.random() * 0.4,
bodyShape: 0.8 + Math.random() * 0.4,
colorVibrancy: 0.8 + Math.random() * 0.4
};
// Breeding state
this.isFemale = Math.random() < 0.5;
this.hasMated = false;
this.eggsLaid = 0;
this.cooldown = 0;
this.searchingTime = 0;
this.preferredMate = null;
this.attractionThreshold = 1.5; // Lower initial threshold
this.courtingPartner = null;
this.courtingTime = 0;
this.dancePhase = 0;
// Food and energy system
this.foodPoints = 0;
this.hunger = 0;
this.targetFood = null;
this.canMate = false;
// Food finding abilities (evolvable traits)
this.visionRange = 30 + Math.random() * 70; // Visual detection range
this.visionAngle = Math.PI / 4 + Math.random() * Math.PI / 2; // Field of view
this.smellSensitivity = 0.01 + Math.random() * 0.09; // How well they detect faint scents
this.foodSpeed = 0.8 + Math.random() * 0.4; // Speed boost when hunting
// Hunting strategy (0-1, where 0 = vision-based, 1 = smell-based)
this.huntingStrategy = Math.random();
if (parents) this.inheritTraits(parents);
}
inheritTraits(parents) {
// Mutation rate approximately 6% per trait
const mutationRate = 0.06;
// Inherit physical traits with mutation
this.bodyWidth = this.inheritTrait(parents[0].bodyWidth, parents[1].bodyWidth, mutationRate);
this.bodyHeight = this.inheritTrait(parents[0].bodyHeight, parents[1].bodyHeight, mutationRate);
this.eyeSize = this.inheritTrait(parents[0].eyeSize, parents[1].eyeSize, mutationRate);
this.finSize = this.inheritTrait(parents[0].finSize, parents[1].finSize, mutationRate);
this.symmetry = this.inheritTrait(parents[0].symmetry, parents[1].symmetry, mutationRate);
// Inherit color traits with mutation
for (let i = 0; i < 3; i++) {
this.bodyColor[i] = this.inheritTrait(parents[0].bodyColor[i], parents[1].bodyColor[i], mutationRate);
this.eyeColor[i] = this.inheritTrait(parents[0].eyeColor[i], parents[1].eyeColor[i], mutationRate);
this.finColor[i] = this.inheritTrait(parents[0].finColor[i], parents[1].finColor[i], mutationRate);
}
// Inherit beauty preferences with mutation
for (const key in this.beautyWeights) {
this.beautyWeights[key] = this.inheritTrait(
parents[0].beautyWeights[key],
parents[1].beautyWeights[key],
mutationRate * 2 // Beauty preferences evolve faster
);
}
// Inherit food-finding abilities
this.visionRange = this.inheritTrait(parents[0].visionRange, parents[1].visionRange, mutationRate);
this.visionAngle = this.inheritTrait(parents[0].visionAngle, parents[1].visionAngle, mutationRate);
this.smellSensitivity = this.inheritTrait(parents[0].smellSensitivity, parents[1].smellSensitivity, mutationRate);
this.foodSpeed = this.inheritTrait(parents[0].foodSpeed, parents[1].foodSpeed, mutationRate);
this.huntingStrategy = this.inheritTrait(parents[0].huntingStrategy, parents[1].huntingStrategy, mutationRate);
}
inheritTrait(parent1Trait, parent2Trait, mutationRate) {
let inherited = (parent1Trait + parent2Trait) / 2;
// Apply mutation
if (Math.random() < mutationRate) {
inherited += (Math.random() - 0.5) * 0.3; // Increased mutation amount
}
// Keep values reasonable but allow more range for physical traits
return Math.max(0.01, Math.min(3.0, inherited));
}
evaluateBeauty(other) {
let score = 0;
score += this.beautyWeights.symmetry * other.symmetry;
score += this.beautyWeights.eyeSize * other.eyeSize;
score += this.beautyWeights.finSize * other.finSize;
// Body shape preference (ideal ratio)
const bodyRatio = other.bodyWidth / other.bodyHeight;
const idealRatio = 1.5;
score += this.beautyWeights.bodyShape * (1 - Math.abs(idealRatio - bodyRatio));
// Color vibrancy (how saturated the colors are)
const colorVibrancy = this.calculateColorVibrancy(other.bodyColor);
score += this.beautyWeights.colorVibrancy * colorVibrancy;
// Well-fed fish are slightly more attractive
if (other.foodPoints > 10) {
score *= 1 + (other.foodPoints - 10) * 0.01; // Max 30% boost at 40 food points
}
// Scale up the score to make differences more significant
return score * 2;
}
calculateColorVibrancy(color) {
const max = Math.max(...color);
const min = Math.min(...color);
return (max - min);
}
update() {
if (this.cooldown > 0) this.cooldown--;
// Find food if not yet able to mate
if (!this.canMate && !this.targetFood) {
let bestFood = null;
let bestScore = -Infinity;
for (const food of foodPellets) {
const dx = food.x - this.x;
const dy = food.y - this.y;
const dist = Math.sqrt(dx * dx + dy * dy);
// Check if can detect food
let canDetect = false;
let detectionScore = 0;
// Visual detection (directional)
const angleToFood = Math.atan2(dy, dx);
const angleDiff = Math.abs(angleToFood - this.angle);
const normalizedAngleDiff = Math.min(angleDiff, 2 * Math.PI - angleDiff);
if (dist < this.visionRange && normalizedAngleDiff < this.visionAngle / 2) {
canDetect = true;
detectionScore = (1 - dist / this.visionRange) * (1 - this.huntingStrategy);
}
// Smell detection (based on scent strength)
const scentStrength = food.getScentStrengthAt(this.x, this.y);
if (scentStrength > this.smellSensitivity) {
canDetect = true;
const smellScore = scentStrength * this.huntingStrategy;
detectionScore = Math.max(detectionScore, smellScore);
}
// Choose the best detected food
if (canDetect && detectionScore > bestScore) {
bestScore = detectionScore;
bestFood = food;
}
}
this.targetFood = bestFood;
}
// Move towards food if hunting, otherwise normal swimming
if (this.targetFood && !this.canMate) {
const dx = this.targetFood.x - this.x;
const dy = this.targetFood.y - this.y;
this.angle = Math.atan2(dy, dx);
// Check if reached food
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist < 10) {
this.foodPoints += this.targetFood.value;
const index = foodPellets.indexOf(this.targetFood);
if (index > -1) {
foodPellets.splice(index, 1);
}
this.targetFood = null;
// Check if can mate now
if (this.foodPoints >= 10) {
this.canMate = true;
}
}
// Move faster when hunting food
this.x += Math.cos(this.angle) * this.speed * this.foodSpeed;
this.y += Math.sin(this.angle) * this.speed * this.foodSpeed;
} else {
// Normal swimming behavior
this.wanderAngle = this.wanderAngle || 0;
this.wanderAngle += (Math.random() - 0.5) * 0.3;
this.angle += Math.sin(this.wanderAngle) * 0.03;
// Normal speed
this.x += Math.cos(this.angle) * this.speed;
this.y += Math.sin(this.angle) * this.speed;
}
// Vary base speed occasionally
if (Math.random() < 0.01) {
this.speed = 0.8 + Math.random() * 1.2;
}
// Hard boundaries - bounce off walls
const margin = 20;
if (this.x < margin) {
this.x = margin;
this.angle = Math.PI - this.angle;
} else if (this.x > width - margin) {
this.x = width - margin;
this.angle = Math.PI - this.angle;
}
if (this.y < margin) {
this.y = margin;
this.angle = -this.angle;
} else if (this.y > height - margin) {
this.y = height - margin;
this.angle = -this.angle;
}
}
draw(ctx) {
ctx.save();
ctx.translate(this.x, this.y);
ctx.rotate(this.angle);
// Draw vision cone if hunting
if (!this.canMate && this.targetFood) {
ctx.fillStyle = 'rgba(255, 255, 0, 0.1)';
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.arc(0, 0, this.visionRange, -this.visionAngle/2, this.visionAngle/2);
ctx.closePath();
ctx.fill();
// Draw scent detection indicator (pulsing nose)
if (this.targetFood && this.targetFood.getScentStrengthAt(this.x, this.y) > this.smellSensitivity) {
ctx.fillStyle = 'rgba(0, 255, 0, 0.3)';
const pulseSize = 5 + Math.sin(Date.now() * 0.005) * 2;
ctx.beginPath();
ctx.arc(this.bodyWidth * 12, 0, pulseSize, 0, Math.PI * 2);
ctx.fill();
}
}
// Draw body
ctx.fillStyle = `rgb(${Math.floor(this.bodyColor[0]*255)},${Math.floor(this.bodyColor[1]*255)},${Math.floor(this.bodyColor[2]*255)})`;
ctx.beginPath();
ctx.ellipse(0, 0, this.bodyWidth * 15, this.bodyHeight * 10, 0, 0, Math.PI * 2);
ctx.fill();
// Draw tail fin
ctx.fillStyle = `rgb(${Math.floor(this.finColor[0]*255)},${Math.floor(this.finColor[1]*255)},${Math.floor(this.finColor[2]*255)})`;
ctx.beginPath();
ctx.moveTo(-this.bodyWidth * 15, 0);
ctx.lineTo(-this.bodyWidth * 15 - this.finSize * 20, -this.finSize * 10);
ctx.lineTo(-this.bodyWidth * 15 - this.finSize * 20, this.finSize * 10);
ctx.closePath();
ctx.fill();
// Draw eyes (symmetry affects eye placement)
const eyeOffset = this.bodyHeight * 3 * this.symmetry;
ctx.fillStyle = `rgb(${Math.floor(this.eyeColor[0]*255)},${Math.floor(this.eyeColor[1]*255)},${Math.floor(this.eyeColor[2]*255)})`;
ctx.beginPath();
ctx.arc(this.bodyWidth * 5, -eyeOffset, this.eyeSize * 5, 0, Math.PI * 2);
ctx.arc(this.bodyWidth * 5, eyeOffset, this.eyeSize * 5, 0, Math.PI * 2);
ctx.fill();
// Draw pupils
ctx.fillStyle = 'black';
ctx.beginPath();
ctx.arc(this.bodyWidth * 5, -eyeOffset, this.eyeSize * 2, 0, Math.PI * 2);
ctx.arc(this.bodyWidth * 5, eyeOffset, this.eyeSize * 2, 0, Math.PI * 2);
ctx.fill();
ctx.restore();
}
}
class FoodPellet {
constructor() {
this.x = Math.random() * (width - 40) + 20;
this.y = Math.random() * (height - 40) + 20;
this.value = 5; // Each pellet is worth 5 points
this.radius = 3;
this.scentStrength = 1.0; // How strong the scent is at the source
this.scentDecayRate = 0.02; // How quickly scent fades with distance
}
draw(ctx) {
ctx.fillStyle = '#00ff00';
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fill();
}
getScentStrengthAt(x, y) {
const dx = x - this.x;
const dy = y - this.y;
const distance = Math.sqrt(dx * dx + dy * dy);
// Scent strength decreases exponentially with distance
return this.scentStrength * Math.exp(-distance * this.scentDecayRate);
}
}
function createInitialGeneration() {
fishes = [];
eggs = [];
foodPellets = [];
totalEggsThisGeneration = 0;
totalFoodThisGeneration = 0;
// Create first pair
const male = new Fish();
male.isFemale = false;
male.bodyColor = [0.2, 0.6, 1]; // Blue
male.eyeColor = [1, 1, 0]; // Yellow
male.finColor = [0.1, 0.3, 0.8]; // Dark blue
male.attractionThreshold = 0; // First generation will mate regardless
male.foodPoints = 10; // First generation starts fed
male.canMate = true; // Ready to mate
const female = new Fish();
female.isFemale = true;
female.bodyColor = [1, 0.6, 0.2]; // Orange
female.eyeColor = [0.8, 0.8, 0.2]; // Light yellow
female.finColor = [0.8, 0.4, 0.1]; // Red-orange
female.attractionThreshold = 0; // First generation will mate regardless
female.foodPoints = 10; // First generation starts fed
female.canMate = true; // Ready to mate
// Spread them out in the tank
male.x = width * 0.3;
male.y = height * 0.5;
female.x = width * 0.7;
female.y = height * 0.5;
fishes.push(male, female);
// Don't spawn food for first generation since they start fed
}
function spawnFood() {
// Spawn food pellets worth 72 points total (20% less than original 90)
while (totalFoodThisGeneration < FOOD_PER_GENERATION) {
foodPellets.push(new FoodPellet());
totalFoodThisGeneration += 5;
}
}
function findMate(fish) {
if (fish.cooldown > 0) return null;
// Must have eaten at least 10 food to mate
if (!fish.canMate) return null;
// Increase searching time and lower standards gradually
fish.searchingTime++;
if (fish.searchingTime > 200) { // Lowered from 300
fish.attractionThreshold *= 0.99; // Lower standards faster
}
if (fish.isFemale) {
// Females only pursue males they find very attractive and who can mate
let bestMale = null;
let highestScore = -Infinity;
for (const other of fishes) {
if (!other.isFemale && other.cooldown === 0 && !other.courtingPartner && other.canMate) {
const score = fish.evaluateBeauty(other);
if (score > highestScore && score >= fish.attractionThreshold) {
highestScore = score;
bestMale = other;
}
}
}
// Only pursue if male is sufficiently attractive
if (bestMale) {
// Check if other females are also interested
let competitors = 0;
for (const otherFemale of fishes) {
if (otherFemale !== fish && otherFemale.isFemale && !otherFemale.courtingPartner && otherFemale.canMate) {
const otherScore = otherFemale.evaluateBeauty(bestMale);
if (otherScore >= otherFemale.attractionThreshold) {
competitors++;
}
}
}
// If too much competition, might not pursue
if (competitors > 1 && Math.random() < 0.3) {
return null;
}
}
return bestMale;
} else {
// Males pursue any female they find attractive enough who can mate
let bestFemale = null;
let highestScore = -Infinity;
for (const other of fishes) {
if (other.isFemale && other.cooldown === 0 && !other.courtingPartner && other.canMate) {
const score = fish.evaluateBeauty(other);
if (score > highestScore && score >= fish.attractionThreshold * 0.5) {
highestScore = score;
bestFemale = other;
}
}
}
return bestFemale;
}
return null;
}
function performCourtship(fish) {
const partner = fish.courtingPartner;
fish.courtingTime++;
// Calculate center point between the two fish
const centerX = (fish.x + partner.x) / 2;
const centerY = (fish.y + partner.y) / 2;
// Smaller, more subtle mating dance
const radius = 15; // Reduced from 30
const speed = 0.05; // Slower dance
fish.dancePhase += speed;
// Subtle figure-8 pattern
const offset = fish.isFemale ? 0 : Math.PI;
const pattern = Math.sin(fish.dancePhase * 2);
fish.x = centerX + Math.cos(fish.dancePhase + offset) * radius * (1 + 0.3 * pattern);
fish.y = centerY + Math.sin(fish.dancePhase + offset) * radius;
// Face each other
fish.angle = Math.atan2(partner.y - fish.y, partner.x - fish.x);
// After 10 seconds (600 frames at 60fps), check if mating succeeds
if (fish.courtingTime > 600) {
if (fish.isFemale && totalEggsThisGeneration < 6) { // Check total eggs, not individual
// For first generation, always mate successfully
let matingSuccess = false;
if (generation === 0) {
matingSuccess = true;
} else {
// Female might reject the male even after courtship
const finalAttraction = fish.evaluateBeauty(partner);
matingSuccess = finalAttraction > (fish.attractionThreshold * 1.1); // Increased threshold for stronger selection
}
if (matingSuccess) {
// Female accepts and lays egg
eggs.push({
x: fish.x,
y: fish.y,
hatchTime: 0,
maxHatchTime: 180,
parents: [fish, partner]
});
fish.eggsLaid++;
totalEggsThisGeneration++;
// Shorter cooldown to allow for multiple matings
fish.cooldown = 60;
partner.cooldown = 30; // Male has shorter cooldown
// Reset attraction threshold for next mating
fish.attractionThreshold = 1.5;
fish.searchingTime = 0;
} else {
// Female rejects after courtship
fish.cooldown = 60; // Can try again sooner
partner.cooldown = 15; // Male recovers quickly from rejection
}
}
// End courtship regardless of outcome
fish.searchingTime = 0;
partner.searchingTime = 0;
fish.courtingPartner = null;
partner.courtingPartner = null;
}
}
function update() {
if (isPaused) return;
// Update all fish
for (const fish of fishes) {
if (fish.courtingPartner) {
// If courting, perform mating dance
performCourtship(fish);
} else {
// Normal swimming and eating
fish.update();
}
// Look for mates only if able
if (totalEggsThisGeneration < 6 && !fish.courtingPartner && fish.canMate) {
const mate = findMate(fish);
if (mate && !mate.courtingPartner) {
// Start courtship
fish.courtingPartner = mate;
mate.courtingPartner = fish;
fish.courtingTime = 0;
mate.courtingTime = 0;
}
}
}
// Update eggs
for (let i = eggs.length - 1; i >= 0; i--) {
const egg = eggs[i];
egg.hatchTime++;
if (egg.hatchTime >= egg.maxHatchTime) {
// All parents die when 6 eggs are laid
if (totalEggsThisGeneration >= 6) {
fishes = [];
generation++;
// Hatch all eggs at once
for (let j = 0; j < eggs.length; j++) {
const newFish = new Fish(eggs[j].parents);
// Ensure we get 3 males and 3 females
newFish.isFemale = j < 3;
newFish.x = eggs[j].x + (Math.random() - 0.5) * 50;
newFish.y = eggs[j].y + (Math.random() - 0.5) * 50;
fishes.push(newFish);
}
eggs = [];
totalEggsThisGeneration = 0;
// Spawn food for new generation
totalFoodThisGeneration = 0;
foodPellets = [];
spawnFood();
break;
}
}
}
}
function draw() {
// Draw dark deep blue background
ctx.fillStyle = "#000522";
ctx.fillRect(0, 0, width, height);
// Draw aquarium edges
ctx.strokeStyle = "#003366";
ctx.lineWidth = 3;
ctx.strokeRect(10, 10, width - 20, height - 20);
// Draw food pellets with scent clouds
for (const food of foodPellets) {
// Draw scent cloud
const gradient = ctx.createRadialGradient(food.x, food.y, 0, food.x, food.y, 100);
gradient.addColorStop(0, 'rgba(0, 255, 0, 0.1)');
gradient.addColorStop(1, 'rgba(0, 255, 0, 0)');
ctx.fillStyle = gradient;
ctx.beginPath();
ctx.arc(food.x, food.y, 100, 0, Math.PI * 2);
ctx.fill();
// Draw food pellet
food.draw(ctx);
}
// Draw fish
for (const fish of fishes) {
fish.draw(ctx);
// Draw heart above courting fish
if (fish.courtingPartner) {
ctx.save();
ctx.translate(fish.x, fish.y - 20);
ctx.fillStyle = 'rgba(255, 105, 180, 0.8)';
ctx.beginPath();
ctx.moveTo(0, -5);
ctx.bezierCurveTo(-5, -10, -10, -5, -10, 0);
ctx.bezierCurveTo(-10, 5, -5, 10, 0, 15);
ctx.bezierCurveTo(5, 10, 10, 5, 10, 0);
ctx.bezierCurveTo(10, -5, 5, -10, 0, -5);
ctx.fill();
ctx.restore();
}
// Draw food bar for fish
if (fish.foodPoints > 0) {
ctx.save();
ctx.translate(fish.x, fish.y + 15);
// Background bar
ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
ctx.fillRect(-15, 0, 30, 3);
// Food bar
const foodBarWidth = Math.min(30, (fish.foodPoints / 40) * 30);
ctx.fillStyle = fish.foodPoints >= 10 ? '#00ff00' : '#ffff00';
ctx.fillRect(-15, 0, foodBarWidth, 3);
ctx.restore();
}
}
// Draw eggs
for (const egg of eggs) {
const progress = egg.hatchTime / egg.maxHatchTime;
ctx.fillStyle = `rgba(255, 255, 150, ${0.7 + progress * 0.3})`;
ctx.beginPath();
ctx.arc(egg.x, egg.y, 5 + progress * 3, 0, Math.PI * 2);
ctx.fill();
// Egg glow
ctx.strokeStyle = `rgba(255, 255, 200, ${0.3 - progress * 0.3})`;
ctx.lineWidth = 2;
ctx.stroke();
}
// Update info panel
updateInfoPanel();
}
function updateInfoPanel() {
const infoDiv = document.getElementById('info');
let html = `<div>Generation: ${generation}</div>`;
html += `<div>Eggs laid: ${totalEggsThisGeneration}/6</div>`;
html += `<div>Food available: ${foodPellets.length * 5}</div>`;
// Debug info
let searchingCount = fishes.filter(f => f.searchingTime > 100 && !f.courtingPartner && f.canMate).length;
let courtingCount = fishes.filter(f => f.courtingPartner).length;
let hungryCount = fishes.filter(f => !f.canMate).length;
if (searchingCount > 0) {
html += `<div>Fish searching: ${searchingCount}</div>`;
}
if (courtingCount > 0) {
html += `<div>Fish courting: ${courtingCount}</div>`;
}
if (hungryCount > 0) {
html += `<div>Fish hungry: ${hungryCount}</div>`;
}
html += `<div style="margin-top: 10px; border-top: 1px solid #666; padding-top: 5px;">Female Stats:</div>`;
// Female stats
let femaleIndex = 0;
for (const fish of fishes) {
if (fish.isFemale) {
html += `<div>Female ${femaleIndex + 1}: ${fish.eggsLaid} eggs, ${fish.foodPoints} food</div>`;
femaleIndex++;
}
}
infoDiv.innerHTML = html;
}
function animate() {
update();
draw();
requestAnimationFrame(animate);
}
document.getElementById("resetBtn").addEventListener("click", () => {
generation = 0;
createInitialGeneration();
});
document.getElementById("pauseBtn").addEventListener("click", () => {
isPaused = !isPaused;
document.getElementById("pauseBtn").textContent = isPaused ? "Resume" : "Pause";
});
window.addEventListener("resize", () => {
width = canvas.width = window.innerWidth;
height = canvas.height = window.innerHeight;
});
createInitialGeneration();
animate();
</script>
</body>
</html>