]> Untitled Git - lightcycles-love.git/blob - scene.lua
404801f5704b14c39c95bda1f451648557332f01
[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             and
73             y1 <= y3 and y3 <= y2
74     elseif x3 == x4 and y1 == y2 then
75         intersect =
76             x1 <= x3 and x3 <= x2
77             and
78             y3 <= y1 and y1 <= y4
79     else
80         print('foo')
81     end
82
83     if intersect then
84         scene.intersection = {
85             a = {x1,y1, x2,y2},
86             b = {x3,y3, x4,y4},
87             }
88     end
89
90     return intersect
91 end
92
93 function doesLineIntersectPlayerPaths(path, x1,y1, x2,y2)
94     local cache = {}
95     cache.a = {}
96     cache.b = {}
97     -- for every line in path,
98     -- check intersection with player line
99     for k,v in pairs(path) do
100         if #cache.a == 2 then
101             if #cache.b == 2 then
102                 if doLinesIntersect(x1,y1, x2,y2, cache.a[1],cache.a[2], cache.b[1],cache.b[2]) then
103                     return true
104                 end
105             end
106             cache.b = cache.a
107             cache.a = {}
108         end
109         table.insert(cache.a,v)
110     end
111     return false
112 end
113
114 -- love collision handler
115 function collision(player)
116     print('Player '..tostring(player)..' crashed!')
117     scene.paused = true
118     --love.event.quit()
119 end
120 love.handlers.collision = collision
121
122 function scene:handleCollisions()
123     -- calculate the last line for each player from current position
124     -- check if line intersects any other path line
125     -- if so, raise collision event for player
126     for _,player in pairs(self.players) do
127         local x1 = player.path[#player.path-1]
128         local y1 = player.path[#player.path]
129         local x2 = player.position.x
130         local y2 = player.position.y
131
132         -- check intersection against each existing path
133         for _,player2 in pairs(self.players) do
134             if doesLineIntersectPlayerPaths(player2.path, x1, y1, x2, y2) then
135                 love.event.push('collision', player)
136             end
137         end
138     end
139 end
140
141 function scene:update(dt)
142     self:updatePlayers(dt)
143     self:handleCollisions()
144 end
145
146 -- quit
147 function scene:quit()
148     for _,player in pairs(self.players) do
149         print(tostring(player)..' generated '.. #player.path / 2 .. ' path points')
150     end
151 end