From: Clifton James Palmer Date: Sat, 8 Dec 2018 16:19:27 +0000 (-0600) Subject: Added player names and stub for collision detection X-Git-Url: http://git.purplebirdman.com/lightcycles-love.git/commitdiff_plain/8f054564538fd6e9b63f6d84796710232ba31908 Added player names and stub for collision detection --- diff --git a/player.lua b/player.lua index 387b54f..ed4f210 100644 --- a/player.lua +++ b/player.lua @@ -1,7 +1,7 @@ require 'vec2' Player = {} -Player.name = 'player' +Player.name = 'none' Player.color = {255, 0, 0} Player.width = 5 Player.height = 5 @@ -30,6 +30,10 @@ function Player:new(o) return o end +function Player:__tostring() + return self.name +end + function Player:drawPath() if #self.path >= 4 then love.graphics.setLineWidth(2) diff --git a/players/1.lua b/players/1.lua index 5d8477a..1ab74fb 100644 --- a/players/1.lua +++ b/players/1.lua @@ -4,6 +4,7 @@ require 'vec2' require 'player' return Player:new({ + name='Player 1', position=vec2:new(100,100), vector=Player.vectors.right, path={}, diff --git a/players/2.lua b/players/2.lua index 2539bc9..2b34fdb 100644 --- a/players/2.lua +++ b/players/2.lua @@ -7,6 +7,7 @@ local width = love.graphics.getWidth() local height = love.graphics.getHeight() return Player:new({ + name='Player 2', position=vec2:new(width-100,height-100), vector=Player.vectors.left, path={}, diff --git a/scene.lua b/scene.lua index 3460899..73164e3 100644 --- a/scene.lua +++ b/scene.lua @@ -50,13 +50,37 @@ function scene:updatePlayers(dt) end end +function doesLineIntersectPlayerPaths(path, x1, y1, x2, y2) + return false +end + +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 + 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 + for _,player2 in pairs(self.players) do + if doesLineIntersectPlayerPaths(player2.path, x1, y1, x2, y2) then + love.event.push('collision', player) + end + end + end +end + function scene:update(dt) self:updatePlayers(dt) + self:handleCollisions() end -- quit function scene:quit() - for i,player in pairs(self.players) do - print('Player '..i..' generated '.. #player.path / 2 .. ' path points') + for _,player in pairs(self.players) do + print(tostring(player)..' generated '.. #player.path / 2 .. ' path points') end end