Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
294 lines (281 sloc) 8.32 KB
// Adjust camera:
var newZ = ( zoom + z*9 ) / 10;
//macro sign: ( @1<0 ? -1 : 1 )
if ( shipAlive && abs(newZ-z)>0.005 ) z += 0.005 * sign(newZ-z);
else z = newZ;
viewCX = scrCX - focus.x * z;
viewCY = scrCY - focus.y * z;
if ( z < 1 ) {
vCX = ( viewCX + vCX*10 ) / 11;
vCY = ( viewCY + vCY*10 ) / 11;
} else {
vCX = ( viewCX + vCX*5 ) / 6;
vCY = ( viewCY + vCY*5 ) / 6;
}
// Stars
if (showStars) {
starX = ( -focus.x + starX*5 ) / 6;
starY = ( -focus.y + starY*5 ) / 6;
bg.style.transform =
bg.style.oTransform =
bg.style.msTransform =
bg.style.mozTransform =
bg.style.webkitTransform =
'translate('+
( starX*.5/(bgW/w) - (bgW-w)/2 )+'px,'+
( starY*.5/(bgH/h) - (bgH-h)/2 )+'px)'+
' rotate(13deg)';
}
var mapZ = (map.height/biggerPlanetD)/2.2;
var grd;
// Nickname methods
for (var method in ctx)
CanvasRenderingContext2D.prototype[method.replace(/(.)[a-z]+/g, '$1')] = ctx[method];
// Clear all
ctx.clearRect(0,0,w,h);
ctxMap.clearRect(0,0,map.width,map.height);
// Draw missiles
missiles.forEach(function(m) {
ctx.fillStyle = 'rgb(255,'+Math.round((sin(ticCounter/2)+1)*127)+',0)';
ctx.strokeStyle = 'rgb(255,'+Math.round((cos(ticCounter/2)+1)*127)+',0)';
ctx.bP();
ctx.arc( z*m.x+vCX, z*m.y+vCY, z*(m.r-1), 0,7 );
ctx.fill();
ctx.lineWidth = 1.5*z;
ctx.stroke();
ctxMap.fillStyle = ctx.fillStyle;
ctxMap.bP();
ctxMap.arc( mapZ*m.x+map.cx, mapZ*m.y+map.cy, mapZ*m.r*4, 0,7 );
ctxMap.cP();
ctxMap.fill();
});
// Draw Cannons
cannons.forEach(function(cannon){
ctx.save();
ctx.translate( z*cannon.x+vCX, z*cannon.y+vCY );
ctx.rotate(cannon.rotate);
if ( qualityLevel < 3 ) {
grd = ctx.createLinearGradient(-15*z, 0, (cannon.build-17)*z*.6, 0);
grd.addColorStop(0, '#800');
grd.addColorStop(1, '#D60');
} else grd = '#D60';
ctx.fillStyle = grd;
ctx.bP();
ctx.lT( -20 *z, -10*z);
ctx.lT((cannon.build-20)*z*.6, -10*z);
ctx.lT((cannon.build-17)*z*.6, -8*z);
ctx.lT((cannon.build-17)*z*.6, 8*z);
ctx.lT((cannon.build-20)*z*.6, 10*z);
ctx.lT( -20 *z, 10*z);
ctx.cP();
ctx.fill();
if ( qualityLevel < 3 ) {
grd = ctx.createLinearGradient(0, -5*z, 0, 5*z);
grd.addColorStop(0, '#920');
grd.addColorStop(.45, '#C50');
grd.addColorStop(.55, '#C50');
grd.addColorStop(1, '#920');
} else grd = '#B40';
ctx.fillStyle = grd;
ctx.fillRect(-15*z, -5*z, (cannon.build-5)*2.6*z, 10*z);
ctx.restore();
});
// Sun rays
// createRadialGradient's first ray must to be 0 for android.
var sunRayZoom = z>.5 ? .45 + z/10 : z;
var sunRayLength = sunRayZoom * sun.r * 3;
if ( qualityLevel < 3 ) {
grd = ctx.createRadialGradient(vCX, vCY, 0, vCX, vCY, sunRayLength);
grd.addColorStop(0, 'rgb('+sun.c[0]+')');
grd.addColorStop(0.2, 'rgba('+sun.c[0]+',0.5)');
grd.addColorStop(1, 'rgba('+sun.c[0]+',0)'); // with stars
// grd.addColorStop(1, 'rgba(0,0,0,0.5)'); // without stars
ctx.fillStyle = grd;
} else {
sunRayLength = sunRayZoom * sun.r * 2;
ctx.fillStyle = '#FC0';
}
ctx.bP();
var timeDif = (now() - start)/16000;
for (var i=timeDif; i<timeDif+7; i+=0.314*2) {
ctx.arc( vCX, vCY, sunRayLength, i,i+0.314 );
ctx.lT(vCX, vCY);
}
ctx.cP();
ctx.fill();
// Draw Planets
for ( var id in planets ) {
var p = planets[id];
// Planet Bright
if ( qualityLevel == 1 ) {
grd = ctx.createRadialGradient(z*p.x+vCX, z*p.y+vCY, 0,
z*p.x+vCX, z*p.y+vCY, z*p.r*2);
grd.addColorStop(0, 'rgb('+p.c[1]+')');
grd.addColorStop(0.48, 'rgb('+p.c[1]+')');
grd.addColorStop(0.48, 'rgba('+p.c[2]+','+(p==sun?0.6:0.3*(z>.5?z:.5))+')');
grd.addColorStop(1, 'rgba(0,0,0,0)');
ctx.fillStyle = grd;
ctx.bP();
ctx.arc( z*p.x+vCX, z*p.y+vCY, z*p.r*(p==sun?3:2), 0,7 );
ctx.cP();
ctx.fill();
}
if ( qualityLevel == 2 ) {
ctx.fillStyle = 'rgb('+p.c[1]+')';
ctx.bP();
ctx.arc( z*p.x+vCX, z*p.y+vCY, z*p.r*.99, 0,7 );
ctx.cP();
ctx.fill();
}
if ( qualityLevel < 3 ) {
// Planet Volume Shadow
grd = ctx.createRadialGradient(z*p.x+vCX, z*p.y+vCY, 0, //z*p.r/9,
z*p.x+vCX, z*p.y+vCY, z*p.r);
grd.addColorStop(0, 'rgba('+p.c[1]+',0)');
grd.addColorStop(1, 'rgb('+p.c[0]+')');
ctx.fillStyle = grd;
} else {
ctx.fillStyle = 'rgb('+p.c[0]+')';
}
ctx.bP();
ctx.arc( z*p.x+vCX, z*p.y+vCY, z*p.r, 0,7 );
ctx.cP();
ctx.fill();
// Planet on the map
var owner = ownerOfPlanet(p);
var ownerColor;
if (owner == -1) ownerColor = 'rgb(220,220,220)';
else if (owner == 0) ownerColor = 'rgb(50,240,50)';
else if (owner == 1) ownerColor = 'rgb(240,100,50)';
else ownerColor = 'rgb(255,0,255)';
ctxMap.fillStyle = p==sun? '#FF7' : ownerColor;
ctxMap.bP();
ctxMap.arc( mapZ*p.x+map.cx, mapZ*p.y+map.cy, mapZ*p.r, 0,7 ); //(p==sun?3:0.6)
ctxMap.cP();
ctxMap.fill();
}
// Draw Boids
boids.forEach(function(b){
ctx.strokeStyle = 'rgba('+b.color[1]+',0.6)';
// Velocity tail
ctx.bP();
ctx.mT( z*b.x+vCX, z*b.y+vCY );
ctx.lT( z*b.vx*-10+z*b.x+vCX, z*b.vy*-10+z*b.y+vCY );
ctx.cP();
ctx.stroke();
// Boid body
ctx.bP();
ctx.strokeStyle = 'rgb('+b.color[0]+')';
ctx.fillStyle = 'rgb('+b.color[1]+')';
ctx.arc( z*b.x+vCX, z*b.y+vCY, z*b.r, 0,7 );
ctx.cP();
ctx.fill();
ctx.lineWidth = 0.8*z;
ctx.stroke();
});
// Draw Ship
if (shipAlive) {
ctxMap.strokeStyle = 'rgba(0,200,200,0.5)';
ctx.bP();
ctxMap.mT( mapZ*ship.x+map.cx, mapZ*ship.y+map.cy );
ctxMap.arc( mapZ*ship.x+map.cx, mapZ*ship.y+map.cy, 2, ship.rotate,ship.rotate+completeRad );
ctxMap.arc( mapZ*ship.x+map.cx, mapZ*ship.y+map.cy, 9, ship.rotate,ship.rotate+completeRad );
/*ctxMap.mT( mapZ*ship.x+map.cx, mapZ*ship.y+map.cy );
ctxMap.arc( mapZ*ship.x+map.cx, mapZ*ship.y+map.cy, 10,
ship.rotate+turn3*2, ship.rotate+turn3*3 );
ctxMap.arc( mapZ*ship.x+map.cx, mapZ*ship.y+map.cy, 10,
ship.rotate+turn3, ship.rotate+turn3*2 );
ctxMap.arc( mapZ*ship.x+map.cx, mapZ*ship.y+map.cy, 10,
ship.rotate, ship.rotate+turn3 );
ctxMap.lT( mapZ*ship.x+map.cx, mapZ*ship.y+map.cy );*/
ctxMap.cP();
ctxMap.stroke();
ctx.save();
ctx.translate( z*ship.x+vCX, z*ship.y+vCY );
ctx.rotate(ship.rotate);
var scale = z<1 ? (z+1)/2 : z;
ctx.scale(scale,scale);
/*
ctx.strokeStyle = 'rgba(255,255,255,0.2)'; // debug
ctx.bP(); // debug
ctx.arc( 0, 0, 20, 0,7 ); // debug
ctx.stroke(); // debug
*/
if (usrInput.btU) { // Back Fire
grd = ctx.createRadialGradient(-9+rand(1),0, 1, -11,0, 4);
grd.addColorStop(0, '#FF0');
grd.addColorStop(1, '#F00');
ctx.fillStyle = grd;
ctx.bP();
ctx.mT(-7.5,0);
ctx.bCT( -11,-7.5, -13,-.5, -20+rand(2),0 );
ctx.bCT( -13,1, -11,7.5, -7.5,0 );
ctx.fill();
}
if (usrInput.btR) { // Fire Clockwise
grd = ctx.createRadialGradient(7.5,-5, 0, 7.5,-6, 2);
grd.addColorStop(0, '#FF0');
grd.addColorStop(1, '#F00');
ctx.fillStyle = grd;
ctx.bP();
ctx.mT(7.5,-4);
ctx.bCT( 10,-6.5, 8,-6.5, 7.5,-11+rand(2) );
ctx.bCT( 7,-6.5, 5,-6.5, 7.5,-4 );
ctx.fill();
}
if (usrInput.btL) { // Fire Anti-Clockwise
grd = ctx.createRadialGradient(7.5,5, 0, 7.5,6, 2);
grd.addColorStop(0, '#FF0');
grd.addColorStop(1, '#F00');
ctx.fillStyle = grd;
ctx.bP();
ctx.mT(7.5,4);
ctx.bCT( 10,6.5, 8,6.5, 7.5,11+rand(2) );
ctx.bCT( 7,6.5, 5,6.5, 7.5,4 );
ctx.fill();
}
// Wings
ctx.fillStyle = '#599';
ctx.bP();
ctx.mT(-2.5,-9.5);
ctx.bCT( 7.5,-12.5, 7.5,12.5, -2.5,9.5 );
ctx.cP();
ctx.fill();
// Body
if ( qualityLevel < 3 ) {
grd = ctx.createRadialGradient(1, 0, 0, 1, 0, 10);
grd.addColorStop(0, '#F0F8F8');
grd.addColorStop(1, '#799');
ctx.fillStyle = grd;
} else {
ctx.fillStyle = '#ACC';
}
ctx.bP();
ctx.mT(-9,-4.5);
ctx.bCT( 0,-10, 9.5,-4, 10,0 );
ctx.bCT( 10,4, 0,10, -9,4.5 );
ctx.lT(-8, 0);
ctx.fill();
// Staff Boids
ctx.lineWidth = 1/(z+1/2);
ctx.strokeStyle = '#181';
var drawCrew = function(pos) {
var t = ship.crew;
ctx.bP();
ctx.fillStyle = t[pos] ? 'rgb('+t[pos].color[1]+')' : '#888';
ctx.arc( -4+(pos*4),0, 2, 0,7 );
ctx.cP();
ctx.fill(); ctx.stroke();
}
drawCrew(0);
drawCrew(1);
drawCrew(2);
ctx.restore();
}
// Draw explosions
explosions.forEach(function(e) {
ctx.fillStyle = 'rgba(255,255,255,'+(20-e.t)/20+')';
ctx.bP();
ctx.arc( z*e.x+vCX, z*e.y+vCY, z*(e.r+e.t*e.inc), 0,7 );
ctx.fill();
});
You can’t perform that action at this time.