diff --git a/src/engine.lua b/src/engine.lua new file mode 100644 index 0000000..210cef6 --- /dev/null +++ b/src/engine.lua @@ -0,0 +1,120 @@ +local HC = require 'libs/hardoncollider' +-- has to come before the sti initialization so I can add the collision tiles +collider = HC(100) + +local sti = require "libs/sti" +local object = require "object" +local ui = require "ui" + +Engine = class() + +Engine.objects = {} + +function Engine:__init() + self.windowWidth = love.graphics.getWidth() + self.windowHeight = love.graphics.getHeight() + + self.map = sti.new("assets/map") + self.collision = self.map:getCollisionMap("collision") + + self:initObjects() +end + +function Engine:checkObjectAnimation(key) + for _, object in ipairs(self.objects) do + if object.relevantKeys then + for _, relevantKey in ipairs(object.relevantKeys) do + if key == relevantKey then + object.controller:stopAnimation() + end + end + end + end +end + +function Engine:checkObjectKeys(dt) + if not ui.active then + for _, object in ipairs(self.objects) do + object.controller.animation:update(dt) + + if object.relevantKeys then + for _, relevantKey in ipairs(object.relevantKeys) do + if love.keyboard.isDown(relevantKey) then + object.controller:sendKey(relevantKey) + end + end + end + end + else + for _, relevantKey in ipairs(ui.relevantKeys) do + if love.keyboard.isDown(relevantKey) then + ui:sendKey(relevantKey) + end + end + end +end + +function Engine:update(dt) + self:checkObjectKeys(dt) + collider:update(dt) + self.map:update(dt) +end + +function Engine:draw() + local translateX = 0 + local translateY = 0 + + -- Draw Range culls unnecessary tiles + self.map:setDrawRange(translateX, translateY, self.windowWidth, self.windowHeight) + + self.map:draw() + ui:draw() + + --self:debugDrawing() +end + +function Engine:initObjects() + self.map:addCustomLayer("object layer", 4) + + local objectLayer = self.map.layers["object layer"] + objectLayer.sprites = {} + + for _, obj in pairs(self.map.layers["objects"].objects) do + local filename = "objects/" .. obj.name .. ".lua" + local objectinfo = love.filesystem.load(filename)() + objectinfo.x = obj.x - object.width / 4 + objectinfo.y = obj.y + + objectinfo.controller.objectinfo = objectinfo + + objectinfo.image = love.graphics.newImage(objectinfo.spritesheet) + objectinfo.controller:init() + + table.insert(objectLayer.sprites, {info = objectinfo}) + table.insert(self.objects, objectinfo) + end + + -- Draw callback for Custom Layer + function objectLayer:draw() + table.sort(self.sprites, object.sortNorthSouth) + + for _, sprite in pairs(self.sprites) do + local x = math.floor(sprite.info.x) + local y = math.floor(sprite.info.y) + + sprite.info.controller.animation:draw(sprite.info.image, x, y) + end + end + + self.map.layers["objects"].visible = false +end + +function Engine:debugDrawing() + -- draw collision shapes for debugging + for shape in pairs(collider:shapesInRange(0, 0, self.windowWidth, self.windowHeight)) do + shape:draw() + end + + -- Draw Collision Map (useful for debugging) + self.map:drawCollisionMap(collision) +end diff --git a/src/main.lua b/src/main.lua index 2f32394..c2e9f0a 100644 --- a/src/main.lua +++ b/src/main.lua @@ -1,122 +1,21 @@ -local HC = require 'libs/hardoncollider' --- has to come before the sti initialization so I can add the collision tiles -collider = HC(100) - class = require 'libs/30log' -local sti = require "libs/sti" - -local object = require "object" -local ui = require "ui" - -local objects = {} +local engine = require 'engine' function love.load(arg) if arg[#arg] == "-debug" then require("mobdebug").start() end -- debugging in ZeroBraineStudio - windowWidth = love.graphics.getWidth() - windowHeight = love.graphics.getHeight() - - map = sti.new("assets/map") - collision = map:getCollisionMap("collision") - - initObjects() - ui:showMessage("Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?") + engine = Engine() end function love.update(dt) - if not ui.active then - for _, object in ipairs(objects) do - object.controller.animation:update(dt) - - if object.relevantKeys then - for _, relevantKey in ipairs(object.relevantKeys) do - if love.keyboard.isDown(relevantKey) then - object.controller:sendKey(relevantKey) - end - end - end - end - else - for _, relevantKey in ipairs(ui.relevantKeys) do - if love.keyboard.isDown(relevantKey) then - ui:sendKey(relevantKey) - end - end - end - - collider:update(dt) - map:update(dt) + engine:update(dt) end function love.keyreleased(key) - for _, object in ipairs(objects) do - if object.relevantKeys then - for _, relevantKey in ipairs(object.relevantKeys) do - if key == relevantKey then - object.controller:stopAnimation() - end - end - end - end + engine:checkObjectAnimation(key) end function love.draw() - -- Translation would normally be based on a player's x/y - local translateX = 0 - local translateY = 0 - - -- Draw Range culls unnecessary tiles - map:setDrawRange(translateX, translateY, windowWidth, windowHeight) - - map:draw() - ui:draw() - - --debugDrawing() -end - -function debugDrawing() - -- draw collision shapes for debugging - for shape in pairs(collider:shapesInRange(0, 0, windowWidth, windowHeight)) do - shape:draw() - end - - -- Draw Collision Map (useful for debugging) - map:drawCollisionMap(collision) -end - -function initObjects() - map:addCustomLayer("object layer", 4) - - local objectLayer = map.layers["object layer"] - objectLayer.sprites = {} - - for _, obj in pairs(map.layers["objects"].objects) do - local filename = "objects/" .. obj.name .. ".lua" - local objectinfo = love.filesystem.load(filename)() - objectinfo.x = obj.x - object.width / 4 - objectinfo.y = obj.y - - objectinfo.controller.objectinfo = objectinfo - - objectinfo.image = love.graphics.newImage(objectinfo.spritesheet) - objectinfo.controller:init() - - table.insert(objectLayer.sprites, {info = objectinfo}) - table.insert(objects, objectinfo) - end - - -- Draw callback for Custom Layer - function objectLayer:draw() - table.sort(self.sprites, object.sortNorthSouth) - - for _, sprite in pairs(self.sprites) do - local x = math.floor(sprite.info.x) - local y = math.floor(sprite.info.y) - - sprite.info.controller.animation:draw(sprite.info.image, x, y) - end - end - - map.layers["objects"].visible = false + engine:draw() end