Sprite Z ordering is easy. You just sort the objects table by its Y value.
If I made the game, and believe me, I've wanted to make something like the old Konami side-scrolling TMNT/Simpsons arcade games, every character and object on screen would be an actor or scenery. In my Adventure game project I use this method. All enemies, scenery, dropped items and the player, etc, are dumped into a table each frame, and that table is then sorted by its Y value using code like this:
Code: Select all
function sort(T) table.sort(T, function(a, b) return a.y < b.y end ) end
table.sort(T) where T is the table name of the table you put all your objects in. This will sort them in ascending order so they get drawn in the correct order. a.y < b.y is the key part here as it sorts the first one before the second one. If you reversed them you'd draw the objects in the wrong order.
Can't help you with the collisions. Well, I can. But it might be different in every game. There's an active thread with a collision function out there somewhere. This is the code I use for finding if two rectangles collide:
Code: Select all
function overlap(x1,y1,w1,h1,x2,y2,w2,h2)
local tl, bl, tr, br = false, false, false, false
if (x2 >= x1 and x2 <= (x1 + w1)) and (y2 >= y1 and y2 <= (y1 + h1)) then tl = true end
if (x2+w2 >= x1 and x2+w2 <= (x1 + w1)) and (y2 >= y1 and y2 <= (y1 + h1)) then tr = true end
if (x2 >= x1 and x2 <= (x1 + w1)) and (y2+h2 >= y1 and y2+h2 <= (y1 + h1)) then bl = true end
if (x2+w2 >= x1 and x2+w2 <= (x1 + w1)) and (y2+h2 >= y1 and y2+h2 <= (y1 + h1)) then br = true end
if (x1 >= x2 and x1 <= (x2 + w2)) and (y1 >= y2 and y1 <= (y2 + h2)) then tl = true end
if (x1+w1 >= x2 and x1+w1 <= (x2 + w2)) and (y1 >= y2 and y1 <= (y2 + h2)) then tr = true end
if (x1 >= x2 and x1 <= (x2 + w2)) and (y1+h1 >= y2 and y1+h1 <= (y2 + h2)) then bl = true end
if (x1+w1 >= x2 and x1+w1 <= (x2 + w2)) and (y1+h1 >= y2 and y1+h1 <= (y2 + h2)) then br = true end
if tl or tr or bl or br then return true else return false end
end
Yes, there might be a more compact way to do it, if so, let me know. As long as it takes the same parameters and returns the same values, I don't care what the function looks like.
Where x, y, w and h are the location and size of each rectangle. Where a rectangle would be a fist and an enemy. 1 would be the fist, 2 would be the enemy. And you would check against each enemy to see if your fist (Or foot) collided. Then take the appropriate action.
This function would also be used for picking up items by checking against every dropped item on screen to see if you walked over it.