Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var player, turns, waiting = false;
  2. var otherLasts = [];
  3.    
  4. /**
  5. * * Game State Comments
  6. * TODO: Left Off Here
  7. * ? Replace This At a Later Time
  8. * ! Section
  9. */
  10.  
  11. //! ==================================================================================================================
  12.  
  13. // BUTTONS
  14.  
  15. // Lock In Ships
  16.  
  17. socket.on('start-turn', data => {
  18.     player.isGo = true;
  19.     waiting = false;
  20.     console.log("Start Turn Packet : " + data);
  21. });
  22.  
  23. socket.on('shot-result', data => {
  24.     let otherPlayerShot = [];
  25.  
  26.     otherPlayerShot.push(data.x); otherPlayerShot.push(data.y);
  27.     otherLasts.push(otherPlayerShot);
  28.     console.log("Shot Result Packet : " + data);
  29. });
  30.  
  31. socket.on('game-over', data => {
  32.     console.log("Game Over Packer : " + data);
  33.     if(data.winner == false) {
  34.         console.log('You lost, loser. lol. gottem. and btw the game was ' +data.turns+ ' long.');
  35.     }
  36.     else {
  37.         console.log('gj, you won. i guess... btw the game was ' +data.turns+ ' long.');
  38.     }
  39. });
  40.  
  41. lockInBtn = new Clickable();
  42. lockInBtn.text = 'Lock In!'; lockInBtn.locate(575, 225);  
  43.  
  44. lockInBtn.onPress = function() {
  45.     player.lockedIn = true;
  46.     turns = 0;
  47.    
  48.     //? Send server the board packet
  49.     socket.emit('ships-placed', {board: board.board, id:gameId})
  50.     console.log("Sending Ships Placed Packet")
  51.     wating = true;
  52. }
  53. lockInBtn.onHover = function() {this.color = '#b30000';}; lockInBtn.onOutside = function() {this.color = '#070707';}
  54.  
  55. // Fire Shot
  56.  
  57. shotBtn = new Clickable();
  58. shotBtn.text = 'FIRE!'; shotBtn.locate(575, 225); shotBtn.textSize = 40;
  59.  
  60. shotBtn.onPress = function(){
  61.     turns += 1;
  62.  
  63.     player.shot = [hitSquare.x-17, hitSquare.y];
  64.     player.playerLasts.push(player.shot);
  65.  
  66.     console.log('Turn ' +turns+ ', you shot here --> ' + (player.shot[0]+1) + ',' + (player.shot[1]+1));
  67.  
  68.     //? Send server the shot packet
  69.     socket.emit('fire-shot', {x:player.shot[0], y:player.shot[1], id:gameId})
  70.     console.log("Sending fire shot packet!")
  71.     waiting = true;
  72.    
  73.     player.isGo = false;
  74. }
  75. shotBtn.onHover = function() {this.color = '#b30000';}; shotBtn.onOutside = function() {this.color = '#070707';}
  76.  
  77.  
  78. //! ==================================================================================================================
  79.  
  80. function setup() {
  81.     // Create the canvas and display grid
  82.     createCanvas(1350, 500);
  83.     board = new Gameboard(10, 500);
  84.     board.create();
  85.  
  86.     // Initialise player object
  87.     player = new Player();
  88.  
  89.     // Create a square for use when firing shots
  90.     hitSquare = new hitSquare(20, 5);
  91. }
  92.  
  93. //! ==================================================================================================================
  94.  
  95. function draw() {
  96.     //* DRAW BOARD
  97.     // Draw background (eventually an image) and display grid visuals
  98.     background('#080808');
  99.     board.display();
  100.  
  101.    
  102.    
  103.     //* GAME STATE 0 - Waiting for Other Player
  104.     // Show text 'Waiting...' with animation
  105.  
  106.  
  107.  
  108.     //* GAME STATE 1 - Placing Ships
  109.  
  110.     // Display the ships
  111.     for(let i=0; i<player.shipArray.length; i++) {
  112.         player.shipArray[i].display();
  113.      
  114.         // If they being clicked on, drag them!
  115.         if (player.shipArray[i].drag == true) {
  116.             player.shipArray[i].dragging();
  117.         }  
  118.     }
  119.    
  120.     // If all the ships are in valid places display the button that allows the user to lock in there ships
  121.     if(player.allPlaced == true && player.lockedIn == false) {    
  122.         lockInBtn.draw();
  123.     }
  124.    
  125.     // If the player has locked in their ships, display a waiting function until I'm told the game starts
  126.     //? When I recieve 'YOUR GO' packet, player.isGo = true;
  127.    
  128.    
  129.  
  130.    
  131.     //* GAME STATE 2 - Firing Shot
  132.  
  133.     // If the shot square is up, display it, will be replaced with  
  134.     if(player.isGo == true) {
  135.         hitSquare.display();
  136.         if(hitSquare.moving == true) {
  137.             hitSquare.move();
  138.         }
  139.         else {
  140.             shotBtn.draw();
  141.         }
  142.     }
  143.  
  144.  
  145.     //* GAME STATE 3 - Recieving Shot
  146.     // Get the shot from the other player and print it onto my canvas
  147.     //? When I recieve 'SHOT' packet...
  148.    
  149.  
  150.     if(otherLasts.length > 0) {
  151.         displayX(otherLasts, 0);
  152.     }
  153.  
  154.     if(player.playerLasts.length > 0) {
  155.         displayX(player.playerLasts, 17*board.tileSize);
  156.     }
  157.  
  158.     //* GAME STATE 4 - Ending Game
  159.     // End the game when I get the finishing packet
  160.    
  161. }
  162.  
  163. //! ==================================================================================================================
  164.  
  165. // Display previous turn results
  166. function displayX(array, offset) {
  167.     for(let i=0; i<array.length; i++ ){
  168.         stroke('#ffffff'); strokeWeight(3);
  169.         line(array[i][0]*board.tileSize +offset, array[i][1]*board.tileSize, (array[i][0]+1)*board.tileSize +offset, (array[i][1]+1)*board.tileSize);
  170.         line(array[i][0]*board.tileSize +offset, (array[i][1]+1)*board.tileSize, (array[i][0]+1)*board.tileSize +offset, array[i][1]*board.tileSize );
  171.     }
  172. }
  173.    
  174. //! ==================================================================================================================
  175.  
  176. function mousePressed() {
  177.     if(player.lockedIn == false) {
  178.         for(let i=0; i<player.shipArray.length; i++) {
  179.             player.shipArray[i].click();
  180.         }
  181.     }
  182.    
  183.     if(player.lockedIn == true && player.isGo == true) {
  184.         if(hitSquare.moving == false) {
  185.             if(mouseX < width && mouseX > width-500 && mouseY < height && mouseY > 0) {
  186.                 hitSquare.moving = true;
  187.             }
  188.         }
  189.         else {
  190.             hitSquare.moving = false;
  191.         }
  192.     }
  193. }
  194.  
  195. function mouseReleased() {
  196.     player.allPlaced = true;
  197.    
  198.     for(let i=0; i<player.shipArray.length; i++) {
  199.         if(player.shipArray[i].drag == true) {
  200.             player.shipArray[i].snap();
  201.         }
  202.        
  203.         if(player.shipArray[i].placed == false) {
  204.             player.allPlaced = false;
  205.         }  
  206.     }  
  207.    
  208.     board.updateGameArray();
  209.    
  210. }
  211.  
  212. function keyReleased() {
  213.     for(let i=0; i<player.shipArray.length; i++) {
  214.         if (player.shipArray[i].drag == true) {
  215.             if(keyCode === 32) {
  216.                 player.shipArray[i].rotateShip();
  217.             }
  218.         }
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement