-- Oh, lua... -- But seriously, use this for associative tables -- Use the # operator for normal 'array-like' tables -- Sigh. function countTableSize(table) local n = 0 for k, v in pairs(table) do n = n + 1 end return n end function shallowcopy(orig) local orig_type = type(orig) local copy if orig_type == 'table' then copy = {} for orig_key, orig_value in pairs(orig) do copy[orig_key] = orig_value end else -- number, string, boolean, etc copy = orig end return copy end function performWithDelay (delay, func, repeats, arg) local timer = MOAITimer.new() timer:setSpan( delay/1000 ) timer:setListener( MOAITimer.EVENT_TIMER_END_SPAN, function () if timer ~= nil then timer:stop() else print("performWithDelay with cleared timer completed with delay of " .. tostring(delay) ) end timer = nil func( arg ) if repeats then if repeats > 1 then performWithDelay( delay, func, repeats - 1, unpack( arg ) ) elseif repeats == 0 then performWithDelay( delay, func, 0, unpack( arg ) ) end end end ) timer:start() return timer end -- Actual game logic code function worldSpaceToBlockSpace( x, y, xOffset, yOffset ) xOffset = xOffset or 0 yOffset = yOffset or 0 local blockLocX = math.ceil( ((x - GRID_OFFSET_X) / TILE_SIZE) + xOffset ) local blockLocY = math.ceil( ((y - GRID_OFFSET_Y) / TILE_SIZE) + yOffset ) return blockLocX, blockLocY end function getBlock( x, y, xOffset, yOffset ) xOffset = xOffset or 0 yOffset = yOffset or 0 local blockLocX, blockLocY = worldSpaceToBlockSpace( x, y, xOffset, yOffset ) if blockLocX >= 0 and blockLocX < BLOCKS_GRID_WIDTH and blockLocY >= 0 and blockLocY < BLOCKS_GRID_HEIGHT then return currentBlocks[blockLocY][blockLocX] else return nil end end