BlueGit

blue-star cellular-evolution

Files

cellular-evolution / src / creature.js

creature.js

0e55f099 History
creature.js 5.4 KiB Download Raw
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_cell, 400);
20
21 newCell.setType(this.dna[i].type);
22 newCell.parentCreature = this;
23 newCell.localPos = new vector(this.dna[i].pos.x * 32, this.dna[i].pos.y * 32)
24
25 newCell.cellRef = i;
26
27 this.cellInstances.push(newCell);
28 }
29 },
30
31 update: function(deltaTime)
32 {
33 for(var i = this.cellInstances.length -1; i >= 0; i--) //reverse loop for deletion of references to destroyed cells
34 {
35 if(this.cellInstances[i].destroyed)
36 this.cellInstances.splice(i, 1);
37 }
38
39 for(var i = 0; i < this.cellInstances.length; i++) //loop through instances instead of dna so that destroyed cells are immediately ignored
40 {
41 var type = this.dna[this.cellInstances[i].cellRef].type;
42
43 if(type == TYPES.LOCOMOTOR || type == TYPES.ROTATOR)
44 {
45 if(this.dna[this.cellInstances[i].cellRef].inputs.length > 0)
46 {
47 var sum = sigmoid(this.getCellInputSum(this.cellInstances[i]));
48
49 //TODO: Act on the sum we've received (ALWAYS between 0 and 1, but VERY rarely exactly 0 or 1)
50
51 this.cellInstances[i].activate(sum, deltaTime);
52 }
53 }
54 }
55
56 //this.transform.rotation += deltaTime * 60;
57 //this.transform.position = this.transform.position.add(this.transform.forward().stretch(deltaTime * 250))
58 },
59
60 consumeEnergy: function(energyNeeded) //returns a number between 0 and 1. 0 meaning no energy was consumed (because we ran out), 1 means all of the energy was consumed.
61 {
62 var stomachRef = -1;
63 var amt = 0;
64
65 for(var i = 0; i < this.cellInstances.length; i++)
66 {
67 if(this.cellInstances[i].type == TYPES.STOMACH)
68 {
69 if(this.cellInstances[i].energy > amt)
70 {
71 stomachRef = i;
72 amt = this.cellInstances[i].energy;
73 }
74 }
75 }
76
77 if(stomachRef == -1)
78 return 0;
79
80 var efficiency = Math.min(1, this.cellInstances[stomachRef].energy / energyNeeded)
81
82 this.cellInstances[stomachRef].energy = Math.max(0, this.cellInstances[stomachRef].energy - energyNeeded)
83
84 return efficiency;
85 },
86
87 draw: function()
88 {
89 for(var i = 0; i < this.cellInstances.length; i++)
90 {
91 if(this.dna[this.cellInstances[i].cellRef].inputs.length > 0)
92 {
93 for(var c = 0; c < this.dna[this.cellInstances[i].cellRef].inputs.length; c++)
94 {
95 var pos = this.findCellInstanceByCellId(this.dna[this.cellInstances[i].cellRef].inputs[c].id).transform.position;
96
97 GameWindow.canvasContext.strokeStyle = 'red';
98 GameWindow.canvasContext.beginPath();
99 GameWindow.canvasContext.moveTo(this.cellInstances[i].transform.position.x, this.cellInstances[i].transform.position.y);
100 GameWindow.canvasContext.lineTo(pos.x, pos.y);
101 GameWindow.canvasContext.stroke();
102 }
103 }
104 }
105 },
106
107 gainEnergy: function(amount)
108 {
109 var stomachRef = -1;
110 var amt = 10000;
111
112 for(var i = 0; i < this.cellInstances.length; i++)
113 {
114 if(this.cellInstances[i].type == TYPES.STOMACH)
115 {
116 if(this.cellInstances[i].energy < amt)
117 {
118 stomachRef = i;
119 amt = this.cellInstances[i].energy;
120 }
121 }
122 }
123
124 if(stomachRef == -1)
125 return 0;
126
127 this.cellInstances[stomachRef].energy = Math.min(cell.maxEnergy, this.cellInstances[stomachRef].energy + amount)
128 },
129
130 getCellInputSum: function(cellInstance, tracer = [])
131 {
132 var cellId = this.dna[cellInstance.cellRef].id;
133
134 if(tracer.indexOf(cellId) >= 0) //is my id already in the tracer?
135 return 0;
136
137 tracer.push(cellId)
138
139 var currentSum = cellInstance.currentSensorSignal;
140 cellInstance.debugShouldDraw = true;
141
142 if(this.dna[cellInstance.cellRef].inputs.length > 0)
143 {
144 for(var i = 0; i < this.dna[cellInstance.cellRef].inputs.length; i++)
145 {
146 var synapse = this.dna[cellInstance.cellRef].inputs[i];
147
148 var cInstance = this.findCellInstanceByCellId(synapse.id);
149 var inputSum = this.getCellInputSum(cInstance, tracer);
150
151 currentSum += sigmoid(inputSum * synapse.strength);
152 }
153 }
154
155 cellInstance.lastSum = currentSum;
156
157 return currentSum + this.dna[cellInstance.cellRef].bias;
158 },
159
160 findCellInstanceByCellId: function(id)
161 {
162 for(var i = 0; i < this.cellInstances.length; i++)
163 {
164 if(this.dna[this.cellInstances[i].cellRef].id == id)
165 return this.cellInstances[i];
166 }
167 },
168
169 onDestroy: function()
170 {
171
172 },
173
174 onCollision: function(other)
175 {
176
177 }
178}