Advertisement
MegaLoler

Love Game Engine

Apr 7th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.62 KB | None | 0 0
  1. require("class")
  2.  
  3. engine = {} -- Engine package
  4.  
  5. -- Love wrappers
  6.  
  7. function love.load()
  8.     engine.game:load()
  9. end
  10.  
  11. function love.quit()
  12.     engine.game:quit()
  13. end
  14.  
  15. function love.focus(f)
  16.     engine.game:focus(f)
  17. end
  18.  
  19. function love.update(dt)
  20.     engine.game:update(dt)
  21. end
  22.  
  23. function love.draw()
  24.     engine.game:draw()
  25. end
  26.  
  27. -- Engine class
  28.  
  29. engine.Engine = newClass()
  30.  
  31. function engine.Engine:init()
  32.     self:setSize()
  33.     self:setTitle()
  34.     self.focused = true
  35.     self.fading = false
  36.     self.fade = 1
  37.     self.fadeSpeed = 0.5
  38.     self.fadeColor = {255, 255, 255}
  39.     self:setScene(engine.Entity)
  40. end
  41.  
  42. -- Sets the engine subclass to load
  43.  
  44. function engine.Engine:setGame()
  45.     engine.game = self:new()
  46. end
  47.  
  48. -- Engine methods
  49.  
  50. function engine.Engine:setTitle(title)
  51.     self.title = title or "Game Engine"
  52.     love.graphics.setCaption(self.title)
  53. end
  54.  
  55. function engine.Engine:setSize(width, height)
  56.     self.width = width or 640
  57.     self.height = height or 480
  58.     love.graphics.setMode(self.width, self.height)
  59. end
  60.  
  61. function engine.Engine:fadeToScene(scene, ...)
  62.     self.fadeTo = scene
  63.     self.fadeArgs = ...
  64.     self.fading = true
  65. end
  66.  
  67. function engine.Engine:setScene(scene, ...)
  68.     if self.scene then self.scene:destroy() end
  69.     self.scene = scene:new(self)
  70.     self.scene:setup(...)
  71. end
  72.  
  73. function engine.Engine:toggleFullscreen()
  74.     love.graphics.toggleFullscreen()
  75. end
  76.  
  77. -- Wrapped Love functions
  78.  
  79. function engine.Engine:load()
  80.     self:setup()
  81. end
  82.  
  83. function engine.Engine:quit()
  84. end
  85.  
  86. function engine.Engine:focus(f)
  87.     self.focused = f
  88. end
  89.  
  90. function engine.Engine:update(dt)
  91.     if self.fading then
  92.         if self.fade >= 1 then
  93.             self.fade = 1
  94.             self.fading = false
  95.             self:setScene(self.fadeTo, self.fadeArgs)
  96.         else
  97.             self.fade = self.fade + self.fadeSpeed * dt
  98.             if self.fade > 1 then self.fade = 1 end
  99.         end
  100.     else
  101.         if self.fade <= 0 then
  102.             self.fade = 0
  103.         else
  104.             self.fade = self.fade - self.fadeSpeed * dt
  105.             if self.fade < 0 then self.fade = 0 end
  106.         end
  107.     end
  108.     self.scene:masterUpdate(dt)
  109. end
  110.  
  111. function engine.Engine:draw()
  112.     self.scene:render()
  113.     love.graphics.setCanvas()
  114.     self.scene:postRender()
  115.     self:drawFade()
  116. end
  117.  
  118. -- Private methods
  119.  
  120. function engine.Engine:drawFade()
  121.     love.graphics.setBlendMode("alpha")
  122.     love.graphics.setColor(self.fadeColor[1], self.fadeColor[2], self.fadeColor[3], (math.cos((1 - self.fade) * math.pi) + 1) * 127)
  123.     love.graphics.polygon("fill", 0, 0, self.width, 0, self.width, self.height, 0, self.height)
  124. end
  125.  
  126. -- Virtual methods
  127.  
  128. function engine.Engine:setup()
  129. end
  130.  
  131. -- Entity class
  132.  
  133. engine.Entity = newClass()
  134.  
  135. function engine.Entity:init(engine)
  136.     self.engine = engine
  137.     self:setSize()
  138.     self:setPosition()
  139.     self:setOrientation()
  140.     self:setScale()
  141.     self:setColor()
  142.     self.children = {}
  143. end
  144.  
  145. function engine.Entity:addChild(child, ...)
  146.     table.insert(self.children, child)
  147.     child:setup(...)
  148. end
  149.  
  150. function engine.Entity:removeChild(child)
  151.     table.remove(self.children, child)
  152.     child:destroy()
  153. end
  154.  
  155. function engine.Entity:setColor(color)
  156.     self.color = color or {255, 255, 255}
  157. end
  158.  
  159. function engine.Entity:setPosition(x, y)
  160.     self.x = x or 0
  161.     self.y = y or 0
  162. end
  163.  
  164. function engine.Entity:setOrientation(r)
  165.     self.orientation = r or 0
  166. end
  167.  
  168. function engine.Entity:setScale(x, y)
  169.     self.scaleX = x or 1
  170.     self.scaleY = y or 1
  171. end
  172.  
  173. function engine.Entity:setSize(width, height)
  174.     self.width = width or self.engine.width
  175.     self.height = height or self.engine.height
  176.     self.canvas = love.graphics.newCanvas(self.width, self.height)
  177. end
  178.  
  179. function engine.Entity:render()
  180.     love.graphics.setCanvas(self.canvas)
  181.     love.graphics.setColor(love.graphics.getBackgroundColor())
  182.     love.graphics.setBlendMode("alpha")
  183.     love.graphics.polygon("fill", 0, 0, self.width, 0, self.width, self.height, 0, self.height)
  184.    
  185.     self:preDraw()
  186.    
  187.     for k, v in pairs(self.children) do
  188.         v:render()
  189.         love.graphics.setCanvas(self.canvas)
  190.         v:postRender()
  191.     end
  192.    
  193.     self:draw()
  194. end
  195.  
  196. function engine.Entity:postRender()
  197.     love.graphics.setBlendMode("alpha")
  198.     love.graphics.setColor(self.color[1], self.color[2], self.color[3])
  199.     love.graphics.drawq(self.canvas, love.graphics.newQuad(0, 0, self.width, self.height, self.width, self.height), self.x + self.width / 2, self.y + self.height / 2, self.orientation, self.scaleX, self.scaleY, self.width / 2, self.height / 2)
  200. end
  201.  
  202. function engine.Entity:masterUpdate(dt)
  203.     for k, v in pairs(self.children) do
  204.         v:masterUpdate(dt)
  205.     end
  206.     self:update(dt)
  207. end
  208.  
  209. -- Virtual methods
  210.  
  211. function engine.Entity:setup(...)
  212. end
  213.  
  214. function engine.Entity:destroy()
  215. end
  216.  
  217. function engine.Entity:update(dt)
  218. end
  219.  
  220. function engine.Entity:preDraw()
  221. end
  222.  
  223. function engine.Entity:draw()
  224. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement