]> Untitled Git - lightcycles-love.git/commitdiff
Fixed crashes better. Bug on collision on 1st line. Cannot tell who crashed.
authorClifton James Palmer <clifton.palmer@gmail.com>
Sun, 9 Dec 2018 00:46:26 +0000 (18:46 -0600)
committerClifton James Palmer <clifton.palmer@gmail.com>
Sun, 9 Dec 2018 00:56:04 +0000 (18:56 -0600)
main.lua
player.lua
scene.lua
vec2.lua

index 69591ddff234fae864e63aa106e846169844a7dd..a0d0ca720923f8ec71259c9fa99719cc6b59c3d4 100644 (file)
--- a/main.lua
+++ b/main.lua
@@ -14,9 +14,7 @@ function love.update(dt)
     if love.keyboard.isDown('escape') then
         love.event.quit()
     end
     if love.keyboard.isDown('escape') then
         love.event.quit()
     end
-    if not scene.paused then
-        scene:update(dt)
-    end
+    scene:update(dt)
 end
 
 function love.quit()
 end
 
 function love.quit()
index 0a37a0b8eff4c03102f655a0bd037a1e64b1692d..9ec8fcd8087dc86d34cbae1a686b1984630401b9 100644 (file)
@@ -9,6 +9,7 @@ Player.acceleration = 100
 Player.position = vec2:new(0, 0)
 Player.vector = vec2:new(0, 0)
 Player.path = {}
 Player.position = vec2:new(0, 0)
 Player.vector = vec2:new(0, 0)
 Player.path = {}
+Player.path.vector = Player.position
 Player.vectors = {
     up    = vec2:new(0, -1),
     down  = vec2:new(0, 1),
 Player.vectors = {
     up    = vec2:new(0, -1),
     down  = vec2:new(0, 1),
@@ -26,6 +27,7 @@ function Player:new(o)
     o = o or {}
     setmetatable(o, self)
     self.__index = self
     o = o or {}
     setmetatable(o, self)
     self.__index = self
+    o.path.vector = o.position
     o:recordPosition()
     return o
 end
     o:recordPosition()
     return o
 end
@@ -35,10 +37,17 @@ function Player:__tostring()
 end
 
 function Player:drawPath()
 end
 
 function Player:drawPath()
-    if #self.path >= 4 then
+    local points = {}
+    local node = self.path
+    while node do
+        table.insert(points, node.vector.x)
+        table.insert(points, node.vector.y)
+        node = node.prev
+    end
+    if #points >= 4 then
         love.graphics.setLineWidth(2)
         love.graphics.setColor(self.color)
         love.graphics.setLineWidth(2)
         love.graphics.setColor(self.color)
-        love.graphics.line(self.path)
+        love.graphics.line(points)
     end
 end
 
     end
 end
 
@@ -51,17 +60,18 @@ function Player:draw()
         self.width,
         self.height
         )
         self.width,
         self.height
         )
-
-    -- add current position
-    self:recordPosition()
     self:drawPath()
     self:drawPath()
-    table.remove(self.path)
-    table.remove(self.path)
 end
 
 function Player:recordPosition()
 end
 
 function Player:recordPosition()
-    table.insert(self.path, self.position.x)
-    table.insert(self.path, self.position.y)
+    local v = vec2:new(self.position.x, self.position.y)
+    self.position = v
+    print('Recording position for '..tostring(self)..': '..tostring(self.position))
+    local node = {}
+    node.vector = v
+    node.prev = self.path
+    self.path.next = node
+    self.path = node
 end
 
 function Player:multiple_keys_are_pressed()
 end
 
 function Player:multiple_keys_are_pressed()
@@ -87,5 +97,5 @@ function Player:update(dt)
             end
         end
     end
             end
         end
     end
-    self.position = self.position + self.vector * self.acceleration * dt
+    self.position:add(self.vector * self.acceleration * dt)
 end
 end
index 83960e6bddf41b3e90c400fcc4bb349d6b18b2de..213d34ad9c668f8c44b2fe8f383a84d6662b11c2 100644 (file)
--- a/scene.lua
+++ b/scene.lua
@@ -68,23 +68,23 @@ function doLinesIntersect(x1,y1, x2,y2, x3,y3, x4,y4)
         -- if lines are parallel, no intersection!
     elseif x1 == x2 and y3 == y4 then
         intersect = (
         -- if lines are parallel, no intersection!
     elseif x1 == x2 and y3 == y4 then
         intersect = (
-                x3 <= x1 and x1 <= x4
+                x3 < x1 and x1 < x4
                 or
                 or
-                x4 <= x1 and x1 <= x3
+                x4 < x1 and x1 < x3
             ) and (
             ) and (
-                y1 <= y3 and y3 <= y2
+                y1 < y3 and y3 < y2
                 or
                 or
-                y2 <= y3 and y3 <= y1
+                y2 < y3 and y3 < y1
             )
     elseif x3 == x4 and y1 == y2 then
         intersect = (
             )
     elseif x3 == x4 and y1 == y2 then
         intersect = (
-                x1 <= x3 and x3 <= x2
+                x1 < x3 and x3 < x2
                 or
                 or
-                x2 <= x3 and x3 <= x1
+                x2 < x3 and x3 < x1
             ) and (
             ) and (
-                y3 <= y1 and y1 <= y4
+                y3 < y1 and y1 < y4
                 or
                 or
-                y4 <= y1 and y1 <= y3
+                y4 < y1 and y1 < y3
             )
     else
         print('You should never see this message')
             )
     else
         print('You should never see this message')
@@ -100,32 +100,24 @@ function doLinesIntersect(x1,y1, x2,y2, x3,y3, x4,y4)
     return intersect
 end
 
     return intersect
 end
 
-function doesLineIntersectPlayerPaths(path, x1,y1, x2,y2)
-    local cache = {}
-    cache.a = {}
-    cache.b = {}
+function doesLineIntersectPlayerPaths(node, v1, v2)
     -- for every line in path,
     -- check intersection with player line
     -- for every line in path,
     -- check intersection with player line
-    for k,v in pairs(path) do
-        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
-            cache.b = cache.a
-            cache.a = {}
+    while node and node.prev do
+        local v3 = node.vector
+        local v4 = node.prev.vector
+        if doLinesIntersect(v1.x,v1.y, v2.x,v2.y, v3.x,v3.y, v4.x,v4.y) then
+            return true
         end
         end
-        table.insert(cache.a,v)
+        node = node.prev
     end
     return false
 end
 
 -- love collision handler
 function collision(player)
     end
     return false
 end
 
 -- love collision handler
 function collision(player)
-    print('Player '..tostring(player)..' crashed!')
+    print(player..' crashed!')
     scene.paused = true
     scene.paused = true
-    --love.event.quit()
 end
 love.handlers.collision = collision
 
 end
 love.handlers.collision = collision
 
@@ -133,24 +125,24 @@ function scene:handleCollisions()
     -- calculate the last line for each player from current position
     -- check if line intersects any other path line
     -- if so, raise collision event for player
     -- calculate the last line for each player from current position
     -- check if line intersects any other path line
     -- if so, raise collision event for player
+    -- only check last line for intersection, since all the rest should be ok
     for _,player in pairs(self.players) do
     for _,player in pairs(self.players) do
-        local x1 = player.path[#player.path-1]
-        local y1 = player.path[#player.path]
-        local x2 = player.position.x
-        local y2 = player.position.y
-
-        -- check intersection against each existing path
+        local v1 = player.path.vector
+        local v2 = player.path.prev.vector
         for _,player2 in pairs(self.players) do
         for _,player2 in pairs(self.players) do
-            if doesLineIntersectPlayerPaths(player2.path, x1, y1, x2, y2) then
-                love.event.push('collision', player)
+            if doesLineIntersectPlayerPaths(player2.path, v1, v2) then
+                love.event.push('collision', tostring(player))
+                break
             end
         end
     end
 end
 
 function scene:update(dt)
             end
         end
     end
 end
 
 function scene:update(dt)
-    self:updatePlayers(dt)
-    self:handleCollisions()
+    if not self.paused then
+        self:updatePlayers(dt)
+        self:handleCollisions()
+    end
 end
 
 -- quit
 end
 
 -- quit
index 4d6a011458aaf9847f616b5cf54cc62481aea43e..34fe853ae80fc56fb91d6aab58823202f78c918b 100644 (file)
--- a/vec2.lua
+++ b/vec2.lua
@@ -37,6 +37,12 @@ function vec2:__add(v)
     return vec2:new(self.x + v.x, self.y + v.y)
 end
 
     return vec2:new(self.x + v.x, self.y + v.y)
 end
 
+function vec2:add(v)
+    assert(isvector(v), "Expected a vec2 type")
+    self.x = self.x + v.x
+    self.y = self.y + v.y
+end
+
 function vec2:__sub(v)
     assert(isvector(v), "Expected a vec2 type")
     return vec2:new(self.x - v.x, self.y - v.y)
 function vec2:__sub(v)
     assert(isvector(v), "Expected a vec2 type")
     return vec2:new(self.x - v.x, self.y - v.y)