Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local blockwidth = 25
- local blockheight = 15
- local map_width = math.floor( 800 /blockwidth)
- local map_height = math.floor( 400 /blockheight)
- pattrns = {
- [1] = {
- {0,0,0,0,0},
- {0,1,1,1,0},
- {0,1,0,1,0},
- {0,1,0,1,0},
- {0,1,1,1,0},
- {0,0,0,0,0},
- },
- [2] = {
- {0,0,0,0,0},
- {0,1,1,0,0},
- {0,0,1,0,0},
- {0,0,1,0,0},
- {0,1,1,1,0},
- {0,0,0,0,0},
- },
- [3] = {
- {0,0,0,0,0},
- {0,1,1,1,0},
- {0,0,0,1,0},
- {0,1,1,1,0},
- {0,1,0,0,0},
- {0,1,1,1,0},
- {0,0,0,0,0},
- },
- }
- wood = {
- type = 0,
- width = 40,
- height = 40,
- foreground = false,
- friction = 0.3,
- restitution = 0.2,
- angle = 0,
- miceCollision = true,
- groundCollision = true,
- dynamic = false,
- fixedRotation = false,
- mass = 0,
- linearDamping = 0,
- angularDamping = 0,
- }
- xml = [[<C><P /><Z><S><S L="840" o="324aa0" H="28" X="409" Y="413" T="12" P="0,0,0.3,0.2,0,0,0,0" /></S><D /><O /></Z></C>]]
- function new_map( width, height, patterns, seed)
- local width = width
- local height = height
- local diverse = diverse
- local pnum = #patterns
- local seed = seed or tonumber(math.random(0,99999)..math.random(0,99999))
- local tiles = {}
- local used = {}
- math.randomseed(seed)
- local function pattern(pattern, x, y)
- for Y,v1 in ipairs(pattern) do
- for X,v in ipairs(v1) do
- if X+x-1>height then
- return false
- elseif Y+y-1>width then
- return false
- end
- end
- end
- for Y,v1 in ipairs(pattern) do
- for X,v in ipairs(v1) do
- if type(tiles[X+x]) == "table" then
- tiles[X+x-1][Y+y-1] = v
- used[X+x-1][Y+y-1] = true
- end
- end
- end
- end
- for x = 0, height do
- tiles[x] = {}
- used[x] = {}
- for y = 0, width do
- used[x][y] = false
- end
- end
- for y = 0, width do
- for x = 0, height do
- if not used[x][y] then
- local pat = math.random(1,pnum)
- pattern(patterns[pat], x, y)
- end
- end
- end
- math.randomseed(tonumber(string.reverse(seed)))
- return tiles
- end
- function load_map(tiles, x, y, w, h, prot)
- local prot = prot or wood
- local x = x or 0
- local y = y or 0
- local tiles = type(tiles)=="table" and tiles or error("table expected at #1, got "..type(tiles))
- local w = w or 40
- local h = h or 40
- prot.width = w
- prot.height = h
- id = 1
- for x,gab in pairs(tiles) do
- for y,val in pairs(gab) do
- if val ~= 0 then
- local w2 = w/2
- local h2 = h/2
- local cx = x*w + w2
- local cy = y*h + h2
- tfm.exec.addPhysicObject(id , cx, cy, prot)
- id = id + 1
- end
- end
- if (x+1)%6 == 0 then
- coroutine.yield(x, y)
- end
- end
- end
- tfm.exec.newGame(xml)
- local map = new_map(map_height, map_width, pattrns)
- co = coroutine.create(load_map)
- function eventLoop(np, lp)
- coroutine.resume(co, map, 0, 0, blockwidth, blockheight, wood)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement