Guest User

Untitled

a guest
May 23rd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. var mapArray = [
  2. [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
  3. [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
  4. [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
  5. [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
  6. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  7. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0],
  8. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0],
  9. [0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
  10. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  11. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  12. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  13. [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
  14. [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
  15. [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
  16. [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
  17. ];
  18. function isPositionWall(ptX, ptY) {
  19. var gridX = Math.floor(ptX / 36);
  20. var gridY = Math.floor(ptY / 36);
  21. if(mapArray[gridX][gridY] == 1)
  22. return true;
  23. }
  24.  
  25. var boatPosX = canvas.height/2 - 50;
  26. var boatPosY = canvas.height/2 - 50;
  27.  
  28. function render(viewport) {
  29. context.save();
  30. context.translate(view.x, view.y);
  31. requestAnimationFrame(render);
  32. var oldPosX = boatPosX;
  33. var oldPosY = boatPosY;
  34.  
  35.  
  36. for (let i = 0; i < mapArray.length; i++) {
  37. for (let j = 0; j < mapArray[i].length; j++) {
  38. if (mapArray[j][i] == 0) {
  39. this.sprite.draw(
  40. background,
  41. 190,
  42. 230,
  43. 26,
  44. 26,
  45. i * this.sprite.width,
  46. j * this.sprite.height,
  47. this.sprite.width,
  48. this.sprite.height
  49. );
  50. }
  51. if (mapArray[j][i] == 1) {
  52. this.sprite.draw(
  53. background,
  54. 30,
  55. 30,
  56. 26,
  57. 26,
  58. i * this.sprite.width,
  59. j * this.sprite.height,
  60. this.sprite.width,
  61. this.sprite.height
  62. );
  63.  
  64. }
  65. if (mapArray[j][i] == 2) {
  66. this.sprite.draw(
  67. background,
  68. 200,
  69. 20,
  70. 26,
  71. 26,
  72. i * this.sprite.width,
  73. j * this.sprite.height,
  74. this.sprite.width,
  75. this.sprite.height
  76. );
  77. }
  78. }
  79. }
  80. this.ship.drawimage(boat, boatPosX, boatPosY, 50, 50);
  81.  
  82. if(isPositionWall(boatPosX, boatPosY)) {
  83. //boatPosX = oldPosY;
  84. console.log("collision");
  85. }
  86. console.log(mapArray[Math.floor(boatPosX / 36)][Math.floor(boatPosX / 36)]);
  87. context.restore();
  88.  
  89. };
  90.  
  91. function move(e) {
  92. if (e.keyCode == 39) {
  93. boatPosX += 5;
  94. view.x -= 5
  95. moveCount++;
  96. console.log(moveCount);
  97. console.log("right");
  98. }
Add Comment
Please, Sign In to add comment