BlueGit

blue-star cellular-evolution

Files

cellular-evolution / js / game.js

game.js

buildo
timfa Tim · pushed by timfa · 5 years ago
08072c5d History
game.js 9.5 KiB Download Raw
1/**
2 * 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.
4 *
5 * In essence, it is a list of cell types with potential Synapses. Any 'sensor' cell can have one or more connections to 'activatable' cells such as locomotion cells or mouths.
6 *
7 * A DNA string should be able to be parsed to a simple string (hence the name), meaning the factory must be able to interpret relations from a data structure that doesn't support references.
8 *
9 * definitions:
10 * - cell: one out of many objects of uniform size that make up a creature
11 * - sensor: a cell that is able to detect things
12 * - activator: a cell that can respond to the signal of a Synapse, using energy from Stomach cells. When no energy is available, the Activators are unable to do anything and as such the creature will essentially be 'dead'
13 * - Synapse: a connection between a sensor and an activator
14 * - braincell: a cell that acts as a hybrid activator/sensor. It receives inputs like an activator would, but instead of doing something on its own, it has outbound Synapse connections of its own to one or more sensors (or other brain cells)
15 *
16 * Types of mutations that can occur:
17 * - add or remove connections between sensors and activators (unlikely)
18 * - add or remove cells of any type (very unlikely)
19 * - change the strength of Synapses (likely)
20 *
21 * Types of cells:
22 * ACTIVATORS
23 * - Mouth: can Bite, which will eat whatever is in front of it, be that a cell or a food pellet
24 * - Locomotor: When activated, pushes the creature in the direction of the creature's center, relative from the locomotor cell
25 * - Rotator: Applies torque when activated. 0.5 is neural, 0 is full left, 1 is full right
26 *
27 * SENSORS
28 * - Eye: Activates whenever there is a cell or food pellet near it within a certain range and in a certain direction
29 *
30 * OTHER
31 * - Stomach: Can contain energy. Each cell/pellet eaten increases energy in any available Stomach cells. All Activators require an amount of energy from any of the available Stomach cells to function.
32 * - Meat: A cell which does nothing except connect other cells. (and potentially function as armor)
33 * - Heart: Does nothing, but when the heart is destroyed the creature will die. Creature cannot have more than one Heart, which is always at 0,0
34 *
35 * A creature will multiply when all its Stomach cells are completely full, draining each of them by 50%. This spawns an Egg which will hatch into a copy of the parent creature with a slight mutation applied as soon as there is space available (read: the parent creature has moved suffficiently far away).
36 *
37 * Examples:
38 * One cell:
39 * {
40 * ID : number. Each new cell will take the first available number so that numbers from deleted cells are re-used
41 * Type: number, corresponding to the cell type.
42 * In: [array of ID's to inbound Synapses and strength, e.g.: {ID:5,Strength:1.2},{ID:8,Strength:1.2}]
43 * Pos: {X, Y} vector, where 0,0 is the center of a creature which will ALWAYS be a Heart. (note that center of mass, as in the average position of all cells, will be used to determine direction for locomotors and such, not 0,0)
44 * }
45 *
46 * The output of a Cell is determined as follows:
47 * OUT = Sigmoid ( IN1Val*IN1Weight + IN2Val*IN2Weight + IN3Val*IN3Weight + ... )
48 * Sigmoid will accept numbers from negative infinity up until positive infinity, and as such the weights of all Synapses need not be clamped. Sigmoid will always output a value between zero and one.
49 *
50 * Functionally, outputs will be determined from the Activators. Activators request their inputs to calculate, which in turn request their inputs to calculate, until all values have been determined.
51 *
52 * Note that any cells which would be disconnected from the heart should be automatically removed from the DNA string
53 *
54 * --
55 *
56 * This file will handle:
57 * - Generating new DNA strings
58 * - Mutating existing DNA strings
59 */
60
61/**
62 * Potential issues:
63 *
64 * Infinite circular connections in braincells.
65 * Solutions:
66 * - Don't allow braincells to connect to braincells (would fix, but is undesirable as it limits brain complexity)
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.
68 */
69
70 function sigmoid(z)
71 {
72 return 1 / (1 + Math.exp(-z));
73 }
74
75/**
76 * Snake Food
77 */
78
79var gem =
80{
81 start: function()
82 {
83 this.renderer.subImage = Math.floor(Math.random() * this.renderer.image.subImages);
84
85 this.transform.scale.x = 0;
86 this.transform.scale.y = 0;
87
88 var scaleUpTween = new TWEEN.Tween(this.transform.scale);
89
90 scaleUpTween.to({x: [1.5, 1], y: [1.5, 1]}, 800);
91
92 scaleUpTween.easing(TWEEN.Easing.Back.Out);
93
94 scaleUpTween.start();
95
96 this.shakeLength = 1000;
97 this.shakes = 4;
98 this.shakeAngle = 45;
99
100 this.minShakeTimer = 5;
101 this.maxShakeTimer = 20;
102 this.shakeTimer = this.minShakeTimer + (Math.random() * (this.maxShakeTimer - this.minShakeTimer));
103 this.currentShakeTimer = 0;
104
105 enemySnake.edibles.push(this);
106
107 GameAudio.play("bubble");
108 },
109
110 update: function(deltaTime)
111 {
112 this.currentShakeTimer += deltaTime;
113
114 if(this.currentShakeTimer >= this.shakeTimer)
115 {
116 this.shake();
117
118 this.shakeTimer = this.minShakeTimer + (Math.random() * (this.maxShakeTimer - this.minShakeTimer));
119
120 this.currentShakeTimer = 0;
121 }
122 },
123
124 onDestroy: function()
125 {
126 gemSpawner.currentActive--;
127
128 enemySnake.edibles.remove(this);
129 },
130
131 onCollision: function(other)
132 {
133 if (other.name == "snake" && !this.destroyed)
134 {
135 other.grow(1, -1);
136 this.destroy();
137 }
138 },
139
140 shake: function(callback, context)
141 {
142 var singleShakeTime = this.shakes / this.shakeLength;
143
144 var shakeTween = new TWEEN.Tween(this.transform);
145
146 var angles = [];
147
148 var offset = Math.round(Math.random() * 5);
149
150 for (var i = 0; i < this.shakes; i++)
151 {
152 var intensiy = 1 - (i / this.shakes);
153
154 // Direction of the shake (is 'i' odd?)
155 var dir = !((i + offset) % 2)? -1 : 1;
156
157 var angleRad = this.shakeAngle;
158
159 var angle = angleRad * dir * intensiy;
160
161 angles.push(angle);
162 }
163
164 angles.push(0);
165
166 shakeTween.to({rotation: angles}, this.shakeLength);
167
168 shakeTween.easing(TWEEN.Easing.Quadratic.InOut);
169
170 if (callback)
171 {
172 shakeTween.onComplete.addOnce(callback, context);
173 }
174
175 shakeTween.start();
176 }
177}
178
179/**
180 * Gem Spawner spawns gems in the world
181 */
182var gemSpawner =
183{
184 start: function()
185 {
186 gemSpawner.currentActive = 0;
187
188 this.maxGems = 7;
189
190 for (var i = 0; i < this.maxGems; i++)
191 {
192 this.spawnGem(i * 200, i == this.maxGems - 1);
193 }
194
195 this.spawnInUpdate = false;
196
197 this.allGems = [];
198 },
199
200 spawnGem: function(delay, enableSpawningAfter)
201 {
202 delay = delay || 0;
203
204 if (gemSpawner.currentActive < this.maxGems)
205 {
206 gemSpawner.currentActive++;
207
208 timingUtil.delay(delay, function()
209 {
210 var x = 100 + Math.random() * (GameWindow.width - 200);
211 var y = 100 + Math.random() * (GameWindow.height - 200);
212
213 this.allGems.push(GameObject.create(gem, x, y, s_gem, 0));
214
215 this.allGems = this.allGems.filter(function(item)
216 {
217 return !item.destroyed;
218 });
219
220 if (enableSpawningAfter)
221 {
222 this.spawnInUpdate = true;
223 }
224 }, this);
225 }
226 },
227
228 onDestroy: function()
229 {
230 for (var i = 0; i < this.allGems.length; i++)
231 {
232 this.allGems[i].destroy();
233 }
234 },
235
236 update: function(deltaTime)
237 {
238 if (this.spawnInUpdate)
239 {
240 this.spawnGem(500 + (Math.random() * 500));
241 }
242 }
243}
244
245/***
246 * All images used in the game are initialized here
247 */
248
249var s_head = new image("images/head.png", 4, 4);
250var s_orb = new image("images/orb.png", 4, 4);
251var s_gem = new image("images/gems.png", 72, 6);
252
253/**
254 * Main script
255 */
256
257window.onload = init;
258
259function DoInheritance()
260{
261 //resultclass = GameObject.inherit(baseclass, childclass);
262}
263
264function init()
265{
266 DoInheritance();
267 //Game.initialize(xpos, ypos, width, height, fps, callback)
268 // -2 to account for borders
269 var width = window.innerWidth - 2;
270 var height = window.innerHeight - 2;
271
272 Game.initialize(0, 0, width, height, gameStart);
273}
274
275function gameStart()
276{
277 GameWindow.backgroundColor = {r:100, g:149, b:237};
278
279 Game.setUpdateFunction(update); // Let the game engine know we want to link our 'update()' to the game's update step.
280
281 GameAudio.init(sounds, soundsFolder);
282
283 p1 = GameObject.create(gemSpawner, GameWindow.width / 2, GameWindow.height / 2, null, 500);
284}
285
286function update(deltaTime)
287{
288
289}
290
291/**
292 * All sounds in the game
293 */
294
295var soundsFolder = "sounds";
296var sounds =
297[
298 {
299 name: "bubble",
300 volume: 1
301 },
302 {
303 name: "glass",
304 volume: 0.9
305 },
306 {
307 name: "tap",
308 volume: 1
309 },
310 {
311 name: "bite",
312 volume: 1
313 },
314 {
315 name: "pop",
316 volume: 0.1
317 }
318
319]; //List of all sound files as strings (do not include file extension)
320