@@ -2,56 +2,103 @@ import { Vector } from "./vector";
22import { v4 as uuidv4 } from "uuid" ;
33export 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 ) ;
0 commit comments