D2H
P
Hjälte
3 460 inlägg
2011-01-15 19:06
Visa spoiler
<script type="text/javascript">
var STARS = 200;
var FAR = 4000;
var SAFE = 50;
var PHASELEN = 15000;
var NPHASES = 6;
var speed;
var score;
var phase;
var toNextPhase;
var nextFrame;
var nextP;
var hiscore;
var lives;
var collision;
var interval;
var tmp;
var fullscreen=false;
var container;
var camera, scene, renderer;
var particles, particle, count = 0;
var mouseX = 0, mouseY = 0;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
var c1,c2;
var bdy = document.getElementById("body");
container = document.createElement( 'div' );
document.body.appendChild( container );
camera = new THREE.Camera( 80, window.innerWidth / window.innerHeight, 1, FAR - 250 );
camera.position.z = FAR;
scene = new THREE.Scene();
init();
reset();
titleScreen();
onWindowResize();
//start();// iphone
function show(id) {
var o = document.getElementById(id);
o.style.display='';
}
function hide(id) {
var o = document.getElementById(id);
o.style.display='none';
}
function titleScreen() {
hiscore = localStorage.getItem("hiscore");
if ( hiscore == 0 || hiscore == undefined || hiscore == null ) hiscore = 0;
var o = document.getElementById("hiscore");
o[removed] = "hi-score<br>"+hiscore;
show("hiscore");
show("title");
show("omiod");
show("like");
interval = setInterval(demo, 1000 / 60);
}
function start() {
hide("start");
hide("hiscore");
hide("title");
hide("omiod");
hide("like");
hide("info");
if (interval != undefined) interval=window.clearInterval(interval);
reset();
updateLives();
interval = setInterval(loop, 1000 / 60);
initPhase( 1 );
}
function gameOver() {
var startext = [];
startext[0] = "START";
startext[1] = "TRY AGAIN";
startext[2] = "ONCE MORE";
startext[3] = "DO IT AGAIN";
startext[4] = "RESTART";
startext[5] = "WANNA PLAY";
startext[6] = "ONE MORE TIME";
startext[7] = "GO !!!";
bdy.style.backgroundColor = '#000';
var o = document.getElementById("start");
o[removed] = startext[ Math.floor(Math.random() * startext.length) ];
show("start")
hiscore = localStorage.getItem("hiscore");
if ( hiscore == 0 || hiscore == undefined || hiscore == null ) hiscore = 0;
if ( hiscore < score ) {
hiscore = score;
localStorage.setItem("hiscore", hiscore);
}
titleScreen();
}
function initPhase( ph ) {
phase = ph;
toNextPhase = 5000 + Math.random() * PHASELEN;
switch ( phase ) {
case 0:
break;
case 1:
break;
case 2:
c1 = Math.random() * 6.28;
if ( Math.random() > 0.5 )
c2 = Math.random() * 0.005;
else
c2 = 0;
break;
case 3:
c1=Math.random()*500 + 10;
c2=Math.random()*20 + 1;
break;
case 4:
c1 = Math.random()*500 + 10;
c2 = c1/2;
break;
case 5:
c1 = Math.random()*10 + 5;
c2 = Math.random()*10 + 5;
break;
}
//console.log("init phase :"+c1+" , "+c2)
}
function updateLives() {
var out = "";
var lv = document.getElementById("lives");
for ( var i = 0; i<lives ; i++ ) out += "¤";
lv[removed] = out;
}
function reset() {
speed = 0;
score = 0;
phase = 4;
nextFrame = 0;
nextP = 0;
lives = 2;
collision = 0;
for ( var i = 0; i < STARS; i ++ ) {
particle = particles[ i ];
particle.position.x = (i % 2) * 1200 - 600;
particle.position.y = -300;
particle.position.z = i * ( FAR / STARS ) ;
particle.scale.x = particle.scale.y = 17;
}
}
function init() {
resetFont();
particles = new Array();
for ( var i = 0; i < STARS; i ++ ) {
//particle = particles[ i ] = new THREE.Particle( new THREE.ParticleCircleMaterial( { color: 0xffffff, opacity: 1, blending: THREE.AdditiveBlending } ) );
//particle = particles[ i ] = new THREE.Particle( new THREE.ParticleCircleMaterial( { color: 0xffffff, opacity: 1, blending: THREE.SubtractiveBlending } ) );
particle = particles[ i ] = new THREE.Particle( new THREE.ParticleCircleMaterial( { color: 0xffffff, opacity: 1 } ) );
scene.addObject( particle );
}
renderer = new THREE.CanvasRenderer();
//renderer.setClearColor( 0xff0000, 1 );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
document.addEventListener( 'touchstart', onDocumentTouchStart, false );
document.addEventListener( 'touchmove', onDocumentTouchMove, false );
document.addEventListener( 'mousedown', onDocumentMouseDown, false );
window.addEventListener( 'resize', onWindowResize, false );
//updateLives();
}
function resetFont() {
var wh = window.innerHeight / 11;
bdy.style.fontSize = wh+'px';
}
function onWindowResize(){
windowHalfX = window.innerWidth / 2;
windowHalfY = window.innerHeight / 2;
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
resetFont();
fullscreen = ( window.innerWidth == window.outerWidth )
console.log("full:"+fullscreen);
//console.log( window.innerWidth +","+ document.body.clientWidth +","+ document.width +","+ document.documentElement.clientWidth +","+ window.outerWidth );
}
function onDocumentMouseMove( event ) {
mouseX = event.clientX - windowHalfX;
mouseY = event.clientY - windowHalfY;
}
function onDocumentTouchStart( event ) {
if ( event.touches.length == 1 ) {
event.preventDefault();
mouseX = event.touches[ 0 ].pageX - windowHalfX;
mouseY = event.touches[ 0 ].pageY - windowHalfY;
}
}
function onDocumentTouchMove( event ) {
if ( event.touches.length == 1 ) {
event.preventDefault();
mouseX = event.touches[ 0 ].pageX - windowHalfX;
mouseY = event.touches[ 0 ].pageY - windowHalfY;
}
}
function onDocumentMouseDown( event ) {
event.preventDefault();
speed = speed *0.75;
}
//
function loop() {
camera.position.x += ( mouseX - camera.position.x ) * .06;
camera.position.y += ( - mouseY - camera.position.y ) * .06;
loopSpeed = speed;
for ( var i = 0; i < STARS; i ++ ) {
particle = particles[ i ];
particle.position.z += loopSpeed;
var color = particles[ i ].materials[ 0 ].color;
if ( speed == 50 ) {
color.r = (particle.position.z / FAR * 0.5 );
color.g = (particle.position.z / FAR * 0.9 );
color.b = (particle.position.z / FAR);
} else {
color.r = color.g = color.b = (particle.position.z / FAR);
}
//color.r = color.g = color.b = (particle.position.z / FAR);
color.updateStyleString();
if (particle.position.z > FAR) {
particle.position.z -= FAR;
nextFrame ++;
switch ( phase ) {
case 1:
if ( Math.random() < 0.95 ) {
particle.position.x = Math.random() * 3000 - 1500;
particle.position.y = Math.random() * 3000 - 1500;
} else {
particle.position.x = camera.position.x + Math.random() * 200 - 100;
particle.position.y = camera.position.y + Math.random() * 200 - 100;
}
break;
case 2:
tmp = Math.random() * 3000 - 1500;
particle.position.x = camera.position.x + Math.cos(c1)*tmp;
particle.position.y = camera.position.y + Math.sin(c1)*tmp;
c1 += c2;
break;
case 3:
particle.position.x = camera.position.x + c1 * Math.cos(nextFrame/c2);
particle.position.y = camera.position.y + c1 * Math.sin(nextFrame/c2);
break;
case 4:
particle.position.x = camera.position.x + Math.random() * c1 - c2;
particle.position.y = camera.position.y + Math.random() * c1 - c2;
break;
case 5:
particle.position.x = 1000 * Math.cos(nextFrame/c1);
particle.position.y = 1000 * Math.sin(nextFrame/c2);
break;
}
}
if ( Math.abs( particle.position.x-camera.position.x) < SAFE && Math.abs( particle.position.y-camera.position.y) < SAFE && Math.abs( particle.position.z-camera.position.z) < SAFE ) {
if ( collision < 0 ) {
lives --;
updateLives();
}
speed = -3;
collision = 50;
}
}
speed += 0.06;
if ( speed > 50 ) {
speed = 50;
score ++;
}
toNextPhase -= Math.floor(speed);
if ( toNextPhase < 0 ) {
initPhase( Math.floor( Math.random() * NPHASES )+1 );
//toNextPhase += PHASELEN;
//console.log(phase);
}
collision --;
if ( collision > 0 ) {
tmp = Math.floor( Math.random()*collision*5);
bdy.style.backgroundColor = 'rgb('+tmp+','+Math.floor( tmp/2 )+',0)';
//} else if ( speed == 50 ) {
// bdy.style.backgroundColor = '#033';
} else {
bdy.style.backgroundColor = '#000';
}
var sc = document.getElementById("score");
sc[removed] = score;
renderer.render( scene, camera );
if ( collision < 0 && lives <=0 ) {
interval=window.clearInterval(interval);
gameOver();
}
}
///
function demo() {
for ( var i = 0; i < STARS; i ++ ) {
particle = particles[ i ];
particle.position.z += 0.1;
var color = particles[ i ].materials[ 0 ].color;
if ( Math.abs(i - collision) < 10 ) {
if ( i % 2 == 0 ) {
color.r = (particle.position.z / FAR);
color.g = color.b = 0;
} else {
color.g = (particle.position.z / FAR);
color.r = color.b = 0;
}
} else {
color.r = color.g = color.b = (particle.position.z / FAR * 0.33);
}
color.updateStyleString();
}
collision ++;
if ( collision >= STARS ) collision = 0;
renderer.render( scene, camera );
if ( !fullscreen )
show("info");
else
hide("info");
}
</script>
Om du ville veta. Plus att det är väldigt underligga kontroller men annars är den väl lite söt antar jag.
Ingen status