BlueGit

blue-star cellular-evolution

Files

cellular-evolution / src / creature.js

creature.js

creature.js 2.6 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_orb, 500);
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]].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 = this.getCellInputSum(this.cellInstances[i]);
48 }
49 }
50 }
51
52 //this.transform.rotation += deltaTime * 60;
53 //this.transform.position = this.transform.position.add(this.transform.forward().stretch(deltaTime * 250))
54 },
55
56 getCellInputSum: function(cellInstance, tracer = [])
57 {
58 var cellId = this.dna[this.cellInstances[i].cellRef].id;
59
60 if(tracer.indexOf(cellId) >= 0) //is my id already in the tracer?
61 return 0;
62
63 tracer.push(cellId)
64
65 sum = cellInstance.currentSensorSignal;
66
67 if(this.dna[cellInstance.cellRef].inputs.length > 0)
68 {
69 for(var i = 0; i < this.dna[cellInstance.cellRef].inputs.length; i++)
70 {
71 sum += this.getCellInputSum(
72 this.findCellByCellInstanceId(this.dna[cellInstance.cellRef].inputs[i].id)
73 );
74 }
75 }
76
77 return sum;
78 },
79
80 findCellByCellInstanceId: function(id)
81 {
82 for(var i = 0; i < this.cellInstances.length; i++)
83 {
84 if(this.dna[this.cellInstances[i].cellRef].id == id)
85 return this.cellInstances[i];
86 }
87 },
88
89 onDestroy: function()
90 {
91
92 },
93
94 onCollision: function(other)
95 {
96
97 }
98}