I am a newb to Love2d and Lua, and am trying to make a game.
One aspect that I am currently wrestling with is how to make polygons for my world map.
I want to create polygons, and I also want to visualise them to help with my development/debugging.
[Edit: The reason to make polygons is to change the game depending on which polygon the character is located in. (I.e. if in a forest polygon, set a forest background, increase likelihood of encountering bears). This should also help prevent my character from walking into mountains and into lakes. The purpose of drawing is only to help me debug.]
Specifically, I don't understand how to use PolygonShape:getPoints() for a list of polygons. I can think of work-arounds, but I'd love to learn how to code neatly and efficiently. This seems like a good learning point.
Could someone kindly help me to make this code work? It is a simplified extract of my working main file.
This is my first post to the forums, so apologies if I haven't understood some fundamental things
Code: Select all
polygon1 = {434,101, 452,112, 420,138}
polygon2 = {377,84 ,434,101, 420,138, 380,140}
polygon3 = {434,101, 451,112, 420,138}
polygon4 = {420,138, 426,150, 452,112}
polygon5 = {426,150 ,452,112, 459,154, 420,190}
polygon6 = {452,112, 472,87, 459,154}
polygon_table = {polygon1,polygon2,polygon3,polygon4,polygon5,polygon6}
p = table.getn(polygon_table) -- p=number of polygons == 6
function love.load()
-- set window screen: https://love2d.org/wiki/love.window.setMode
love.window.setMode(800, 450, {resizable=true, vsync=false, minwidth=400, minheight=300})
for i = 1,p do -- (i=polygon number)
print("first two vertices for polygon "..i.." == {"..polygon_table[i][1]..","..polygon_table[i][1]..","..polygon_table[i][1]..","..polygon_table[i][1].."}")
end
end
function love.update()
end
function love.draw()
-- Demonstrate that polygon printing works for example Polygon1
love.graphics.polygon('line',polygon1)
for i = 1,p do
polygon={}
polygon[i] = love.physics.newPolygonShape(polygon_table[i])
--love.graphics.polygon('line', PolygonShape:getPoints()) -- this part is currently confusing me - how do i specify to get the points for each polygon[i]?
end
end