Skip to content

Commit

Permalink
freed scale from sprite drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
Retrisma committed Oct 7, 2023
1 parent 2ed18e3 commit 1f01b74
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 28 deletions.
Binary file added Graphics/Fonts/ElixiR.ttf
Binary file not shown.
16 changes: 3 additions & 13 deletions collision.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,9 @@ function Rectangle:new(x, y, width, height, xoff, yoff)
return setmetatable(o, rectangle_mt)
end

--returns a rectangle that is scaled to the current window scale
function Rectangle:translate()
return Rectangle:new(
self.x * window.scale, self.y * window.scale,
self.width * window.scale, self.height * window.scale
)
end

--checks to see if this rectangle intersects with a given point
function Rectangle:containspoint(x, y)
local sr = self:translate()
return x >= sr.x and x <= sr.x + sr.width and y >= sr.y and y <= sr.y + sr.height
return x >= self.x and x <= self.x + self.width and y >= self.y and y <= self.y + self.height
end

--checks to see if this rectangle intersects with another rectangle, with an optional offset applied to this
Expand All @@ -47,11 +38,10 @@ function Rectangle:draw(color, mode)

color = color or { r = 1, g = 1, b = 1 }
love.graphics.setColor(color.r, color.g, color.b)
local sr = self:translate()
love.graphics.rectangle(
mode,
sr.x, sr.y,
sr.width, sr.height
self.x, self.y,
self.width, self.height
)
love.graphics.setColor(1, 1, 1)
end
16 changes: 16 additions & 0 deletions drawing.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function drawobject(o)
local drawmode = o.drawmode or "relative"
local visible = o.visible or true
if visible then
if drawmode == "absolute" then
love.graphics.push()
love.graphics.origin()
end

o:draw()

if drawmode == "absolute" then
love.graphics.pop()
end
end
end
7 changes: 5 additions & 2 deletions input.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ mouse = { x = 0, y = 0, p = false, op = false, held = false }

function updatemouse()
mouse.x, mouse.y = love.mouse.getPosition()
mouse.x = mouse.x - camera.fx * window.scale * -1
mouse.y = mouse.y - camera.fy * window.scale * -1
--mouse.x = mouse.x - camera.fx * window.scale * -1
--mouse.y = mouse.y - camera.fy * window.scale * -1

mouse.x = mouse.x + camera.fx
mouse.x = mouse.y + camera.fy

mouse.p = false

Expand Down
21 changes: 16 additions & 5 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require "actor"
require "player"
require "triggers"
require "tools"
require "drawing"
sti = require "lib/sti"
moonshine = require "lib/moonshine"

Expand All @@ -31,14 +32,14 @@ function love.load()

loadassets()

font = love.graphics.setFont(fonts["CommonCase"])
font = love.graphics.setFont(fonts["ElixiR"])
map = loadmap("slime")

for _,v in pairs(p) do
if v.user == "player" then player = v break end
end

Button:add(500, 300, "button", function() showdebug = not showdebug end)
Button:add(300, 2900, "button", function() showdebug = not showdebug end)
Textbox:add(500, 290, "debug button", { scroll = false })
end

Expand All @@ -49,6 +50,13 @@ function love.update(dt)
updatecamera(dt)
updatemouse()

debug.mx = mouse.x
debug.my = mouse.y
debug.px = player.x
debug.py = player.y
debug.cx = camera.x
debug.cy = camera.y

--update all sprites
for _,v in pairs(p) do
v:update(rate)
Expand All @@ -61,7 +69,7 @@ function love.update(dt)

if love.keyboard.isDown("d") then camera.x = camera.x + rate * 500 end
if love.keyboard.isDown("a") then camera.x = camera.x - rate * 500 end
if love.keyboard.isDown("width") then camera.y = camera.y - rate * 500 end
if love.keyboard.isDown("w") then camera.y = camera.y - rate * 500 end
if love.keyboard.isDown("s") then camera.y = camera.y + rate * 500 end
else
camfollowsprite(player)
Expand All @@ -78,11 +86,14 @@ function love.draw()
love.graphics.push()

--set drawing offset to camera position
love.graphics.translate(camera.fx * window.scale * -1, camera.fy * window.scale * -1)
love.graphics.scale(window.scale)
love.graphics.translate(camera.fx * -1, camera.fy * -1)

--sort the sprite table by drawing depth
table.sort(p, function(a, b) return (a.depth or 5) < (b.depth or 5) end)
--draw all sprites
for _,v in pairs(p) do
v:draw()
drawobject(v)
end

--draw mouse cursor
Expand Down
14 changes: 8 additions & 6 deletions sprite.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ Sprite = {
alpha = 1,
flipped = false,
visible = true,
user = ""
user = "",
drawmode = "relative",
depth = 5
}
local sprite_mt = class(Sprite)

Expand Down Expand Up @@ -35,8 +37,8 @@ end
function Sprite:draw()
if self.visible then
love.graphics.draw(
self.image, self.x * window.scale, self.y * window.scale, self.r,
(self.flipped and -1 or (not self.flipped and 1)) * window.scale, window.scale
self.image, self.x, self.y, self.r,
(self.flipped and -1 or (not self.flipped and 1))
)
end
end
Expand Down Expand Up @@ -120,12 +122,12 @@ end

function AnimatedSprite:drawselfanim()
if self.visible then
local xoff = self.flipped and self.width * window.scale or (not self.flipped and 0)
local xoff = self.flipped and self.width or (not self.flipped and 0)

love.graphics.draw(
self.anim.image, self.anim.quads[self.frame],
self.x * window.scale + xoff, self.y * window.scale, self.r,
(self.flipped and -1 or (not self.flipped and 1)) * window.scale, window.scale
self.x + xoff, self.y, self.r,
(self.flipped and -1 or (not self.flipped and 1))
)
end
end
Expand Down
14 changes: 13 additions & 1 deletion tools.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function printdebug()
if type(v) == "table" or type(v) == "userdata" then
out = out .. (k .. ": " .. type(v)) .. "\n"
else
out = out .. (k .. ": " .. v) .. "\n"
out = out .. (k .. ": " .. tostring(v)) .. "\n"
end
end

Expand All @@ -26,6 +26,18 @@ function math.mid(a, b, c)
end
end

function table.find(t, o)
for i, v in ipairs(t) do
if v == o then
return i
end
end
end

function table.removevalue(t, o)
table.remove(t, table.find(t, o))
end

function qerp(current, target, dt, ease)
if math.abs(current - target) < 0.01 then
return target
Expand Down
2 changes: 1 addition & 1 deletion ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,5 @@ function Textbox:update(dt)
end

function Textbox:draw()
love.graphics.print(self.text, self.x * window.scale, self.y * window.scale, 0, window.scale)
love.graphics.print(self.text, self.x, self.y, 0)
end

0 comments on commit 1f01b74

Please sign in to comment.