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