-
Notifications
You must be signed in to change notification settings - Fork 2
/
microbe.js
187 lines (147 loc) · 4.56 KB
/
microbe.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
var THREE = require('three');
var TWEEN = require('tween.js');
var Simplex = require('perlin-simplex');
var simplex = new Simplex();
var microbeTexture = THREE.ImageUtils.loadTexture( "gray_microbe_128.png" );
function Microbe(position, size, color) {
var self = this;
this.health = 30;
this.position = position.clone();
this.velocity = new THREE.Vector3(0, 0, 0);
this.acceleration = new THREE.Vector3(0, 0, 0);
this.color = color;
this.hungry = true;
this.size = size;
var brightness = 0.5;
var mesh = null;
var adult = false;
var MAX_STEP_SIZE = this.size / 8;
var stepSize = MAX_STEP_SIZE;
var perlinXSeed = Math.random() * 100;
var perlinYSeed = Math.random() * 1000;
// Microbe visual representation
createMesh();
// speed up
this.boost = function() {
// already boosted
if (stepSize >= MAX_STEP_SIZE + MAX_STEP_SIZE / 2) {
return;
}
stepSize += MAX_STEP_SIZE / 2;
setTimeout( function() {
stepSize -= MAX_STEP_SIZE / 2;
}, 400);
};
this.update = function() {
perlinXSeed += 0.01;
perlinYSeed += 0.01;
var previousVelocity = this.velocity.clone();
var x = simplex.noise(perlinXSeed, perlinYSeed);
var y = simplex.noise(perlinYSeed, perlinXSeed);
var movement = new THREE.Vector3(x, y, 0);
clampVectorLength(movement, stepSize);
this.applyForce(movement);
this.velocity.add(this.acceleration);
clampVectorLength(this.velocity, stepSize);
this.position.add(this.velocity);
mesh.position.setX(this.position.x);
mesh.position.setY(this.position.y);
mesh.position.setZ(0);
// Clear acceleration.
this.acceleration.multiplyScalar(0);
faceHeading(previousVelocity, this.velocity);
};
this.eat = function(calories) {
this.health += calories;
this.hungry = false;
setTimeout(function(){
self.hungry = true;
}, 400);
// become adult
if (!adult && this.health > 60) {
adult = true;
this.size *= 1.5;
mesh.scale.x = this.size;
mesh.scale.y = this.size;
}
var tween = new TWEEN.Tween( { brightness: brightness } )
.to( { brightness: 0.8 }, 400 )
.easing( TWEEN.Easing.Sinusoidal.InOut )
.onUpdate( function () {
mesh.material.uniforms.brightness.value = this.brightness;
} )
.repeat( 1 )
.yoyo( true )
.start();
};
// starvation
var starvationInterval = setInterval(function() {
self.starve();
},10 * 1000);
this.starve = function() {
self.health -= 10;
var tween = new TWEEN.Tween( { brightness: brightness } )
.to( { brightness: 0.2 }, 400 )
.easing( TWEEN.Easing.Sinusoidal.InOut )
.onUpdate( function () {
mesh.material.uniforms.brightness.value = this.brightness;
} )
.repeat( 1 )
.yoyo( true )
.start();
};
this.applyForce = function(force) {
this.acceleration.add(force);
clampVectorLength(this.acceleration, stepSize);
};
this.getMesh = function() {
return mesh;
};
this.die = function() {
clearInterval(starvationInterval);
this.color = new THREE.Vector4( 0.0, 0.0, 0.0, 0.0 );
mesh.material.uniforms.color.value = this.color;
};
function createMesh() {
var geometry = new THREE.PlaneGeometry(1.0, 1.0);
var material = new THREE.ShaderMaterial({
uniforms: {
texture: {type: 't', value: microbeTexture},
color: {type: 'v4', value: self.color},
brightness: {type: 'f', value: brightness}
},
vertexShader: document.getElementById('vertShader').text,
fragmentShader: document.getElementById('fragShader').text,
transparent: true
});
mesh = new THREE.Mesh(geometry, material);
mesh.translateX(self.position.x);
mesh.translateY(self.position.y);
mesh.scale.x = self.size;
mesh.scale.y = self.size;
// rotate mesh 90 degrees.
mesh.rotateOnAxis(new THREE.Vector3(0,0,1).normalize(), Math.PI/2);
}
function clampVectorLength(v, maxLength) {
if (v.length() > maxLength) {
v.setLength(maxLength);
}
}
function faceHeading(previousVelocity, velocity) {
// rotate microbe in direction it's heading.
// compute angle between current velocity and new velocity.
if(previousVelocity.length() === 0) {
previousVelocity.setX(1);
}
// angle = arccos( ( aXb ) / |a| * |b|)
var angle = Math.acos((previousVelocity.dot(velocity)) / (previousVelocity.length() * velocity.length()));
// determin angle direction, positive or negative
var angleDirection = (previousVelocity.x * velocity.y) - (previousVelocity.y * velocity.x);
if (angleDirection < 0) {
angle *=-1;
}
// rotate by angle around Z axis.
mesh.rotateOnAxis(new THREE.Vector3(0,0,1).normalize(), angle);
}
}
module.exports = Microbe;