10 scene.width = love.graphics.getWidth()
11 scene.height = love.graphics.getHeight()
14 scene.grid.bgcolor = {0.2, 0.2, 0.5}
15 scene.grid.linecolor = {0.3, 0.3, 0.6}
20 table.insert(scene.players, require('players/1'))
21 table.insert(scene.players, require('players/2'))
25 function scene:drawGrid()
26 love.graphics.setBackgroundColor(self.grid.bgcolor)
27 love.graphics.setColor(self.grid.linecolor)
29 love.graphics.setLineWidth(2)
30 for x=0,self.width,self.grid.delta do
31 love.graphics.line(x, 0, x, self.height)
33 for y=0,self.height,self.grid.delta do
34 love.graphics.line(0, y, self.width, y)
37 love.graphics.setLineWidth(0.5)
38 for x=0,self.width,self.grid.delta/2 do
39 love.graphics.line(x, 0, x, self.height)
41 for y=0,self.height,self.grid.delta/2 do
42 love.graphics.line(0, y, self.width, y)
46 function scene:drawPlayers()
47 for _,player in pairs(self.players) do
56 -- draw intersection if it's there
57 if self.debug and self.intersection then
58 love.graphics.setColor(0, 255, 0)
59 love.graphics.line(self.intersection.a)
60 love.graphics.setColor(0, 0, 255)
61 love.graphics.line(self.intersection.b)
66 function scene:updatePlayers(dt)
67 for _,player in pairs(self.players) do
72 function doLinesIntersect(x1,y1, x2,y2, x3,y3, x4,y4)
73 local intersect = false
75 if x1 == x2 and x3 == x4
76 or y1 == y2 and y3 == y4
78 -- if lines are parallel, no intersection!
79 elseif x1 == x2 and y3 == y4 then
89 elseif x3 == x4 and y1 == y2 then
100 print('You should never see this message')
104 scene.intersection = {
113 function doesLineIntersectPlayerPaths(node, v1, v2)
114 -- for every line in path,
115 -- check intersection with player line
116 while node and node.prev do
117 local v3 = node.vector
118 local v4 = node.prev.vector
119 if doLinesIntersect(v1.x,v1.y, v2.x,v2.y, v3.x,v3.y, v4.x,v4.y) then
127 -- love collision handler
128 function collision(player)
129 print(player..' crashed!')
132 love.handlers.collision = collision
134 function scene:handleCollisions()
135 -- calculate the last line for each player from current position
136 -- check if line intersects any other path line
137 -- if so, raise collision event for player
138 -- only check last line for intersection, since all the rest should be ok
139 for _,player in pairs(self.players) do
140 local v1 = player.path.vector
141 local v2 = player.path.prev.vector
144 if v1.x < 0 or v1.x > love.graphics.getWidth()
145 or v1.y < 0 or v1.y > love.graphics.getHeight()
147 love.event.push('collision', tostring(player))
152 for _,player2 in pairs(self.players) do
153 if doesLineIntersectPlayerPaths(player2.path, v1, v2) then
154 love.event.push('collision', tostring(player))
161 function scene:update(dt)
162 if not self.paused then
163 self:updatePlayers(dt)
164 self:handleCollisions()
169 function scene:quit()
170 for _,player in pairs(self.players) do
171 print(tostring(player)..' generated '.. #player.path / 2 .. ' path points')