Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Canvas board game
  2. // Global variables
  3. var ctx = null;
  4. var tileW = 100, tileH = 100;
  5. var mapW = 10, mapH = 10;
  6.  
  7. var tileMap = [
  8.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  9.     0, 0, 2, 1, 3, 1, 2, 1, 0, 0,
  10.     0, 0, 1, 2, 1, 2, 3, 2, 0, 0,
  11.     0, 0, 2, 1, 3, 1, 2, 1, 0, 0,
  12.     0, 0, 1, 2, 1, 2, 1, 2, 0, 0,
  13.     0, 0, 3, 1, 2, 1, 3, 1, 0, 0,
  14.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  15.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  16.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  17. ];
  18.  
  19. // Left andright positions of beads, traps and text
  20. var tiles = [
  21.     {left: 220, bottom: 715},
  22.     {left: 320, bottom: 715},
  23.     {left: 420, bottom: 715, data: {type: "TRAP", move: "BACKWARDS", steps: 1, instruction: "Move back 1 step.", text: "A camp of evil thieves lays ahead of you, they are too many to confront and you decide to walk around them."}},
  24.     {left: 520, bottom: 715},
  25.     {left: 620, bottom: 715},
  26.     {left: 720, bottom: 715},
  27.     {left: 720, bottom: 615},
  28.     {left: 620, bottom: 615, data: {type: "TRAP", move: "FORWARDS", steps: 3, instruction: "Move forwards 3 steps.", text: "A friendly looking dragon is ahead of you and you decide to hitch a ride."}},
  29.     {left: 520, bottom: 615},
  30.     {left: 420, bottom: 615},
  31.     {left: 320, bottom: 615},
  32.     {left: 220, bottom: 615},
  33.     {left: 220, bottom: 515},
  34.     {left: 320, bottom: 515},
  35.     {left: 420, bottom: 515, data: {type: 'TRAP', move: "GAMBLE", instruction: "Roll 2, 4 or 6 to move forward or roll 1, 3 and 5 to move backwards.", text: "You are robbed of your travel supplies and continuing the journey may be dangerous. You decide to gamble on continuing the journey(roll: 2, 4, 6) or go back to get new supplies(roll: 1, 3, 5)."}},
  36.     {left: 520, bottom: 515},
  37.     {left: 620, bottom: 515},
  38.     {left: 720, bottom: 515},
  39.     {left: 720, bottom: 415},
  40.     {left: 620, bottom: 415},
  41.     {left: 520, bottom: 415},
  42.     {left: 420, bottom: 415},
  43.     {left: 320, bottom: 415},
  44.     {left: 220, bottom: 415},
  45.     {left: 220, bottom: 315, data: {type: 'TRAP', move: "BACKWARDS", steps: 3, instruction: "Move backwards 3 steps.", text: "A group of nightwalkers see you an you have to run!"}},
  46.     {left: 320, bottom: 315},
  47.     {left: 420, bottom: 315},
  48.     {left: 520, bottom: 315},
  49.     {left: 620, bottom: 315, data: {type: 'TRAP', move: "ROLLHIGH" , steps: 1, instruction: "Roll a 3 or above to win or move backwards 1 step.", text: "A war is being faught and you have to assist."}},
  50.     {left: 720, bottom: 315}
  51. ];
  52.  
  53. // Draw canvas when window has loaded
  54. window.onload = function() {
  55.     ctx = document.querySelector(".canvas").getContext("2d");
  56.     this.requestAnimationFrame(drawGame);
  57.     ctx.font = 'bold 16px "Open Sans", sans-serif';
  58. };
  59.  
  60. function drawGame() {
  61.     if(ctx == null) {
  62.         alert("Your browser does not support HTML canvas. Upgrade it to play the game.");
  63.         return;
  64.     }
  65.  
  66.     for(var y = 0; y < mapH; y++) {
  67.         for(var x = 0; x < mapW; x++) {
  68.  
  69.             switch(tileMap[((y*mapW)+x)]) {
  70.                 case 1:
  71.                     ctx.fillStyle = "#FFFFFF";
  72.                     break;
  73.                 case 2:
  74.                     ctx.fillStyle = "#5A6C7A";
  75.                     break;  
  76.                 case 3:
  77.                     ctx.fillStyle = "#D3A52F";
  78.                     break;
  79.                 default:
  80.                     ctx.fillStyle = "#FFFFFF";
  81.             }
  82.             ctx.fillRect(x*tileW, y*tileH, tileW, tileH);
  83.         }
  84.     }
  85.  
  86.     // Draw board border
  87.     ctx.beginPath();
  88.     ctx.rect(200, 100, 600, 500);
  89.     ctx.lineWidth = "2";
  90.     ctx.strokeStyle = "#5A6C7A";
  91.     ctx.stroke();
  92.  
  93.     // Draw board seperation lines
  94.     ctx.beginPath();
  95.     ctx.moveTo(200, 204);
  96.     ctx.lineTo(700, 204);
  97.  
  98.     ctx.moveTo(300, 304);
  99.     ctx.lineTo(800, 304);
  100.  
  101.     ctx.moveTo(200, 404);
  102.     ctx.lineTo(700, 404);
  103.  
  104.     ctx.moveTo(300, 504);
  105.     ctx.lineTo(800, 504);
  106.  
  107.     ctx.lineWidth = "8";
  108.     ctx.strokeStyle = "#000000";
  109.     ctx.stroke();
  110.  
  111.     // Draw images
  112.     var throneImg = new Image();
  113.     var warningImg = new Image();
  114.  
  115.     throneImg.onload = function (){
  116.         ctx.drawImage(throneImg, 640, 620);
  117.  
  118.         ctx.drawImage(warningImg, 427, 125);
  119.         ctx.drawImage(warningImg, 627, 225);
  120.         ctx.drawImage(warningImg, 427, 325);
  121.         ctx.drawImage(warningImg, 627, 525);
  122.         ctx.drawImage(warningImg, 227, 525);
  123.     }
  124.     throneImg.src = "images/iron_throne_small.png";
  125.     warningImg.src = "images/warning.png";
  126.  
  127.     // Draw text
  128.     ctx.fillStyle = "#000000";
  129.     ctx.fillText("GOAL", 727, 525);
  130.     ctx.fillText("Who can reach the Iron Throne first?", 300, 700);
  131.     ctx.fillText("Watch out for the yellow tiles, danger may be lurking!", 200, 70);
  132.     ctx.fillText("If you roll a 6, you can roll again to avoid them!", 200, 90);
  133.  
  134.     ctx.fillStyle = "#FFFFFF";
  135.     ctx.fillText("START", 223, 120);
  136. };
  137.  
  138. // Function to spin dice on click
  139. (function(){
  140.     var div = document.querySelector('.dice-container');
  141.     var dice = document.querySelector('#dice');
  142.     var open = false;
  143.    
  144.     div.addEventListener('click', function(){
  145.       if(open){
  146.         dice.className = 'icon-dice';  
  147.       } else{
  148.         dice.className = 'icon-dice rotate';
  149.       }
  150.       open = !open;
  151.     });
  152. })();
  153.  
  154. // Function to generate a number from 1-6
  155. function rollDice() {
  156.     var playerRoll = document.querySelector(".player-roll");
  157.     var roll = 2 /*Math.floor(Math.random() * 6) + 1;*/
  158.     playerRoll.innerHTML = "You rolled a " + roll + ".";
  159.    
  160.     return roll;
  161. };
  162.  
  163. // Move bead based on roll
  164. // ...code
  165. var currentPosition = 0; // starting positon
  166. var button = document.getElementById("dice");
  167.  
  168. var playerTurnContainer = document.querySelector(".player-turn");
  169. var playerTurn = null;
  170. var player1 = localStorage.getItem("player1");
  171. var player2 = localStorage.getItem("player2");
  172. var bead = document.querySelector(".bead1");
  173. playerTurnContainer.innerHTML = player1;
  174.  
  175. // Display bead based on chosen players
  176. player1Img = document.querySelector(".bead1");
  177. player2Img = document.querySelector(".bead2");
  178. var player1FN = player1.split(' ', 1)[0];
  179. var player2FN = player2.split(' ', 1)[0];
  180. player1Img.src = "images/" + player1FN + "_small.png";
  181. player2Img.src = "images/" + player2FN + "_small.png";
  182.  
  183. function beadPosition() {
  184.     if(bead == document.querySelector(".bead1")) {
  185.         bead.style.left = tiles[currentPosition].left + 'px';
  186.         bead.style.bottom = tiles[currentPosition].bottom + 'px';
  187.     } else if(bead == document.querySelector(".bead2")){
  188.         bead.style.left = tiles[currentPosition].left + 30 + 'px';
  189.         bead.style.bottom = tiles[currentPosition].bottom + 'px';
  190.     }
  191. };
  192.  
  193. function moveBead(amount) {
  194.     // Set current position
  195.     if (currentPosition + amount >= tiles.length) {
  196.         // not enough tiles left, go to last
  197.         currentPosition = tiles.length - 1;
  198.     } else {
  199.         currentPosition += amount;
  200.     }
  201.  
  202.     beadPosition();
  203.  
  204.     // If on last, win!
  205.     if (currentPosition === tiles.length - 1) {
  206.         setTimeout(() => alert('You win!'), 1); // Temporary
  207.  
  208.        
  209.         if(bead == player2) {
  210.             var playerTurn = player2;
  211.         } else {
  212.             var playerTurn = player1;
  213.         } // Where to place this
  214.  
  215.         // localStorage.removeItem("player1");
  216.         // localStorage.removeItem("player2");
  217.         localStorage.setItem("winner", playerTurn); // Add winner to local storage for finale page
  218.         // Disabled for testing: When a bead lands on the last tile go to window.location = "file:///F:/School_Noroff/2019_Year2_Semester1/2020-01-27_Semester-Project2_Lisa-Ingemann/WF/Web/finale.html";
  219.     }
  220. };
  221.  
  222. button.addEventListener("click", (e) => {
  223.     var roll = rollDice();
  224.     console.log("roll " + roll);
  225.  
  226.     moveBead(roll);
  227.     console.log("Position " + currentPosition);
  228.  
  229.     // TRAPS
  230.     var trapContainer = document.querySelector(".message-trap");
  231.     var trapInfo = document.querySelector(".text-trap");
  232.     var trapInstructions = document.querySelector(".instruction");
  233.     var trapH3 = document.querySelector(".message-trap h3");
  234.  
  235.     trapContainer.style.display = "none";
  236.  
  237.     if(roll == 6) { // If player rolls a 6 they get another roll and skip any traps
  238.         trapInfo.innerHTML = "You rolled a 6 and skip any traps and get to roll again.";
  239.         trapInstructions.innerHTML = "Roll again.";
  240.         trapH3.innerHTML = "You Rolled a 6";
  241.         trapContainer.style.display = "block";
  242.         return;
  243.     } else {
  244.         // Switch player. Set bead to player2
  245.  
  246.         /*
  247.         // Attempt at switching players turn
  248.         if(playerTurnContainer.innerHTML == player2) {
  249.             playerTurnContainer.innerHTML = player1;
  250.             playerTurn = player1;
  251.             var bead = player1Img;
  252.         } else if(playerTurnContainer.innerHTML == player1) {
  253.             playerTurnContainer.innerHTML = player2;
  254.             playerTurn = player2;
  255.             var bead = player2Img;
  256.         } */
  257.     } // I think traps needs to go in here too somehow?
  258.  
  259.     // Traps go into esle statement if 6 is rolled conditonal - to avoid the traps
  260.     // If bead lands on yellow tile, activate trap
  261.     if(tiles[currentPosition].data == undefined){
  262.         trapContainer.style.display = "none";
  263.     } else if(tiles[currentPosition].data.type == "TRAP") {
  264.         // Display trap message and instructions if bead has landed on a trap
  265.         trapContainer.style.display = "block";
  266.         trapInfo.innerHTML = tiles[currentPosition].data.text;
  267.         trapInstructions.innerHTML = tiles[currentPosition].data.instruction;
  268.     }
  269. });
  270.  
  271. var trapOkBtn = document.querySelector(".accept-trap");
  272. trapOkBtn.addEventListener("click", (e) => {
  273.     if(tiles[currentPosition].data.move == "BACKWARDS") {
  274.         var backwards = tiles[currentPosition].data.steps;
  275.  
  276.         currentPosition -= backwards;
  277.  
  278.         beadPosition();
  279.  
  280.     } else if(tiles[currentPosition].data.move == "FORWARDS") {
  281.         var forwards = tiles[currentPosition].data.steps;
  282.  
  283.         currentPosition += forwards;
  284.  
  285.         beadPosition();
  286.  
  287.     } else if(tiles[currentPosition].data.move == "GAMBLE") {
  288.         // Wait for current player to roll again;
  289.         var roll = rollDice();
  290.         if((roll == 2) || (roll == 4) || (roll == 6)) {
  291.             if (currentPosition + roll >= tiles.length) {
  292.                 // not enough tiles left, go to last
  293.                 currentPosition = tiles.length - 1;
  294.             } else {
  295.                 currentPosition += roll;
  296.             }
  297.  
  298.             beadPosition();
  299.  
  300.         } else {
  301.             if (currentPosition + roll >= tiles.length) {
  302.                 // not enough tiles left, go to last
  303.                 currentPosition = tiles.length - 1;
  304.             } else {
  305.                 currentPosition -= roll;
  306.             }
  307.  
  308.             beadPosition();
  309.         }
  310.  
  311.     } else if(tiles[currentPosition].data.move == "ROLLHIGH") {
  312.         var roll = rollDice(); // Wait for user to roll
  313.         if(roll >= 3) {
  314.             currentPosition = tiles.length - 1;
  315.  
  316.             beadPosition();
  317.             setTimeout(() => alert('You win!'), 1);
  318.                    
  319.             // localStorage.removeItem("player1");
  320.             // localStorage.removeItem("player2");
  321.             localStorage.setItem("winner", playerTurn); // Set the bead that won. Help
  322.             // When a bead lands on the last tile go to window.location = "file:///F:/School_Noroff/2019_Year2_Semester1/2020-01-27_Semester-Project2_Lisa-Ingemann/WF/Web/finale.html";
  323.                
  324.         } else {
  325.             var backwards = tiles[currentPosition].data.steps;
  326.  
  327.             currentPosition -= roll;
  328.  
  329.             beadPosition();
  330.         }
  331.     }
  332. }); // Event listener to process traps
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement