Advertisement
Guest User

Untitled

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