BlueGit

blue-star cellular-evolution

Files

cellular-evolution / src / creature.js

creature.js

creature.js 3.7 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 draw: function()
61 {
62 for(var i = 0; i < this.cellInstances.length; i++)
63 {
64 if(this.dna[this.cellInstances[i].cellRef].inputs.length > 0)
65 {
66 for(var c = 0; c < this.dna[this.cellInstances[i].cellRef].inputs.length; c++)
67 {
68 var pos = this.findCellInstanceByCellId(this.dna[this.cellInstances[i].cellRef].inputs[c].id).transform.position;
69
70 GameWindow.canvasContext.strokeStyle = 'red';
71 GameWindow.canvasContext.beginPath();
72 GameWindow.canvasContext.moveTo(this.cellInstances[i].transform.position.x, this.cellInstances[i].transform.position.y);
73 GameWindow.canvasContext.lineTo(pos.x, pos.y);
74 GameWindow.canvasContext.stroke();
75 }
76 }
77 }
78 },
79
80 getCellInputSum: function(cellInstance, tracer = [])
81 {
82 var cellId = this.dna[cellInstance.cellRef].id;
83
84 if(tracer.indexOf(cellId) >= 0) //is my id already in the tracer?
85 return 0;
86
87 tracer.push(cellId)
88
89 sum = cellInstance.currentSensorSignal;
90
91 if(this.dna[cellInstance.cellRef].inputs.length > 0)
92 {
93 for(var i = 0; i < this.dna[cellInstance.cellRef].inputs.length; i++)
94 {
95 sum += sigmoid(this.getCellInputSum(this.findCellInstanceByCellId(this.dna[cellInstance.cellRef].inputs[i].id), tracer) * this.dna[cellInstance.cellRef].inputs[i].strength);
96 }
97 }
98
99 cellInstance.lastSum = sum;
100
101 return sum;
102 },
103
104 findCellInstanceByCellId: function(id)
105 {
106 for(var i = 0; i < this.cellInstances.length; i++)
107 {
108 if(this.dna[this.cellInstances[i].cellRef].id == id)
109 return this.cellInstances[i];
110 }
111 },
112
113 onDestroy: function()
114 {
115
116 },
117
118 onCollision: function(other)
119 {
120
121 }
122}