]> Untitled Git - lightcycles-love.git/commitdiff
Extracted scene to separate file. Added second player
authorClifton James Palmer <clifton.palmer@gmail.com>
Sat, 8 Dec 2018 15:19:21 +0000 (09:19 -0600)
committerClifton James Palmer <clifton.palmer@gmail.com>
Sat, 8 Dec 2018 15:19:21 +0000 (09:19 -0600)
main.lua
scene.lua [new file with mode: 0644]

index c364b1300c11dea9cc187791e7b5a7993ff8a521..a0d0ca720923f8ec71259c9fa99719cc6b59c3d4 100644 (file)
--- a/main.lua
+++ b/main.lua
@@ -1,43 +1,23 @@
 -- main
 
-require 'vec2'
-require 'player'
+require 'scene'
 
 function love.load()
-    player = Player:new({position=vec2:new(100,100), vector=Player.vectors.right})
+    scene:load()
 end
 
 function love.draw()
-    -- set bg
-    local bgcolor = {0.2, 0.2, 0.5}
-    love.graphics.setBackgroundColor(bgcolor)
-
-    -- draw basic grid
-    love.graphics.setColor(0.3, 0.3, 0.6)
-    local width = love.graphics.getWidth()
-    local height = love.graphics.getHeight()
-    local delta = 50
-
-    for x=0,width,delta do
-        love.graphics.line(x, 0, x, height)
-    end
-
-    for y=0,height,delta do
-        love.graphics.line(0, y, width, y)
-    end
-
-    -- misc
-    player:draw()
+    scene:draw()
 end
 
 function love.update(dt)
     if love.keyboard.isDown('escape') then
         love.event.quit()
     end
-    player:update(dt)
+    scene:update(dt)
 end
 
 function love.quit()
     print('Thanks for playing!')
-    print('Recorded ' .. #player.path / 2 .. ' player path points')
+    scene:quit()
 end
diff --git a/scene.lua b/scene.lua
new file mode 100644 (file)
index 0000000..0ccc1a3
--- /dev/null
+++ b/scene.lua
@@ -0,0 +1,78 @@
+-- main
+
+require 'vec2'
+require 'player'
+
+
+scene = {}
+scene.width = love.graphics.getWidth()
+scene.height = love.graphics.getHeight()
+scene.players = {}
+scene.grid = {}
+scene.grid.bgcolor = {0.2, 0.2, 0.5}
+scene.grid.linecolor = {0.3, 0.3, 0.6}
+scene.grid.delta = 50
+
+-- load
+function scene:load()
+    table.insert(scene.players, Player:new({
+        position=vec2:new(100,100),
+        vector=Player.vectors.right,
+        path={},
+        }))
+
+    table.insert(scene.players, Player:new({
+        position=vec2:new(self.width-100,self.height-100),
+        vector=Player.vectors.left,
+        path={},
+        color={255, 255, 0},
+        keys={
+            up='up',
+            down='down',
+            left='left',
+            right='right',
+            }
+        }))
+end
+
+-- draw
+function scene:drawGrid()
+    love.graphics.setBackgroundColor(self.grid.bgcolor)
+    love.graphics.setColor(self.grid.linecolor)
+
+    for x=0,self.width,self.grid.delta do
+        love.graphics.line(x, 0, x, self.height)
+    end
+    for y=0,self.height,self.grid.delta do
+        love.graphics.line(0, y, self.width, y)
+    end
+end
+
+function scene:drawPlayers()
+    for _,player in pairs(self.players) do
+        player:draw()
+    end
+end
+
+function scene:draw()
+    self:drawGrid()
+    self:drawPlayers()
+end
+
+-- update
+function scene:updatePlayers(dt)
+    for _,player in pairs(self.players) do
+        player:update(dt)
+    end
+end
+
+function scene:update(dt)
+    self:updatePlayers(dt)
+end
+
+-- quit
+function scene:quit()
+    for i,player in pairs(self.players) do
+        print('Player '..i..' generated '.. #player.path / 2 .. ' path points')
+    end
+end