diff --git a/sti/init.lua b/sti/init.lua index 5650c0d..83d2dbb 100644 --- a/sti/init.lua +++ b/sti/init.lua @@ -157,9 +157,13 @@ function Map:loadPlugins(plugins) local pluginModulePath = cwd .. 'plugins.' .. plugin local ok, pluginModule = pcall(require, pluginModulePath) if ok then - for k, func in pairs(pluginModule) do - if not self[k] then - self[k] = func + if pluginModule.loadStyle == "callable" then + pluginModule(self) + else + for k, func in pairs(pluginModule) do + if not self[k] then + self[k] = func + end end end end diff --git a/sti/plugins/tile_collections.lua b/sti/plugins/tile_collections.lua new file mode 100755 index 0000000..11d5e08 --- /dev/null +++ b/sti/plugins/tile_collections.lua @@ -0,0 +1,45 @@ +--- plugin to support tile collections for STI +-- @module tile_collections.lua +-- @author Anirudh Katoch (spopo | katoch.anirudh at gmail.com) +-- @copyright 2020 +-- @license MIT/X11 + +local tile_collections = { + tile_collections_LICENSE = "MIT/X11", + tile_collections_URL = "https://github.com/karai17/Simple-Tiled-Implementation", + tile_collections_VERSION = "0.0.0.001", + tile_collections_DESCRIPTION = "(Partially?) Supporting tile collections for STI.", + + loadStyle = "callable", + + --- Adds support for TileCollections to the map + hook = function(thismodule, map) + local newTilesets = {} + -- We're gonna hack around and convert all the tiles in a tile collections into individual tilesets + -- Usually people use tile collections for images with different sizes which are drawn sparsely (and not as uniform tiles) + -- We are not optimizing for performance here. Any optimization can be done in future versions of this plugin, and may even be unneeded + for _, tileset in ipairs(map.tilesets) do + if not tileset.image then + --This is a tileset defined by a collection of images + local mt = {__index = tileset} + for _, tile in ipairs(tileset.tiles) do + table.insert(newTilesets, setmetatable({ + originalTileset = tileset, + columns = 1, + tilewidth = tile.width, + tileheight = tile.height, + imagewidth = tile.width, + imageheight = tile.height, + firstgid = tileset.firstgid+tile.id, + image = tile.image + }, mt)) + end + else + table.insert(newTilesets, tileset) + end + end + map.tilesets = newTilesets + end +} + +return setmetatable(tile_collections, {__call = tile_collections.hook}) \ No newline at end of file diff --git a/tutorials/01-introduction-to-sti.md b/tutorials/01-introduction-to-sti.md index a4bd53f..848a1f2 100644 --- a/tutorials/01-introduction-to-sti.md +++ b/tutorials/01-introduction-to-sti.md @@ -230,7 +230,7 @@ function love.load() end ``` -Cool, we can now move our player around the screen! But that's only half the battle. We also want to centre the player in the screen so that the player never runs away from us. Instead, we want the world to move around our player. How do we accomplish this? LÖVE provides graphics transform tools such as `translate`, `rotate`, and `scale` that will give us the illusion that our player is static and the world is dynamic, instead of the other way around. +Cool, we can now move our player around the screen! But that's only half the battle. We also want to centre the player in the screen so that the player never runs away from us. Instead, we want the world to move around our player. How do we accomplish this? STI Map object's draw method lets us pass the translation and scale factors as parameters. ```lua function love.draw() @@ -239,11 +239,8 @@ function love.draw() local tx = math.floor(player.x - love.graphics.getWidth() / 2) local ty = math.floor(player.y - love.graphics.getHeight() / 2) - -- Transform world - love.graphics.translate(-tx, -ty) - -- Draw world - map:draw() + map:draw(-tx, -ty) end ``` @@ -263,12 +260,8 @@ function love.draw() local tx = math.floor(player.x - screen_width / 2) local ty = math.floor(player.y - screen_height / 2) - -- Transform world - love.graphics.scale(scale) - love.graphics.translate(-tx, -ty) - -- Draw world - map:draw() + map:draw(-tx, -ty, scale, scale) end ``` @@ -371,12 +364,8 @@ function love.draw() local tx = math.floor(player.x - screen_width / 2) local ty = math.floor(player.y - screen_height / 2) - -- Transform world - love.graphics.scale(scale) - love.graphics.translate(-tx, -ty) - -- Draw world - map:draw() + map:draw(-tx, -ty, scale, scale) end ```