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

167 lines
4.4 KiB
JavaScript
Raw Normal View History

2015-05-27 16:18:24 +02:00
var Coin = function(x,y,z, callback) {
2015-05-11 14:37:41 +02:00
this.ready = false;
this.got = false;
this.init(x,y,z);
if (callback) {
this.callback = callback;
this.rotating = true;
}
2015-05-11 12:04:37 +02:00
}
2015-05-12 14:39:09 +02:00
var _toto = new Audio();
2015-05-12 14:43:27 +02:00
Coin.extension = _toto.canPlayType("audio/x-vorbis") === "" ? ".ogg" : ".mp3";
2015-05-12 14:39:09 +02:00
2015-06-05 15:47:36 +02:00
Coin.domElement = document.createElement('canvas');
Coin.domElement.style.position = 'absolute';
Coin.domElement.style.cssFloat = 'top-left';
2015-06-05 15:47:36 +02:00
Coin.domElement.style.top = "0px";
Coin.domElement.style.left = "0px";
2015-06-05 15:47:36 +02:00
Coin.image = new Image();
Coin.image.src = '/static/img/redcoin.png';
Coin.initSize = function() {
try {
Coin.domElement.width = container_size.width();
Coin.domElement.height = container_size.height();
} catch (e) {
setTimeout(100, Coin.initSize);
return;
}
}
2015-06-05 15:47:36 +02:00
Coin.update = function() {
var x;
try {
x = container_size.width() * 4.25 / 5;
Coin.domElement.width = container_size.width();
Coin.domElement.height = container_size.height();
} catch (e) {
return;
}
2015-06-05 15:47:36 +02:00
Coin.domElement.width = Coin.domElement.width;
Coin.ctx.drawImage(Coin.image, x + 75,25,30,30);
2015-06-05 15:47:36 +02:00
Coin.ctx.fillStyle = 'red';
Coin.ctx.strokeStyle = 'black';
Coin.ctx.font = "30px Verdana";
2015-06-05 18:04:59 +02:00
Coin.ctx.lineWidth = 5;
2015-06-10 17:38:05 +02:00
Coin.ctx.strokeText(Coin.total - 1 + " / " + Coin.max, x, 50);
Coin.ctx.fillText(Coin.total - 1 + " / " + Coin.max, x, 50);
2015-06-05 15:47:36 +02:00
Coin.ctx.stroke();
2015-06-05 18:04:59 +02:00
Coin.ctx.fill();
2015-06-05 15:47:36 +02:00
}
Coin.image.onload = Coin.update;
Coin.total = 1;
Coin.ctx = Coin.domElement.getContext('2d');
Coin.update();
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);
},1000);
})(this,x,y,z);
}
}
Coin.prototype.addToScene = function(scene) {
scene.add(this.mesh);
}
Coin.prototype.update = function() {
var self = this;
if (this.ready && this.rotating)
this.mesh.rotation.y += 0.1
2015-05-11 14:37:41 +02:00
}
Coin.prototype.get = function() {
2015-05-11 14:42:50 +02:00
if (!this.got) {
this.got = true;
// Call the callback if any
if (this.callback)
this.callback();
2015-05-20 15:20:59 +02:00
if (this.mesh) {
this.mesh.visible = false;
this.mesh.raycastable = false;
2015-05-20 15:20:59 +02:00
}
2015-05-11 14:42:50 +02:00
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
2015-05-12 11:21:06 +02:00
var music = document.getElementById('music');
if (music !== null) {
var wasPlaying = !music.paused;
music.pause();
2015-05-12 11:21:06 +02:00
setTimeout(function() {
Coin.lastSound.play();
setTimeout(function() {
if (wasPlaying) {
music.play();
}
}, Coin.lastSound.duration*1000);
}, Coin.nextSound.duration*1000);
}
2015-05-11 17:07:32 +02:00
} else {
2015-05-12 14:39:09 +02:00
Coin.nextSound = new Audio('/static/data/music/redcoins/' + Coin.total + Coin.extension);
2015-05-12 11:05:52 +02:00
Coin.nextSound.preload = "auto";
2015-05-11 17:07:32 +02:00
}
2015-06-05 15:47:36 +02:00
Coin.update();
2015-05-11 14:42:50 +02:00
}
2015-05-11 12:04:37 +02:00
}
2015-05-12 14:48:10 +02:00
Coin.lastSound = new Audio('/static/data/music/starappears' + Coin.extension);
2015-05-12 11:05:52 +02:00
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.init = function(scale) {
2015-06-10 17:38:05 +02:00
Coin.max = 8;
2015-05-29 10:11:20 +02:00
if (!Coin.initialized) {
Coin.initialized = true;
2015-05-29 10:11:20 +02:00
if (scale === undefined) {
scale = 0.005;
}
2015-05-29 10:11:20 +02:00
Coin._loader.load(
static_path + 'data/coin/Coin.obj',
function(object) {
object.traverse(function (mesh) {
if (mesh instanceof THREE.Mesh) {
mesh.scale.set(scale,scale,scale);
mesh.material.color.setHex(0xff0000);
mesh.geometry.computeVertexNormals();
mesh.raycastable = true;
Coin.BASIC_MESH = mesh
}
});
}
);
Coin.nextSound = new Audio(static_path + 'data/music/redcoins/1' + Coin.extension);
}
}