]> Untitled Git - lightcycles-love.git/blob - scene.lua
213d34ad9c668f8c44b2fe8f383a84d6662b11c2
[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.a)
50         love.graphics.setColor(0, 0, 255)
51         love.graphics.line(self.intersection.b)
52     end
53 end
54
55 -- update
56 function scene:updatePlayers(dt)
57     for _,player in pairs(self.players) do
58         player:update(dt)
59     end
60 end
61
62 function doLinesIntersect(x1,y1, x2,y2, x3,y3, x4,y4)
63     local intersect = false
64
65     if x1 == x2 and x3 == x4
66     or y1 == y2 and y3 == y4
67     then
68         -- if lines are parallel, no intersection!
69     elseif x1 == x2 and y3 == y4 then
70         intersect = (
71                 x3 < x1 and x1 < x4
72                 or
73                 x4 < x1 and x1 < x3
74             ) and (
75                 y1 < y3 and y3 < y2
76                 or
77                 y2 < y3 and y3 < y1
78             )
79     elseif x3 == x4 and y1 == y2 then
80         intersect = (
81                 x1 < x3 and x3 < x2
82                 or
83                 x2 < x3 and x3 < x1
84             ) and (
85                 y3 < y1 and y1 < y4
86                 or
87                 y4 < y1 and y1 < y3
88             )
89     else
90         print('You should never see this message')
91     end
92
93     if intersect then
94         scene.intersection = {
95             a = {x1,y1, x2,y2},
96             b = {x3,y3, x4,y4},
97             }
98     end
99
100     return intersect
101 end
102
103 function doesLineIntersectPlayerPaths(node, v1, v2)
104     -- for every line in path,
105     -- check intersection with player line
106     while node and node.prev do
107         local v3 = node.vector
108         local v4 = node.prev.vector
109         if doLinesIntersect(v1.x,v1.y, v2.x,v2.y, v3.x,v3.y, v4.x,v4.y) then
110             return true
111         end
112         node = node.prev
113     end
114     return false
115 end
116
117 -- love collision handler
118 function collision(player)
119     print(player..' crashed!')
120     scene.paused = true
121 end
122 love.handlers.collision = collision
123
124 function scene:handleCollisions()
125     -- calculate the last line for each player from current position
126     -- check if line intersects any other path line
127     -- if so, raise collision event for player
128     -- only check last line for intersection, since all the rest should be ok
129     for _,player in pairs(self.players) do
130         local v1 = player.path.vector
131         local v2 = player.path.prev.vector
132         for _,player2 in pairs(self.players) do
133             if doesLineIntersectPlayerPaths(player2.path, v1, v2) then
134                 love.event.push('collision', tostring(player))
135                 break
136             end
137         end
138     end
139 end
140
141 function scene:update(dt)
142     if not self.paused then
143         self:updatePlayers(dt)
144         self:handleCollisions()
145     end
146 end
147
148 -- quit
149 function scene:quit()
150     for _,player in pairs(self.players) do
151         print(tostring(player)..' generated '.. #player.path / 2 .. ' path points')
152     end
153 end