/** * It's a thing! */ var creature = { dna: {}, cellInstances: [], start: function() { }, initCells() { for(var i = 0; i < this.dna.length; i++) { 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); newCell.setType(this.dna[i].type); newCell.parentCreature = this; newCell.localPos = new vector(this.dna[i].pos.x * 32, this.dna[i].pos.y * 32) newCell.cellRef = i; this.cellInstances.push(newCell); } }, update: function(deltaTime) { for(var i = this.cellInstances.length -1; i >= 0; i--) //reverse loop for deletion of references to destroyed cells { if(this.cellInstances[i].destroyed) this.cellInstances.splice(i, 1); } for(var i = 0; i < this.cellInstances.length; i++) //loop through instances instead of dna so that destroyed cells are immediately ignored { var type = this.dna[this.cellInstances[i]].type; if(type == TYPES.LOCOMOTOR || type == TYPES.ROTATOR) { if(this.dna[this.cellInstances[i].cellRef].inputs.length > 0) { var sum = this.getCellInputSum(this.cellInstances[i]); } } } //this.transform.rotation += deltaTime * 60; //this.transform.position = this.transform.position.add(this.transform.forward().stretch(deltaTime * 250)) }, getCellInputSum: function(cellInstance, tracer = []) { var cellId = this.dna[this.cellInstances[i].cellRef].id; if(tracer.indexOf(cellId) >= 0) //is my id already in the tracer? return 0; tracer.push(cellId) sum = cellInstance.currentSensorSignal; if(this.dna[cellInstance.cellRef].inputs.length > 0) { for(var i = 0; i < this.dna[cellInstance.cellRef].inputs.length; i++) { sum += this.getCellInputSum( this.findCellByCellInstanceId(this.dna[cellInstance.cellRef].inputs[i].id) ); } } return sum; }, findCellByCellInstanceId: function(id) { for(var i = 0; i < this.cellInstances.length; i++) { if(this.dna[this.cellInstances[i].cellRef].id == id) return this.cellInstances[i]; } }, onDestroy: function() { }, onCollision: function(other) { } }