Yeah. couple of notes (I dont really know the state of everything so please take these notes with limited value):
- Beware physics gaps (I think you can fatten the collider detection a bit). I noticed many times I ‘slipped through’ the colliders and fell to my doom
- Not sure using string.format in get_key is a good idea - this is called alot. And it essentially generates a new string everytime. While not an issue atm, might be worth a simple refactor. (suggestion below)
- The map Vs tile mask calc is a little tricky because I used a simple fixed square image for dumping the tilemap on. I tried using the WIDTH and HEIGHT but in the render tile section, I often got values outside that (which was weird - again, I might be misunderstanding a whole heap). Would recommend testing this a bit. It happened a couple of times where all the tiles changed too! Kinda freaked me out, I thought I had done something wrong, but I noticed your render tiles iterates a different table (viewport) than the tile pool being maintained. Not sure if this is a possible source of that (ie the two getting out of sync).
Otherwise its all very very cool what you have done. And actually, I love the premise. Has hints of the old Boulderdash (on C64)
With regards to the key lookup. If you really want to use say a combo id for a tile X/Y pair you could use a little of bit ops fun
Consider X and Y have a maximum size of a 16 bit value (65535). You could then simply combine them into an int. So the upper and lower portions of the int have the X+Y.
If you only use positive numbers. Its as simple as:
key = Y * 0xFFFF + X – this is same as Y * 65536 + X , I just like using HEX notation (old habits)
If you want to still use negative tile values, it still works, you just need to make sure the sign bit is set on each when you set them and then extract them. This can be done by using bit op library like so:
local cleanY = bit.lshift( bit.band(Y, 0xFFFF), 16) -- cleans the value for use and shifts to top half of int.
local cleanX = bit.band(X, 0xFFFF)
local key = bit.bor( Y, X ) -- Merges the number into a single key.
-- For extraction you do the reverse
local Xval = bit. band( key, 0xFFFF) -- Fetches just the lower 16bits of the key
local Yval = bit.band( bit.rshift( key, 16 ), 0xFFFF ) -- Shifts the key 16 bits right then grabs it like X.
There are a bazillion other ways you can do this sort of thing too. Even just multiplying the Y by 1000 and adding the X. Then opposite to extract. Assuming your X and Y are less than 1000
Anyway. If you have any q’s. Happy to help.