var fov = 343; frameRate(40); textFont(createFont("Agency FB"),40); //Utilities { var keys = []; var pkeys = []; var wasPressed = mouseIsPressed; var keyPressed = function(){ keys[keyCode]=true; }; var keyReleased = function(){ keys[keyCode]=false; }; var collisionBoxes = []; } //Collisions { var doRectsOverlap = function(x11,y11,z11,x12,y12,z12,x21,y21,z21,x22,y22,z22){ var getPoint = function(x,y,z){ return {x:x,y:y,z:z}; }; var l1= getPoint(x11,y11,z11); var r1 = getPoint(x12,y12,z12); var l2 = getPoint(x21,y21,z21); var r2 = getPoint(x22,y22,z22); if(l1.x>r2.x||r1.xr2.y||r1.yr2.z||r1.z0;tier--){ var t = tiers[tier]; var t2 = tiers[tier-1]; for(var i = 0;i0||a2.length>0){ if(!a1.length){ for(var y in a2){ g.push(a2[y]); } a2 = []; } else if(!a2.length){ for(var y in a1){ g.push(a1[y]); } a1 = []; } else if(a1[0][v]0){ firstNegativeIndex=parseInt(i,0); } if(face[i][2]-zMin<0){ numOfNegatives+=1; } } if(numOfNegatives===0){ return face; } var l = face.length; if(numOfNegatives===l){ return [[0,0,0]]; } var newFace = []; var lastNegativeIndex = (firstNegativeIndex+numOfNegatives-1)%l; var index; var moveIndex = function(m){ index+=m; if(index<0){ index+=l; } index%=l; }; var indexMoved = function(n,m){ var i = parseInt(n,0); i+=m; if(i<0){ i+=l; } i%=l; return i; }; var index=indexMoved(lastNegativeIndex,1); var getScale = function(a,b,c){ return (c-a)/(b-a); }; var lerpFactor = getScale(face[firstNegativeIndex][2],face[indexMoved(firstNegativeIndex,-1)][2],zMin); newFace.push(lerpPoint(face[firstNegativeIndex],face[indexMoved(firstNegativeIndex,-1)],lerpFactor)); lerpFactor = getScale(face[lastNegativeIndex][2],face[indexMoved(lastNegativeIndex,1)][2],zMin); newFace.push(lerpPoint(face[lastNegativeIndex],face[indexMoved(lastNegativeIndex,1)],lerpFactor)); while(index!==firstNegativeIndex){ newFace.push(face[index]); moveIndex(1); } /*var t = ""; for(var i in newFace){ t+="["; for(var b in newFace[i]){ t+=newFace[i][b]+","; } t+="],"; } println(t);*/ return newFace; }; var lightVector = normVec([2,8,4]); var onlyRender = []; var gem = function(x,y,z,s){ var a = [285,50,50,4,250,1]; //*1.732 this.faces = [ [[[x,y+s,z],[x+s/4,y+s*0.5,z+s/4*1.732],[x+s/2,y+s*0.5,z]],a], [[[x,y+s,z],[x-s/4,y+s*0.5,z+s/4*1.732],[x+s/4,y+s*0.5,z+s/4*1.732]],a], [[[x,y+s,z],[x-s/2,y+s*0.5,z],[x-s/4,y+s*0.5,z+s/4*1.732]],a], [[[x,y+s,z],[x-s/4,y+s*0.5,z-s/4*1.732],[x-s/2,y+s*0.5,z]],a], [[[x,y+s,z],[x+s/4,y+s*0.5,z-s/4*1.732],[x-s/4,y+s*0.5,z-s/4*1.732]],a], [[[x,y+s,z],[x+s/2,y+s*0.5,z],[x+s/4,y+s*0.5,z-s/4*1.732]],a], [[[x+s/4,y+s*0.5,z+s/4*1.732],[x+s/4,y-s*0.5,z+s/4*1.732],[x+s/2,y-s*0.5,z],[x+s/2,y+s*0.5,z]],a], [[[x-s/4,y+s*0.5,z-s/4*1.732],[x-s/4,y-s*0.5,z-s/4*1.732],[x-s/2,y-s*0.5,z],[x-s/2,y+s*0.5,z]],a], [[[x+s/4,y+s*0.5,z+s/4*1.732],[x-s/4,y+s*0.5,z+s/4*1.732],[x-s/4,y-s*0.5,z+s/4*1.732],[x+s/4,y-s*0.5,z+s/4*1.732]],a], [[[x+s/4,y+s*0.5,z-s/4*1.732],[x+s/4,y-s*0.5,z-s/4*1.732],[x-s/4,y-s*0.5,z-s/4*1.732],[x-s/4,y+s*0.5,z-s/4*1.732]],a], [[[x+s/4,y+s*0.5,z-s/4*1.732],[x+s/2,y+s*0.5,z],[x+s/2,y-s*0.5,z],[x+s/4,y-s*0.5,z-s/4*1.732]],a], [[[x-s/4,y+s*0.5,z+s/4*1.732],[x-s/2,y+s*0.5,z],[x-s/2,y-s*0.5,z],[x-s/4,y-s*0.5,z+s/4*1.732]],a], [[[x,y-s,z],[x+s/2,y-s*0.5,z],[x+s/4,y-s*0.5,z+s/4*1.732]],a], [[[x,y-s,z],[x+s/4,y-s*0.5,z+s/4*1.732],[x-s/4,y-s*0.5,z+s/4*1.732]],a], [[[x,y-s,z],[x-s/4,y-s*0.5,z+s/4*1.732],[x-s/2,y-s*0.5,z]],a], [[[x,y-s,z],[x-s/2,y-s*0.5,z],[x-s/4,y-s*0.5,z-s/4*1.732]],a], [[[x,y-s,z],[x-s/4,y-s*0.5,z-s/4*1.732],[x+s/4,y-s*0.5,z-s/4*1.732]],a], [[[x,y-s,z],[x+s/4,y-s*0.5,z-s/4*1.732],[x+s/2,y-s*0.5,z]],a], ]; this.run = function(){ for(var i in this.faces){ for(var b in this.faces[i][0]){ this.faces[i][0][b]=rotateNodeY(this.faces[i][0][b],[x,0,z],3); } } }; }; var dopePillar = function(x,y,z,h){ var a = [305,305,305,0]; var s = 10; var faces = []; var addBox = function(x,y,z,w,h,d){ faces.push([[[x,y,z],[x,y+h,z],[x+w,y+h,z],[x+w,y,z]],a]); faces.push([[[x,y,z+d],[x+w,y,z+d],[x+w,y+h,z+d],[x,y+h,z+d]],a]); faces.push([[[x,y,z],[x,y,z+d],[x,y+h,z+d],[x,y+h,z]],a]); faces.push([[[x+w,y,z],[x+w,y+h,z],[x+w,y+h,z+d],[x+w,y,z+d]],a]); faces.push([[[x,y,z],[x+w,y,z],[x+w,y,z+d],[x,y,z+d]],a]); faces.push([[[x,y+h,z],[x,y+h,z+d],[x+w,y+h,z+d],[x+w,y+h,z]],a]); }; var degrees = [random(0,360)]; var ranges = [random(0,1)]; addBox(x-s/2,y-h/2,z-s/2,s,h,s); for(var i = 0;i<6;i++){ degrees.push(random(0,360)); ranges.push(random(0,1)); var r = random(20,40); var r2 = random(0,360); addBox(x-s/2+cos(r2)*r,y+random(-0.4,0.15)*h,z-s/2+sin(r2)*r,s/random(1,2),h/random(3,5),s/random(1,2)); } this.faces=faces; this.run = function(){ for(var i = 0;i=0;i--){ b.push(a[i]); } return b; }; var pedestal = function(x,y,z){ var a = [255,255,255,0,255,1]; var s = 30; var faces = []; var reflectOver = function(a,b){ return (b-a)+b; }; var fourWayFace = function(n){ faces.push([n,a]); var n2 = []; for(var i in n){ var nn = n[i]; n2.push([reflectOver(nn[0],x),nn[1],reflectOver(nn[2],z)]); } faces.push([n2,a]); var n2=[]; for(var i in n){ var nn = n[i]; n2.push([x-z+reflectOver(nn[2],z),nn[1],z-x+reflectOver(nn[0],x)]); } faces.push([reverseArray(n2),a]); var n2=[]; for(var i in n){ var nn = n[i]; n2.push([x-z+nn[2],nn[1],z-x+nn[0]]); } faces.push([reverseArray(n2),a]); }; var twoWayFace = function(n){ faces.push([n,a]); var n2 = []; for(var i in n){ var nn = n[i]; n2.push([reflectOver(nn[0],x),nn[1],reflectOver(nn[2],z)]); } faces.push([n2,a]); }; var h = 2.288; fourWayFace([[x+s/3,y,z+s/1.5],[x-s/3,y,z+s/1.5],[x-s/3,y-s*2/3,z+s/h],[x+s/3,y-s*2/3,z+s/h]]); twoWayFace([[x+s/3,y,z+s/1.5],[x+s/3,y-s*2/3,z+s/h],[x+s/h,y-s*2/3,z+s/3],[x+s/1.5,y,z+s/3]]); twoWayFace([[x+s/3,y-s*2/3,z+s/h],[x+s/3,y-s,z+s/3],[x+s/h,y-s*2/3,z+s/3]]); twoWayFace([[x+s/3,y-s,z+s/3],[x+s/3,y-s*2/3,z+s/h],[x+s/3,y-s*2/3,z+s/3]]); twoWayFace([[x+s/3,y-s,z+s/3],[x+s/3,y-s*2/3,z+s/3],[x+s/h,y-s*2/3,z+s/3]]); twoWayFace([[x+s/3,y,z-s/1.5],[x+s/1.5,y,z-s/3],[x+s/h,y-s*2/3,z-s/3],[x+s/3,y-s*2/3,z-s/h]]); twoWayFace([[x+s/3,y-s*2/3,z-s/h],[x+s/h,y-s*2/3,z-s/3],[x+s/3,y-s,z-s/3]]); twoWayFace([[x-s/3,y-s,z+s/3],[x-s/h,y-s*2/3,z+s/3],[x-s/3,y-s*2/3,z+s/3]]); twoWayFace([[x-s/3,y-s,z+s/3],[x-s/3,y-s*2/3,z+s/3],[x-s/3,y-s*2/3,z+s/h]]); fourWayFace([[x-s/3,y-s*2/3,z+s/3],[x-s/h,y-s*2/3,z+s/3],[x-s/h,y-s*2/3,z-s/3],[x-s/3,y-s*2/3,z-s/3]]); fourWayFace([[x-s/3,y-s*2/3,z+s/3],[x-s/3,y-s*2/3,z-s/3],[x,y-s*2.6/3,z]]); this.faces = faces; onlyRender.push(new gem(x,y-s*1.3,z,8)); this.run = function(){ }; }; } //Game Mechanics { var player = function(x,y,z){ this.x=x; this.y=y; this.z=z; this.w=30; this.h=50; this.d=30; this.xSpeed = 0; this.ySpeed = 0; this.zSpeed = 0; this.cameraOffset = [0,0,0]; this.cameraRotation = [0,0,0]; var movingTicks = 0; this.onGround=false; this.setPosition=function(x,y,z,rX,rY,rZ){ this.x=x; this.y=y; this.z=z; this.cameraRotation=[rX,rY,rZ]; }; this.run = function(){ var oG; { var movementSpeed=1; if(keys[16]){ movementSpeed*=2.5; } if(!this.onGround){ movementSpeed/=40; } if(this.onGround){ this.xSpeed=0; this.zSpeed=0; } this.xSpeed*=0.99; this.zSpeed*=0.99; if(!keys[87]&&!keys[83]&&!keys[68]&&!keys[65]&&movingTicks*10%180===0){ movingTicks = 0; } else if(this.onGround){ movingTicks++; if(keys[16]){ movingTicks++; } } this.cameraOffset[1] = -abs(sin(movingTicks*10)*4); if(keys[32]&&this.onGround){ this.ySpeed=-3; } if(keys[65]){ this.zSpeed+=2*movementSpeed*sin(this.cameraRotation[1]); this.xSpeed+=-2*movementSpeed*cos(this.cameraRotation[1]); } if(keys[68]){ this.zSpeed+=-2*movementSpeed*sin(this.cameraRotation[1]); this.xSpeed+=2*movementSpeed*cos(this.cameraRotation[1]); } if(keys[87]){ this.xSpeed+=2*movementSpeed*sin(this.cameraRotation[1]); this.zSpeed+=2*movementSpeed*cos(this.cameraRotation[1]); } if(keys[83]){ this.xSpeed+=-sin(this.cameraRotation[1])*movementSpeed; this.zSpeed+=-cos(this.cameraRotation[1])*movementSpeed; } oG = this.onGround; this.onGround=false; if(mouseIsPressed){ var s = 2; this.cameraRotation[0]+=(mouseY-pmouseY)/s; this.cameraRotation[1]+=(mouseX-pmouseX)/s; this.cameraRotation[0]=constrain(this.cameraRotation[0],-90,90); } } var fps = 10; for(var i = 0;i130){ ticksRun2++; } if(ticksRun2===10){ popText("SPACE",40,500); } if(ticksRun2===50){ popText("TO",40,200); } if(ticksRun2===90){ popText("JUMP",40,400); } if(triggered[4]){ ticksRun3++; } if(ticksRun3===10){ popText("GRAB",40); } if(ticksRun3===50){ popText("THE",40); } if(ticksRun3===90){ popText("CRYSTAL",40); } if(mainPlayer.z>0&&!triggered[0]){ collisionBoxes.push(new collisionBox(-50,10000,140,100,5,100,[1,1,1,1,1,1])); triggered[0]=true; } if(mainPlayer.z>130&&!triggered[1]){ collisionBoxes.push(new collisionBox(-50000,25,280,100,5,100,[1,1,1,1,1,1])); triggered[1]=true; } if(mainPlayer.z>250&&!triggered[2]){ collisionBoxes.push(new collisionBox(50000,25,140*3,100,5,100,[1,1,1,1,1,1])); triggered[2]=true; } if(mainPlayer.z>370&&!triggered[3]){ collisionBoxes.push(new collisionBox(-50,-10000,140*4,100,5,100,[1,1,1,1,1,1])); triggered[3]=true; } if(mainPlayer.z>550&&mainPlayer.onGround){ triggered[4]=true; } if(triggered[0]){ collisionBoxes[c].move(0,(25-collisionBoxes[c].y)/10,0); } if(triggered[1]){ collisionBoxes[c+1].move((-50-collisionBoxes[c+1].x)/10,0,0); } if(triggered[2]){ collisionBoxes[c+2].move((-50-collisionBoxes[c+2].x)/10,0,0); } if(triggered[3]){ collisionBoxes[c+3].move(0,(25-collisionBoxes[c+3].y)/10,0); } var getScale = function(a,b,c){ return (c-a)/(b-a); }; if(triggered[4]&&collisionBoxes[4].z<1000){ collisionBoxes[c+3].move(0,0,getScale(140*4-10,1100,collisionBoxes[c+3].z)*getScale(1100,140*4-10,collisionBoxes[c+3].z)*20); } if(levelComplete){ framesDone++; cameraData=this.coolVantage; if(framesDone%150===10){ popText("SUPER",80,500); } if(framesDone%150===85){ popText("HOT",80,300); } if(!wasPressed&&mouseIsPressed){ currentLevel++; textToDisplay = []; collisionBoxes = []; onlyRender = []; levels[currentLevel].setup(); } } for(var i in clickPoints){ var xx = (mainPlayer.x-clickPoints[i][0]); var yy = (mainPlayer.y-clickPoints[i][1]); var zz = (mainPlayer.z-clickPoints[i][2]); var r = constrain(7000/sqrt(xx*xx+yy*yy+zz*zz),0,Infinity); var nn = nodeToXY(cameraTranslateNode(clickPoints[i])); if(dist(mouseX-300,mouseY-300,nn[0],nn[1])0){ cZ=min(n[0]*n[0]+n[1]*n[1]+n[2]*n[2],cZ); } } var n = [0,0,0]; for(var e in face){ var n2 = cameraTranslateNode(face[e]); n[0]+=n2[0]/face.length; n[1]+=n2[1]/face.length; n[2]+=n2[2]/face.length; } cZ=min(n[0]*n[0]+n[1]*n[1]+n[2]*n[2],cZ); //cZ=min(n[2]*n[2],cZ); if(!behind){ if(faces[i][1][5]){ cZ-=9000000*faces[i][1][5]; } faces_.push([a,-cZ,faces[i][1]]); } } } for(var b in collisionBoxes){ var faces = collisionBoxes[b].faces; collisionBoxes[b].run(); for(var i in faces){ var face = faces[i][0]; var a = []; var cZ = Infinity; var behind=true; for(var e in face){ var n = cameraTranslateNode(face[e]); behind=behind&&n[2]<0; a.push(n); cZ=min(n[0]*n[0]+n[1]*n[1]+n[2]*n[2],cZ); } if(!behind){ if(faces[i][1][5]){ cZ-=9000000*faces[i][1][5]; } faces_.push([a,-cZ,faces[i][1]]); } } } faces_ = mergeSort(faces_,1); for(var i in faces_){ var face = faces_[i][0]; //Finding the Face Normal and alignment var normal = faceNormal(face); var alignment = (proj(cameraRotateNode(lightVector),normal)+1)/2; var a3 = pow(alignment,30); var c = [faces_[i][2][0],faces_[i][2][1],faces_[i][2][2]]; c[0]+= a3*100*faces_[i][2][3]; c[1]+= a3*100*faces_[i][2][3]; c[2]+= a3*100*faces_[i][2][3]; alignment = (90+140*alignment)/255; fill(c[0]*alignment,c[1]*alignment,c[2]*alignment,faces_[i][2][4]?faces_[i][2][4]:255); stroke(c[0]*alignment,c[1]*alignment,c[2]*alignment,faces_[i][2][4]?faces_[i][2][4]:255); //Determines if the face is in front or behind the camera face=stayPositive(face); if(face.length!==1){ //Getting the Projected View var faceProjected = []; for(var b in face){ faceProjected.push(nodeToXY(face[b])); } if(clockwise([faceProjected[0],faceProjected[1],faceProjected[2]])){ beginShape(); for(var m =0;m1000){ wipeLevel(); } popMatrix(); pkeys=keys; wasPressed=mouseIsPressed; };