BlueGit

blue-star cellular-evolution

Files

cellular-evolution / src / cell.js

cell.js

energy consumption
timfa Tim · pushed by timfa · 5 years ago
8cfeabd0 History
cell.js 2.2 KiB Download Raw
1/**
2 * Part of a creature!
3 */
4
5var cell =
6{
7 localPos: new vector(0, 0),
8 parentCreature: null,
9 type: -1,
10 cellRef: -1,
11 currentSensorSignal: 0, //only used for sensors, else forever zero
12 lastSum: 0, //used for debug purposes
13 energy: 100,
14 maxEnergy: 100,
15
16 start: function()
17 {
18 this.transform.scale.x = 0;
19 this.transform.scale.y = 0;
20
21 var scaleUpTween = new TWEEN.Tween(this.transform.scale);
22
23 scaleUpTween.to({x: [1.5, 1], y: [1.5, 1]}, Math.random() * 3200);
24
25 scaleUpTween.easing(TWEEN.Easing.Back.Out);
26
27 scaleUpTween.start();
28 },
29
30 activate: function(strength, deltaTime)
31 {
32 this.lastSum = strength;
33
34 switch(this.type)
35 {
36 case TYPES.LOCOMOTOR:
37 var power = this.parentCreature.consumeEnergy(strength * deltaTime)
38 var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);
39
40 this.parentCreature.transform.position = this.parentCreature.transform.position.subtract(rotated.stretch(deltaTime * power));
41 break;
42 case TYPES.ROTATOR:
43 var power = this.parentCreature.consumeEnergy(strength * deltaTime)
44 this.parentCreature.transform.rotation += (strength - 0.5) * deltaTime * 50 * power;
45 break;
46 }
47 },
48
49 draw: function()
50 {
51 this.defaultDraw();
52
53 GameWindow.canvasContext.fillStyle = 'black';
54 GameWindow.canvasContext.fillText(this.lastSum, this.transform.position.x, this.transform.position.y);
55 },
56
57 update: function()
58 {
59 var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);
60 this.transform.position = rotated.stretch(this.transform.scale.x).add(this.parentCreature.transform.position);
61
62 if(this.type == TYPES.EYE)
63 this.transform.rotation = rotated.toAngle();
64
65 if(this.type == TYPES.LOCOMOTOR)
66 this.transform.rotation = rotated.toAngle() - 90;
67
68 if(this.type == TYPES.STOMACH)
69 this.currentSensorSignal = this.energy;
70 },
71
72 setType: function(typeID)
73 {
74 this.renderer.subImage = typeID;
75 this.type = typeID;
76 }
77}