Advertisement
Guest User

Untitled

a guest
May 26th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. /*Sphere's own tilemap engine is confusing so I wrote my own.
  2. This is a simplfied version of the script.
  3. */
  4.  
  5. const tilePath = "textures/tilesets/";
  6.  
  7. var tileset = function(source, width, height)
  8. {
  9. this.path = tilePath + source;
  10. this.tile = {};
  11. this.tile.width = width;
  12. this.tile.height = height;
  13.  
  14. this.Area = {};
  15. this.Area.width = Math.ceil(GetScreenWidth() / width);
  16. this.Area.height = Math.ceil(GetScreenHeight() / height);
  17.  
  18. this.atlas = [];
  19. };
  20.  
  21. tileset.prototype = {};
  22.  
  23. // Unpacks a tileset
  24. tileset.prototype.unpack = function()
  25. {
  26. // Begin unpacking the tileset.
  27. Print("Unpacking tilset \"" + this.path + "\"");
  28. var source = LoadImage(this.path);
  29. source.blit(0,0);
  30. var tile = 0;
  31. for (var y = 0; y < source.height / this.tile.height; y++)
  32. {
  33. for (var x = 0; x < source.width / this.tile.width; x++)
  34. {
  35. var cache = GrabSurface(
  36. x * this.tile.width,
  37. y * this.tile.height,
  38. this.tile.width,
  39. this.tile.height
  40. );
  41.  
  42. var tilename = "tile_" + x + "-" + y;
  43. this.atlas.push([cache, tilename, 0]);
  44.  
  45. tile += 1;
  46.  
  47. Print(" Unpacked texture " + this.atlas.length + " - " + tilename);
  48. };
  49. };
  50.  
  51. // Generate sample tile.
  52. Print(" Generating Test tile...");
  53. var cache = CreateSurface(
  54. this.tile.width,
  55. this.tile.height,
  56. CreateColor(0, 0, 0, 0)
  57. );
  58.  
  59. var color = CreateColor(255, 125, 125);
  60.  
  61. cache.line(0, 0, this.tile.width, 0, color);
  62. cache.line(this.tile.width - 1, 0, this.tile.width - 1, this.tile.height, color);
  63. cache.line(this.tile.width, this.tile.height - 1, 0, this.tile.height - 1, color);
  64. cache.line(0, this.tile.height - 1, 0, 0, color);
  65.  
  66. this.atlas.unshift([cache, "tile_test", 0]);
  67.  
  68. Print(" ...Done.");
  69. Print("Unpacking complete.");
  70.  
  71. //Clear no-longer used memory :3
  72. GarbageCollect();
  73. };
  74.  
  75. // Aligns a tile on the y axis.
  76. tileset.prototype.align = function(id, val)
  77. {
  78. this.atlas[id][2] = val;
  79. };
  80.  
  81. // Blits a tile to the screen.
  82. tileset.prototype.blit = function(id, pX, pY)
  83. {
  84. this.atlas[id][0].blit(pX * this.tile.width, pY * this.tile.height, 0.5);
  85. };
  86.  
  87. // Gets the tileset width.
  88. tileset.prototype.width = function()
  89. {
  90. return this.tile.width;
  91. };
  92.  
  93. // Gets the tileset height.
  94. tileset.prototype.height = function()
  95. {
  96. return this.tile.height;
  97. };
  98.  
  99. // Gets the width of the renderable area.
  100. tileset.prototype.spaceWidth = function()
  101. {
  102. return this.Area.width;
  103. }
  104.  
  105. // Gets the height of the renderable area.
  106. tileset.prototype.spaceHeight = function()
  107. {
  108. return this.Area.height;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement