Guest User

Untitled

a guest
Jun 23rd, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. function IsoRenderer(TW, OFFSETX, OFFSETY) {
  2. this.tilesets = {};
  3. this.TW = TW;
  4. this.MODs = {};
  5. this.OFFSETX = OFFSETX; this.OFFSETY = OFFSETY;
  6. this.TWs = {};
  7. this.ctx = null;
  8. //this.TWBs = {};
  9. this.setCtx = function(ctx) {
  10. this.ctx = ctx;
  11. }
  12.  
  13. this.isoToScreen = function(x, y) {
  14. var TW = this.TW;
  15. var posX = (x - y) * (TW / 2);
  16. var posY = (x + y) * (TW / 4);
  17.  
  18. return [posX, posY];
  19. }
  20.  
  21. this.screenToIso = function(posX, posY, round)
  22. {
  23. var TW = this.TW;
  24. posX -= this.OFFSETX;
  25. posY -= this.OFFSETY;
  26. //posY -= TW/4;
  27. var x = (posX / (TW / 2) + posY / (TW / 4))/2;
  28. var y = posY / (TW / 4) - x;
  29.  
  30. if (round)
  31. return [Math.round(x), Math.round(y)];
  32. else
  33. return [x, y];
  34. }
  35.  
  36. this.getMousePos = function(canvas, evt) {
  37. var rect = canvas.getBoundingClientRect();
  38. return {
  39. x: evt.clientX - rect.left,
  40. y: evt.clientY - rect.top
  41. };
  42. }
  43.  
  44. this.loadTileset = function(name, url, TW, MOD, onLoad) {
  45. var self = this;
  46.  
  47. var img = new Image();
  48. img.addEventListener("load", function(){
  49. self.addTileset(name, img, TW, MOD);
  50. onLoad();
  51. });
  52. img.src = url;
  53. }
  54.  
  55. this.addTileset = function(name, image, TW, MOD) {
  56. this.tilesets[name] = image;
  57. this.TWs[name] = TW;
  58. this.MODs[name] = MOD;
  59. //this.TWBs[name] = TWB;
  60. }
  61.  
  62. this.drawTile = function(name, i, x,y, offX, offY) {
  63. var pos = this.isoToScreen(x,y);
  64. var M = this.MODs[name];
  65. var my = Math.floor(i / M), mx = i % M;
  66. var TWB = this.TWs[name];
  67. offX += this.OFFSETX;
  68. offY += this.OFFSETY;
  69.  
  70. this.ctx.drawImage(
  71. this.tilesets[name],
  72. mx*TWB, my*TWB, TWB, TWB,
  73. offX+ pos[0]-(this.TW/2), offY + pos[1]-(this.TW/2),
  74. TWB, TWB
  75. );
  76. }
  77.  
  78. this.drawLevel = function(text, x,y, offX, offY, highlight) {
  79. var pos = this.isoToScreen(x,y);
  80. var TW = this.TW;
  81. offX += this.OFFSETX;
  82. offY += this.OFFSETY;
  83.  
  84. this.ctx.strokeStyle = "black";
  85. if (highlight)
  86. this.ctx.fillStyle = '#C9C9FF';
  87. else
  88. this.ctx.fillStyle = 'white';
  89.  
  90. this.ctx.beginPath();
  91. this.ctx.arc(offX+pos[0], offY+pos[1], 9, 0,2*Math.PI);
  92. this.ctx.stroke();
  93. this.ctx.fill();
  94.  
  95. this.ctx.font = "bold 12px Arial";
  96. this.ctx.lineWidth = 1;
  97. this.ctx.textBaseline = 'middle';
  98. this.ctx.textAlign = "center";
  99.  
  100. this.ctx.fillStyle = 'black';
  101. this.ctx.fillText(text, offX+ pos[0], offY + pos[1]);
  102. }
  103.  
  104. this.drawText = function(text, x,y, offX, offY, color, color2) {
  105. var pos = this.isoToScreen(x,y);
  106. var TW = this.TW;
  107. offX += this.OFFSETX;
  108. offY += this.OFFSETY;
  109.  
  110. this.ctx.font = "bold 14px Arial";
  111. this.ctx.lineWidth = 1;
  112. this.ctx.textBaseline = 'middle';
  113. this.ctx.textAlign = "center";
  114.  
  115. if (color2) {
  116. this.ctx.strokeStyle = color2;
  117. this.ctx.strokeText(text, offX+ pos[0], offY + pos[1]);
  118. }
  119. this.ctx.fillStyle = color;
  120. this.ctx.fillText(text, offX+ pos[0], offY + pos[1]);
  121. }
  122.  
  123. this.drawWire = function(color, x,y, offX, offY, block) {
  124. offX += this.OFFSETX;
  125. offY += this.OFFSETY;
  126. var pos = this.isoToScreen(x,y);
  127. var Hx = this.TW/2, Hy = this.TW/4;
  128. var Cx = offX+pos[0], Cy = offY+pos[1];
  129.  
  130. this.ctx.beginPath();
  131. this.ctx.moveTo(Cx, Cy-Hy);
  132. this.ctx.lineTo(Cx+Hx, Cy);
  133. this.ctx.lineTo(Cx, Cy+Hy);
  134. this.ctx.lineTo(Cx-Hx, Cy);
  135. this.ctx.lineTo(Cx, Cy-Hy);
  136.  
  137. if (block) {
  138. this.ctx.fillStyle = color;
  139. this.ctx.fill();
  140. } else {
  141. this.ctx.strokeStyle = color;
  142. this.ctx.stroke();
  143. }
  144. }
  145.  
  146. this.renderIsoFormat = function(map) {
  147. var W = map.meta.width, H = map.meta.height,
  148. T = map.meta.contentType || 'map';
  149.  
  150. if (map.meta.wireframe) {
  151. var wire = map.meta.wireframe;
  152. for(var y=0; y<H; y++) for(var x=0;x<W;x++) {
  153. this.drawWire(wire.color||'red', x,y,wire.offsetX||0,wire.offsetY||0, wire.block||false);
  154. }
  155. }
  156.  
  157. for(var y=0; y<H; y++) for(var x=0;x<W;x++) {
  158. var tile = T == 'map' ? map.content[x+','+y] : map.content[y][x];
  159.  
  160. if (typeof tile !== 'undefined' && tile !== null) {
  161. if (Array.isArray(tile)) {
  162. for(var ltile of tile) {
  163. ltile = ltile.split('/');
  164. var type = ltile[0], id = parseInt(ltile[1]);
  165. this.drawTile(type, id, x,y,0,0);
  166. }
  167. } else {
  168. tile = tile.split('/');
  169. var type = tile[0], id = parseInt(tile[1]);
  170. this.drawTile(type, id, x,y,0,0);
  171. }
  172. }
  173. }
  174. }
  175. }
Add Comment
Please, Sign In to add comment