From cca4d9c1bf4033c79e4bd61a257c6ea02557524c Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 19 Mar 2017 04:19:59 +0100 Subject: Moving draw away to abstract classes --- not/Decoration.lua | 11 +---------- not/Hero.lua | 27 +++++++++++++-------------- not/PhysicalBody.lua | 1 + not/Platform.lua | 13 ++----------- not/Sprite.lua | 46 +++++++++++++++++++++++++++++++++++++++++++--- 5 files changed, 60 insertions(+), 38 deletions(-) (limited to 'not') diff --git a/not/Decoration.lua b/not/Decoration.lua index 5bfc328..a57c143 100644 --- a/not/Decoration.lua +++ b/not/Decoration.lua @@ -23,14 +23,5 @@ function Decoration:getPosition() return self.x, self.y end function Decoration:draw(offset_x, offset_y, scale) - -- locals - local offset_x = offset_x or 0 - local offset_y = offset_y or 0 - local scale = scale or 1 - local x, y = self:getPosition() - -- pixel grid - local draw_x = (math.floor(x) + offset_x) * scale - local draw_y = (math.floor(y) + offset_y) * scale - -- draw - Sprite.draw(self, draw_x, draw_y, 0, scale, scale) + Sprite.draw(self, offset_x, offset_y, scale) end \ No newline at end of file diff --git a/not/Hero.lua b/not/Hero.lua index a6facd9..7be3104 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -280,24 +280,23 @@ function Hero:controlreleased (set, action, key) end end +-- TODO: comment them and place them somewhere properly +function Hero:getAngle () + return self.angle +end +function Hero:getHorizontalMirror() + return self.facing +end +function Hero:getOffset () + return 12,15 -- TODO: WHY? How about creating body as polygon and using 0,0 instead. LIKE EVERYWHERE ELSE? Make it obsolete both in here and in `not.Sprite`. +end + -- Draw of `Hero` -- TODO: see `not.PhysicalBody.draw` and `not.Sprite.draw`. function Hero:draw (offset_x, offset_y, scale, debug) - -- draw only alive if not self.alive then return end - -- locals - local offset_x = offset_x or 0 - local offset_y = offset_y or 0 - local scale = scale or 1 - local debug = debug or false - local x, y = self:getPosition() - -- pixel grid ; `approx` selected to prevent floating characters on certain conditions - local approx = math.floor - if (y - math.floor(y)) > 0.5 then approx = math.ceil end - local draw_y = (approx(y) + offset_y) * scale - local draw_x = (math.floor(x) + offset_x) * scale - -- sprite draw - Sprite.draw(self, draw_x, draw_y, self.angle, self.facing*scale, scale, 12, 15) + PhysicalBody.draw(self, offset_x, offset_y, scale, debug) + -- debug draw if debug then for _,fixture in pairs(self.body:getFixtureList()) do diff --git a/not/PhysicalBody.lua b/not/PhysicalBody.lua index 52e9357..bbea3ac 100644 --- a/not/PhysicalBody.lua +++ b/not/PhysicalBody.lua @@ -30,4 +30,5 @@ end -- Draw of `PhysicalBody`. function PhysicalBody:draw (offset_x, offset_y, scale, debug) -- TODO: Move debug part here from `not.Hero.draw`. + Sprite.draw(self, offset_x, offset_y, scale) end diff --git a/not/Platform.lua b/not/Platform.lua index ecf0377..7dcea6c 100644 --- a/not/Platform.lua +++ b/not/Platform.lua @@ -55,17 +55,8 @@ end -- Draw of `Platform` -- TODO: see todos in `not.Sprite.draw`. function Platform:draw (offset_x, offset_y, scale, debug) - -- locals - local offset_x = offset_x or 0 - local offset_y = offset_y or 0 - local scale = scale or 1 - local debug = debug or false - local x, y = self:getPosition() - -- pixel grid - local draw_x = (math.floor(x) + offset_x) * scale - local draw_y = (math.floor(y) + offset_y) * scale - -- sprite draw - Sprite.draw(self, draw_x, draw_y, 0, scale, scale) + Sprite.draw(self, offset_x, offset_y, scale) + -- debug draw if debug then love.graphics.setColor(255, 69, 0, 140) diff --git a/not/Sprite.lua b/not/Sprite.lua index f9a9cb4..a8785d3 100644 --- a/not/Sprite.lua +++ b/not/Sprite.lua @@ -79,16 +79,56 @@ function Sprite:getQuad () end end +-- TODO: Following five methods are stupid, do something about them! +-- Sprite can't be moved by itself. Positioning should be handled by children's methods. +function Sprite:getPosition () + return 0,0 +end +-- Sprite can't be rotated by itself. Rotation should be handled by children's methods. +function Sprite:getAngle () + return 0 +end +-- Sprite can't be mirrored by itself. Mirroring should be handled by children's methods. +function Sprite:getHorizontalMirror () + return 1 +end +function Sprite:getVerticalMirror () + return 1 +end +-- Sprite can't be offset by itself. Offsetting should be handled by children's methods. +function Sprite:getOffset () + return 0,0 +end + -- Drawing self to LOVE2D buffer. -- If there is no Quad, it will draw entire image. It won't draw anything if there is no image. -- TODO: it doesn't follow same pattern as `not.Hero.draw`. It should implement so it can be called from `not.World`. -- TODO: change children if above changes are in effect: `not.Platform`, `not.Decoration`. -function Sprite:draw (...) +function Sprite:draw (offset_x, offset_y, scale, debug) + local offset_x = offset_x or 0 + local offset_y = offset_y or 0 + local debug = debug or false + local i, q = self:getImage(), self:getQuad() + local x, y = self:getPosition() + local angle = self:getAngle() + + local scaleX = self:getHorizontalMirror()*(scale or 1) + local scaleY = self:getVerticalMirror()*(scale or 1) + + -- pixel grid ; `approx` selected to prevent floating characters on certain conditions + local approx = math.floor + if (y - math.floor(y)) > 0.5 then approx = math.ceil end + local draw_y = (approx(y) + offset_y) * scale + local draw_x = (math.floor(x) + offset_x) * scale + if i then love.graphics.setColor(255,255,255,255) - if q then love.graphics.draw(i, q, ...) - else love.graphics.draw(i, ...) end + if q then + love.graphics.draw(i, q, draw_x, draw_y, angle, scaleX, scaleY, self:getOffset()) + else + love.graphics.draw(i, draw_x, draw_y, angle, scaleX, scaleY, self:getOffset()) + end end end -- cgit v1.1