Advertisement
Guest User

Untitled

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