Advertisement
here2share

ZZZ: 3D Shooter -- Javascript To Python

Feb 8th, 2015
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 9.13 KB | None | 0 0
  1. /**
  2.  * http://www.benjoffe.com/code/demos/canvascape
  3.  *
  4.  * Copyright (c) 2009, Benjamin Joffe
  5.  * All rights reserved.
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions are met:
  8.  *
  9.  *     * Redistributions of source code must retain the above copyright
  10.  *       notice, this list of conditions and the following disclaimer.
  11.  *     * Redistributions in binary form must reproduce the above copyright
  12.  *       notice, this list of conditions and the following disclaimer in the
  13.  *       documentation and/or other materials provided with the distribution.
  14.  *
  15.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY
  16.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18.  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR AND CONTRIBUTORS BE LIABLE FOR ANY
  19.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25.  */
  26.  
  27. var map;
  28. var canvas;
  29. var overlay;
  30. //variables initiated at the bottom of the code...
  31.  
  32. var pi=Math.PI;
  33.  
  34. var total=0;
  35.  
  36. Number.prototype.range=function(){
  37.     return (this+2*pi)%(2*pi);
  38. }
  39. Number.prototype.roundC=function(){
  40.     return Math.round(this*100)/100;
  41. }
  42.  
  43. var total=0;
  44.  
  45. var samples=200;
  46.  
  47.  
  48. var arena=[];
  49. arena[0]=[1,1,1,1,1,1,1,1,1,1]
  50. arena[1]=[1,0,0,0,0,0,0,0,0,1]
  51. arena[2]=[1,0,0,1,0,1,1,1,0,1]
  52. arena[3]=[1,0,1,0,0,0,0,1,0,1]
  53. arena[4]=[1,0,0,0,0,1,0,1,0,1]
  54. arena[5]=[1,0,1,1,0,0,0,0,0,1]
  55. arena[6]=[1,0,0,1,0,1,1,1,0,1]
  56. arena[7]=[1,1,0,1,0,0,0,1,0,1]
  57. arena[8]=[1,0,0,1,0,1,0,0,0,1]
  58. arena[9]=[1,1,1,1,1,1,1,1,1,1]
  59.  
  60.  
  61. var playerPos=[4,4]; // x,y (from top left)
  62. var playerDir=0.4; // theta, facing right=0=2pi
  63. var playerPosZ=1;
  64. var key=[0,0,0,0,0]; // left, right, up, down
  65.  
  66. var playerVelY=0;
  67.  
  68.  
  69. var face=[];
  70.  
  71. function wallDistance(theta){
  72.  
  73.     var data=[];
  74.     face=[];
  75.  
  76.     var x = playerPos[0], y = playerPos[1];
  77.     var deltaX, deltaY;
  78.     var distX, distY;
  79.     var stepX, stepY;
  80.     var mapX, mapY
  81.    
  82.     var atX=Math.floor(x), atY=Math.floor(y);
  83.  
  84.     var thisRow=-1;
  85.     var thisSide=-1;
  86.  
  87.     var lastHeight=0;
  88.  
  89.     for (var i=0; i<samples; i++) {
  90.         theta+=pi/(3*samples)+2*pi;
  91.         theta%=2*pi;
  92.  
  93.         mapX = atX, mapY = atY;
  94.  
  95.         deltaX=1/Math.cos(theta);
  96.         deltaY=1/Math.sin(theta);
  97.  
  98.         if (deltaX>0) {
  99.             stepX = 1;
  100.             distX = (mapX + 1 - x) * deltaX;
  101.         }
  102.         else {
  103.             stepX = -1;
  104.             distX = (x - mapX) * (deltaX*=-1);     
  105.         }
  106.         if (deltaY>0) {
  107.             stepY = 1;
  108.             distY = (mapY + 1 - y) * deltaY;
  109.         }
  110.         else {
  111.             stepY = -1;
  112.             distY = (y - mapY) * (deltaY*=-1);
  113.         }
  114.        
  115.  
  116.         for (var j=0; j<20; j++) {
  117.             if (distX < distY) {
  118.                 mapX += stepX;
  119.                 if (arena[mapX][mapY]) {
  120.                     if (thisRow!=mapX || thisSide!=0) {
  121.                         if (i>0) {
  122.                             data.push(i);
  123.                             data.push(lastHeight);
  124.                         }
  125.                         data.push(i);
  126.                         data.push(distX);
  127.                         thisSide=0;
  128.                         thisRow=mapX;
  129.                         face.push(1+stepX);
  130.                     }
  131.                     lastHeight=distX;
  132.                     break;
  133.                 }
  134.                 distX += deltaX;
  135.             }
  136.             else {
  137.                 mapY += stepY;
  138.                 if (arena[mapX][mapY]) {
  139.                     if (thisRow!=mapY || thisSide!=1) {
  140.                         if (i>0) {
  141.                             data.push(i);
  142.                             data.push(lastHeight);
  143.                         }  
  144.                         data.push(i);
  145.                         data.push(distY);
  146.                         thisSide=1;
  147.                         thisRow=mapY;
  148.                         face.push(2+stepY)
  149.                     }
  150.                     lastHeight=distY;
  151.                     break;
  152.                 }
  153.                 distY += deltaY;
  154.             }
  155.         }
  156.     }
  157.     data.push(i);
  158.     data.push(lastHeight);
  159.    
  160.     return data;
  161. }
  162.  
  163.  
  164. function drawCanvas(){
  165.  
  166.     canvas.clearRect(0,0,400, 300);
  167.  
  168.     var theta = playerDir-pi/6;
  169.  
  170.     var wall=wallDistance(theta);
  171.  
  172.     map.beginPath();
  173.     map.clearRect(0,0,80,80);
  174.     map.fillStyle="#3366CC";
  175.     map.arc(playerPos[0]*8, playerPos[1]*8, 3, 0, 2*pi, true);
  176.     map.fill();
  177.     map.beginPath();
  178.     map.moveTo(8*playerPos[0], 8*playerPos[1]);
  179.  
  180.  
  181.     var linGrad;
  182.    
  183.     var tl,tr,bl,br;
  184.    
  185.     var theta1,theta2,fix1,fix2;
  186.    
  187.     for (var i=0; i<wall.length; i+=4) {
  188.  
  189.         theta1=playerDir-pi/6 + pi*wall[i]/(3*samples);
  190.         theta2=playerDir-pi/6 + pi*wall[i+2]/(3*samples);
  191.        
  192.         fix1 = Math.cos(theta1-playerDir);
  193.         fix2 = Math.cos(theta2-playerDir);
  194.  
  195.         var h=2-playerPosZ;
  196.  
  197.         var wallH1=100/(wall[i+1]*fix1);
  198.         var wallH2=100/(wall[i+3]*fix2);
  199.  
  200.         tl=[wall[i]*2, 150-wallH1*h];
  201.         tr=[wall[i+2]*2, 150-wallH2*h];
  202.         br=[wall[i+2]*2, tr[1]+wallH2*2];
  203.         bl=[wall[i]*2, tl[1]+wallH1*2];
  204.  
  205.         var shade1=Math.floor(wallH1*2+20); if (shade1>255) shade1=255;
  206.         var shade2=Math.floor(wallH2*2+20); if (shade2>255) shade2=255;
  207.  
  208.         linGrad = canvas.createLinearGradient(tl[0],0,tr[0],0);
  209.         linGrad.addColorStop(0, 'rgba('+(face[i/4]%2==0 ? shade1 : 0)+','+(face[i/4]==1 ? shade1 : 0)+','+(face[i/4]==2 ? 0 : shade1)+',1.0)');
  210.         linGrad.addColorStop(1, 'rgba('+(face[i/4]%2==0 ? shade2 : 0)+','+(face[i/4]==1 ? shade2 : 0)+','+(face[i/4]==2 ? 0 : shade2)+',1.0)');
  211.  
  212.         canvas.beginPath();
  213.         canvas.moveTo(tl[0], tl[1]);
  214.         canvas.lineTo(tr[0], tr[1]);
  215.         canvas.lineTo(br[0], br[1]);
  216.         canvas.lineTo(bl[0], bl[1]);
  217.         canvas.fillStyle = linGrad;
  218.         canvas.fill();
  219.  
  220.    
  221.         map.lineTo(playerPos[0]*8+Math.cos(theta1)*(wall[i+1])*8, playerPos[1]*8+Math.sin(theta1)*(wall[i+1])*8);
  222.         map.lineTo(playerPos[0]*8+Math.cos(theta2)*(wall[i+3])*8, playerPos[1]*8+Math.sin(theta2)*(wall[i+3])*8);
  223.  
  224.        
  225.     }
  226.     map.fillStyle="#FF0000"
  227.     map.fill();
  228.    
  229. }
  230.  
  231. function nearWall(x,y){
  232.     var xx,yy;
  233.     if (isNaN(x)) x=playerPos[0];
  234.     if (isNaN(y)) y=playerPos[1];
  235.     for (var i=-0.1; i<=0.1; i+=0.2) {
  236.         xx=Math.floor(x+i);
  237.         for (var j=-0.1; j<=0.1; j+=0.2) {
  238.             yy=Math.floor(y+j);
  239.             if (arena[xx][yy]) return true;
  240.         }
  241.     }
  242.     return false;
  243. }
  244.  
  245. var xOff = 0;
  246. var yOff = 0;
  247. function wobbleGun(){
  248.     var mag=playerVelY;
  249.    xOff = (10+Math.cos(total/6.23)*mag*90)
  250.    yOff = (10+Math.cos(total/5)*mag*90)
  251.     overlay.style.backgroundPosition = xOff + "px " + yOff + "px";
  252. }
  253.  
  254.  
  255. var jumpCycle=0;
  256.  
  257. var audio = window.Audio && new Audio("/img/canvascape/shoot.wav");
  258.  
  259. function shoot()
  260. {
  261.     audio && audio.play();
  262.     canvas.save();
  263.     canvas.strokeStyle = "#FFFF00";
  264.     canvas.beginPath();
  265.    
  266.     canvas.moveTo(190+xOff, 140+yOff);
  267.     canvas.lineTo(250+xOff, 200+yOff);
  268.     canvas.closePath();
  269.     canvas.stroke();
  270.     canvas.restore();
  271.     setTimeout('drawCanvas()',100);
  272. }
  273.  
  274.  
  275. function update(){
  276.  
  277.     total++;
  278.  
  279.     var change=false;
  280.  
  281.     if (jumpCycle) {
  282.         jumpCycle--;
  283.         change=true;
  284.         playerPosZ = 1 + jumpCycle*(20-jumpCycle)/110;
  285.     }
  286.     else if (key[4]) jumpCycle=20;
  287.    
  288.     if (key[0]) {
  289.         if (!key[1]) {
  290.             playerDir-=0.07; //left
  291.             change=true;
  292.         }
  293.     }
  294.     else if (key[1]) {
  295.         playerDir+=0.07; //right
  296.         change=true;
  297.     }
  298.  
  299.     if (change) {
  300.         playerDir+=2*pi;
  301.         playerDir%=2*pi;
  302.         document.getElementById("sky").style.backgroundPosition=Math.floor(1-playerDir/(2*pi)*2400)+"px 0";
  303.     }
  304.  
  305.     if (key[2] && !key[3]) {
  306.         if (playerVelY<0.1) playerVelY += 0.02;
  307.     }
  308.     else if (key[3] && !key[2]) {
  309.         if (playerVelY>-0.1) playerVelY -= 0.02;
  310.     }
  311.     else {
  312.         if (playerVelY<-0.02) playerVelY += 0.015;
  313.         else if (playerVelY>0.02) playerVelY -= 0.015;
  314.         else playerVelY=0;
  315.     }
  316.    
  317.    
  318.     if (playerVelY!=0) {
  319.  
  320.         var oldX=playerPos[0];;
  321.         var oldY=playerPos[1];     
  322.         var newX=oldX+Math.cos(playerDir)*playerVelY;
  323.         var newY=oldY+Math.sin(playerDir)*playerVelY;
  324.  
  325.         if (!nearWall(newX, oldY)) {
  326.             playerPos[0]=newX;
  327.             oldX=newX;
  328.             change=true;
  329.         }
  330.         if (!nearWall(oldX, newY)) {
  331.             playerPos[1]=newY;
  332.             change=true;
  333.         }
  334.  
  335.     }
  336.    
  337.     if (playerVelY) wobbleGun();
  338.     if (change) drawCanvas();
  339.  
  340. }
  341.  
  342.  
  343. function changeKey(which, to){
  344.     switch (which){
  345.         case 65: case 37: key[0]=to; break; // left
  346.         case 87: case 38: key[2]=to; break; // up
  347.         case 68: case 39: key[1]=to; break; // right
  348.         case 83: case 40: key[3]=to; break; // down
  349.         case 32: key[4]=to; break; // space bar;
  350.         case 17: key[5]=to; break; // ctrl
  351.         case 66: if (to) { shoot() } break; // b
  352.     }
  353. }
  354. document.onkeydown=function(e){changeKey((e||window.event).keyCode, 1);}
  355. document.onkeyup=function(e){changeKey((e||window.event).keyCode, 0);}
  356.  
  357.  
  358. function initUnderMap(){
  359.     var underMap=document.getElementById("underMap").getContext("2d");
  360.     underMap.fillStyle="#FFF";
  361.     underMap.fillRect(0,0, 200, 200);
  362.     underMap.fillStyle="#444";
  363.     for (var i=0; i<arena.length; i++) {
  364.         for (var j=0; j<arena[i].length; j++) {
  365.             if (arena[i][j]) underMap.fillRect(i*8, j*8, 8, 8);
  366.         }  
  367.     }
  368. }
  369.  
  370.  
  371. window.onload=function(){
  372.     var ele = document.getElementById("map");
  373.     if (!ele.getContext)
  374.     {
  375.       alert('An error occured creating a Canvas 2D context. This may be because you are using an old browser, if not please contact me and I\'ll see if I can fix the error.');
  376.       return;
  377.     }
  378.     map=ele.getContext("2d");
  379.     canvas=document.getElementById("canvas").getContext("2d");
  380.     overlay=document.getElementById("overlay");
  381.     document.getElementById("sky").style.backgroundPosition=Math.floor(-playerDir/(2*pi)*2400)+"px 0";
  382.     drawCanvas();
  383.     initUnderMap();
  384.     setInterval(update, 35);
  385. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement