From: Clifton James Palmer Date: Sat, 8 Dec 2018 18:23:11 +0000 (-0600) Subject: Added collision detection and pause on crash X-Git-Url: http://git.purplebirdman.com/lightcycles-love.git/commitdiff_plain/7f2cb3d1095471a2c4881249e0d6239f34f2a3b8?ds=sidebyside Added collision detection and pause on crash --- diff --git a/main.lua b/main.lua index a0d0ca7..69591dd 100644 --- a/main.lua +++ b/main.lua @@ -14,7 +14,9 @@ function love.update(dt) if love.keyboard.isDown('escape') then love.event.quit() end - scene:update(dt) + if not scene.paused then + scene:update(dt) + end end function love.quit() diff --git a/scene.lua b/scene.lua index 73164e3..84c4473 100644 --- a/scene.lua +++ b/scene.lua @@ -5,6 +5,7 @@ require 'player' scene = {} +scene.paused = false scene.width = love.graphics.getWidth() scene.height = love.graphics.getHeight() scene.players = {} @@ -16,7 +17,7 @@ scene.grid.delta = 50 -- 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 @@ -41,6 +42,12 @@ end function scene:draw() self:drawGrid() self:drawPlayers() + + -- draw intersection if it's there + if self.intersection then + love.graphics.setColor(0, 255, 0) + love.graphics.line(self.intersection) + end end -- update @@ -50,10 +57,53 @@ function scene:updatePlayers(dt) end end +function doLinesIntersect(x1, y1, x2, y2, x3, y3, x4, y4) + if x1 == x2 and x3 == x4 + or y1 == y2 and y3 == y4 + then + -- if lines are parallel, no intersection! + return false + 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 + end +end + function doesLineIntersectPlayerPaths(path, x1, y1, x2, y2) + local stash = {} + -- 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 + end + stash = {} + else + table.insert(stash,v) + end + end return false end +-- love collision handler +function collision(player) + print('Player '..tostring(player)..' crashed!') + --love.event.quit() +end +love.handlers.collision = collision + function scene:handleCollisions() -- calculate the last line for each player from current position -- check if line intersects any other path line