Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Sphere's own tilemap engine is confusing so I wrote my own.
- This is a simplfied version of the script.
- */
- const tilePath = "textures/tilesets/";
- var tileset = function(source, width, height)
- {
- this.path = tilePath + source;
- this.tile = {};
- this.tile.width = width;
- this.tile.height = height;
- this.Area = {};
- this.Area.width = Math.ceil(GetScreenWidth() / width);
- this.Area.height = Math.ceil(GetScreenHeight() / height);
- this.atlas = [];
- };
- tileset.prototype = {};
- // Unpacks a tileset
- tileset.prototype.unpack = function()
- {
- // Begin unpacking the tileset.
- Print("Unpacking tilset \"" + this.path + "\"");
- var source = LoadImage(this.path);
- source.blit(0,0);
- var tile = 0;
- for (var y = 0; y < source.height / this.tile.height; y++)
- {
- for (var x = 0; x < source.width / this.tile.width; x++)
- {
- var cache = GrabSurface(
- x * this.tile.width,
- y * this.tile.height,
- this.tile.width,
- this.tile.height
- );
- var tilename = "tile_" + x + "-" + y;
- this.atlas.push([cache, tilename, 0]);
- tile += 1;
- Print(" Unpacked texture " + this.atlas.length + " - " + tilename);
- };
- };
- // Generate sample tile.
- Print(" Generating Test tile...");
- var cache = CreateSurface(
- this.tile.width,
- this.tile.height,
- CreateColor(0, 0, 0, 0)
- );
- var color = CreateColor(255, 125, 125);
- cache.line(0, 0, this.tile.width, 0, color);
- cache.line(this.tile.width - 1, 0, this.tile.width - 1, this.tile.height, color);
- cache.line(this.tile.width, this.tile.height - 1, 0, this.tile.height - 1, color);
- cache.line(0, this.tile.height - 1, 0, 0, color);
- this.atlas.unshift([cache, "tile_test", 0]);
- Print(" ...Done.");
- Print("Unpacking complete.");
- //Clear no-longer used memory :3
- GarbageCollect();
- };
- // Aligns a tile on the y axis.
- tileset.prototype.align = function(id, val)
- {
- this.atlas[id][2] = val;
- };
- // Blits a tile to the screen.
- tileset.prototype.blit = function(id, pX, pY)
- {
- this.atlas[id][0].blit(pX * this.tile.width, pY * this.tile.height, 0.5);
- };
- // Gets the tileset width.
- tileset.prototype.width = function()
- {
- return this.tile.width;
- };
- // Gets the tileset height.
- tileset.prototype.height = function()
- {
- return this.tile.height;
- };
- // Gets the width of the renderable area.
- tileset.prototype.spaceWidth = function()
- {
- return this.Area.width;
- }
- // Gets the height of the renderable area.
- tileset.prototype.spaceHeight = function()
- {
- return this.Area.height;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement