9 scene.width = love.graphics.getWidth()
10 scene.height = love.graphics.getHeight()
13 scene.grid.bgcolor = {0.2, 0.2, 0.5}
14 scene.grid.linecolor = {0.3, 0.3, 0.6}
19 table.insert(scene.players, require('players/1'))
20 table.insert(scene.players, require('players/2'))
24 function scene:drawGrid()
25 love.graphics.setBackgroundColor(self.grid.bgcolor)
26 love.graphics.setColor(self.grid.linecolor)
28 love.graphics.setLineWidth(2)
29 for x=0,self.width,self.grid.delta do
30 love.graphics.line(x, 0, x, self.height)
32 for y=0,self.height,self.grid.delta do
33 love.graphics.line(0, y, self.width, y)
36 love.graphics.setLineWidth(0.5)
37 for x=0,self.width,self.grid.delta/2 do
38 love.graphics.line(x, 0, x, self.height)
40 for y=0,self.height,self.grid.delta/2 do
41 love.graphics.line(0, y, self.width, y)
45 function scene:drawPlayers()
46 for _,player in pairs(self.players) do
55 -- draw intersection if it's there
56 if self.intersection then
57 love.graphics.setColor(0, 255, 0)
58 love.graphics.line(self.intersection.a)
59 love.graphics.setColor(0, 0, 255)
60 love.graphics.line(self.intersection.b)
65 function scene:updatePlayers(dt)
66 for _,player in pairs(self.players) do
71 function doLinesIntersect(x1,y1, x2,y2, x3,y3, x4,y4)
72 local intersect = false
74 if x1 == x2 and x3 == x4
75 or y1 == y2 and y3 == y4
77 -- if lines are parallel, no intersection!
78 elseif x1 == x2 and y3 == y4 then
88 elseif x3 == x4 and y1 == y2 then
99 print('You should never see this message')
103 scene.intersection = {
112 function doesLineIntersectPlayerPaths(node, v1, v2)
113 -- for every line in path,
114 -- check intersection with player line
115 while node and node.prev do
116 local v3 = node.vector
117 local v4 = node.prev.vector
118 if doLinesIntersect(v1.x,v1.y, v2.x,v2.y, v3.x,v3.y, v4.x,v4.y) then
126 -- love collision handler
127 function collision(player)
128 print(player..' crashed!')
131 love.handlers.collision = collision
133 function scene:handleCollisions()
134 -- calculate the last line for each player from current position
135 -- check if line intersects any other path line
136 -- if so, raise collision event for player
137 -- only check last line for intersection, since all the rest should be ok
138 for _,player in pairs(self.players) do
139 local v1 = player.path.vector
140 local v2 = player.path.prev.vector
141 for _,player2 in pairs(self.players) do
142 if doesLineIntersectPlayerPaths(player2.path, v1, v2) then
143 love.event.push('collision', tostring(player))
150 function scene:update(dt)
151 if not self.paused then
152 self:updatePlayers(dt)
153 self:handleCollisions()
158 function scene:quit()
159 for _,player in pairs(self.players) do
160 print(tostring(player)..' generated '.. #player.path / 2 .. ' path points')