Guest User

Javascript faulty code

a guest
Jul 7th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Untitled Document</title>
  5. </head>
  6.  
  7. <body>
  8. <canvas id="canvas" width="600" height="600"></canvas>
  9. <script src="https://code.jquery.com/jquery-2.1.0.js"></script>
  10. <script>
  11. var canvas = document.getElementById("canvas");
  12. var ctx = canvas.getContext("2d");
  13. var canvasWidth = canvas.width;
  14. var canvasHeight = canvas.height;
  15.  
  16. Math.seed = 87965089;
  17. // in order to work 'Math.seed' must NOT be undefined,
  18. // so in any case, you HAVE to provide a Math.seed
  19. Math.seededRandom = function() {
  20.     max = 18;
  21.     min = 1;
  22.  
  23.     Math.seed = (Math.seed * 9301 + 49297) % 233280;
  24.     var rnd = Math.seed / 233280;
  25.  
  26.     return Math.round(min + rnd * (max - min));
  27. };
  28.  
  29.  
  30.  
  31. var TerrainGenerator = function() {
  32.     this.y = 560;
  33.     this.x = 700;
  34.     this.height = 40;
  35.     this.randomDigit = Math.seededRandom();
  36.     if (this.randomDigit > 0) {
  37.         this.biome = 1;
  38.     }
  39. };
  40.  
  41. TerrainGenerator.prototype.Create = function() {
  42.     if (this.x <= 700) {
  43.         //select biome
  44.         if (this.biome == 1) {
  45.             //select a landscape to build
  46.             this.randomDigit = Math.seededRandom();
  47.             if (this.randomDigit > 0 && this.randomDigit < 19) {
  48.                 //select hill height (between 3 and 4 atm)
  49.                 this.randomDigit = Math.seededRandom();
  50.                 if (this.randomDigit == 1 || this.randomDigit == 2 || this.randomDigit < 19) {
  51.                     var hillHeight = 4;
  52.                     console.log()
  53.                     //create hill with a height of 4
  54.                     this.randomDigit = Math.seededRandom();
  55.                     var firstHillBlockWidth = this.randomDigit * 40 * 2;
  56.                     var hillIndent = Math.floor(firstHillBlockWidth / hillHeight / 2);
  57.                     var secondHillBlockWidth = firstHillBlockWidth - hillIndent * 2;
  58.                     this.randomDigit = Math.seededRandom();
  59.                     if (this.randomDigit > 9) {
  60.                         terrainTiles.push(new MissileShooter(this.x + hillIndent - 5,this.y - this.height + 10));
  61.                     }
  62.                     var thirdHillBlockWidth = secondHillBlockWidth - hillIndent * 2;
  63.                     this.randomDigit = Math.seededRandom();
  64.                     if (this.randomDigit > 9) {
  65.                         terrainTiles.push(new MissileShooter(this.x + (hillIndent * 2) - 5,this.y - this.height * 2 + 10));
  66.                     }
  67.                     var fourthHillBlockWidth = thirdHillBlockWidth - hillIndent * 2;
  68.                     this.randomDigit = Math.seededRandom();
  69.                     if (this.randomDigit > 9) {
  70.                         terrainTiles.push(new MissileShooter(this.x + (hillIndent * 3) - 5,this.y - this.height * 3 + 10));
  71.                     }
  72.                     terrainTiles.push(new Terrain(this.x,this.y,firstHillBlockWidth,40));
  73.                     terrainTiles.push(new Terrain(this.x + hillIndent,this.y - this.height,secondHillBlockWidth,40));
  74.                     terrainTiles.push(new Terrain(this.x + hillIndent * 2,this.y - this.height * 2,thirdHillBlockWidth,40));
  75.                     terrainTiles.push(new Terrain(this.x + hillIndent * 3,this.y - this.height * 3,fourthHillBlockWidth,40));
  76.                     this.x += firstHillBlockWidth;
  77.                 } else { if (this.randomDigit == 3 || this.randomDigit == 4) {
  78.                     var hillHeight = 7;
  79.                     this.randomDigit = Math.seededRandom();
  80.                     var firstHillBlockWidth = this.randomDigit * 40 * 2;
  81.                     var hillIndent = Math.floor(firstHillBlockWidth / hillHeight / 2);
  82.                     var secondHillBlockWidth = firstHillBlockWidth - hillIndent * 2;
  83.                     var thirdHillBlockWidth = secondHillBlockWidth - hillIndent * 2;
  84.                     var fourthHillBlockWidth = thirdHillBlockWidth - hillIndent * 2;
  85.                     var fithHillBlockWidth = fourthHillBlockWidth - hillIndent * 2;
  86.                     var sixthHillBlockWidth = fithHillBlockWidth - hillIndent * 2;
  87.                     var seventhHillBlockWidth = sixthHillBlockWidth - hillIndent * 2;
  88.                     terrainTiles.push(new Terrain(this.x,this.y,firstHillBlockWidth,40));
  89.                     terrainTiles.push(new Terrain(this.x + hillIndent,this.y - this.height,secondHillBlockWidth,40));
  90.                     terrainTiles.push(new Terrain(this.x + hillIndent * 2,this.y - this.height * 2,thirdHillBlockWidth,40));
  91.                     terrainTiles.push(new Terrain(this.x + hillIndent * 3,this.y - this.height * 3,fourthHillBlockWidth,40));
  92.                     terrainTiles.push(new Terrain(this.x + hillIndent * 4,this.y - this.height * 4,fithHillBlockWidth,40));
  93.                     terrainTiles.push(new Terrain(this.x + hillIndent * 5,this.y - this.height * 5,sixthHillBlockWidth,40));
  94.                     terrainTiles.push(new Terrain(this.x + hillIndent * 6,this.y - this.height * 6,seventhHillBlockWidth,40));
  95.                     this.x += firstHillBlockWidth;
  96.  
  97.                 } else { if (this.randomDigit == 5 || this.randomDigit == 6) {
  98.                     //create a hill with a height of 3
  99.                     var hillHeight = 3;
  100.                     this.randomDigit = Math.seededRandom();
  101.                     var firstHillBlockWidth = this.randomDigit * 40;
  102.                     var hillIndent = Math.floor(firstHillBlockWidth / hillHeight / 2);
  103.                     var secondHillBlockWidth = firstHillBlockWidth - hillIndent * 2;
  104.                     var thirdHillBlockWidth = secondHillBlockWidth - hillIndent * 2;
  105.                     terrainTiles.push(new Terrain(this.x,                 this.y,                  firstHillBlockWidth,40));
  106.                     terrainTiles.push(new Terrain(this.x + hillIndent,    this.y - this.height,    secondHillBlockWidth,40));
  107.                     terrainTiles.push(new Terrain(this.x + hillIndent * 2,this.y - this.height * 2,thirdHillBlockWidth,40));
  108.                     console.log(firstHillBlockWidth);
  109.                     this.x += firstHillBlockWidth;
  110.                 } else { if (this.randomDigit == 7 || this.randomDigit == 8) {
  111.                     var hillHeight = 6;
  112.                     this.randomDigit = Math.seededRandom();
  113.                     var firstHillBlockWidth = this.randomDigit * 40 * 2;
  114.                     var hillIndent = Math.floor(firstHillBlockWidth / hillHeight / 2);
  115.                     var secondHillBlockWidth = firstHillBlockWidth - hillIndent * 2;
  116.                     var thirdHillBlockWidth = secondHillBlockWidth - hillIndent * 2;
  117.                     var fourthHillBlockWidth = thirdHillBlockWidth - hillIndent * 2;
  118.                     var fithHillBlockWidth = fourthHillBlockWidth - hillIndent * 2;
  119.                     var sixthHillBlockWidth = fithHillBlockWidth - hillIndent * 2;
  120.                     terrainTiles.push(new Terrain(this.x,this.y,firstHillBlockWidth,40));
  121.                     terrainTiles.push(new Terrain(this.x + hillIndent,this.y - this.height,secondHillBlockWidth,40));
  122.                     terrainTiles.push(new Terrain(this.x + hillIndent * 2,this.y - this.height * 2,thirdHillBlockWidth,40));
  123.                     terrainTiles.push(new Terrain(this.x + hillIndent * 3,this.y - this.height * 3,fourthHillBlockWidth,40));
  124.                     terrainTiles.push(new Terrain(this.x + hillIndent * 4,this.y - this.height * 4,fithHillBlockWidth,40));
  125.                     terrainTiles.push(new Terrain(this.x + hillIndent * 5,this.y - this.height * 5,sixthHillBlockWidth,40));
  126.                     this.x += firstHillBlockWidth;
  127.                 } else { if (this.randomDigit == 9 || this.randomDigit == 10) {
  128.                     var hillHeight = 5;
  129.                     this.randomDigit = Math.seededRandom();
  130.                     var firstHillBlockWidth = this.randomDigit * 40 * 2;
  131.                     var hillIndent = Math.floor(firstHillBlockWidth / hillHeight / 2);
  132.                     var secondHillBlockWidth = firstHillBlockWidth - hillIndent * 2;
  133.                     var thirdHillBlockWidth = secondHillBlockWidth - hillIndent * 2;
  134.                     var fourthHillBlockWidth = thirdHillBlockWidth - hillIndent * 2;
  135.                     var fithHillBlockWidth = fourthHillBlockWidth - hillIndent * 2;
  136.                     var sixthHillBlockWidth = fithHillBlockWidth - hillIndent * 2;
  137.                     terrainTiles.push(new Terrain(this.x,this.y,firstHillBlockWidth,40));
  138.                     terrainTiles.push(new Terrain(this.x + hillIndent,this.y - this.height,secondHillBlockWidth,40));
  139.                     terrainTiles.push(new Terrain(this.x + hillIndent * 2,this.y - this.height * 2,thirdHillBlockWidth,40));
  140.                     terrainTiles.push(new Terrain(this.x + hillIndent * 3,this.y - this.height * 3,fourthHillBlockWidth,40));
  141.                     terrainTiles.push(new Terrain(this.x + hillIndent * 4,this.y - this.height * 4,fithHillBlockWidth,40));
  142.                     this.x += firstHillBlockWidth;
  143.                 } else { if (this.randomDigit == 11 || this.randomDigit == 12) {
  144.                     var hillHeight = 2;
  145.                     this.randomDigit = Math.seededRandom();
  146.                     var firstHillBlockWidth = this.randomDigit * 40 * 2;
  147.                     var hillIndent = Math.floor(firstHillBlockWidth / hillHeight / 2);
  148.                     var secondHillBlockWidth = firstHillBlockWidth - hillIndent * 2;
  149.                     terrainTiles.push(new Terrain(this.x,this.y,firstHillBlockWidth,40));
  150.                     terrainTiles.push(new Terrain(this.x + hillIndent,this.y - this.height,secondHillBlockWidth,40));
  151.                 } else {
  152.                     var hillHeight = 1;
  153.                     this.randomDigit = Math.seededRandom();
  154.                     var firstHillBlockWidth = this.randomDigit * 40 * 4;
  155.                     var hillIndent = Math.floor(firstHillBlockWidth / hillHeight / 2);
  156.                     terrainTiles.push(new Terrain(this.x,this.y,firstHillBlockWidth,40));
  157.                 }
  158.                 }
  159.                 }
  160.  
  161.                 }
  162.             }
  163.             }
  164.  
  165.             } else {
  166.                 //create a plains
  167.                 this.randomDigit = Math.seededRandom();
  168.                 //make size of plains
  169.                 var plainsSize = Math.floor((this.randomDigit / 2 )* 40);
  170.                 //create the base of plains
  171.                 terrainTiles.push(new Terrain(this.x,this.y,plainsSize,40));
  172.                 this.randomDigit = Math.seededRandom();
  173.                 //decide where the first ' bump will be'
  174.                 var firstIndent = Math.floor((this.randomDigit / 4) * 40);
  175.                 this.randomDigit = Math.seededRandom();
  176.                 //decide how wide the bump is and create it
  177.                 var blockWidth = Math.floor((this.randomDigit / 4) * 40);
  178.                 terrainTiles.push(new Terrain(this.x + firstIndent,this.y - this.height,blockWidth,40));
  179.                 this.randomDigit = Math.seededRandom();
  180.                 console.log('blockWidth = ' + blockWidth);
  181.                 //decide where the second bump will be
  182.                 var secondIndent = this.x + firstIndent + blockWidth + Math.floor(this.randomDigit / 4) * 40;
  183.                 this.randomDigit = Math.seededRandom();
  184.                 //decide how wide the second bump will be and draw it
  185.                 var blockWidth = Math.floor((this.randomDigit / 4) * 40);
  186.                 terrainTiles.push(new Terrain(this.x + secondIndent,this.y - this.height,blockWidth,40));
  187.                 console.log('blockWidth = ' + blockWidth);
  188.                 this.x += plainsSize;
  189.             }
  190.         }
  191.     }
  192. };
  193.  
  194. var Missiles = function (x,y) {
  195.     this.x = x;
  196.     this.y = y;
  197.     this.width = 30;
  198.     this.height = 20;
  199.     this.speed = 5;
  200.     this.deathSequence = false
  201. };
  202.  
  203. Missiles.prototype.Draw = function () {
  204.     if (this.x >= -200) {
  205.         ctx.beginPath();
  206.         ctx.fillStyle = "Black";
  207.         ctx.rect(this.x,this.y,this.width,this.height);
  208.         ctx.stroke();
  209.         ctx.fill();
  210.     } else {
  211.         missileCount.splice(this,1);
  212.         console.log('removed a missile')
  213.     }
  214. };
  215.  
  216. Missiles.prototype.Move = function () {
  217.     if (this.deathSequence) {
  218.         this.y += this.speed;
  219.     } else {
  220.         this.x -= this.speed;
  221.     }
  222. //  if (direction == 'left') {
  223. //      this.x -= this.speed;
  224. //  }
  225. };
  226.  
  227. Missiles.prototype.Collisions = function () {
  228.     for (var i = 0; i < terrainTiles.length; i++) {
  229.         if (this.x < terrainTiles[i].x + terrainTiles[i].width && this.x > terrainTiles[i].x + terrainTiles[i].width / 2) {
  230.             if (this.y < terrainTiles[i].y + terrainTiles[i].height && this.y + this.height > terrainTiles[i].y) {
  231.                 this.deathSequence = true;
  232.             }
  233.         }
  234.     }
  235. };
  236.  
  237. var MissileShooter = function (x,y) {
  238.     this.x = x;
  239.     this.y = y;
  240.     this.width = 20;
  241.     this.height = 20;
  242.     this.direction = undefined;
  243.     this.type = 'spawner';
  244. };
  245.  
  246. MissileShooter.prototype.Draw = function () {
  247.     ctx.beginPath();
  248.     ctx.fillStyle = "Grey";
  249.     ctx.rect(this.x,this.y,this.width,this.height);
  250.     ctx.stroke();
  251.     ctx.fill();
  252. };
  253.  
  254. MissileShooter.prototype.Spawn = function () {
  255.     if(~~(Math.random() * 50) == 3) {
  256.         missileCount.push(new Missiles(this.x,this.y))
  257.     }
  258. };
  259.  
  260.  
  261.  
  262. var Terrain = function (x,y,width,height) {
  263.     this.x = x;
  264.     this.y = y;
  265.     this.width = width;
  266.     this.height = height;
  267.     this.type = 'static Terrain';
  268.     this.imageSize = 40;
  269.     this.img = new Image();
  270.     this.img.src = "Assets/textures/grass.png";
  271. };
  272.  
  273. Terrain.prototype.Draw = function () {
  274.     if (this.x < - 1500) {
  275.         terrainTiles.shift();
  276.     }
  277.     ctx.drawImage(this.img,0,0,this.width,this.height,this.x,this.y,this.width,this.height);
  278. };
  279.  
  280. var Player = function (X,Y,Width,Height) {
  281.     this.x = X;
  282.     this.y = Y;
  283.     this.width = Width;
  284.     this.height = Height;
  285.     this.speed = 8;
  286.     this.upMove = false;
  287.     this.downMove = false;
  288.     this.leftMove = false;
  289.     this.rightMove = false;
  290.     this.jumpSpeed = 0;
  291.     this.jumplock = false;
  292. };
  293.  
  294. Player.prototype.Draw = function () {
  295.     ctx.beginPath();
  296.     ctx.fillStyle = "Red";
  297.     ctx.rect(this.x,this.y,this.width,this.height);
  298.     ctx.stroke();
  299.     ctx.fill();
  300. };
  301.  
  302. Player.prototype.Move = function (direction,type) {
  303.     if (type === 'keydown') {
  304.         if (direction === 'w') {
  305.             this.upMove = true;
  306.         } else {
  307.             if (direction === 's') {
  308.                 this.downMove = true;
  309.             } else {
  310.                 if (direction === 'a') {
  311.                     this.leftMove = true;
  312.                 } else {
  313.                     if (direction === 'd') {
  314.                         this.rightMove = true;
  315.                     }
  316.                 }
  317.             }
  318.         }
  319.     }
  320.     if (type === 'keyup') {
  321.         if (direction === 'w') {
  322.             this.upMove = false;
  323.         } else {
  324.             if (direction === 's') {
  325.                 this.downMove = false;
  326.             } else {
  327.                 if (direction === 'a') {
  328.                     this.leftMove = false;
  329.                 } else {
  330.                     if (direction === 'd') {
  331.                         this.rightMove = false;
  332.                     }
  333.                 }
  334.             }
  335.         }
  336.     }
  337. };
  338.  
  339. Player.prototype.MoveUpdater = function () {
  340.     if (this.upMove == true) {
  341.         if (this.jumplock == false) {
  342.             this.jumpSpeed = 23;
  343.             //this.jumplock = true;
  344.         }
  345.     }
  346.     this.y -= this.jumpSpeed;
  347.     if (this.jumpSpeed > 0) {
  348.         this.jumpSpeed -= 1;
  349.     }
  350.     if (this.downMove == true) {
  351.         this.y += this.speed;
  352.     }
  353.     if (this.leftMove == true) {
  354.         for (var i = 0; i < terrainTiles.length; i++) {
  355.             terrainTiles[i].x += this.speed;
  356.         }
  357.         for (var i = 0; i < missileCount.length; i++) {
  358.             missileCount[i].x += this.speed;
  359.         }
  360.         terrainEngine.x += this.speed;
  361.  
  362.     }
  363.     if (this.rightMove == true) {
  364.         for (var i = 0; i < terrainTiles.length; i++) {
  365.             terrainTiles[i].x -= this.speed;
  366.         }
  367.         for (var i = 0; i < missileCount.length; i++) {
  368.             missileCount[i].x -= this.speed;
  369.         }
  370.         terrainEngine.x -= this.speed;
  371.     }
  372. };
  373.  
  374. Player.prototype.Collisions = function() {
  375.     for (var i = 0; i < terrainTiles.length; i++) {
  376.         //collisions with top face of terrain
  377.         if (this.y + this.height > terrainTiles[i].y && this.y + this.height < terrainTiles[i].y + terrainTiles[i].height / 2) {   
  378.             if (this.x < terrainTiles[i].x + terrainTiles[i].width && this.x + this.width > terrainTiles[i].x) {
  379.                 this.y = terrainTiles[i].y - this.height;
  380.                 this.jumplock = false;
  381.             }
  382.         }
  383.         //collisiosn with left face of terrain
  384.         if (this.x + this.width > terrainTiles[i].x && this.x + this.width < terrainTiles[i].x + terrainTiles[i].width / 2) {
  385.             if (this.y < terrainTiles[i].y + terrainTiles[i].height && this.y + this.height > terrainTiles[i].y) {
  386.                 var overstep = this.x + this.width - terrainTiles[i].x;
  387.                 for (var value = 0; value < terrainTiles.length; value++) {
  388.                     terrainTiles[value].x += overstep;
  389.                 }
  390.                 for (var value = 0; value < missileCount.length; value++) {
  391.                     missileCount[value].x += overstep;
  392.                 }
  393.             }
  394.         }
  395.  
  396.         //collisions with right face of terrain
  397.         if (this.x < terrainTiles[i].x + terrainTiles[i].width && this.x > terrainTiles[i].x + terrainTiles[i].width / 2) {
  398.             if (this.y < terrainTiles[i].y + terrainTiles[i].height && this.y + this.height > terrainTiles[i].y) {
  399.                 var overstep = terrainTiles[i].x + terrainTiles[i].width - this.x;
  400.                 for (var value = 0; value < terrainTiles.length; value++) {
  401.                     terrainTiles[value].x -= overstep;
  402.                 }
  403.                 for (var value = 0; value < missileCount.length; value++) {
  404.                     missileCount[value].x -= overstep;
  405.                 }
  406.             }
  407.         }
  408.     }
  409.     for (var i = 0; i < missileCount.length; i++) {
  410.         //top face collisions
  411.         if (this.y + this.height > missileCount[i].y && this.y + this.height < missileCount[i].y + missileCount[i].height / 2) {   
  412.             if (this.x < missileCount[i].x + missileCount[i].width && this.x + this.width > missileCount[i].x) {
  413.                 this.jumpSpeed = 19;
  414.                 this.jumplock = true;
  415.                 missileCount[i].deathSequence = true;
  416.             }
  417.         }
  418.         //left face collisions
  419.         if (this.x + this.width > missileCount[i].x && this.x + this.width < missileCount[i].x + missileCount[i].width / 2) {
  420.             if (this.y < missileCount[i].y + missileCount[i].height && this.y + this.height > missileCount[i].y) {
  421.                 console.log('GAME OVER!!!!!!');
  422.             }
  423.         }
  424.         //right face collisions
  425.         if (this.x < missileCount[i].x + missileCount[i].width && this.x > missileCount[i].x + missileCount[i].width / 2) {
  426.             if (this.y < missileCount[i].y + missileCount[i].height && this.y + this.height > missileCount[i].y) {
  427.                 console.log('GAMEOVER!!!!!!!');
  428.             }
  429.         }
  430.     }
  431. };
  432.  
  433. terrainTiles = [];
  434. missileCount = [];
  435. players = [];
  436. players.push(new Player(200,200,40,40));
  437. terrainTiles.push(new Terrain(0,560,700,40));
  438. terrainTiles.push(new Terrain(100,520,700,40));
  439. terrainTiles.push(new Terrain(310,480,200,40));
  440.  
  441. var keyActions = {
  442.             87: "w",
  443.             65: "a",
  444.             83: "s",
  445.             68: "d"
  446. };
  447.  
  448. $("body").keydown(function (event) {
  449.     var direction = keyActions[event.keyCode];
  450.     var type = 'keydown';
  451.     for (var i = 0; i < players.length; i++) {
  452.         players[i].Move(direction,type);
  453.     }
  454. });
  455.  
  456. $("body").keyup(function (event) {
  457.     var direction = keyActions[event.keyCode];
  458.     var type = 'keyup';
  459.     for (var i = 0; i < players.length; i++) {
  460.         players[i].Move(direction,type);
  461.     }
  462. });
  463.  
  464.  
  465.        
  466. terrainEngine = new TerrainGenerator();      
  467.  
  468. var gravity = 8;
  469.  
  470. setInterval(function() {
  471.    
  472.     ctx.clearRect(0,0,canvasWidth,canvasHeight);
  473.     ctx.strokeRect(0,0,canvasWidth,canvasHeight);
  474.     for (var i = 0; i < players.length; i++) {
  475.         players[i].MoveUpdater();
  476.         players[i].y += gravity;
  477.         players[i].Collisions();
  478.         players[i].Draw();
  479.     }
  480.     terrainEngine.Create();
  481.  
  482.  
  483.     for (var i = 0; i < missileCount.length; i++) {
  484.         missileCount[i].Move();
  485.         missileCount[i].Draw();
  486.     }
  487.  
  488.     for (var i = 0; i < terrainTiles.length; i++) {
  489.         terrainTiles[i].Draw();
  490.         if (terrainTiles[i].type == 'spawner') {
  491.             terrainTiles[i].Spawn();
  492.         }
  493.     }
  494.  
  495. }, 30);
  496.  
  497. </script>
  498. </body>
  499. </html>
Add Comment
Please, Sign In to add comment