BlueGit

blue-star cellular-evolution

Files

cellular-evolution / src / cell.js

cell.js

change energy gain
timfa Tim · pushed by timfa · 5 years ago
c8ac507a History
cell.js 2.8 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: 1,
14 maxEnergy: 1,
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 onCollision: function(other)
55 {
56 if (this.type == TYPES.MOUTH && !this.destroyed)
57 {
58 if(other.parentCreature == this.parentCreature)
59 return;
60
61 this.parentCreature.gainEnergy(this.maxEnergy / 3);
62 other.destroy();
63 }
64 },
65
66 draw: function()
67 {
68 this.defaultDraw();
69
70 if(this.debugShouldDraw)
71 {
72 GameWindow.canvasContext.fillStyle = 'black';
73 GameWindow.canvasContext.fillText(this.lastSum, this.transform.position.x, this.transform.position.y);
74 }
75 },
76
77 update: function()
78 {
79 var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);
80 this.transform.position = rotated.stretch(this.transform.scale.x).add(this.parentCreature.transform.position);
81
82 if(this.type == TYPES.EYE)
83 this.transform.rotation = rotated.toAngle();
84
85 if(this.type == TYPES.LOCOMOTOR)
86 this.transform.rotation = rotated.toAngle() - 90;
87
88 if(this.type == TYPES.STOMACH)
89 this.currentSensorSignal = (this.energy / this.maxEnergy);
90 },
91
92 setType: function(typeID)
93 {
94 this.renderer.subImage = typeID;
95 this.type = typeID;
96 }
97}