BlueGit

blue-star cellular-evolution

Files

cellular-evolution / src / gem.js

gem.js

make creature setup
timfa Tim · pushed by timfa · 5 years ago
8ad077e6 History
gem.js 2.3 KiB Download Raw
1/**
2 * Snake Food
3 */
4
5var gem =
6{
7 start: function()
8 {
9 this.renderer.subImage = Math.floor(Math.random() * this.renderer.image.subImages);
10
11 this.transform.scale.x = 0;
12 this.transform.scale.y = 0;
13
14 var scaleUpTween = new TWEEN.Tween(this.transform.scale);
15
16 scaleUpTween.to({x: [1.5, 1], y: [1.5, 1]}, 800);
17
18 scaleUpTween.easing(TWEEN.Easing.Back.Out);
19
20 scaleUpTween.start();
21
22 this.shakeLength = 1000;
23 this.shakes = 4;
24 this.shakeAngle = 45;
25
26 this.minShakeTimer = 5;
27 this.maxShakeTimer = 20;
28 this.shakeTimer = this.minShakeTimer + (Math.random() * (this.maxShakeTimer - this.minShakeTimer));
29 this.currentShakeTimer = 0;
30
31 GameAudio.play("bubble");
32 },
33
34 update: function(deltaTime)
35 {
36 this.currentShakeTimer += deltaTime;
37
38 if(this.currentShakeTimer >= this.shakeTimer)
39 {
40 this.shake();
41
42 this.shakeTimer = this.minShakeTimer + (Math.random() * (this.maxShakeTimer - this.minShakeTimer));
43
44 this.currentShakeTimer = 0;
45 }
46 },
47
48 onDestroy: function()
49 {
50 gemSpawner.currentActive--;
51
52 enemySnake.edibles.remove(this);
53 },
54
55 onCollision: function(other)
56 {
57 if (other.name == "snake" && !this.destroyed)
58 {
59 other.grow(1, -1);
60 this.destroy();
61 }
62 },
63
64 shake: function(callback, context)
65 {
66 var singleShakeTime = this.shakes / this.shakeLength;
67
68 var shakeTween = new TWEEN.Tween(this.transform);
69
70 var angles = [];
71
72 var offset = Math.round(Math.random() * 5);
73
74 for (var i = 0; i < this.shakes; i++)
75 {
76 var intensiy = 1 - (i / this.shakes);
77
78 // Direction of the shake (is 'i' odd?)
79 var dir = !((i + offset) % 2)? -1 : 1;
80
81 var angleRad = this.shakeAngle;
82
83 var angle = angleRad * dir * intensiy;
84
85 angles.push(angle);
86 }
87
88 angles.push(0);
89
90 shakeTween.to({rotation: angles}, this.shakeLength);
91
92 shakeTween.easing(TWEEN.Easing.Quadratic.InOut);
93
94 if (callback)
95 {
96 shakeTween.onComplete.addOnce(callback, context);
97 }
98
99 shakeTween.start();
100 }
101}