Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require("class")
- engine = {} -- Engine package
- -- Love wrappers
- function love.load()
- engine.game:load()
- end
- function love.quit()
- engine.game:quit()
- end
- function love.focus(f)
- engine.game:focus(f)
- end
- function love.update(dt)
- engine.game:update(dt)
- end
- function love.draw()
- engine.game:draw()
- end
- -- Engine class
- engine.Engine = newClass()
- function engine.Engine:init()
- self:setSize()
- self:setTitle()
- self.focused = true
- self.fading = false
- self.fade = 1
- self.fadeSpeed = 0.5
- self.fadeColor = {255, 255, 255}
- self:setScene(engine.Entity)
- end
- -- Sets the engine subclass to load
- function engine.Engine:setGame()
- engine.game = self:new()
- end
- -- Engine methods
- function engine.Engine:setTitle(title)
- self.title = title or "Game Engine"
- love.graphics.setCaption(self.title)
- end
- function engine.Engine:setSize(width, height)
- self.width = width or 640
- self.height = height or 480
- love.graphics.setMode(self.width, self.height)
- end
- function engine.Engine:fadeToScene(scene, ...)
- self.fadeTo = scene
- self.fadeArgs = ...
- self.fading = true
- end
- function engine.Engine:setScene(scene, ...)
- if self.scene then self.scene:destroy() end
- self.scene = scene:new(self)
- self.scene:setup(...)
- end
- function engine.Engine:toggleFullscreen()
- love.graphics.toggleFullscreen()
- end
- -- Wrapped Love functions
- function engine.Engine:load()
- self:setup()
- end
- function engine.Engine:quit()
- end
- function engine.Engine:focus(f)
- self.focused = f
- end
- function engine.Engine:update(dt)
- if self.fading then
- if self.fade >= 1 then
- self.fade = 1
- self.fading = false
- self:setScene(self.fadeTo, self.fadeArgs)
- else
- self.fade = self.fade + self.fadeSpeed * dt
- if self.fade > 1 then self.fade = 1 end
- end
- else
- if self.fade <= 0 then
- self.fade = 0
- else
- self.fade = self.fade - self.fadeSpeed * dt
- if self.fade < 0 then self.fade = 0 end
- end
- end
- self.scene:masterUpdate(dt)
- end
- function engine.Engine:draw()
- self.scene:render()
- love.graphics.setCanvas()
- self.scene:postRender()
- self:drawFade()
- end
- -- Private methods
- function engine.Engine:drawFade()
- love.graphics.setBlendMode("alpha")
- love.graphics.setColor(self.fadeColor[1], self.fadeColor[2], self.fadeColor[3], (math.cos((1 - self.fade) * math.pi) + 1) * 127)
- love.graphics.polygon("fill", 0, 0, self.width, 0, self.width, self.height, 0, self.height)
- end
- -- Virtual methods
- function engine.Engine:setup()
- end
- -- Entity class
- engine.Entity = newClass()
- function engine.Entity:init(engine)
- self.engine = engine
- self:setSize()
- self:setPosition()
- self:setOrientation()
- self:setScale()
- self:setColor()
- self.children = {}
- end
- function engine.Entity:addChild(child, ...)
- table.insert(self.children, child)
- child:setup(...)
- end
- function engine.Entity:removeChild(child)
- table.remove(self.children, child)
- child:destroy()
- end
- function engine.Entity:setColor(color)
- self.color = color or {255, 255, 255}
- end
- function engine.Entity:setPosition(x, y)
- self.x = x or 0
- self.y = y or 0
- end
- function engine.Entity:setOrientation(r)
- self.orientation = r or 0
- end
- function engine.Entity:setScale(x, y)
- self.scaleX = x or 1
- self.scaleY = y or 1
- end
- function engine.Entity:setSize(width, height)
- self.width = width or self.engine.width
- self.height = height or self.engine.height
- self.canvas = love.graphics.newCanvas(self.width, self.height)
- end
- function engine.Entity:render()
- love.graphics.setCanvas(self.canvas)
- love.graphics.setColor(love.graphics.getBackgroundColor())
- love.graphics.setBlendMode("alpha")
- love.graphics.polygon("fill", 0, 0, self.width, 0, self.width, self.height, 0, self.height)
- self:preDraw()
- for k, v in pairs(self.children) do
- v:render()
- love.graphics.setCanvas(self.canvas)
- v:postRender()
- end
- self:draw()
- end
- function engine.Entity:postRender()
- love.graphics.setBlendMode("alpha")
- love.graphics.setColor(self.color[1], self.color[2], self.color[3])
- 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)
- end
- function engine.Entity:masterUpdate(dt)
- for k, v in pairs(self.children) do
- v:masterUpdate(dt)
- end
- self:update(dt)
- end
- -- Virtual methods
- function engine.Entity:setup(...)
- end
- function engine.Entity:destroy()
- end
- function engine.Entity:update(dt)
- end
- function engine.Entity:preDraw()
- end
- function engine.Entity:draw()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement