Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions sti/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions sti/plugins/tile_collections.lua
Original file line number Diff line number Diff line change
@@ -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})
19 changes: 4 additions & 15 deletions tutorials/01-introduction-to-sti.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
```

Expand All @@ -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
```

Expand Down Expand Up @@ -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
```

Expand Down