]> Untitled Git - lightcycles-love.git/blob - scene.lua
73164e335115a455e7f799ecb5059298b1726d7d
[lightcycles-love.git] / scene.lua
1 -- main
2
3 require 'vec2'
4 require 'player'
5
6
7 scene = {}
8 scene.width = love.graphics.getWidth()
9 scene.height = love.graphics.getHeight()
10 scene.players = {}
11 scene.grid = {}
12 scene.grid.bgcolor = {0.2, 0.2, 0.5}
13 scene.grid.linecolor = {0.3, 0.3, 0.6}
14 scene.grid.delta = 50
15
16 -- load
17 function scene:load()
18     table.insert(scene.players, require('players/1'))
19     table.insert(scene.players, require('players/2'))
20 end
21
22 -- draw
23 function scene:drawGrid()
24     love.graphics.setBackgroundColor(self.grid.bgcolor)
25     love.graphics.setColor(self.grid.linecolor)
26
27     for x=0,self.width,self.grid.delta do
28         love.graphics.line(x, 0, x, self.height)
29     end
30     for y=0,self.height,self.grid.delta do
31         love.graphics.line(0, y, self.width, y)
32     end
33 end
34
35 function scene:drawPlayers()
36     for _,player in pairs(self.players) do
37         player:draw()
38     end
39 end
40
41 function scene:draw()
42     self:drawGrid()
43     self:drawPlayers()
44 end
45
46 -- update
47 function scene:updatePlayers(dt)
48     for _,player in pairs(self.players) do
49         player:update(dt)
50     end
51 end
52
53 function doesLineIntersectPlayerPaths(path, x1, y1, x2, y2)
54     return false
55 end
56
57 function scene:handleCollisions()
58     -- calculate the last line for each player from current position
59     -- check if line intersects any other path line
60     -- if so, raise collision event for player
61     for _,player in pairs(self.players) do
62         local x1 = player.path[#player.path-1]
63         local y1 = player.path[#player.path]
64         local x2 = player.position.x
65         local y2 = player.position.y
66
67         -- check intersection against each existing path
68         for _,player2 in pairs(self.players) do
69             if doesLineIntersectPlayerPaths(player2.path, x1, y1, x2, y2) then
70                 love.event.push('collision', player)
71             end
72         end
73     end
74 end
75
76 function scene:update(dt)
77     self:updatePlayers(dt)
78     self:handleCollisions()
79 end
80
81 -- quit
82 function scene:quit()
83     for _,player in pairs(self.players) do
84         print(tostring(player)..' generated '.. #player.path / 2 .. ' path points')
85     end
86 end