2015-05-27 16:18:24 +02:00
|
|
|
var Coin = function(x,y,z, callback) {
|
2015-07-08 15:09:33 +02:00
|
|
|
this.raycastable = true;
|
|
|
|
this.children = [];
|
2015-05-11 14:37:41 +02:00
|
|
|
this.ready = false;
|
|
|
|
this.got = false;
|
|
|
|
this.init(x,y,z);
|
2015-08-28 14:17:25 +02:00
|
|
|
this.rotating = true;
|
2015-05-28 10:43:58 +02:00
|
|
|
if (callback) {
|
|
|
|
this.callback = callback;
|
|
|
|
}
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-05-11 12:04:37 +02:00
|
|
|
|
2015-10-01 15:24:32 +02:00
|
|
|
function instantToColor(instant) {
|
|
|
|
|
|
|
|
var r = Math.floor(255 * instant);
|
|
|
|
var g = Math.floor(255 * (1-instant));
|
|
|
|
|
|
|
|
return 'rgb(' + r + ',' + g + ',0)';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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-07-22 15:08:57 +02:00
|
|
|
Coin.sounds = [];
|
|
|
|
for (var i = 0; i < 8; i++) {
|
|
|
|
Coin.sounds.push(new Audio('/static/data/music/redcoins/' + (i+1) + Coin.extension));
|
|
|
|
Coin.sounds[Coin.sounds.length-1].preload = "auto";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-05 15:47:36 +02:00
|
|
|
Coin.domElement = document.createElement('canvas');
|
|
|
|
Coin.domElement.style.position = 'absolute';
|
2015-06-05 17:15:59 +02:00
|
|
|
Coin.domElement.style.cssFloat = 'top-left';
|
2015-06-05 15:47:36 +02:00
|
|
|
Coin.domElement.style.top = "0px";
|
2015-06-05 17:15:59 +02:00
|
|
|
Coin.domElement.style.left = "0px";
|
2015-06-05 15:47:36 +02:00
|
|
|
|
2015-08-28 14:17:25 +02:00
|
|
|
Coin.lvl = 0;
|
2015-10-01 15:24:32 +02:00
|
|
|
Coin.blinking = false;
|
|
|
|
Coin.blinkingToRed = true;
|
|
|
|
Coin.colorInstant = 0;
|
2015-08-28 14:17:25 +02:00
|
|
|
|
2015-07-09 11:44:54 +02:00
|
|
|
Coin.setSize = function(width,height) {
|
|
|
|
this.domElement.width = width;
|
|
|
|
this.domElement.height = height;
|
|
|
|
this.update();
|
2015-07-09 17:48:46 +02:00
|
|
|
};
|
2015-07-09 11:44:54 +02:00
|
|
|
|
2015-08-28 14:17:25 +02:00
|
|
|
// Coin.image = new Image();
|
|
|
|
// Coin.image.src = '/static/img/redcoin.png';
|
2015-06-05 15:47:36 +02:00
|
|
|
|
2015-06-05 17:15:59 +02:00
|
|
|
Coin.initSize = function() {
|
|
|
|
try {
|
2015-08-28 21:34:29 +02:00
|
|
|
Coin.domElement.width = containerSize.width();
|
|
|
|
Coin.domElement.height = containerSize.height();
|
2015-06-05 17:15:59 +02:00
|
|
|
} catch (e) {
|
|
|
|
setTimeout(100, Coin.initSize);
|
|
|
|
return;
|
|
|
|
}
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-06-05 17:15:59 +02:00
|
|
|
|
2015-06-05 15:47:36 +02:00
|
|
|
Coin.update = function() {
|
2015-06-05 17:15:59 +02:00
|
|
|
|
|
|
|
var x;
|
|
|
|
try {
|
2015-08-28 21:34:29 +02:00
|
|
|
x = containerSize.width() * 4.75 / 5;
|
2015-10-01 15:24:32 +02:00
|
|
|
|
|
|
|
if (Coin.domElement.width === undefined) {
|
|
|
|
Coin.domElement.width = containerSize.width();
|
|
|
|
Coin.domElement.height = containerSize.height();
|
|
|
|
}
|
2015-06-05 17:15:59 +02:00
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-01 15:24:32 +02:00
|
|
|
if (Coin.blinking) {
|
|
|
|
|
|
|
|
Coin.colorInstant += Coin.blinkingToRed ? 0.025 : -0.025;
|
|
|
|
|
|
|
|
if (Coin.colorInstant < 0 || Coin.colorInstant > 1) {
|
|
|
|
|
|
|
|
Coin.colorInstant = Math.clamp(Coin.colorInstant, 0, 1);
|
|
|
|
Coin.blinkingToRed = !Coin.blinkingToRed;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Coin.colorInstant = 0;
|
|
|
|
Coin.blinkingToRed = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-28 14:17:25 +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
|
|
|
|
2015-08-28 14:17:25 +02:00
|
|
|
// Coin.ctx.fillStyle = 'red';
|
|
|
|
// Coin.ctx.strokeStyle = 'black';
|
2015-06-05 15:47:36 +02:00
|
|
|
|
2015-08-28 14:17:25 +02:00
|
|
|
// Coin.ctx.font = "30px Verdana";
|
|
|
|
// Coin.ctx.lineWidth = 5;
|
|
|
|
// Coin.ctx.strokeText(Coin.total + " / " + Coin.max, x, 50);
|
|
|
|
// Coin.ctx.fillText(Coin.total + " / " + Coin.max, x, 50);
|
2015-06-05 15:47:36 +02:00
|
|
|
|
2015-08-28 14:17:25 +02:00
|
|
|
// Coin.ctx.stroke();
|
|
|
|
// Coin.ctx.fill();
|
2015-06-05 15:47:36 +02:00
|
|
|
|
2015-08-28 14:17:25 +02:00
|
|
|
|
|
|
|
var width = 10;
|
|
|
|
var height = 100;
|
|
|
|
var lvl = Coin.lvl;
|
|
|
|
|
2015-10-01 15:24:32 +02:00
|
|
|
// if (Coin.previousLvl < lvl)
|
|
|
|
// Coin.domElement.width = Coin.domElement.width;
|
|
|
|
|
|
|
|
Coin.ctx.save();
|
|
|
|
|
|
|
|
Coin.ctx.clearRect(x-70, 20, width +71, height);
|
|
|
|
Coin.ctx.fillStyle = instantToColor(Coin.colorInstant);
|
2015-08-28 14:17:25 +02:00
|
|
|
Coin.ctx.fillRect(x,20 + (1-lvl)*height,10,(lvl*height));
|
|
|
|
|
|
|
|
Coin.ctx.beginPath();
|
|
|
|
Coin.ctx.moveTo(x,20);
|
|
|
|
Coin.ctx.lineTo(x,120);
|
|
|
|
Coin.ctx.lineTo(x+width,120);
|
|
|
|
Coin.ctx.lineTo(x+width,20);
|
2015-06-05 15:47:36 +02:00
|
|
|
Coin.ctx.stroke();
|
2015-08-28 14:17:25 +02:00
|
|
|
|
|
|
|
Coin.ctx.fillStyle = 'black';
|
|
|
|
Coin.ctx.font="20px Arial";
|
|
|
|
Coin.ctx.fillText('Score', x - 60, 25);
|
2015-06-05 15:47:36 +02:00
|
|
|
|
2015-10-01 15:24:32 +02:00
|
|
|
Coin.ctx.restore();
|
|
|
|
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-06-05 15:47:36 +02:00
|
|
|
|
2015-10-01 15:24:32 +02:00
|
|
|
setInterval(function() {
|
|
|
|
Coin.update();
|
|
|
|
}, 50);
|
|
|
|
|
2015-08-28 14:17:25 +02:00
|
|
|
Coin.set = function() {
|
|
|
|
var newLvl = Coin.total / Coin.max;
|
|
|
|
Coin.lvl+=0.01*Math.sign(newLvl-Coin.lvl);
|
|
|
|
if (Math.abs(Coin.lvl-newLvl) > 0.005) {
|
2015-10-01 15:24:32 +02:00
|
|
|
Coin.shouldUpdate = true;
|
2015-08-28 14:17:25 +02:00
|
|
|
setTimeout(function() {
|
|
|
|
Coin.set(newLvl);
|
|
|
|
},50);
|
2015-10-01 15:24:32 +02:00
|
|
|
} else {
|
|
|
|
Coin.shouldUpdate = Coin.blinking;
|
2015-08-28 14:17:25 +02:00
|
|
|
}
|
2015-08-28 21:34:29 +02:00
|
|
|
};
|
2015-08-28 14:17:25 +02:00
|
|
|
|
2015-10-01 15:24:32 +02:00
|
|
|
Coin.blink = function(param) {
|
|
|
|
var blinking = param === undefined ? true : !!param;
|
|
|
|
Coin.blinking = blinking;
|
|
|
|
Coin.shouldUpdate = true;
|
|
|
|
};
|
|
|
|
|
2015-08-28 14:17:25 +02:00
|
|
|
// Coin.image.onload = Coin.update;
|
2015-06-05 15:47:36 +02:00
|
|
|
|
|
|
|
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();
|
2015-07-09 11:44:54 +02:00
|
|
|
this.mesh.material = this.mesh.material.clone();
|
2015-05-11 12:04:37 +02:00
|
|
|
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);
|
|
|
|
}
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-05-11 12:04:37 +02:00
|
|
|
|
2015-07-08 15:09:33 +02:00
|
|
|
Coin.prototype.raycast = function(raycaster, intersects) {
|
|
|
|
if (this.mesh !== undefined) {
|
|
|
|
|
|
|
|
var intersectsThis = [];
|
|
|
|
this.mesh.raycast(raycaster, intersectsThis);
|
|
|
|
|
|
|
|
// Add closest object
|
|
|
|
if (intersectsThis[0] !== undefined) {
|
|
|
|
|
|
|
|
intersectsThis[0].object = this;
|
|
|
|
intersects.push(intersectsThis[0]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-05-11 12:04:37 +02:00
|
|
|
Coin.prototype.addToScene = function(scene) {
|
|
|
|
scene.add(this.mesh);
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-05-11 12:04:37 +02:00
|
|
|
|
|
|
|
Coin.prototype.update = function() {
|
2015-05-28 10:43:58 +02:00
|
|
|
var self = this;
|
|
|
|
if (this.ready && this.rotating)
|
2015-07-01 10:14:15 +02:00
|
|
|
this.mesh.rotation.y += 0.1;
|
2015-07-07 10:42:37 +02:00
|
|
|
|
|
|
|
if (this.got) {
|
2015-07-07 10:19:49 +02:00
|
|
|
if (this.mesh.material.opacity > 0.02) {
|
|
|
|
|
|
|
|
// First update
|
|
|
|
this.mesh.rotation.y += 0.3;
|
|
|
|
this.mesh.position.y += 0.05;
|
|
|
|
this.mesh.material.opacity -= 0.05;
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
this.mesh.visible = false;
|
2015-07-09 11:44:54 +02:00
|
|
|
this.raycastable = false;
|
2015-07-07 10:19:49 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
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;
|
2015-05-28 10:43:58 +02:00
|
|
|
|
2015-08-28 17:35:29 +02:00
|
|
|
if (typeof Coin.onCoinGot === 'function') {
|
|
|
|
Coin.onCoinGot(Coin.total + 1);
|
|
|
|
}
|
|
|
|
|
2015-05-28 10:43:58 +02:00
|
|
|
// Call the callback if any
|
|
|
|
if (this.callback)
|
|
|
|
this.callback();
|
|
|
|
|
2015-07-07 10:42:37 +02:00
|
|
|
this.mesh.material.transparent = true;
|
|
|
|
this.mesh.material.opacity = 1;
|
2015-07-07 10:19:49 +02:00
|
|
|
|
2015-08-28 14:17:25 +02:00
|
|
|
Coin.sounds[Coin.total ++].play();
|
|
|
|
if (Coin.total === 8) {
|
2015-07-29 11:04:38 +02:00
|
|
|
if (typeof Coin.onLastCoin === 'function') {
|
|
|
|
Coin.onLastCoin();
|
|
|
|
}
|
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');
|
2015-06-02 14:14:32 +02:00
|
|
|
if (music !== null) {
|
|
|
|
var wasPlaying = !music.paused;
|
|
|
|
music.pause();
|
2015-05-12 11:21:06 +02:00
|
|
|
setTimeout(function() {
|
2015-06-02 14:14:32 +02:00
|
|
|
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
|
|
|
}
|
2015-06-05 15:47:36 +02:00
|
|
|
|
2015-08-28 14:17:25 +02:00
|
|
|
Coin.set();
|
2015-05-11 14:42:50 +02:00
|
|
|
}
|
2015-07-01 10:14:15 +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-08-28 14:17:25 +02:00
|
|
|
Coin.total = 0;
|
2015-05-11 12:04:37 +02:00
|
|
|
Coin.BASIC_MESH = null;
|
|
|
|
|
|
|
|
Coin._loader = new THREE.OBJLoader();
|
2015-05-28 10:43:58 +02:00
|
|
|
|
|
|
|
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-28 10:43:58 +02:00
|
|
|
|
2015-05-29 10:11:20 +02:00
|
|
|
if (scale === undefined) {
|
|
|
|
scale = 0.005;
|
2015-05-28 10:43:58 +02:00
|
|
|
}
|
2015-05-29 10:11:20 +02:00
|
|
|
|
|
|
|
Coin._loader.load(
|
2015-06-25 16:51:47 +02:00
|
|
|
'/static/data/coin/Coin.obj',
|
2015-05-29 10:11:20 +02:00
|
|
|
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;
|
2015-07-01 10:14:15 +02:00
|
|
|
Coin.BASIC_MESH = mesh;
|
2015-05-29 10:11:20 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2015-06-25 16:51:47 +02:00
|
|
|
Coin.nextSound = new Audio('/static/data/music/redcoins/1' + Coin.extension);
|
2015-05-29 10:11:20 +02:00
|
|
|
}
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|