Skip to content

Commit 41a3a9d

Browse files
committed
removed food searching bug
1 parent 8bd6f76 commit 41a3a9d

File tree

4 files changed

+117
-70
lines changed

4 files changed

+117
-70
lines changed

node_modules/.cache/.eslintcache

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/core/creature.js

Lines changed: 77 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,103 @@ import { Vector } from "./vector";
22
import { v4 as uuidv4 } from "uuid";
33
export class Creature {
44
constructor(x, y, r, gender, creatures) {
5+
//gene
56
this.id = uuidv4();
6-
this.pos = new Vector(x, y);
77
this.r = r;
8+
this.gender = gender;
89
this.sightRange = 5;
910
this.smellRange = 20;
10-
this.gender = gender;
11-
this.vel = new Vector(0, 0);
12-
this.acc = new Vector(0, 0);
13-
this.acceleration = 0;
14-
this.birth = Date.now();
11+
this.dob = Date.now();
1512
this.color = "#ffffff";
16-
this.isAdult = false;
17-
this.lastBirth = Date.now();
18-
this.isAbleToMate = false;
13+
this.parents = null;
14+
this.children = null;
15+
this.isSightActivated = true;
16+
this.isSmellActivated = true;
17+
18+
//food
1919
this.isFoodFound = false;
2020
this.foundFoodPosition = null;
2121
this.foodId = null;
2222
this.lastFoodTime = Date.now();
23-
this.canEatFood = false;
23+
this.energy = 600;
24+
25+
//reproduction
26+
this.fertility = "fertile";
27+
this.fertilityChance = 1;
28+
this.fertilityType = "all";
29+
this.fertilityConstraints = "all";
30+
this.minFertilityEnergy = 600;
31+
this.isFertilityMemoryActivated = true;
32+
this.lastBirth = Date.now();
33+
34+
//movement
35+
this.pos = new Vector(x, y);
36+
this.vel = new Vector(0, 0);
37+
38+
//immunity
2439
this.isInfected = false;
2540
this.infectionTime = null;
26-
this.energy = 600;
27-
this.parents = null;
28-
this.children = null;
41+
2942
creatures.current.push(this);
3043
}
3144

32-
updateToAdult() {
33-
if (this.birth + 10000 < Date.now()) {
34-
this.isAdult = true;
35-
}
45+
isAdult() {
46+
return this.dob + 10000 < Date.now();
3647
}
37-
updateAbleToMate() {
38-
if (this.lastBirth + 10000 < Date.now()) {
39-
this.isAbleToMate = true;
40-
}
48+
49+
isMale() {
50+
return this.gender == "M";
4151
}
4252

43-
updateNotAbleToMate() {
44-
this.isAbleToMate = false;
45-
this.lastBirth = Date.now();
53+
isFemale() {
54+
return this.gender == "F";
55+
}
56+
57+
isBisexual() {
58+
return this.gender == "B";
59+
}
60+
61+
isFertile() {
62+
if (this.fertility.localeCompare("infertile") === 0) return false;
63+
if (
64+
this.fertility.localeCompare("semiFertile") === 0 &&
65+
this.fertilityChance < Math.random()
66+
)
67+
return false;
68+
return true;
69+
}
70+
71+
canReproduce(partner) {
72+
if (partner.isMale()&&this.lastBirth+5000<Date.now()) {
73+
if (
74+
this.isFertilityMemoryActivated &&
75+
this.energy > this.minFertilityEnergy
76+
) {
77+
return true;
78+
} else if (!this.isFertilityMemoryActivated) {
79+
return true;
80+
} else if (
81+
this.isFertilityMemoryActivated &&
82+
this.energy < this.minFertilityEnergy
83+
) {
84+
return false;
85+
}
86+
}
87+
return false;
4688
}
4789

90+
91+
canSearchFood(){
92+
return this.isSightActivated&&this.isSmellActivated;
93+
}
4894
updateFoodFound(pos, fid) {
4995
this.isFoodFound = true;
5096
this.foundFoodPosition = pos;
5197
this.foodId = fid;
5298
}
99+
53100
updateFoodEaten() {
54-
this.energy = this.energy + 600 + Math.random() * 400;
101+
this.energy = this.energy + 800 + Math.random() * 400;
55102
this.isFoodFound = false;
56103
this.foundFoodPosition = null;
57104
this.foodId = null;
@@ -63,21 +110,12 @@ export class Creature {
63110
this.foodId = null;
64111
}
65112

66-
updateLastFoodTime() {
67-
this.lastFoodTime = Date.now();
68-
}
69-
70-
updateCanEatFood() {
71-
if (this.energy < 300) {
72-
this.canEatFood = true;
73-
} else {
74-
this.canEatFood = false;
75-
}
113+
canEatFood() {
114+
return this.energy < 2000;
76115
}
77116

78117
canDie() {
79118
return this.energy < 20;
80-
// return false;
81119
}
82120

83121
infect() {
@@ -97,7 +135,7 @@ export class Creature {
97135
updateColor() {
98136
if (this.isInfected) {
99137
this.color = "#FF0000";
100-
} else if (this.isAdult) {
138+
} else if (this.isAdult()) {
101139
this.color = this.gender == "F" ? "#FF75BF" : "#2ED9FF";
102140
} else {
103141
this.color = "#ffffff";
@@ -112,7 +150,9 @@ export class Creature {
112150
this.energy -= 1;
113151
}
114152
}
153+
115154
}
155+
116156
drawBall(ctx) {
117157
ctx.beginPath();
118158
ctx.arc(this.pos.x, this.pos.y, this.r, 0, 2 * Math.PI);
@@ -186,9 +226,6 @@ export class Creature {
186226

187227
updateCreature(ctx, speed) {
188228
this.reposition(ctx, speed);
189-
this.updateToAdult();
190-
this.updateAbleToMate();
191-
this.updateCanEatFood();
192229
this.disInfect();
193230
this.updateColor();
194231
this.drawBall(ctx);

src/components/gaFunctions/mating.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@ export function matingPool(creatures) {
88
const observer = creatures.current[i];
99
if (
1010
!vis[i] &&
11-
observer.gender == "F" &&
12-
observer.isAdult &&
13-
observer.isAbleToMate
11+
observer.isFemale() &&
12+
observer.isAdult() &&
13+
observer.isFertile()
1414
) {
1515
vis[i] = true;
1616
for (let j = i + 1; j < l; j++) {
1717
const target = creatures.current[j];
1818
if (
1919
!vis[j] &&
20-
target.gender == "M" &&
21-
target.isAdult &&
20+
target.isMale() &&
21+
target.isAdult() &&
22+
target.isFertile() &&
23+
observer.canReproduce(target) &&
2224
observer.pos.subtr(target.pos).mag() < 5
2325
) {
2426
pairs.push({ mother: observer, father: target });
@@ -36,6 +38,8 @@ export function matingPool(creatures) {
3638
Math.random() > 0.5 ? "F" : "M",
3739
creatures
3840
);
39-
mother.updateNotAbleToMate();
41+
mother.energy -= 600;
42+
mother.lastBirth = Date.now();
4043
});
44+
4145
}
Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,39 @@
11
export function findFood(creatures, foods) {
2-
creatures.current.forEach((creature, index) => {
3-
if (!creature.isFoodFound && creature.canEatFood) {
4-
for (let i = 0; i < foods.current.length; i++) {
5-
const food = foods.current[i];
2+
const l = creatures.current.length;
3+
for (let i = 0; i < l; i++) {
4+
const creature = creatures.current[i];
5+
6+
if (
7+
!creature.isFoodFound &&
8+
creature.canSearchFood() &&
9+
creature.canEatFood()
10+
) {
11+
12+
for (let j = 0; j < foods.current.length; j++) {
13+
const food = foods.current[j];
614
if (food.pos.subtr(creature.pos).mag() < creature.smellRange) {
715
creature.updateFoodFound(food.pos, food.id);
816
break;
917
}
1018
}
11-
} else {
12-
if (
13-
creature.isFoodFound &&
14-
creature.canEatFood &&
15-
creature.pos.subtr(creature.foundFoodPosition).mag() <
16-
creature.sightRange
17-
) {
18-
let foodIndex = -1;
19-
for (var i = 0; i < foods.current.length; i++) {
20-
if (foods.current[i].id.localeCompare(creature.foodId) === 0) {
21-
foodIndex = i;
22-
break;
23-
}
24-
}
25-
if (foodIndex !== -1) {
26-
foods.current.splice(foodIndex, 1);
27-
creature.updateFoodEaten();
28-
creature.updateLastFoodTime();
19+
} if (
20+
creature.isFoodFound &&
21+
creature.canEatFood() &&
22+
creature.canSearchFood() &&
23+
creature.pos.subtr(creature.foundFoodPosition).mag() < creature.sightRange
24+
) {
25+
26+
let foodIndex = -1;
27+
for (var j = 0; j < foods.current.length; j++) {
28+
if (foods.current[j].id.localeCompare(creature.foodId) === 0) {
29+
foodIndex = j;
30+
break;
2931
}
3032
}
33+
if (foodIndex !== -1) {
34+
foods.current.splice(foodIndex, 1);
35+
creature.updateFoodEaten();
36+
}
3137
}
32-
});
38+
}
3339
}

0 commit comments

Comments
 (0)