BlueGit

blue-star cellular-evolution

Merge branch 'creatureinitialization'

timfa Tim · pushed by timfa

Authored 2021-10-12 · 5 years ago

Hash
9d821f72af4eecf4b7eee807ef070e59df9140f9
9 files changed +259 −16
images/orb.png Binary file +0 −0

The contents of this binary file are not shown.

js/game.js +176 −7
@@ -1,3 +1,63 @@
1 /**
2 * Part of a creature!
3 */
4
5 var cell =
6 {
7 start: function()
8 {
9
10 },
11
12 setType: function(typeID)
13 {
14 this.renderer.subImage = typeID;
15 }
16 }
17
18 /**
19 * It's a thing!
20 */
21
22 var creature =
23 {
24 dna: {},
25 cellInstances: [],
26
27 start: function()
28 {
29
30 },
31
32 initCells()
33 {
34 for(var i = 0; i < this.dna.length; i++)
35 {
36 var newCell = GameObject.create(cell, this.transform.position.x + (this.dna[i].pos.x * 32), this.transform.position.y + (this.dna[i].pos.y * 32), s_orb, 500);
37
38 newCell.setType(this.dna[i].type);
39
40 this.cellInstances.push(newCell);
41 }
42 },
43
44 update: function(deltaTime)
45 {
46
47 },
48
49 onDestroy: function()
50 {
51
52 },
53
54 onCollision: function(other)
55 {
56
57 }
58 }
59
60
1 61 /**
2 62 * DNA strings contain the makeup of a creature, seriving as a template for the creature factory
3 63 * The DNA string must be automatically generatable so that random mutations can occur.
@@ -67,10 +127,117 @@
67 127 * - use value 0 when the last cell of a circle is detected. e.g.: A B and C are a loop. A requests input from B, which requests input from C, which requests input from A. A would complete the circle and start a loop, so instead 0 is returned.
68 128 */
69 129
70 function sigmoid(z)
71 {
72 return 1 / (1 + Math.exp(-z));
73 }
130 //TODO: Make sort of random maybe
131 function GenerateNewDNA(startingCells = 10)
132 {
133 var dna = [
134 {
135 id: 0,
136 type: TYPES.HEART,
137 inputs: [],
138 pos: {x:0, y:0}
139 }
140 ];
141
142 for(var i = 0; i < startingCells; i++)
143 AddRandomCell(dna)
144
145 return dna;
146 }
147
148 function AddRandomCell(creature)
149 {
150 var totalWeight = 0;
151 var weights = Object.values(Weights);
152
153 for (var i = 0; i < weights.length; i++)
154 {
155 totalWeight += weights[i];
156 }
157
158 var current = Math.random() * totalWeight;
159 var selected;
160
161 for (const [key, value] of Object.entries(Weights))
162 {
163 current -= value;
164
165 if(current <= 0)
166 {
167 selected = key;
168 break;
169 }
170 }
171
172 var id = 1;
173
174 for(var i = 0; i < creature.length; i++)
175 {
176 while(id == creature[i].id)
177 id++;
178 }
179
180 var done = false;
181 var pos = {x:0, y:0}
182
183 while(!done)
184 {
185 done = true;
186
187 for(var i = 0; i < creature.length; i++)
188 {
189 if(pos.x == creature[i].pos.x && pos.y == creature[i].pos.y)
190 {
191 done = false;
192 var val = Math.random() > 0.5? -1 : 1;
193 var key = Math.random() > 0.5? "y" : "x";
194
195 pos[key] += val;
196 break;
197 }
198 }
199 }
200
201 var cell =
202 {
203 id: id,
204 type: TYPES[selected],
205 inputs: [],
206 pos: pos
207 }
208
209 creature.push(cell);
210 }
211
212 var TYPES =
213 {
214 HEART: 0,
215 BRAIN: 1,
216 MOUTH: 2,
217 LOCOMOTOR: 3,
218 ROTATOR: 4,
219 EYE: 5,
220 STOMACH: 6,
221 MEAT: 7
222 }
223
224 var Weights =
225 {
226 BRAIN: 1,
227 MOUTH: 2,
228 LOCOMOTOR: 2,
229 ROTATOR: 1,
230 EYE: 2,
231 STOMACH: 3,
232 MEAT: 4
233 }
234
235
236
237 function sigmoid(z)
238 {
239 return 1 / (1 + Math.exp(-z));
240 }
74 241
75 242 /**
76 243 * Snake Food
@@ -102,8 +269,6 @@ var gem =
102 269 this.shakeTimer = this.minShakeTimer + (Math.random() * (this.maxShakeTimer - this.minShakeTimer));
103 270 this.currentShakeTimer = 0;
104 271
105 enemySnake.edibles.push(this);
106
107 272 GameAudio.play("bubble");
108 273 },
109 274
@@ -247,7 +412,7 @@ var gemSpawner =
247 412 */
248 413
249 414 var s_head = new image("images/head.png", 4, 4);
250 var s_orb = new image("images/orb.png", 4, 4);
415 var s_orb = new image("images/orb.png", 8, 4);
251 416 var s_gem = new image("images/gems.png", 72, 6);
252 417
253 418 /**
@@ -281,6 +446,10 @@ function gameStart()
281 446 GameAudio.init(sounds, soundsFolder);
282 447
283 448 p1 = GameObject.create(gemSpawner, GameWindow.width / 2, GameWindow.height / 2, null, 500);
449 tester = GameObject.create(creature, GameWindow.width / 2, GameWindow.height / 2, null, 500);
450
451 tester.dna = GenerateNewDNA();
452 tester.initCells();
284 453 }
285 454
286 455 function update(deltaTime)
@@ -1,3 +1,63 @@
1/**
2 * Part of a creature!
3 */
4
5var cell =
6{
7 start: function()
8 {
9
10 },
11
12 setType: function(typeID)
13 {
14 this.renderer.subImage = typeID;
15 }
16}
17
18/**
19 * It's a thing!
20 */
21
22var creature =
23{
24 dna: {},
25 cellInstances: [],
26
27 start: function()
28 {
29
30 },
31
32 initCells()
33 {
34 for(var i = 0; i < this.dna.length; i++)
35 {
36 var newCell = GameObject.create(cell, this.transform.position.x + (this.dna[i].pos.x * 32), this.transform.position.y + (this.dna[i].pos.y * 32), s_orb, 500);
37
38 newCell.setType(this.dna[i].type);
39
40 this.cellInstances.push(newCell);
41 }
42 },
43
44 update: function(deltaTime)
45 {
46
47 },
48
49 onDestroy: function()
50 {
51
52 },
53
54 onCollision: function(other)
55 {
56
57 }
58}
59
60
1/**
61/**
2 * DNA strings contain the makeup of a creature, seriving as a template for the creature factory
62 * DNA strings contain the makeup of a creature, seriving as a template for the creature factory
3 * The DNA string must be automatically generatable so that random mutations can occur.
63 * The DNA string must be automatically generatable so that random mutations can occur.
@@ -67,10 +127,117 @@
67 * - use value 0 when the last cell of a circle is detected. e.g.: A B and C are a loop. A requests input from B, which requests input from C, which requests input from A. A would complete the circle and start a loop, so instead 0 is returned.
127 * - use value 0 when the last cell of a circle is detected. e.g.: A B and C are a loop. A requests input from B, which requests input from C, which requests input from A. A would complete the circle and start a loop, so instead 0 is returned.
68 */
128 */
69
129
70 function sigmoid(z)
130//TODO: Make sort of random maybe
71 {
131function GenerateNewDNA(startingCells = 10)
72 return 1 / (1 + Math.exp(-z));
132{
73 }
133 var dna = [
134 {
135 id: 0,
136 type: TYPES.HEART,
137 inputs: [],
138 pos: {x:0, y:0}
139 }
140 ];
141
142 for(var i = 0; i < startingCells; i++)
143 AddRandomCell(dna)
144
145 return dna;
146}
147
148function AddRandomCell(creature)
149{
150 var totalWeight = 0;
151 var weights = Object.values(Weights);
152
153 for (var i = 0; i < weights.length; i++)
154 {
155 totalWeight += weights[i];
156 }
157
158 var current = Math.random() * totalWeight;
159 var selected;
160
161 for (const [key, value] of Object.entries(Weights))
162 {
163 current -= value;
164
165 if(current <= 0)
166 {
167 selected = key;
168 break;
169 }
170 }
171
172 var id = 1;
173
174 for(var i = 0; i < creature.length; i++)
175 {
176 while(id == creature[i].id)
177 id++;
178 }
179
180 var done = false;
181 var pos = {x:0, y:0}
182
183 while(!done)
184 {
185 done = true;
186
187 for(var i = 0; i < creature.length; i++)
188 {
189 if(pos.x == creature[i].pos.x && pos.y == creature[i].pos.y)
190 {
191 done = false;
192 var val = Math.random() > 0.5? -1 : 1;
193 var key = Math.random() > 0.5? "y" : "x";
194
195 pos[key] += val;
196 break;
197 }
198 }
199 }
200
201 var cell =
202 {
203 id: id,
204 type: TYPES[selected],
205 inputs: [],
206 pos: pos
207 }
208
209 creature.push(cell);
210}
211
212var TYPES =
213{
214 HEART: 0,
215 BRAIN: 1,
216 MOUTH: 2,
217 LOCOMOTOR: 3,
218 ROTATOR: 4,
219 EYE: 5,
220 STOMACH: 6,
221 MEAT: 7
222}
223
224var Weights =
225{
226 BRAIN: 1,
227 MOUTH: 2,
228 LOCOMOTOR: 2,
229 ROTATOR: 1,
230 EYE: 2,
231 STOMACH: 3,
232 MEAT: 4
233}
234
235
236
237function sigmoid(z)
238{
239 return 1 / (1 + Math.exp(-z));
240}
74
241
75/**
242/**
76 * Snake Food
243 * Snake Food
@@ -102,8 +269,6 @@ var gem =
102 this.shakeTimer = this.minShakeTimer + (Math.random() * (this.maxShakeTimer - this.minShakeTimer));
269 this.shakeTimer = this.minShakeTimer + (Math.random() * (this.maxShakeTimer - this.minShakeTimer));
103 this.currentShakeTimer = 0;
270 this.currentShakeTimer = 0;
104
271
105 enemySnake.edibles.push(this);
106
107 GameAudio.play("bubble");
272 GameAudio.play("bubble");
108 },
273 },
109
274
@@ -247,7 +412,7 @@ var gemSpawner =
247 */
412 */
248
413
249var s_head = new image("images/head.png", 4, 4);
414var s_head = new image("images/head.png", 4, 4);
250var s_orb = new image("images/orb.png", 4, 4);
415var s_orb = new image("images/orb.png", 8, 4);
251var s_gem = new image("images/gems.png", 72, 6);
416var s_gem = new image("images/gems.png", 72, 6);
252
417
253/**
418/**
@@ -281,6 +446,10 @@ function gameStart()
281 GameAudio.init(sounds, soundsFolder);
446 GameAudio.init(sounds, soundsFolder);
282
447
283 p1 = GameObject.create(gemSpawner, GameWindow.width / 2, GameWindow.height / 2, null, 500);
448 p1 = GameObject.create(gemSpawner, GameWindow.width / 2, GameWindow.height / 2, null, 500);
449 tester = GameObject.create(creature, GameWindow.width / 2, GameWindow.height / 2, null, 500);
450
451 tester.dna = GenerateNewDNA();
452 tester.initCells();
284}
453}
285
454
286function update(deltaTime)
455function update(deltaTime)
js/lastbuild.txt +12 −0
@@ -6,3 +6,15 @@
6 6 -09-10-2021- 16:24
7 7 -09-10-2021- 16:24
8 8 -10-10-2021- 0:29
9 -12-10-2021- 19:50
10 -12-10-2021- 19:50
11 -12-10-2021- 19:51
12 -12-10-2021- 19:52
13 -12-10-2021- 19:54
14 -12-10-2021- 19:54
15 -12-10-2021- 19:55
16 -12-10-2021- 19:58
17 -12-10-2021- 20:00
18 -12-10-2021- 20:01
19 -12-10-2021- 20:02
20 -12-10-2021- 20:03
@@ -6,3 +6,15 @@
6-09-10-2021- 16:24
6-09-10-2021- 16:24
7-09-10-2021- 16:24
7-09-10-2021- 16:24
8-10-10-2021- 0:29
8-10-10-2021- 0:29
9-12-10-2021- 19:50
10-12-10-2021- 19:50
11-12-10-2021- 19:51
12-12-10-2021- 19:52
13-12-10-2021- 19:54
14-12-10-2021- 19:54
15-12-10-2021- 19:55
16-12-10-2021- 19:58
17-12-10-2021- 20:00
18-12-10-2021- 20:01
19-12-10-2021- 20:02
20-12-10-2021- 20:03
src/cell.js added +16 −0
@@ -0,0 +1,16 @@
1 /**
2 * Part of a creature!
3 */
4
5 var cell =
6 {
7 start: function()
8 {
9
10 },
11
12 setType: function(typeID)
13 {
14 this.renderer.subImage = typeID;
15 }
16 }
@@ -0,0 +1,16 @@
1/**
2 * Part of a creature!
3 */
4
5var cell =
6{
7 start: function()
8 {
9
10 },
11
12 setType: function(typeID)
13 {
14 this.renderer.subImage = typeID;
15 }
16}
src/creature.js added +41 −0
@@ -0,0 +1,41 @@
1 /**
2 * It's a thing!
3 */
4
5 var creature =
6 {
7 dna: {},
8 cellInstances: [],
9
10 start: function()
11 {
12
13 },
14
15 initCells()
16 {
17 for(var i = 0; i < this.dna.length; i++)
18 {
19 var newCell = GameObject.create(cell, this.transform.position.x + (this.dna[i].pos.x * 32), this.transform.position.y + (this.dna[i].pos.y * 32), s_orb, 500);
20
21 newCell.setType(this.dna[i].type);
22
23 this.cellInstances.push(newCell);
24 }
25 },
26
27 update: function(deltaTime)
28 {
29
30 },
31
32 onDestroy: function()
33 {
34
35 },
36
37 onCollision: function(other)
38 {
39
40 }
41 }
@@ -0,0 +1,41 @@
1/**
2 * It's a thing!
3 */
4
5var creature =
6{
7 dna: {},
8 cellInstances: [],
9
10 start: function()
11 {
12
13 },
14
15 initCells()
16 {
17 for(var i = 0; i < this.dna.length; i++)
18 {
19 var newCell = GameObject.create(cell, this.transform.position.x + (this.dna[i].pos.x * 32), this.transform.position.y + (this.dna[i].pos.y * 32), s_orb, 500);
20
21 newCell.setType(this.dna[i].type);
22
23 this.cellInstances.push(newCell);
24 }
25 },
26
27 update: function(deltaTime)
28 {
29
30 },
31
32 onDestroy: function()
33 {
34
35 },
36
37 onCollision: function(other)
38 {
39
40 }
41}
src/dnastring.js +9 −6
@@ -68,18 +68,18 @@
68 68 */
69 69
70 70 //TODO: Make sort of random maybe
71 function GenerateNewDNA(startingCells = 5)
71 function GenerateNewDNA(startingCells = 10)
72 72 {
73 73 var dna = [
74 74 {
75 75 id: 0,
76 76 type: TYPES.HEART,
77 77 inputs: [],
78 position: {x:0, y:0}
78 pos: {x:0, y:0}
79 79 }
80 80 ];
81 81
82 for(var i = 5; i < startingCells; i++)
82 for(var i = 0; i < startingCells; i++)
83 83 AddRandomCell(dna)
84 84
85 85 return dna;
@@ -103,7 +103,10 @@ function AddRandomCell(creature)
103 103 current -= value;
104 104
105 105 if(current <= 0)
106 {
106 107 selected = key;
108 break;
109 }
107 110 }
108 111
109 112 var id = 1;
@@ -162,11 +165,11 @@ var Weights =
162 165 {
163 166 BRAIN: 1,
164 167 MOUTH: 2,
165 LOCOMOTOR: 1,
168 LOCOMOTOR: 2,
166 169 ROTATOR: 1,
167 EYE: 3,
170 EYE: 2,
168 171 STOMACH: 3,
169 MEAT: 5
172 MEAT: 4
170 173 }
171 174
172 175
@@ -68,18 +68,18 @@
68 */
68 */
69
69
70//TODO: Make sort of random maybe
70//TODO: Make sort of random maybe
71function GenerateNewDNA(startingCells = 5)
71function GenerateNewDNA(startingCells = 10)
72{
72{
73 var dna = [
73 var dna = [
74 {
74 {
75 id: 0,
75 id: 0,
76 type: TYPES.HEART,
76 type: TYPES.HEART,
77 inputs: [],
77 inputs: [],
78 position: {x:0, y:0}
78 pos: {x:0, y:0}
79 }
79 }
80 ];
80 ];
81
81
82 for(var i = 5; i < startingCells; i++)
82 for(var i = 0; i < startingCells; i++)
83 AddRandomCell(dna)
83 AddRandomCell(dna)
84
84
85 return dna;
85 return dna;
@@ -103,7 +103,10 @@ function AddRandomCell(creature)
103 current -= value;
103 current -= value;
104
104
105 if(current <= 0)
105 if(current <= 0)
106 {
106 selected = key;
107 selected = key;
108 break;
109 }
107 }
110 }
108
111
109 var id = 1;
112 var id = 1;
@@ -162,11 +165,11 @@ var Weights =
162{
165{
163 BRAIN: 1,
166 BRAIN: 1,
164 MOUTH: 2,
167 MOUTH: 2,
165 LOCOMOTOR: 1,
168 LOCOMOTOR: 2,
166 ROTATOR: 1,
169 ROTATOR: 1,
167 EYE: 3,
170 EYE: 2,
168 STOMACH: 3,
171 STOMACH: 3,
169 MEAT: 5
172 MEAT: 4
170}
173}
171
174
172
175
src/gem.js +0 −2
@@ -28,8 +28,6 @@ var gem =
28 28 this.shakeTimer = this.minShakeTimer + (Math.random() * (this.maxShakeTimer - this.minShakeTimer));
29 29 this.currentShakeTimer = 0;
30 30
31 enemySnake.edibles.push(this);
32
33 31 GameAudio.play("bubble");
34 32 },
35 33
@@ -28,8 +28,6 @@ var gem =
28 this.shakeTimer = this.minShakeTimer + (Math.random() * (this.maxShakeTimer - this.minShakeTimer));
28 this.shakeTimer = this.minShakeTimer + (Math.random() * (this.maxShakeTimer - this.minShakeTimer));
29 this.currentShakeTimer = 0;
29 this.currentShakeTimer = 0;
30
30
31 enemySnake.edibles.push(this);
32
33 GameAudio.play("bubble");
31 GameAudio.play("bubble");
34 },
32 },
35
33
src/images.js +1 −1
@@ -3,5 +3,5 @@
3 3 */
4 4
5 5 var s_head = new image("images/head.png", 4, 4);
6 var s_orb = new image("images/orb.png", 4, 4);
6 var s_orb = new image("images/orb.png", 8, 4);
7 7 var s_gem = new image("images/gems.png", 72, 6);
@@ -3,5 +3,5 @@
3 */
3 */
4
4
5var s_head = new image("images/head.png", 4, 4);
5var s_head = new image("images/head.png", 4, 4);
6var s_orb = new image("images/orb.png", 4, 4);
6var s_orb = new image("images/orb.png", 8, 4);
7var s_gem = new image("images/gems.png", 72, 6);
7var s_gem = new image("images/gems.png", 72, 6);
src/main.js +4 −0
@@ -29,6 +29,10 @@ function gameStart()
29 29 GameAudio.init(sounds, soundsFolder);
30 30
31 31 p1 = GameObject.create(gemSpawner, GameWindow.width / 2, GameWindow.height / 2, null, 500);
32 tester = GameObject.create(creature, GameWindow.width / 2, GameWindow.height / 2, null, 500);
33
34 tester.dna = GenerateNewDNA();
35 tester.initCells();
32 36 }
33 37
34 38 function update(deltaTime)
@@ -29,6 +29,10 @@ function gameStart()
29 GameAudio.init(sounds, soundsFolder);
29 GameAudio.init(sounds, soundsFolder);
30
30
31 p1 = GameObject.create(gemSpawner, GameWindow.width / 2, GameWindow.height / 2, null, 500);
31 p1 = GameObject.create(gemSpawner, GameWindow.width / 2, GameWindow.height / 2, null, 500);
32 tester = GameObject.create(creature, GameWindow.width / 2, GameWindow.height / 2, null, 500);
33
34 tester.dna = GenerateNewDNA();
35 tester.initCells();
32}
36}
33
37
34function update(deltaTime)
38function update(deltaTime)