]> Untitled Git - lightcycles-love.git/blobdiff - scene.lua
Updated right angle message
[lightcycles-love.git] / scene.lua
index 84c4473e1b91d523bf11356a24b8b209e009e3b7..83960e6bddf41b3e90c400fcc4bb349d6b18b2de 100644 (file)
--- a/scene.lua
+++ b/scene.lua
@@ -17,7 +17,7 @@ scene.grid.delta = 50
 -- load
 function scene:load()
     table.insert(scene.players, require('players/1'))
 -- load
 function scene:load()
     table.insert(scene.players, require('players/1'))
-    --table.insert(scene.players, require('players/2'))
+    table.insert(scene.players, require('players/2'))
 end
 
 -- draw
 end
 
 -- draw
@@ -46,7 +46,9 @@ function scene:draw()
     -- draw intersection if it's there
     if self.intersection then
         love.graphics.setColor(0, 255, 0)
     -- draw intersection if it's there
     if self.intersection then
         love.graphics.setColor(0, 255, 0)
-        love.graphics.line(self.intersection)
+        love.graphics.line(self.intersection.a)
+        love.graphics.setColor(0, 0, 255)
+        love.graphics.line(self.intersection.b)
     end
 end
 
     end
 end
 
@@ -57,42 +59,64 @@ function scene:updatePlayers(dt)
     end
 end
 
     end
 end
 
-function doLinesIntersect(x1, y1, x2, y2, x3, y3, x4, y4)
+function doLinesIntersect(x1,y1, x2,y2, x3,y3, x4,y4)
+    local intersect = false
+
     if x1 == x2 and x3 == x4
     or y1 == y2 and y3 == y4
     then
         -- if lines are parallel, no intersection!
     if x1 == x2 and x3 == x4
     or y1 == y2 and y3 == y4
     then
         -- if lines are parallel, no intersection!
-        return false
+    elseif x1 == x2 and y3 == y4 then
+        intersect = (
+                x3 <= x1 and x1 <= x4
+                or
+                x4 <= x1 and x1 <= x3
+            ) and (
+                y1 <= y3 and y3 <= y2
+                or
+                y2 <= y3 and y3 <= y1
+            )
+    elseif x3 == x4 and y1 == y2 then
+        intersect = (
+                x1 <= x3 and x3 <= x2
+                or
+                x2 <= x3 and x3 <= x1
+            ) and (
+                y3 <= y1 and y1 <= y4
+                or
+                y4 <= y1 and y1 <= y3
+            )
     else
     else
-        -- if lines are not parallel, they must intersect
-        -- do segments overlap?
-        if
-            x1 <= x3 and x3 <= x2
-        and y1 <= y3 and y3 <= y2
-        or
-            x3 <= x1 and x1 <= x4
-        and y3 <= y1 and y1 <= y4
-        then
-            scene.paused = true
-            scene.intersection = {x1, y1, x2, y2, x3, y3, x4, y4}
-            return true
-        end
+        print('You should never see this message')
     end
     end
+
+    if intersect then
+        scene.intersection = {
+            a = {x1,y1, x2,y2},
+            b = {x3,y3, x4,y4},
+            }
+    end
+
+    return intersect
 end
 
 end
 
-function doesLineIntersectPlayerPaths(path, x1, y1, x2, y2)
-    local stash = {}
+function doesLineIntersectPlayerPaths(path, x1,y1, x2,y2)
+    local cache = {}
+    cache.a = {}
+    cache.b = {}
     -- for every line in path,
     -- check intersection with player line
     for k,v in pairs(path) do
     -- for every line in path,
     -- check intersection with player line
     for k,v in pairs(path) do
-        if #stash == 4 then
-            if doLinesIntersect(x1, y1, x2, y2, stash[4], stash[3], stash[2], stash[1]) then
-                return true
+        if #cache.a == 2 then
+            if #cache.b == 2 then
+                if doLinesIntersect(x1,y1, x2,y2, cache.a[1],cache.a[2], cache.b[1],cache.b[2]) then
+                    return true
+                end
             end
             end
-            stash = {}
-        else
-            table.insert(stash,v)
+            cache.b = cache.a
+            cache.a = {}
         end
         end
+        table.insert(cache.a,v)
     end
     return false
 end
     end
     return false
 end
@@ -100,6 +124,7 @@ end
 -- love collision handler
 function collision(player)
     print('Player '..tostring(player)..' crashed!')
 -- love collision handler
 function collision(player)
     print('Player '..tostring(player)..' crashed!')
+    scene.paused = true
     --love.event.quit()
 end
 love.handlers.collision = collision
     --love.event.quit()
 end
 love.handlers.collision = collision