| 1 | /** |
| 2 | * It's a thing! |
| 3 | */ |
| 4 | |
| 5 | var 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 | this.cellInstances[i].lastSum = sum; |
| 49 | |
| 50 | //TODO: Act on the sum we've received (ALWAYS between 0 and 1, but VERY rarely exactly 0 or 1) |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | //this.transform.rotation += deltaTime * 60; |
| 56 | //this.transform.position = this.transform.position.add(this.transform.forward().stretch(deltaTime * 250)) |
| 57 | }, |
| 58 | |
| 59 | draw: function() |
| 60 | { |
| 61 | for(var i = 0; i < this.cellInstances.length; i++) |
| 62 | { |
| 63 | if(this.dna[this.cellInstances[i].cellRef].inputs.length > 0) |
| 64 | { |
| 65 | for(var c = 0; c < this.dna[this.cellInstances[i].cellRef].inputs.length; c++) |
| 66 | { |
| 67 | var pos = this.findCellInstanceByCellId(this.dna[this.cellInstances[i].cellRef].inputs[c].id).transform.position; |
| 68 | |
| 69 | GameWindow.canvasContext.strokeStyle = 'red'; |
| 70 | GameWindow.canvasContext.beginPath(); |
| 71 | GameWindow.canvasContext.moveTo(this.cellInstances[i].transform.position.x, this.cellInstances[i].transform.position.y); |
| 72 | GameWindow.canvasContext.lineTo(pos.x, pos.y); |
| 73 | GameWindow.canvasContext.stroke(); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | }, |
| 78 | |
| 79 | getCellInputSum: function(cellInstance, tracer = []) |
| 80 | { |
| 81 | var cellId = this.dna[cellInstance.cellRef].id; |
| 82 | |
| 83 | if(tracer.indexOf(cellId) >= 0) //is my id already in the tracer? |
| 84 | return 0; |
| 85 | |
| 86 | tracer.push(cellId) |
| 87 | |
| 88 | sum = cellInstance.currentSensorSignal; |
| 89 | |
| 90 | if(this.dna[cellInstance.cellRef].inputs.length > 0) |
| 91 | { |
| 92 | for(var i = 0; i < this.dna[cellInstance.cellRef].inputs.length; i++) |
| 93 | { |
| 94 | sum += sigmoid(this.getCellInputSum(this.findCellInstanceByCellId(this.dna[cellInstance.cellRef].inputs[i].id), tracer) * this.dna[cellInstance.cellRef].inputs[i].strength); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | cellInstance.lastSum = sum; |
| 99 | |
| 100 | return sum; |
| 101 | }, |
| 102 | |
| 103 | findCellInstanceByCellId: function(id) |
| 104 | { |
| 105 | for(var i = 0; i < this.cellInstances.length; i++) |
| 106 | { |
| 107 | if(this.dna[this.cellInstances[i].cellRef].id == id) |
| 108 | return this.cellInstances[i]; |
| 109 | } |
| 110 | }, |
| 111 | |
| 112 | onDestroy: function() |
| 113 | { |
| 114 | |
| 115 | }, |
| 116 | |
| 117 | onCollision: function(other) |
| 118 | { |
| 119 | |
| 120 | } |
| 121 | } |