diff options
| author | Eltrac <hi@guhub.cn> | 2025-03-13 10:55:24 +0800 |
|---|---|---|
| committer | Eltrac <hi@guhub.cn> | 2025-03-13 10:55:24 +0800 |
| commit | aa3d4e377fddf077e2b8cc77f17c1eefd5f97b5e (patch) | |
| tree | f13c7b5caedb666c696e3cf91a1b36295b85f0c6 | |
| parent | 7a48e0303241ff30572028d3c8cc303c09b04fa3 (diff) | |
refactor: remove body:draw method
| -rw-r--r-- | objects/Body.lua | 30 | ||||
| -rw-r--r-- | objects/Obstacle.lua | 4 | ||||
| -rw-r--r-- | render/draws/objects.lua | 32 |
3 files changed, 19 insertions, 47 deletions
diff --git a/objects/Body.lua b/objects/Body.lua index 4ca50d1..30ac165 100644 --- a/objects/Body.lua +++ b/objects/Body.lua @@ -15,34 +15,4 @@ function Body:new(shape, sizeA, sizeB, zoom) return obj end -function Body:draw(x, y) - if self.shape == "circle" then - love.graphics.circle("fill", x, y, self.r) - elseif self.shape == "rectangle" then - love.graphics.rectangle("fill", x - self.w / 2, y - self.h / 2, self.w, self.h) - end -end - -function Body:drawSprite(spriteName, x, y, angle) - local sprite = sprite.get(spriteName) - local w, h = (self.shape == "circle" and self.r or self.w), (self.shape == "circle" and self.r or self.h) - - love.graphics.draw(sprite, x, y, angle, - w / sprite:getWidth(), - h / sprite:getHeight() - ) -end - -function Body:drawQuad(sheet, index, x, y, angle) - if self.shape == "circle" then - local r = self.r - local graphicR = r * self.zoom - sprite.drawQuad(sheet, index, x - r, y - r, graphicR, graphicR, angle or 0) - else - local w, h = self.w, self.h - local graphicW, graphicH = w * self.zoom, h * self.zoom - sprite.drawQuad(sheet, index, x - w/2, y - h/2, graphicW, graphicH, angle or 0) - end -end - return Body diff --git a/objects/Obstacle.lua b/objects/Obstacle.lua index 51a1ee6..74297cd 100644 --- a/objects/Obstacle.lua +++ b/objects/Obstacle.lua @@ -13,7 +13,9 @@ function Obstacle:new(name, location, body) presetLocation = (type(location) == "string" and location) or nil, x = (type(location) == "table" and location.x) or nil, y = (type(location) == "table" and location.y) or nil, - body = body or Body:new("rectangle", 50, 50) + body = body or Body:new("rectangle", 50, 50), + w = 50, + h = 50, } setmetatable(obj, Obstacle) diff --git a/render/draws/objects.lua b/render/draws/objects.lua index eb474a4..aa948d6 100644 --- a/render/draws/objects.lua +++ b/render/draws/objects.lua @@ -1,4 +1,5 @@ local enemyData = require "data.enemyData" +local graphics = require "utils.graphics" require "data.directions" local draws = {} @@ -19,14 +20,17 @@ function draws.player() index = moving[G.player.facing][cf] end - G.player.body:drawQuad("apple", index, G.player.x, G.player.y) - utils.resetGraphics() + local r = G.player.body.r + local graphicR = r * G.player.body.zoom + local x, y = G.player.x - r, G.player.y - r + sprite.drawQuad("apple", index, x, y, graphicR, graphicR, 0) + graphics.reset() end function draws.bullets() for k, bullet in pairs(G.currentRoom.objects.shots) do local x, y, angle = bullet.x, bullet.y, bullet.orientation - bullet.body:drawSprite(bullet.type, x, y, angle, config.bulletSize) + sprite.draw(bullet.type, x, y, angle, config.bulletSize, config.bulletSize) end end @@ -35,11 +39,7 @@ function draws.enemies() local data = enemyData[enemy.type] if data.sprite then -- show enemy size - if config.debug then - love.graphics.setColor({0.5,0.5,0.5}) - enemy.body:draw(enemy.x, enemy.y) - utils.resetGraphics() - end + -- add filter to indicate enemy status if enemy.stunned then @@ -50,13 +50,13 @@ function draws.enemies() -- draw sprite local index = data.sprite.frames[enemy:facing()][enemy.frameTimer.currentFrame] - enemy.body:drawQuad(data.sprite.name, index, enemy.x, enemy.y, nil, data.sprite.zoom) - utils.resetGraphics() - elseif data.appearance.color then - love.graphics.setColor(data.appearance.color or {1,1,1}) - enemy.body:draw(enemy.x, enemy.y) + local r = enemy.body.r + local graphicR = r * enemy.body.zoom + local x, y = enemy.x - r, enemy.y - r + sprite.drawQuad(data.sprite.name, index, x, y, graphicR, graphicR, 0) + graphics.reset() end - utils.resetGraphics() + graphics.reset() end end @@ -70,8 +70,8 @@ function draws.obstacles() for _, obstacle in ipairs(G.currentRoom.objects.obstacles) do if obstacle ~= nil then love.graphics.setColor(0.85,0.85,0.85) - obstacle.body:draw(obstacle.x, obstacle.y) - utils.resetGraphics() + graphics.drawRect(obstacle) + graphics.reset() end end end |
