Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.93 KB | None | 0 0
  1. <!DOCTYPE html>
  2.  
  3. <html>
  4. <head>
  5. <title>Minecraft Map</title>
  6. <style>
  7. canvas{
  8. border: 1px solid rgb(150,150,150);
  9. }
  10. </style>
  11. <script type="text/javascript">
  12. var cvs, ctx;
  13. var t = [];
  14. var mousePos = null;
  15. var player = null;
  16.  
  17. var tla = [];
  18. var staticMeshes = [];
  19.  
  20. var mapa = [];
  21. var TilesTlo = [];
  22.  
  23. var TilesLantern = [];
  24.  
  25. var W = 64 , H = 64;
  26. var COL =8 , ROW = 8;
  27.  
  28. function getMousePos(evt){
  29. var rect = cvs.getBoundingClientRect();
  30. return{
  31. x: evt.clientX - rect.left,
  32. y: evt.clientY - rect.top
  33. };
  34. }
  35. function CreateMapa(){
  36. mapa.push([1,1,1,1, 1,1,1,1]);
  37. mapa.push([1,0,0,0, 0,0,0,1]);
  38. mapa.push([1,0,0,0, 0,0,0,1]);
  39. mapa.push([1,0,0,0, 0,0,0,0]);
  40. mapa.push([1,0,0,0, 0,0,0,0]);
  41. mapa.push([1,0,0,0, 0,0,0,1]);
  42. mapa.push([1,0,0,0, 0,0,0,1]);
  43. mapa.push([1,1,1,1, 1,1,1,1]);
  44. }
  45.  
  46. function CreateMapaLanterns(){
  47. MapaLanterns.push([0,0,0,0, 0,0,0,0]);
  48. MapaLanterns.push([0,0,0,0, 0,0,0,0]);
  49. MapaLanterns.push([0,0,0,0, 0,0,1,0]);
  50. MapaLanterns.push([0,0,0,0, 0,0,0,0]);
  51. MapaLanterns.push([0,0,0,0, 0,0,0,0]);
  52. MapaLanterns.push([0,0,0,0, 0,0,1,0]);
  53. MapaLanterns.push([0,0,0,0, 0,0,0,0]);
  54. MapaLanterns.push([0,0,0,0, 0,0,0,0]);
  55. }
  56.  
  57. function CreateTilesTlo(){
  58. for(var r = 0; r < ROW; r++){
  59. TilesTlo[r] = [];
  60. for(var c = 0; c < COL; c++){
  61. if(mapa[r][c] === 1){
  62. TilesTlo[r][c] = new Tile(c, r, W, H, tla[0]);
  63. }else{
  64. TilesTlo[r][c] = new Tile(c, r, W, H, null);
  65. }
  66. }
  67. }
  68. }
  69.  
  70.  
  71. function Init(){
  72. tla.push(document.getElementById('img1'));
  73. staticMeshes.push(document.getElementById('lantern'));
  74. CreateMapa();
  75. CreateTilesTlo();
  76.  
  77. cvs = document.getElementById("cvs");
  78. ctx = cvs.getContext("2d");
  79.  
  80. t.push(new Pawn(100,100,W,H));
  81. t.push(new Pawn(200,200,W,H));
  82. t.push(new Pawn(300,300,W,H));
  83. t.push(new Pawn(400,400,W,H));
  84.  
  85. player = new Pawn(0,0,W,H, tla[0]);
  86.  
  87. }
  88. function BoundingRect (p_x, p_y, p_w, p_h,p_img){
  89. this.x = p_x;
  90. this.y = p_y;
  91. this.w = p_w;
  92. this.h = p_h;
  93. this.ToString = function (){
  94. return "{" + this.x + ";" + this.y + ";" + this.w + ";" + this.h + "}";
  95. };
  96. }
  97.  
  98. function Pawn(p_x,p_y,p_w,p_h,p_img){
  99. this.x = p_x;
  100. this.y = p_y;
  101. this.w = p_w;
  102. this.h = p_h;
  103. this.img = p_img;
  104. this.boundingRect = new BoundingRect(
  105. this.x - this.w / 2,
  106. this.y - this.h / 2,
  107. this.w, this.h
  108. );
  109. this.UpdateCoords = function (p_x, p_y){
  110. this.x = p_x
  111. this.y = p_y
  112. this.boundingRect.x = this.x - this.w / 2;
  113. this.boundingRect.y = this.y - this.h / 2;
  114. };
  115. }
  116.  
  117.  
  118. function Tile(p_x,p_y,p_w,p_h,p_img){
  119. this.x = p_x;
  120. this.y = p_y;
  121. this.w = p_w;
  122. this.h = p_h;
  123. this.img = p_img;
  124. this.boundingRect = new BoundingRect(
  125. this.x - this.w / 2,
  126. this.y - this.h / 2,
  127. this.w, this.h
  128. );
  129.  
  130. }
  131.  
  132. function update(evt){
  133. mousePos = getMousePos(evt);
  134. }
  135.  
  136. function Draw(){
  137. ctx.clearRect(0,0,cvs.width, cvs.height);
  138.  
  139. document.getElementById("d1").innerHTML = t.length;
  140.  
  141. for (var r = 0; r < TilesTlo.length; r++){
  142. for(var c = 0; c < TilesTlo[r].length; c++){
  143. if(TilesTlo[r][c].img !== null){
  144. ctx.drawImage(TilesTlo[r][c].img,
  145. TilesTlo[r][c].x * TilesTlo[r][c].w,
  146. TilesTlo[r][c].y * TilesTlo[r][c].h,
  147. TilesTlo[r][c].w,
  148. TilesTlo[r][c].h);
  149. }
  150. }
  151. }
  152.  
  153. player.UpdateCoords(mousePos.x,mousePos.y);
  154.  
  155. ctx.strokeStyle = "rgb(0,0,255)";
  156. ctx.strokeRect(
  157. player.boundingRect.x,
  158. player.boundingRect.y,
  159. player.boundingRect.w,
  160. player.boundingRect.h
  161. );
  162. ctx.drawImage(player.img,
  163. player.boundingRect.x,
  164. player.boundingRect.y,
  165. player.boundingRect.w,
  166. player.boundingRect.h)
  167. }
  168. </script>
  169. </head>
  170. <body onload="Init(); Draw();">
  171. <img id="img1" src="stonebrick.png" width="0" height="0"/>
  172. <img id="lantern" src="lantern.png" width="0" height=="0"/>
  173. <canvas id="cvs" width="512" height="512"
  174. onmousemove="update(event); Draw();">
  175. Browser does not support canvas...
  176. </canvas>
  177. <div id="d1"> </div>
  178.  
  179. </body>
  180. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement