]> Untitled Git - lightcycles-love.git/blob - scene.lua
Added collision detection and pause on crash
[lightcycles-love.git] / scene.lua
1 -- main
2
3 require 'vec2'
4 require 'player'
5
6
7 scene = {}
8 scene.paused = false
9 scene.width = love.graphics.getWidth()
10 scene.height = love.graphics.getHeight()
11 scene.players = {}
12 scene.grid = {}
13 scene.grid.bgcolor = {0.2, 0.2, 0.5}
14 scene.grid.linecolor = {0.3, 0.3, 0.6}
15 scene.grid.delta = 50
16
17 -- load
18 function scene:load()
19     table.insert(scene.players, require('players/1'))
20     --table.insert(scene.players, require('players/2'))
21 end
22
23 -- draw
24 function scene:drawGrid()
25     love.graphics.setBackgroundColor(self.grid.bgcolor)
26     love.graphics.setColor(self.grid.linecolor)
27
28     for x=0,self.width,self.grid.delta do
29         love.graphics.line(x, 0, x, self.height)
30     end
31     for y=0,self.height,self.grid.delta do
32         love.graphics.line(0, y, self.width, y)
33     end
34 end
35
36 function scene:drawPlayers()
37     for _,player in pairs(self.players) do
38         player:draw()
39     end
40 end
41
42 function scene:draw()
43     self:drawGrid()
44     self:drawPlayers()
45
46     -- draw intersection if it's there
47     if self.intersection then
48         love.graphics.setColor(0, 255, 0)
49         love.graphics.line(self.intersection)
50     end
51 end
52
53 -- update
54 function scene:updatePlayers(dt)
55     for _,player in pairs(self.players) do
56         player:update(dt)
57     end
58 end
59
60 function doLinesIntersect(x1, y1, x2, y2, x3, y3, x4, y4)
61     if x1 == x2 and x3 == x4
62     or y1 == y2 and y3 == y4
63     then
64         -- if lines are parallel, no intersection!
65         return false
66     else
67         -- if lines are not parallel, they must intersect
68         -- do segments overlap?
69         if
70             x1 <= x3 and x3 <= x2
71         and y1 <= y3 and y3 <= y2
72         or
73             x3 <= x1 and x1 <= x4
74         and y3 <= y1 and y1 <= y4
75         then
76             scene.paused = true
77             scene.intersection = {x1, y1, x2, y2, x3, y3, x4, y4}
78             return true
79         end
80     end
81 end
82
83 function doesLineIntersectPlayerPaths(path, x1, y1, x2, y2)
84     local stash = {}
85     -- for every line in path,
86     -- check intersection with player line
87     for k,v in pairs(path) do
88         if #stash == 4 then
89             if doLinesIntersect(x1, y1, x2, y2, stash[4], stash[3], stash[2], stash[1]) then
90                 return true
91             end
92             stash = {}
93         else
94             table.insert(stash,v)
95         end
96     end
97     return false
98 end
99
100 -- love collision handler
101 function collision(player)
102     print('Player '..tostring(player)..' crashed!')
103     --love.event.quit()
104 end
105 love.handlers.collision = collision
106
107 function scene:handleCollisions()
108     -- calculate the last line for each player from current position
109     -- check if line intersects any other path line
110     -- if so, raise collision event for player
111     for _,player in pairs(self.players) do
112         local x1 = player.path[#player.path-1]
113         local y1 = player.path[#player.path]
114         local x2 = player.position.x
115         local y2 = player.position.y
116
117         -- check intersection against each existing path
118         for _,player2 in pairs(self.players) do
119             if doesLineIntersectPlayerPaths(player2.path, x1, y1, x2, y2) then
120                 love.event.push('collision', player)
121             end
122         end
123     end
124 end
125
126 function scene:update(dt)
127     self:updatePlayers(dt)
128     self:handleCollisions()
129 end
130
131 -- quit
132 function scene:quit()
133     for _,player in pairs(self.players) do
134         print(tostring(player)..' generated '.. #player.path / 2 .. ' path points')
135     end
136 end