BlueGit

blue-star cellular-evolution

Files

cellular-evolution / src / cell.js

cell.js

cell.js 2.5 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 debugShouldDraw: false,
16
17 start: function()
18 {
19 this.transform.scale.x = 0;
20 this.transform.scale.y = 0;
21
22 var scaleUpTween = new TWEEN.Tween(this.transform.scale);
23
24 scaleUpTween.to({x: [1.5, 1], y: [1.5, 1]}, Math.random() * 3200);
25
26 scaleUpTween.easing(TWEEN.Easing.Back.Out);
27
28 scaleUpTween.start();
29 },
30
31 activate: function(strength, deltaTime)
32 {
33 this.lastSum = strength;
34
35 switch(this.type)
36 {
37 case TYPES.LOCOMOTOR:
38 var actualStrength = Math.max(0, strength)
39 if(actualStrength > 0)
40 {
41 var power = this.parentCreature.consumeEnergy(actualStrength * deltaTime)
42 var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);
43
44 this.parentCreature.transform.position = this.parentCreature.transform.position.subtract(rotated.stretch(deltaTime * power));
45 }
46 break;
47 case TYPES.ROTATOR:
48 var power = this.parentCreature.consumeEnergy(Math.abs(strength) * deltaTime)
49 this.parentCreature.transform.rotation += (strength) * deltaTime * 100 * power;
50 break;
51 }
52 },
53
54 draw: function()
55 {
56 this.defaultDraw();
57
58 if(this.debugShouldDraw)
59 {
60 GameWindow.canvasContext.fillStyle = 'black';
61 GameWindow.canvasContext.fillText(this.lastSum, this.transform.position.x, this.transform.position.y);
62 }
63 },
64
65 update: function()
66 {
67 var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);
68 this.transform.position = rotated.stretch(this.transform.scale.x).add(this.parentCreature.transform.position);
69
70 if(this.type == TYPES.EYE)
71 this.transform.rotation = rotated.toAngle();
72
73 if(this.type == TYPES.LOCOMOTOR)
74 this.transform.rotation = rotated.toAngle() - 90;
75
76 if(this.type == TYPES.STOMACH)
77 this.currentSensorSignal = (this.energy / this.maxEnergy);
78 },
79
80 setType: function(typeID)
81 {
82 this.renderer.subImage = typeID;
83 this.type = typeID;
84 }
85}