简单的游戏地图生成器

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

-- Use this function to perform your initial setup
function setup()
    print("Simple Map Sample!!")
    textMode(CORNER)
    spriteMode(CORNER)
    
    gridCount = 20
    
    
    scaleX = 50
    scaleY = 50
    -- number > 4
    plantSeed = 20.0
    -- number > 3
    minerialSeed = 20.0
    
    --[[
    parameter.integer("scaleX",20,100,50)
    parameter.integer("scaleY",20,100,50)
    parameter.number("plantSeed",5.0,100.0,10.0)
    parameter.number("minerialSeed",4.0,100.0,10.0,resetMapTable)
    --parameter.integer("gridCount",1,10,6)
    --]]
    
    imgMap = image((gridCount+1)*scaleX,(gridCount+1)*scaleY)
    
    mapT = {}
    
    tree1 = "松树"
    tree2 = "杨树"
    tree3 = "小草"
    mine1 = "铁矿"
    mine2 = "铜矿"
    
    imgTree1 = "Planet Cute:Tree Short"
    imgTree2 = "Planet Cute:Tree Tall"
    imgTree3 = "Platformer Art:Grass"
    imgMine1 = "Platformer Art:Mushroom"
    imgMine2 = "Small World:Treasure"
    
    itemTable = {[tree1]=imgTree1,[tree2]=imgTree2,[tree3]=imgTree3,[mine1]=imgMine1,[mine2]=imgMine2}
    
    
    -- 3*3 
    mapTable = {{pos=vec2(1,1),plant=nil,mineral=mine1},{pos=vec2(1,2),plant=nil,mineral=nil},{pos=vec2(1,3),plant=tree3,mineral=nil},
                {pos=vec2(2,1),plant=tree1,mineral=nil},{pos=vec2(2,2),plant=tree2,mineral=mine2},{pos=vec2(2,3),plant=nil,mineral=nil},
                {pos=vec2(3,1),plant=nil,mineral=nil},{pos=vec2(3,2),plant=nil,mineral=mine2},{pos=vec2(3,3),plant=tree3,mineral=nil}}

    --print(randomPlant())
    --print(randomMinerial())

    --mapTable = createMap()
    --mapTable = {}
    mapTable = createMap()
end

function resetMapTable()
    mapTable = createMap()
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)
    
    --if #mapTable == 0 then mapTable = createMap() end

    -- This sets the line thickness
    -- strokeWidth(5)

    -- Do your drawing here

    sImgMap = drawMap()
    sprite(sImgMap,0,0)
end

function createMap()
    for i=1,gridCount,1 do
        for j=1,gridCount,1 do
            mapItem = {pos=vec2(i,j),plant=randomPlant(),mineral=randomMinerial()}
            table.insert(mapT,mapItem)
            --print(unpack(mapItem))
            --print(mapItem[plant],mapItem[mineral])
        end
    end
    return mapT
end

function randomPlant()
    local seed = math.random(1.0,plantSeed)
    local result = nil
    if seed >= 1 and seed < 2 then result = tree1 end
    if seed >= 2 and seed < 3 then result = tree2 end
    if seed >= 3 and seed < 4 then result = tree3 end
    if seed >= 4 and seed <= plantSeed then result = nil end
    
    return result
end

function randomMinerial()
    local seed = math.random(1.0,minerialSeed)
    local result = nil

    if seed >= 1 and seed < 2 then result = mine1 end
    if seed >= 2 and seed < 3 then result = mine2 end
    if seed >= 3 and seed <= minerialSeed then result = nil end
    
    return result

end

function getImg(name)
    return itemTable[name]
end

function drawMap()
    
    setContext(imgMap)
    
    for i = 1,gridCount*gridCount,1 do
        drawGround(mapTable[i].pos)
        if mapTable[i].plant ~= nil then drawTree(mapTable[i].pos,mapTable[i].plant) end
        if mapTable[i].mineral ~= nil then drawMineral(mapTable[i].pos,mapTable[i].mineral) end
    end
    
    setContext()
    return imgMap
end

function drawGround(position)
    local x,y = scaleX * position.x, scaleY * position.y
    strokeWidth(1)
    fill(5,155,40,255)
    fill(0,100,40,255)
    rect(x,y,scaleX,scaleY)
end

function drawTree(position,plant)
    local x,y = scaleX * position.x, scaleY * position.y
    
    sprite(itemTable[plant],x,y,scaleX*4/10,scaleY)
    
    fill(100,100,200,255)
    --text(plant,x,y)

end

function drawMineral(position,mineral)
    local x,y = scaleX * position.x, scaleY * position.y
    
    sprite(itemTable[mineral],x+scaleX/2,y,scaleX/2,scaleX/2)

    fill(100,100,200,255)
    text(mineral,x+scaleX/2,y)
    
end