update to convert poly to mesh
This commit is contained in:
5
main.lua
5
main.lua
@@ -28,11 +28,6 @@
|
||||
]]
|
||||
|
||||
-- require('mobdebug').start()
|
||||
if os.getenv("LOCAL_LUA_DEBUGGER_VSCODE") == "1" then
|
||||
require("lldebugger").start()
|
||||
end
|
||||
|
||||
|
||||
require 'src/Dependencies'
|
||||
|
||||
if os.getenv("LOCAL_LUA_DEBUGGER_VSCODE") == "1" then
|
||||
|
||||
351
src/Level.lua
351
src/Level.lua
@@ -1,17 +1,23 @@
|
||||
|
||||
-- require('mobdebug').start()
|
||||
|
||||
Level = Class{}
|
||||
Level = Class {}
|
||||
|
||||
function Level:init()
|
||||
|
||||
self.segments = self:createLevel()
|
||||
self.polygon = self:createPolygon()
|
||||
self.player = {}
|
||||
self.mesh = {}
|
||||
|
||||
end
|
||||
|
||||
function Level:update(dt)
|
||||
self:createPolygon()
|
||||
if #self.polygonPoints < 3 then
|
||||
self.mesh = {}
|
||||
else
|
||||
print_r(self.polygonPoints)
|
||||
self.mesh = poly2mesh(self.polygonPoints)
|
||||
end
|
||||
end
|
||||
|
||||
function Level:render()
|
||||
@@ -20,21 +26,32 @@ function Level:render()
|
||||
end
|
||||
|
||||
function Level:renderOuterSegments()
|
||||
for k,segment in pairs(self.segments) do
|
||||
segment:render()
|
||||
end
|
||||
for k, segment in pairs(self.segments) do segment:render() end
|
||||
end
|
||||
|
||||
function Level:renderBackground()
|
||||
love.graphics.draw(gImages["img1"],LEVEL_RENDER_OFFSET,LEVEL_RENDER_OFFSET_TOP)
|
||||
love.graphics.draw(gImages["img1"], LEVEL_RENDER_OFFSET,LEVEL_RENDER_OFFSET_TOP)
|
||||
love.graphics.draw(self.mesh, 0, 0)
|
||||
end
|
||||
|
||||
function Level:createLevel()
|
||||
local level = {
|
||||
[1] = Segment({LEVEL_RENDER_OFFSET,LEVEL_RENDER_OFFSET_TOP},{VIRTUAL_WIDTH - LEVEL_RENDER_OFFSET, LEVEL_RENDER_OFFSET_TOP},.5,'down'),
|
||||
[2] = Segment({VIRTUAL_WIDTH - LEVEL_RENDER_OFFSET, LEVEL_RENDER_OFFSET_TOP},{VIRTUAL_WIDTH - LEVEL_RENDER_OFFSET, VIRTUAL_HEIGHT - LEVEL_RENDER_OFFSET},.5,'left'),
|
||||
[3] = Segment({VIRTUAL_WIDTH - LEVEL_RENDER_OFFSET, VIRTUAL_HEIGHT - LEVEL_RENDER_OFFSET},{LEVEL_RENDER_OFFSET, VIRTUAL_HEIGHT - LEVEL_RENDER_OFFSET},.5,'up'),
|
||||
[4] = Segment({LEVEL_RENDER_OFFSET, VIRTUAL_HEIGHT - LEVEL_RENDER_OFFSET},{LEVEL_RENDER_OFFSET,LEVEL_RENDER_OFFSET_TOP},.5,'right')
|
||||
[1] = Segment({LEVEL_RENDER_OFFSET, LEVEL_RENDER_OFFSET_TOP}, {
|
||||
VIRTUAL_WIDTH - LEVEL_RENDER_OFFSET, LEVEL_RENDER_OFFSET_TOP
|
||||
}, .5, 'down'),
|
||||
[2] = Segment({
|
||||
VIRTUAL_WIDTH - LEVEL_RENDER_OFFSET, LEVEL_RENDER_OFFSET_TOP
|
||||
}, {
|
||||
VIRTUAL_WIDTH - LEVEL_RENDER_OFFSET,
|
||||
VIRTUAL_HEIGHT - LEVEL_RENDER_OFFSET
|
||||
}, .5, 'left'),
|
||||
[3] = Segment({
|
||||
VIRTUAL_WIDTH - LEVEL_RENDER_OFFSET,
|
||||
VIRTUAL_HEIGHT - LEVEL_RENDER_OFFSET
|
||||
}, {LEVEL_RENDER_OFFSET, VIRTUAL_HEIGHT - LEVEL_RENDER_OFFSET}, .5, 'up'),
|
||||
[4] = Segment({
|
||||
LEVEL_RENDER_OFFSET, VIRTUAL_HEIGHT - LEVEL_RENDER_OFFSET
|
||||
}, {LEVEL_RENDER_OFFSET, LEVEL_RENDER_OFFSET_TOP}, .5, 'right')
|
||||
}
|
||||
-- local level = {
|
||||
-- [1] = Segment({LEVEL_RENDER_OFFSET,LEVEL_RENDER_OFFSET_TOP},{VIRTUAL_WIDTH - LEVEL_RENDER_OFFSET, LEVEL_RENDER_OFFSET_TOP},.5),
|
||||
@@ -47,56 +64,58 @@ function Level:createLevel()
|
||||
return level
|
||||
end
|
||||
|
||||
function Level:insideBounds2(x,y)
|
||||
function Level:insideBounds2(x, y)
|
||||
local shape = love.physics.newPolygonShape(self.polygonPoints)
|
||||
return shape:testPoint(x,y)
|
||||
return shape:testPoint(x, y)
|
||||
end
|
||||
|
||||
function Level:insideBounds(x,y, segments)
|
||||
if not segments then
|
||||
segments = self.segments
|
||||
end
|
||||
function Level:insideBounds(x, y, segments)
|
||||
if not segments then segments = self.segments end
|
||||
|
||||
local up,down,left,right = false
|
||||
local up, down, left, right = false
|
||||
local upSeg, downSeg, leftSeg, rightSeg = nil
|
||||
|
||||
-- find closest segments
|
||||
for i, segment in pairs(segments) do
|
||||
-- check raycast on y axis
|
||||
if segment.vertical then
|
||||
if y <= math.max(segment.firstPointY, segment.secondPointY) and
|
||||
y >= math.min(segment.firstPointY, segment.secondPointY) then
|
||||
if x <= segment.firstPointX then
|
||||
if not rightSeg then
|
||||
rightSeg = segment
|
||||
elseif math.abs(segment.firstPointX - x) < math.abs(rightSeg.firstPointX - x) then
|
||||
rightSeg = segment
|
||||
end
|
||||
else
|
||||
if not leftSeg then
|
||||
leftSeg = segment
|
||||
elseif math.abs(segment.firstPointX - x) < math.abs(leftSeg.firstPointX - x) then
|
||||
leftSeg = segment
|
||||
end
|
||||
if y <= math.max(segment.firstPointY, segment.secondPointY) and y >=
|
||||
math.min(segment.firstPointY, segment.secondPointY) then
|
||||
if x <= segment.firstPointX then
|
||||
if not rightSeg then
|
||||
rightSeg = segment
|
||||
elseif math.abs(segment.firstPointX - x) <
|
||||
math.abs(rightSeg.firstPointX - x) then
|
||||
rightSeg = segment
|
||||
end
|
||||
else
|
||||
if not leftSeg then
|
||||
leftSeg = segment
|
||||
elseif math.abs(segment.firstPointX - x) <
|
||||
math.abs(leftSeg.firstPointX - x) then
|
||||
leftSeg = segment
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if segment.horizontal then
|
||||
if x <= math.max(segment.firstPointX, segment.secondPointX) and
|
||||
x >= math.min(segment.firstPointX, segment.secondPointX) then
|
||||
if y <= segment.firstPointY then
|
||||
if not downSeg then
|
||||
downSeg = segment
|
||||
elseif math.abs(segment.firstPointY - y) < math.abs(downSeg.firstPointY - y) then
|
||||
downSeg = segment
|
||||
end
|
||||
else
|
||||
if not upSeg then
|
||||
upSeg = segment
|
||||
elseif math.abs(segment.firstPointY - y) < math.abs(upSeg.firstPointY - y) then
|
||||
upSeg = segment
|
||||
end
|
||||
if x <= math.max(segment.firstPointX, segment.secondPointX) and x >=
|
||||
math.min(segment.firstPointX, segment.secondPointX) then
|
||||
if y <= segment.firstPointY then
|
||||
if not downSeg then
|
||||
downSeg = segment
|
||||
elseif math.abs(segment.firstPointY - y) <
|
||||
math.abs(downSeg.firstPointY - y) then
|
||||
downSeg = segment
|
||||
end
|
||||
else
|
||||
if not upSeg then
|
||||
upSeg = segment
|
||||
elseif math.abs(segment.firstPointY - y) <
|
||||
math.abs(upSeg.firstPointY - y) then
|
||||
upSeg = segment
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -104,23 +123,23 @@ function Level:insideBounds(x,y, segments)
|
||||
return false
|
||||
end
|
||||
|
||||
if rightSeg.face ~= 'left' or leftSeg.face ~= 'right' or upSeg.face ~= 'down' or downSeg.face ~= 'up' then
|
||||
return false
|
||||
end
|
||||
if rightSeg.face ~= 'left' or leftSeg.face ~= 'right' or upSeg.face ~=
|
||||
'down' or downSeg.face ~= 'up' then return false end
|
||||
return true
|
||||
end
|
||||
|
||||
function Level:insideBounds3(x,y)
|
||||
function Level:insideBounds3(x, y)
|
||||
x = math.floor(x)
|
||||
y = math.floor(y)
|
||||
local oddNodes = false
|
||||
local j = #self.polygon
|
||||
local polygon = self.polygon
|
||||
for i = 1, #polygon do
|
||||
if (polygon[i].y < y and polygon[j].y >= y or polygon[j].y < y and polygon[i].y >= y) then
|
||||
if (polygon[i].x + ( y - polygon[i].y ) / (polygon[j].y - polygon[i].y) * (polygon[j].x - polygon[i].x) < x) then
|
||||
oddNodes = not oddNodes
|
||||
end
|
||||
if (polygon[i].y < y and polygon[j].y >= y or polygon[j].y < y and
|
||||
polygon[i].y >= y) then
|
||||
if (polygon[i].x + (y - polygon[i].y) /
|
||||
(polygon[j].y - polygon[i].y) * (polygon[j].x - polygon[i].x) <
|
||||
x) then oddNodes = not oddNodes end
|
||||
end
|
||||
j = i
|
||||
end
|
||||
@@ -128,17 +147,18 @@ function Level:insideBounds3(x,y)
|
||||
end
|
||||
|
||||
function Level:createPolygon()
|
||||
local polygon = {}
|
||||
local polygonPoints = {}
|
||||
local j = 1
|
||||
for i,segment in ipairs(self.segments) do
|
||||
local polygon = {}
|
||||
local polygonPoints = {}
|
||||
local j = 1
|
||||
for i, segment in ipairs(self.segments) do
|
||||
polygon[i] = {}
|
||||
polygon[i+1] = {}
|
||||
polygon[i].x, polygon[i].y, polygon[i+1].x, polygon[i+1].y = segment:segmentToPoints()
|
||||
polygon[i + 1] = {}
|
||||
polygon[i].x, polygon[i].y, polygon[i + 1].x, polygon[i + 1].y =
|
||||
segment:segmentToPoints()
|
||||
polygonPoints[j] = polygon[i].x
|
||||
polygonPoints[j+1] = polygon[i].y
|
||||
polygonPoints[j+2] = polygon[i+1].x
|
||||
polygonPoints[j+3] = polygon[i+1].y
|
||||
polygonPoints[j + 1] = polygon[i].y
|
||||
polygonPoints[j + 2] = polygon[i + 1].x
|
||||
polygonPoints[j + 3] = polygon[i + 1].y
|
||||
j = j + 4
|
||||
i = i + 1
|
||||
end
|
||||
@@ -146,24 +166,22 @@ function Level:createPolygon()
|
||||
return polygon
|
||||
end
|
||||
|
||||
function Level:pointOnEdge(x,y,segments)
|
||||
if not segments then
|
||||
segments = self.segments
|
||||
end
|
||||
function Level:pointOnEdge(x, y, segments)
|
||||
if not segments then segments = self.segments end
|
||||
local margin = 2
|
||||
for i,segment in pairs(segments) do
|
||||
if segment.vertical then --vertical line
|
||||
if x >= segment.firstPointX - margin and x <= segment.firstPointX + margin and
|
||||
y <= math.max(segment.firstPointY, segment.secondPointY) and
|
||||
y >= math.min(segment.firstPointY, segment.secondPointY)
|
||||
then
|
||||
for i, segment in pairs(segments) do
|
||||
if segment.vertical then -- vertical line
|
||||
if x >= segment.firstPointX - margin and x <= segment.firstPointX +
|
||||
margin and y <=
|
||||
math.max(segment.firstPointY, segment.secondPointY) and y >=
|
||||
math.min(segment.firstPointY, segment.secondPointY) then
|
||||
return true
|
||||
end
|
||||
elseif segment.horizontal then --horizontal line
|
||||
if y >= segment.firstPointY - margin and y <= segment.firstPointY + margin and
|
||||
x <= math.max(segment.firstPointX, segment.secondPointX) and
|
||||
x >= math.min(segment.firstPointX, segment.secondPointX)
|
||||
then
|
||||
elseif segment.horizontal then -- horizontal line
|
||||
if y >= segment.firstPointY - margin and y <= segment.firstPointY +
|
||||
margin and x <=
|
||||
math.max(segment.firstPointX, segment.secondPointX) and x >=
|
||||
math.min(segment.firstPointX, segment.secondPointX) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
@@ -171,21 +189,21 @@ function Level:pointOnEdge(x,y,segments)
|
||||
return false
|
||||
end
|
||||
|
||||
function Level:getTouchingSegment(x,y)
|
||||
function Level:getTouchingSegment(x, y)
|
||||
local margin = 2
|
||||
for i,segment in pairs(self.segments) do
|
||||
if segment.vertical then --vertical line
|
||||
if x >= segment.firstPointX - margin and x <= segment.firstPointX + margin and
|
||||
y <= math.max(segment.firstPointY, segment.secondPointY) and
|
||||
y >= math.min(segment.firstPointY, segment.secondPointY)
|
||||
then
|
||||
for i, segment in pairs(self.segments) do
|
||||
if segment.vertical then -- vertical line
|
||||
if x >= segment.firstPointX - margin and x <= segment.firstPointX +
|
||||
margin and y <=
|
||||
math.max(segment.firstPointY, segment.secondPointY) and y >=
|
||||
math.min(segment.firstPointY, segment.secondPointY) then
|
||||
return i, segment
|
||||
end
|
||||
elseif segment.horizontal then --horizontal line
|
||||
if y >= segment.firstPointY - margin and y <= segment.firstPointY + margin and
|
||||
x <= math.max(segment.firstPointX, segment.secondPointX) and
|
||||
x >= math.min(segment.firstPointX, segment.secondPointX)
|
||||
then
|
||||
elseif segment.horizontal then -- horizontal line
|
||||
if y >= segment.firstPointY - margin and y <= segment.firstPointY +
|
||||
margin and x <=
|
||||
math.max(segment.firstPointX, segment.secondPointX) and x >=
|
||||
math.min(segment.firstPointX, segment.secondPointX) then
|
||||
return i, segment
|
||||
end
|
||||
end
|
||||
@@ -196,19 +214,21 @@ end
|
||||
function Level:cutLevel()
|
||||
|
||||
local newSegs = self.player.trailSegments
|
||||
local startSegi, startSeg = self.player.trailStartSegment[1],self.player.trailStartSegment[2]
|
||||
local endSegi, endSeg = self.player.trailFinishSegment[1],self.player.trailFinishSegment[2]
|
||||
local startSegi, startSeg = self.player.trailStartSegment[1],
|
||||
self.player.trailStartSegment[2]
|
||||
local endSegi, endSeg = self.player.trailFinishSegment[1],
|
||||
self.player.trailFinishSegment[2]
|
||||
local first = 0
|
||||
local last = 0
|
||||
local firstFace = ''
|
||||
|
||||
--check if it is same start and finish segment
|
||||
-- check if it is same start and finish segment
|
||||
if startSegi == endSegi then
|
||||
-- print("cutlevel same start and end segment: "..tostring(startSegi)..' - '..tostring(endSegi))
|
||||
local s = {}
|
||||
-- split the start/end segment
|
||||
local new = {}
|
||||
local split = self.segments[startSegi]:copy()
|
||||
local split = self.segments[startSegi]:copy()
|
||||
|
||||
newSegs[1]:joinPerpendicular(split)
|
||||
newSegs[#newSegs]:joinPerpendicular(split)
|
||||
@@ -221,7 +241,7 @@ function Level:cutLevel()
|
||||
end
|
||||
local part1, part2, temp = {}
|
||||
local inOrder = false
|
||||
local j,k = 1,1
|
||||
local j, k = 1, 1
|
||||
local insertedNewSegs = false
|
||||
-- print("-- Create new segments table")
|
||||
-- create the new set of segments
|
||||
@@ -231,7 +251,8 @@ function Level:cutLevel()
|
||||
-- print("Reached the segment being cut")
|
||||
-- print_r(self.segments[k])
|
||||
-- print("split the segment")
|
||||
part1, temp, part2 = self.segments[k]:splitInThreeWithSegments(newSegs[1],newSegs[#newSegs])
|
||||
part1, temp, part2 = self.segments[k]:splitInThreeWithSegments(
|
||||
newSegs[1], newSegs[#newSegs])
|
||||
-- print("part1")
|
||||
part1:debug()
|
||||
-- print("part2")
|
||||
@@ -245,7 +266,7 @@ function Level:cutLevel()
|
||||
-- print("-- insert new segments into new set")
|
||||
for i, segment in pairs(newSegs) do
|
||||
if i + 1 < #newSegs then
|
||||
newSegs[i+1]:joinPerpendicular(segment)
|
||||
newSegs[i + 1]:joinPerpendicular(segment)
|
||||
end
|
||||
new[j] = segment:copy()
|
||||
j = j + 1
|
||||
@@ -266,7 +287,7 @@ function Level:cutLevel()
|
||||
new = self:orderSegments(new)
|
||||
-- print_r(new)
|
||||
-- print("calculate faces")
|
||||
self:calculateSegmentFaces(new,new[1].face)
|
||||
self:calculateSegmentFaces(new, new[1].face)
|
||||
-- print_r(new)
|
||||
self.segments = new
|
||||
else
|
||||
@@ -279,8 +300,8 @@ function Level:cutLevel()
|
||||
local j = 1
|
||||
local last = #self.segments
|
||||
local insertedNewSegments = false
|
||||
local savedStart,savedEnd = {}
|
||||
local board2StartSegi,board2EndSegi = 0
|
||||
local savedStart, savedEnd = {}
|
||||
local board2StartSegi, board2EndSegi = 0
|
||||
-- create first board
|
||||
while k <= #self.segments do
|
||||
-- print("-----start Loop for "..tostring(k).."-----")
|
||||
@@ -292,14 +313,15 @@ function Level:cutLevel()
|
||||
newSegs[1]:joinPerpendicular(self.segments[k])
|
||||
newSegs[1]:debug()
|
||||
-- print("newSegs[1] is joined -- finished")
|
||||
local startPart1, startPart2 = self.segments[k]:splitInTwoWithSegment(newSegs[1])
|
||||
local startPart1, startPart2 =
|
||||
self.segments[k]:splitInTwoWithSegment(newSegs[1])
|
||||
-- print("-- Segment k split into 2 segments by newsegs[1]: ")
|
||||
startPart1:debug()
|
||||
startPart2:debug()
|
||||
|
||||
if self.segments[last]:endEqualsStartOf(self.segments[k]) or
|
||||
self.segments[last]:startEqualsStartOf(self.segments[k]) then -- last one is linked to the start of this one
|
||||
--keep first part of segment
|
||||
-- keep first part of segment
|
||||
-- print("-- keep first part of the k segment")
|
||||
self.segments[k] = startPart1
|
||||
savedStart = startPart2
|
||||
@@ -327,10 +349,11 @@ function Level:cutLevel()
|
||||
end
|
||||
-- print_r(board1)
|
||||
|
||||
local endPart1, endPart2 = self.segments[k]:splitInTwoWithSegment(newSegs[#newSegs])
|
||||
local endPart1, endPart2 =
|
||||
self.segments[k]:splitInTwoWithSegment(newSegs[#newSegs])
|
||||
-- print("-- proceed to insert the second split segment")
|
||||
if self.segments[last]:endEqualsEndOf(self.segments[k]) or
|
||||
self.segments[last]:startEqualsEndOf(self.segments[k]) then -- next one is linked to the start of this one
|
||||
self.segments[last]:startEqualsEndOf(self.segments[k]) then -- next one is linked to the start of this one
|
||||
self.segments[k] = endPart1
|
||||
savedEnd = endPart2
|
||||
-- print("-- keep first part of the k segment")
|
||||
@@ -353,7 +376,8 @@ function Level:cutLevel()
|
||||
newSegs[#newSegs]:joinPerpendicular(self.segments[k])
|
||||
newSegs[#newSegs]:debug()
|
||||
-- print("newSegs[#newSegs] is joined -- finished")
|
||||
local endPart1, endPart2 = self.segments[k]:splitInTwoWithSegment(newSegs[#newSegs])
|
||||
local endPart1, endPart2 =
|
||||
self.segments[k]:splitInTwoWithSegment(newSegs[#newSegs])
|
||||
-- print("-- Segment k split into 2 segments by newsegs[#newSegs]: ")
|
||||
endPart1:debug()
|
||||
endPart2:debug()
|
||||
@@ -387,7 +411,8 @@ function Level:cutLevel()
|
||||
end
|
||||
-- print_r(board1)
|
||||
|
||||
local startPart1, startPart2 = self.segments[k]:splitInTwoWithSegment(newSegs[1])
|
||||
local startPart1, startPart2 =
|
||||
self.segments[k]:splitInTwoWithSegment(newSegs[1])
|
||||
-- print("-- Segment k split into 2 segments by newsegs[1]: ")
|
||||
startPart1:debug()
|
||||
startPart2:debug()
|
||||
@@ -398,7 +423,7 @@ function Level:cutLevel()
|
||||
savedstart = startPart2
|
||||
else
|
||||
self.segments[k] = startPart2
|
||||
savedStart= startPart1
|
||||
savedStart = startPart1
|
||||
end
|
||||
|
||||
board1[j] = self.segments[k]:copy()
|
||||
@@ -417,7 +442,7 @@ function Level:cutLevel()
|
||||
end
|
||||
-- print("order segments in cutlevel board1")
|
||||
board1 = self:orderSegments(board1)
|
||||
self:calculateSegmentFaces(board1,board1[1].face)
|
||||
self:calculateSegmentFaces(board1, board1[1].face)
|
||||
-- print("PREPARE BOARD 1 -- FINAL")
|
||||
-- print_r(board1)
|
||||
|
||||
@@ -485,7 +510,7 @@ function Level:cutLevel()
|
||||
|
||||
-- print("order segments in cutlevel board2")
|
||||
board2 = self:orderSegments(board2)
|
||||
self:calculateSegmentFaces(board2,board2[1].face)
|
||||
self:calculateSegmentFaces(board2, board2[1].face)
|
||||
-- print("PREPARE BOARD 2 -- FINAL")
|
||||
-- print_r(board2)
|
||||
|
||||
@@ -494,20 +519,21 @@ function Level:cutLevel()
|
||||
elseif self:containsBalls(board2) == 0 then
|
||||
self.segments = board1
|
||||
else
|
||||
self.segments = self:getPerimeter(board1) > self:getPerimeter(board2) and board1 or board2
|
||||
self.segments = self:getPerimeter(board1) >
|
||||
self:getPerimeter(board2) and board1 or board2
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
function Level:orderSegments(segs)
|
||||
--returns the list of segments in the level linked to each other in order and perpendicular to each other
|
||||
-- returns the list of segments in the level linked to each other in order and perpendicular to each other
|
||||
local new = {}
|
||||
-- -- print('----- order Segments : ')
|
||||
-- print_s(segs)
|
||||
|
||||
new[1] = segs[1]
|
||||
table.remove(segs,1)
|
||||
table.remove(segs, 1)
|
||||
|
||||
local found = false
|
||||
local k = #segs
|
||||
@@ -524,18 +550,19 @@ function Level:orderSegments(segs)
|
||||
|
||||
-- -- print("new[i] end equals start of ")
|
||||
-- s:debug()
|
||||
if new[i].vertical == s.vertical and new[i].horizontal == s.horizontal then
|
||||
if new[i].vertical == s.vertical and new[i].horizontal ==
|
||||
s.horizontal then
|
||||
-- -- print("join contiguous "..tostring(i).. 'with'..tostring(j))
|
||||
new[i]:joinContiguous(s)
|
||||
table.remove(segs,j)
|
||||
table.remove(segs, j)
|
||||
i = i - 1
|
||||
k = k - 1
|
||||
break
|
||||
else
|
||||
-- -- print("join perpendicular "..tostring(i).. 'with'..tostring(j))
|
||||
s:joinPerpendicular(new[i])
|
||||
new[i+1] = s
|
||||
table.remove(segs,j)
|
||||
new[i + 1] = s
|
||||
table.remove(segs, j)
|
||||
break
|
||||
end
|
||||
elseif new[i]:endEqualsEndOf(s) then
|
||||
@@ -544,33 +571,38 @@ function Level:orderSegments(segs)
|
||||
-- -- print("new[i] end equals end of ")
|
||||
-- s:debug()
|
||||
s:switchDirection()
|
||||
if new[i].vertical == s.vertical and new[i].horizontal == s.horizontal then
|
||||
if new[i].vertical == s.vertical and new[i].horizontal ==
|
||||
s.horizontal then
|
||||
-- -- print("join contiguous "..tostring(i).. 'with'..tostring(j))
|
||||
new[i]:joinContiguous(s)
|
||||
table.remove(segs,j)
|
||||
table.remove(segs, j)
|
||||
i = i - 1
|
||||
k = k - 1
|
||||
break
|
||||
else
|
||||
-- -- print("join perpendicular "..tostring(i).. 'with'..tostring(j))
|
||||
s:joinPerpendicular(new[i])
|
||||
new[i+1] = s
|
||||
table.remove(segs,j)
|
||||
new[i + 1] = s
|
||||
table.remove(segs, j)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local fi, fs = 0,{}
|
||||
local fi, fs = 0, {}
|
||||
|
||||
if not found then
|
||||
-- -- print("search didnt yield any segment")
|
||||
local margin = 2
|
||||
while not found and margin < 5 do
|
||||
fi, fs = self:findNearestSegment(new[i].secondPointX,new[i].secondPointY,segs,margin)
|
||||
fi, fs = self:findNearestSegment(new[i].secondPointX,
|
||||
new[i].secondPointY, segs,
|
||||
margin)
|
||||
|
||||
if not fs then
|
||||
fi, fs = self:findNearestSegment(new[i].firstPointX,new[i].firstPointY,segs,margin)
|
||||
fi, fs = self:findNearestSegment(new[i].firstPointX,
|
||||
new[i].firstPointY, segs,
|
||||
margin)
|
||||
|
||||
if not fs then
|
||||
margin = margin + 1
|
||||
@@ -586,20 +618,20 @@ function Level:orderSegments(segs)
|
||||
if new[i].vertical == fs.vertical then
|
||||
-- -- print("join contiguous "..tostring(i).. 'with'..tostring(fi))
|
||||
new[i]:joinContiguous(fs)
|
||||
table.remove(segs,fi)
|
||||
table.remove(segs, fi)
|
||||
i = i - 1
|
||||
k = k - 1
|
||||
else
|
||||
-- -- print("join perpendicular "..tostring(i).. 'with'..tostring(fi))
|
||||
fs:joinPerpendicular(new[i])
|
||||
new[i+1] = fs
|
||||
table.remove(segs,fi)
|
||||
new[i + 1] = fs
|
||||
table.remove(segs, fi)
|
||||
end
|
||||
end
|
||||
end
|
||||
if not found then
|
||||
-- print("ERROR -- order segments does not find next segment to ")
|
||||
new[i]:debug("number: " ..tostring(i))
|
||||
new[i]:debug("number: " .. tostring(i))
|
||||
break
|
||||
else
|
||||
found = false
|
||||
@@ -619,13 +651,13 @@ function Level:calculateSegmentFaces(segments, firstFace)
|
||||
if k + 1 <= #segments then
|
||||
if face == 'up' then
|
||||
if s.direction == 'right' then
|
||||
if segments[k+1].direction == 'up' then
|
||||
if segments[k + 1].direction == 'up' then
|
||||
face = 'left'
|
||||
else
|
||||
face = 'right'
|
||||
end
|
||||
else
|
||||
if segments[k+1].direction == 'up' then
|
||||
if segments[k + 1].direction == 'up' then
|
||||
face = 'right'
|
||||
else
|
||||
face = 'left'
|
||||
@@ -633,13 +665,13 @@ function Level:calculateSegmentFaces(segments, firstFace)
|
||||
end
|
||||
elseif face == 'down' then
|
||||
if s.direction == 'right' then
|
||||
if segments[k+1].direction == 'up' then
|
||||
if segments[k + 1].direction == 'up' then
|
||||
face = 'right'
|
||||
else
|
||||
face = 'left'
|
||||
end
|
||||
else
|
||||
if segments[k+1].direction == 'up' then
|
||||
if segments[k + 1].direction == 'up' then
|
||||
face = 'left'
|
||||
else
|
||||
face = 'right'
|
||||
@@ -647,13 +679,13 @@ function Level:calculateSegmentFaces(segments, firstFace)
|
||||
end
|
||||
elseif face == 'right' then
|
||||
if s.direction == 'up' then
|
||||
if segments[k+1].direction == 'right' then
|
||||
if segments[k + 1].direction == 'right' then
|
||||
face = 'down'
|
||||
else
|
||||
face = 'up'
|
||||
end
|
||||
else
|
||||
if segments[k+1].direction == 'right' then
|
||||
if segments[k + 1].direction == 'right' then
|
||||
face = 'up'
|
||||
else
|
||||
face = 'down'
|
||||
@@ -661,13 +693,13 @@ function Level:calculateSegmentFaces(segments, firstFace)
|
||||
end
|
||||
elseif face == 'left' then
|
||||
if s.direction == 'up' then
|
||||
if segments[k+1].direction == 'right' then
|
||||
if segments[k + 1].direction == 'right' then
|
||||
face = 'up'
|
||||
else
|
||||
face = 'down'
|
||||
end
|
||||
else
|
||||
if segments[k+1].direction == 'right' then
|
||||
if segments[k + 1].direction == 'right' then
|
||||
face = 'down'
|
||||
else
|
||||
face = 'up'
|
||||
@@ -680,36 +712,28 @@ end
|
||||
|
||||
function Level:getPerimeter(segments)
|
||||
local p = 0
|
||||
if not segments then
|
||||
segments = self.segments
|
||||
end
|
||||
for k, s in pairs(segments) do
|
||||
p = p + s:length()
|
||||
end
|
||||
if not segments then segments = self.segments end
|
||||
for k, s in pairs(segments) do p = p + s:length() end
|
||||
return p
|
||||
end
|
||||
|
||||
function Level:findNearestSegment(x,y,segments,margin)
|
||||
if not segments then
|
||||
segments = self.segments
|
||||
end
|
||||
if not margin then
|
||||
margin = 2
|
||||
end
|
||||
function Level:findNearestSegment(x, y, segments, margin)
|
||||
if not segments then segments = self.segments end
|
||||
if not margin then margin = 2 end
|
||||
-- find a touching segment within margins
|
||||
for i,segment in pairs(segments) do
|
||||
if segment.vertical then --vertical line
|
||||
if x >= segment.firstPointX - margin and x <= segment.firstPointX + margin and
|
||||
y <= math.max(segment.firstPointY, segment.secondPointY) and
|
||||
y >= math.min(segment.firstPointY, segment.secondPointY)
|
||||
then
|
||||
for i, segment in pairs(segments) do
|
||||
if segment.vertical then -- vertical line
|
||||
if x >= segment.firstPointX - margin and x <= segment.firstPointX +
|
||||
margin and y <=
|
||||
math.max(segment.firstPointY, segment.secondPointY) and y >=
|
||||
math.min(segment.firstPointY, segment.secondPointY) then
|
||||
return i, segment
|
||||
end
|
||||
elseif segment.horizontal then --horizontal line
|
||||
if y >= segment.firstPointY - margin and y <= segment.firstPointY + margin and
|
||||
x <= math.max(segment.firstPointX, segment.secondPointX) and
|
||||
x >= math.min(segment.firstPointX, segment.secondPointX)
|
||||
then
|
||||
elseif segment.horizontal then -- horizontal line
|
||||
if y >= segment.firstPointY - margin and y <= segment.firstPointY +
|
||||
margin and x <=
|
||||
math.max(segment.firstPointX, segment.secondPointX) and x >=
|
||||
math.min(segment.firstPointX, segment.secondPointX) then
|
||||
return i, segment
|
||||
end
|
||||
end
|
||||
@@ -719,13 +743,12 @@ end
|
||||
|
||||
function Level:containsBalls(segments)
|
||||
-- returns numbere of balls inside the bounds passed in or self.segments
|
||||
if not segments then
|
||||
segments = self.segments
|
||||
end
|
||||
if not segments then segments = self.segments end
|
||||
local counter = 0
|
||||
|
||||
for k,b in pairs(self.balls) do
|
||||
if self:insideBounds(b.x + 4, b.y + 4,segments) or self:pointOnEdge(b.x+4, b.y+4, seegments) then
|
||||
for k, b in pairs(self.balls) do
|
||||
if self:insideBounds(b.x + 4, b.y + 4, segments) or
|
||||
self:pointOnEdge(b.x + 4, b.y + 4, seegments) then
|
||||
counter = counter + 1
|
||||
end
|
||||
end
|
||||
|
||||
43
src/Util.lua
43
src/Util.lua
@@ -71,3 +71,46 @@ function print_s(segments)
|
||||
s:debug(tostring(k))
|
||||
end
|
||||
end
|
||||
|
||||
-- convert a list of points forming a polygon {x1, y1, x2, y2, ...} into a mesh
|
||||
|
||||
function poly2mesh(points)
|
||||
-- remove duplicates???
|
||||
local polypts = love.math.triangulate(points)
|
||||
local tlist
|
||||
|
||||
local vnums = {}
|
||||
local vcoords = {}
|
||||
do
|
||||
local verthash = {}
|
||||
local n = 0
|
||||
local v
|
||||
-- use unique vertices by using a coordinate hash table
|
||||
for i = 1, #polypts do
|
||||
for j = 1, 3 do
|
||||
local px = polypts[i][j * 2 - 1]
|
||||
local py = polypts[i][j * 2]
|
||||
if not verthash[px] then verthash[px] = {} end
|
||||
if not verthash[px][py] then
|
||||
n = n + 1
|
||||
verthash[px][py] = n
|
||||
vcoords[n * 2 - 1] = px
|
||||
vcoords[n * 2] = py
|
||||
v = n
|
||||
else
|
||||
v = verthash[px][py]
|
||||
end
|
||||
vnums[(i - 1) * 3 + j] = v
|
||||
end
|
||||
end
|
||||
end
|
||||
local mesh = love.graphics.newMesh(#vcoords, "triangles", "static")
|
||||
for i = 1, #vcoords / 2 do
|
||||
local x, y = vcoords[i * 2 - 1], vcoords[i * 2]
|
||||
|
||||
-- Here's where the UVs are assigned
|
||||
mesh:setVertex(i, x, y, x / 50, y / 50, .5, .5, .9, 1)
|
||||
end
|
||||
mesh:setVertexMap(vnums)
|
||||
return mesh
|
||||
end
|
||||
|
||||
@@ -21,7 +21,7 @@ function PlayState:enter()
|
||||
end
|
||||
|
||||
function PlayState:update(dt)
|
||||
-- self.level:update(dt)
|
||||
self.level:update(dt)
|
||||
self.player:update(dt)
|
||||
self:updateBalls(dt, self.level)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user