3d-interface/static/js/prototype/Coin.js

75 lines
2.0 KiB
JavaScript
Raw Normal View History

2015-05-11 12:04:37 +02:00
var Coin = function(x,y,z) {
2015-05-11 14:37:41 +02:00
this.ready = false;
this.got = false;
this.init(x,y,z);
2015-05-11 12:04:37 +02:00
}
Coin.prototype.init = function(x,y,z) {
if (Coin.BASIC_MESH !== null) {
this.mesh = Coin.BASIC_MESH.clone();
this.mesh.position.x = x;
this.mesh.position.y = y;
this.mesh.position.z = z;
2015-05-11 14:37:41 +02:00
this.ready = true;
2015-05-11 16:47:45 +02:00
this.mesh.raycastable = true;
2015-05-11 12:04:37 +02:00
} else {
(function(self,x,y,z) {
setTimeout(function() {
self.init(x,y,z);
2015-05-11 16:47:45 +02:00
Coin.nextSound = new Audio(static_path + 'data/music/redcoins/1.mp3');
2015-05-11 12:04:37 +02:00
},1000);
})(this,x,y,z);
}
}
Coin.prototype.addToScene = function(scene) {
scene.add(this.mesh);
}
Coin.prototype.update = function() {
2015-05-11 14:37:41 +02:00
if (this.ready)
(function(self) {
self.update = function() {
self.mesh.rotation.y += 0.1;
}
})(this);
}
Coin.prototype.get = function() {
2015-05-11 14:42:50 +02:00
if (!this.got) {
this.got = true;
this.mesh.visible = false;
Coin.total ++;
2015-05-11 16:47:45 +02:00
Coin.nextSound.play();
2015-05-12 10:30:29 +02:00
if (Coin.total === 9) {
2015-05-12 11:05:52 +02:00
// You got the last coin
setTimeout(function() {Coin.lastSound.play();}, Coin.nextSound.duration + 1000);
2015-05-11 17:07:32 +02:00
} else {
Coin.nextSound = new Audio('/static/data/music/redcoins/' + Coin.total + '.mp3');
2015-05-12 11:05:52 +02:00
Coin.nextSound.preload = "auto";
2015-05-11 17:07:32 +02:00
}
2015-05-11 14:42:50 +02:00
}
2015-05-11 12:04:37 +02:00
}
2015-05-12 11:05:52 +02:00
Coin.lastSound = new Audio('/static/data/music/starappears.mp3');
Coin.lastSound.preload = "auto";
2015-05-11 16:47:45 +02:00
Coin.total = 1;
2015-05-11 12:04:37 +02:00
Coin.BASIC_MESH = null;
Coin._loader = new THREE.OBJLoader();
Coin._loader.load(
static_path + 'data/coin/Coin.obj',
function(object) {
object.traverse(function (mesh) {
if (mesh instanceof THREE.Mesh) {
mesh.scale.set(0.005,0.005,0.005);
mesh.material.color.setHex(0xff0000);
mesh.geometry.computeVertexNormals();
mesh.raycastable = true;
Coin.BASIC_MESH = mesh
}
});
}
);