Skip to content

Commit

Permalink
sort draw order by y/layer/type
Browse files Browse the repository at this point in the history
  • Loading branch information
parameterized committed Sep 21, 2018
1 parent 6e11e98 commit 3930b2b
Show file tree
Hide file tree
Showing 11 changed files with 273 additions and 155 deletions.
22 changes: 16 additions & 6 deletions entities.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,22 @@ end
function entities.client.draw()
for _, v in pairs(client.currentState.entities) do
if not v.destroyed and v.drawBody then
v:drawBody()
end
end
for _, v in pairs(client.currentState.entities) do
if not v.destroyed and v.drawHP then
v:drawHP()
scene.add{
draw = function()
v:drawBody()
end,
y = v.y,
type = 'entityBody'
}
if v.drawHP then
scene.add{
draw = function()
v:drawHP()
end,
y = v.y,
type = 'entityHP'
}
end
end
end
end
8 changes: 4 additions & 4 deletions entityDefs/slime.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function slime.server:spawn()
if (i2-1) % 2 == 0 then -- x
v[i2] = v2*img:getWidth() - img:getWidth()/2
else
v[i2] = v2*img:getWidth()*-1 + img:getHeight()/2
v[i2] = v2*img:getWidth()*-1
end
end
end
Expand Down Expand Up @@ -156,7 +156,7 @@ function slime.client:spawn()
if (i2-1) % 2 == 0 then -- x
v[i2] = v2*img:getWidth() - img:getWidth()/2
else
v[i2] = v2*img:getWidth()*-1 + img:getHeight()/2
v[i2] = v2*img:getWidth()*-1
end
end
end
Expand Down Expand Up @@ -215,7 +215,7 @@ function slime.client:drawBody()
love.graphics.draw(img,
0, 0,
0, 1, 1,
lume.round(img:getWidth()/2), lume.round(img:getHeight()/2))
lume.round(img:getWidth()/2), img:getHeight())
love.graphics.pop()
love.graphics.setShader(_shader)
end
Expand All @@ -236,7 +236,7 @@ function slime.client:drawHP()
love.graphics.push()
local vx, vy = self.body:getPosition()
love.graphics.translate(lume.round(vx), lume.round(vy))
love.graphics.draw(canvases.hpBar, lume.round(-canvases.hpBar:getWidth()/2), 10)
love.graphics.draw(canvases.hpBar, lume.round(-canvases.hpBar:getWidth()/2), 2)
love.graphics.pop()
end

Expand Down
9 changes: 9 additions & 0 deletions hud.lua
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ end

function hud.mousepressed(mx, my, btn)
mx, my = window2game(mx, my)
mx, my = lume.round(mx), lume.round(my)
local chatFieldPressed = false
for _, v in pairs(hud.buttons) do
if mx > v.x and mx < v.x + v.img:getWidth() and my > v.y and my < v.y + v.img:getHeight() then
Expand All @@ -168,6 +169,7 @@ end

function hud.draw()
local mx, my = window2game(love.mouse.getPosition())
mx, my = lume.round(mx), lume.round(my)

love.graphics.setColor(1, 1, 1)
love.graphics.draw(gfx.hud.frame, 0, 0)
Expand Down Expand Up @@ -230,4 +232,11 @@ function hud.draw()
love.graphics.setColor(1, 1, 1)
love.graphics.setFont(fonts.c17)
text.print(player.name, 44, 5)

-- draggables
local heldItem = lootBags.client.heldItem
if heldItem.bagId then
love.graphics.setColor(1, 1, 1, 0.8)
love.graphics.rectangle('fill', mx + heldItem.offset.x, my + heldItem.offset.y, 15, 15)
end
end
1 change: 1 addition & 0 deletions loadassets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ math.randomseed(love.timer.getTime())

love.keyboard.setKeyRepeat(true)
love.graphics.setDefaultFilter('nearest', 'nearest')
love.graphics.setLineStyle('rough')

gsx, gsy = 480, 270
canvases = {
Expand Down
70 changes: 35 additions & 35 deletions lootBags.lua
Original file line number Diff line number Diff line change
Expand Up @@ -135,41 +135,41 @@ function lootBags.client.draw()
mx, my = lume.round(mx), lume.round(my)
mx, my = camera:screen2world(mx, my)
for _, bag in pairs(client.currentState.lootBags) do
local a = 1
if bag.life and client.serverTime - bag.spawnTime > bag.life - 3 then
local t = (client.serverTime - bag.spawnTime - bag.life + 3)/3
a = math.cos(t*5*math.pi)/4 + 1/4 + (1-t)/2
end
love.graphics.setColor(1, 1, 1, a)
love.graphics.push()
love.graphics.translate(lume.round(bag.x), lume.round(bag.y))
local img = gfx.items[bag.type]
love.graphics.draw(img, 0, 0, 0, 1, 1, lume.round(img:getWidth()/2), img:getHeight())
if bag.id == lootBags.client.closest.id and lootBags.client.closest.dist < 30 then
local img = gfx.ui.bag
love.graphics.push()
love.graphics.translate(-lume.round(img:getWidth()/2), -img:getHeight() - 20)
love.graphics.draw(img, 0, 0)
local bmx = mx - (lume.round(bag.x) - lume.round(img:getWidth()/2))
local bmy = my - (lume.round(bag.y) - img:getHeight() - 20)
for _, slot in ipairs(lootBags.client.slots) do
if bmx >= slot.x and bmx <= slot.x + slot.w
and bmy >= slot.y and bmy <= slot.y + slot.h then
love.graphics.setColor(1, 1, 1, 0.8)
else
love.graphics.setColor(1, 1, 1, 0.2)
scene.add{
draw = function()
local a = 1
if bag.life and client.serverTime - bag.spawnTime > bag.life - 3 then
local t = (client.serverTime - bag.spawnTime - bag.life + 3)/3
a = math.cos(t*5*math.pi)/4 + 1/4 + (1-t)/2
end
love.graphics.rectangle('fill', slot.x, slot.y, slot.w, slot.h)
end
love.graphics.setColor(1, 0, 0, 0.4)
love.graphics.rectangle('fill', bmx, bmy, 2, 2)
love.graphics.pop()
end
love.graphics.pop()
end
local heldItem = lootBags.client.heldItem
if heldItem.bagId then
love.graphics.setColor(1, 1, 1, 0.8)
love.graphics.rectangle('fill', mx + heldItem.offset.x, my + heldItem.offset.y, 15, 15)
love.graphics.setColor(1, 1, 1, a)
love.graphics.push()
love.graphics.translate(lume.round(bag.x), lume.round(bag.y))
local img = gfx.items[bag.type]
love.graphics.draw(img, 0, 0, 0, 1, 1, lume.round(img:getWidth()/2), img:getHeight())
if bag.id == lootBags.client.closest.id and lootBags.client.closest.dist < 30 then
local img = gfx.ui.bag
love.graphics.push()
love.graphics.translate(-lume.round(img:getWidth()/2), -img:getHeight() - 20)
love.graphics.draw(img, 0, 0)
local bmx = mx - (lume.round(bag.x) - lume.round(img:getWidth()/2))
local bmy = my - (lume.round(bag.y) - img:getHeight() - 20)
for _, slot in ipairs(lootBags.client.slots) do
if bmx >= slot.x and bmx <= slot.x + slot.w
and bmy >= slot.y and bmy <= slot.y + slot.h then
love.graphics.setColor(1, 1, 1, 0.8)
else
love.graphics.setColor(1, 1, 1, 0.2)
end
love.graphics.rectangle('fill', slot.x, slot.y, slot.w, slot.h)
end
love.graphics.setColor(1, 0, 0, 0.4)
love.graphics.rectangle('fill', bmx, bmy, 2, 2)
love.graphics.pop()
end
love.graphics.pop()
end,
y = bag.y
}
end
end
17 changes: 16 additions & 1 deletion main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require 'client'
require 'menu'
require 'hud'
require 'physics'
require 'scene'
require 'world'
require 'player'
require 'projectiles'
Expand All @@ -24,6 +25,7 @@ function love.load()
gameState = 'menu'
time = 0
gameTime = 0
gcTimer = 10
-- don't shoot if pressing ui
uiMouseDown = false
drawDebug = false
Expand Down Expand Up @@ -52,12 +54,14 @@ function setGameCanvas2x()
local _color = {love.graphics.getColor()}
love.graphics.setShader()
love.graphics.setCanvas(canvases.game2x)
love.graphics.setBlendMode('alpha', 'premultiplied')
love.graphics.setColor(1, 1, 1)
love.graphics.push()
love.graphics.origin()
love.graphics.draw(canvases.game, 0, 0, 0, 2, 2)
love.graphics.pop()
love.graphics.setCanvas(canvases.game)
love.graphics.setBlendMode('alpha')
love.graphics.clear()
love.graphics.setCanvas(canvases.game2x)
love.graphics.setShader(_shader)
Expand All @@ -74,6 +78,11 @@ function love.update(dt)
client.update(dt)
end
menu.update(dt)
gcTimer = gcTimer - dt
if gcTimer < 0 then
collectgarbage()
gcTimer = 10
end
end

function love.mousepressed(x, y, btn, isTouch)
Expand Down Expand Up @@ -139,6 +148,8 @@ end

function love.draw()
local mx, my = window2game(love.mouse.getPosition())
mx, my = lume.round(mx), lume.round(my)
love.graphics.setBlendMode('alpha')
love.graphics.setCanvas(canvases.game2x)
love.graphics.clear()
love.graphics.setCanvas(canvases.game)
Expand All @@ -152,6 +163,9 @@ function love.draw()
projectiles.client.draw()
player.draw()

scene.draw()
scene.reset()

if drawDebug then
if server.running then
local serverBodies = physics.server.world:getBodies()
Expand All @@ -177,14 +191,15 @@ function love.draw()
chat.draw()

love.graphics.setColor(1, 1, 1)
love.graphics.draw(gfx.cursors.main, lume.round(mx), lume.round(my), 0, 1, 1, 0, 0) -- hotspot 0, 0
love.graphics.draw(gfx.cursors.main, mx, my, 0, 1, 1, 0, 0) -- hotspot 0, 0
end

menu.draw()

-- draw game on game2x
setGameCanvas2x()
love.graphics.setCanvas()
love.graphics.setBlendMode('alpha', 'premultiplied')
love.graphics.clear(0, 0, 0)
love.graphics.setColor(1, 1, 1)
love.graphics.draw(canvases.game2x, ssx/2-gameScale*gsx/2, ssy/2-gameScale*gsy/2, 0, gameScale/2, gameScale/2)
Expand Down
3 changes: 2 additions & 1 deletion menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ end

function menu.mousepressed(mx, my, btn)
mx, my = window2game(mx, my)
mx, my = lume.round(mx), lume.round(my)
if gameState == 'menu' then
menu.activeInput = nil
for _, v in pairs(menu.buttons[menu.state] or {}) do
Expand Down Expand Up @@ -363,7 +364,7 @@ end
function menu.draw()
if gameState == 'menu' then
local mx, my = window2game(love.mouse.getPosition())

mx, my = lume.round(mx), lume.round(my)
if menu.state == 'main' then
love.graphics.setColor(1, 1, 1)
local logoFrameIdx = math.floor(menu.logoAnimTimer*12) % #anims.logo.quads + 1
Expand Down
Loading

0 comments on commit 3930b2b

Please sign in to comment.