Advertisement
Guest User

Untitled

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