]> Untitled Git - lightcycles-love.git/blob - scene.lua
Increased player speed
[lightcycles-love.git] / scene.lua
1 -- main
2
3 require 'vec2'
4 require 'player'
5
6
7 scene = {}
8 scene.debug = false
9 scene.paused = false
10 scene.width = love.graphics.getWidth()
11 scene.height = love.graphics.getHeight()
12 scene.players = {}
13 scene.grid = {}
14 scene.grid.bgcolor = {0.2, 0.2, 0.5}
15 scene.grid.linecolor = {0.3, 0.3, 0.6}
16 scene.grid.delta = 50
17
18 -- load
19 function scene:load()
20     table.insert(scene.players, require('players/1'))
21     table.insert(scene.players, require('players/2'))
22 end
23
24 -- draw
25 function scene:drawGrid()
26     love.graphics.setBackgroundColor(self.grid.bgcolor)
27     love.graphics.setColor(self.grid.linecolor)
28
29     love.graphics.setLineWidth(2)
30     for x=0,self.width,self.grid.delta do
31         love.graphics.line(x, 0, x, self.height)
32     end
33     for y=0,self.height,self.grid.delta do
34         love.graphics.line(0, y, self.width, y)
35     end
36
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)
40     end
41     for y=0,self.height,self.grid.delta/2 do
42         love.graphics.line(0, y, self.width, y)
43     end
44 end
45
46 function scene:drawPlayers()
47     for _,player in pairs(self.players) do
48         player:draw()
49     end
50 end
51
52 function scene:draw()
53     self:drawGrid()
54     self:drawPlayers()
55
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)
62     end
63 end
64
65 -- update
66 function scene:updatePlayers(dt)
67     for _,player in pairs(self.players) do
68         player:update(dt)
69     end
70 end
71
72 function doLinesIntersect(x1,y1, x2,y2, x3,y3, x4,y4)
73     local intersect = false
74
75     if x1 == x2 and x3 == x4
76     or y1 == y2 and y3 == y4
77     then
78         -- if lines are parallel, no intersection!
79     elseif x1 == x2 and y3 == y4 then
80         intersect = (
81                 x3 < x1 and x1 < x4
82                 or
83                 x4 < x1 and x1 < x3
84             ) and (
85                 y1 < y3 and y3 < y2
86                 or
87                 y2 < y3 and y3 < y1
88             )
89     elseif x3 == x4 and y1 == y2 then
90         intersect = (
91                 x1 < x3 and x3 < x2
92                 or
93                 x2 < x3 and x3 < x1
94             ) and (
95                 y3 < y1 and y1 < y4
96                 or
97                 y4 < y1 and y1 < y3
98             )
99     else
100         print('You should never see this message')
101     end
102
103     if intersect then
104         scene.intersection = {
105             a = {x1,y1, x2,y2},
106             b = {x3,y3, x4,y4},
107             }
108     end
109
110     return intersect
111 end
112
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
120             return true
121         end
122         node = node.prev
123     end
124     return false
125 end
126
127 -- love collision handler
128 function collision(player)
129     print(player..' crashed!')
130     scene.paused = true
131 end
132 love.handlers.collision = collision
133
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
142
143         -- outside boundary
144         if v1.x < 0 or v1.x > love.graphics.getWidth()
145         or v1.y < 0 or v1.y > love.graphics.getHeight()
146         then
147             love.event.push('collision', tostring(player))
148             break
149         end
150
151         -- inside boundary
152         for _,player2 in pairs(self.players) do
153             if doesLineIntersectPlayerPaths(player2.path, v1, v2) then
154                 love.event.push('collision', tostring(player))
155                 break
156             end
157         end
158     end
159 end
160
161 function scene:update(dt)
162     if not self.paused then
163         self:updatePlayers(dt)
164         self:handleCollisions()
165     end
166 end
167
168 -- quit
169 function scene:quit()
170     for _,player in pairs(self.players) do
171         print(tostring(player)..' generated '.. #player.path / 2 .. ' path points')
172     end
173 end