Skip to content

Commit

Permalink
bag e open, synced items, outline color
Browse files Browse the repository at this point in the history
  • Loading branch information
parameterized committed Sep 21, 2018
1 parent 3930b2b commit 96884ae
Show file tree
Hide file tree
Showing 16 changed files with 188 additions and 70 deletions.
26 changes: 26 additions & 0 deletions client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ function client.connect(ip, port)
else
print('error decoding client rpc stateUpdate')
end
end,
bagUpdate = function(self, data)
local ok, data = pcall(json.decode, data)
if ok then
client.currentState.lootBags[data.id] = data
else
print('error decoding cient rpc bagUpdate')
end
end
}
client.nutClient:addUpdate(function(self)
Expand Down Expand Up @@ -258,6 +266,24 @@ function client.spawnProjectile(data)
end
end

function client.moveItem(data)
if client.connected then
client.nutClient:sendRPC('moveItem', json.encode(data))
end
end

function client.takeItem(data)
if client.connected then
client.nutClient:sendRPC('takeItem', json.encode(data))
end
end

function client.dropItem(data)
if client.connected then
client.nutClient:sendRPC('dropItem', json.encode(data))
end
end

function client.close()
client.nutClient:close()
client.connected = false
Expand Down
23 changes: 17 additions & 6 deletions entityDefs/slime.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,24 @@ function slime.server:damage(d, clientId)
local y = self.y + (math.random()*2-1)*64
self:new{x=x, y=y}:spawn()
end
if math.random() < 0.5 then
local i = math.min(math.floor(math.random()*3), 2) + 1
local type = ({'lootBag', 'lootBag1', 'lootBagFuse'})[i]
local items = {}
local choices = {none=50, sword=25, shield=25}
for i=1, 3 do
choice = lume.weightedchoice(choices)
if choice ~= 'none' then table.insert(items, choice) end
end
-- todo: sparse arrays not encodable with json - temp solution before move to bitser
local numItems = #items
for i=1, 8 do
items[i] = items[i] or 'none'
end
if numItems ~= 0 then
local type = lume.randomchoice{'lootBag', 'lootBag1', 'lootBagFuse'}
lootBags.server.spawn{
x = self.x, y = self.y,
items = {'apl', 'banan'},
life = 30,
type = type
items = items,
type = type,
life = 30
}
end
self:destroy()
Expand Down Expand Up @@ -209,6 +219,7 @@ function slime.client:drawBody()
-- gfx.enemies.slime1
local img = gfx.enemies[self.slimeType]
shaders.outline:send('stepSize', {1/img:getWidth(), 1/img:getHeight()})
shaders.outline:send('outlineColor', {0, 0, 0, 1})
love.graphics.push()
local vx, vy = self.body:getPosition()
love.graphics.translate(lume.round(vx), lume.round(vy))
Expand Down
Binary file modified gfx/items/loot-fuse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified gfx/items/loot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified gfx/items/loot1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gfx/items/shield.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gfx/items/sword.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 8 additions & 2 deletions hud.lua
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,13 @@ function hud.draw()
-- 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)
local bag = client.currentState.lootBags[heldItem.bagId]
if bag then
local item = bag.items[heldItem.slotId]
if item and item ~= 'none' then
love.graphics.setColor(1, 1, 1)
love.graphics.draw(gfx.items[item], mx + heldItem.offset.x, my + heldItem.offset.y)
end
end
end
end
4 changes: 3 additions & 1 deletion loadassets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ gfx = {
items = {
lootBag = love.graphics.newImage('gfx/items/loot.png'),
lootBag1 = love.graphics.newImage('gfx/items/loot1.png'),
lootBagFuse = love.graphics.newImage('gfx/items/loot-fuse.png')
lootBagFuse = love.graphics.newImage('gfx/items/loot-fuse.png'),
sword = love.graphics.newImage('gfx/items/sword.png'),
shield = love.graphics.newImage('gfx/items/shield.png')
}
}

Expand Down
113 changes: 81 additions & 32 deletions lootBags.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ lootBags = {
container = {}
},
client = {
closest = {id=nil, dist=nil},
heldItem = {bagId=nil, itemId=nil, offset={x=0, y=0}}
openRange = 30,
closest = {id=nil, dist=nil, open=false},
heldItem = {bagId=nil, slotId=nil, offset={x=0, y=0}}
}
}

Expand Down Expand Up @@ -71,37 +72,50 @@ for i=1, 4 do
end

function lootBags.client.update(dt)
local closest = {id=nil, dist=nil}
local closestBag = lootBags.client.closest
local px, py = player.body:getPosition()
local lastId = closestBag.id
closestBag.id = nil
closestBag.dist = nil
for _, bag in pairs(client.currentState.lootBags) do
local dist = math.sqrt((bag.x - px)^2 + (bag.y - py)^2)
if not closest.dist or dist < closest.dist then
closest.id = bag.id
closest.dist = dist
if not closestBag.dist or dist < closestBag.dist then
closestBag.id = bag.id
closestBag.dist = dist
end
end
lootBags.client.closest = closest
-- todo: open bag should be able to not be closest
if not closestBag.id or closestBag.id ~= lastId or closestBag.dist > lootBags.client.openRange then
closestBag.open = false
end
local heldItem = lootBags.client.heldItem
if not closestBag.id or closestBag.id ~= heldItem.bagId or closestBag.dist > lootBags.client.openRange then
heldItem.bagId = nil
heldItem.slotId = nil
end
end

function lootBags.client.mousepressed(x, y, btn)
local mx, my = window2game(x, y)
mx, my = lume.round(mx), lume.round(my)
mx, my = camera:screen2world(mx, my)
local bagId = lootBags.client.closest.id
if bagId and lootBags.client.closest.dist < 30 then
local bag = client.currentState.lootBags[bagId]
local closestBag = lootBags.client.closest
if closestBag.id and closestBag.open then
local bag = client.currentState.lootBags[closestBag.id]
local img = gfx.ui.bag
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
for slotId, 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
local heldItem = lootBags.client.heldItem
heldItem.bagId = bag.id
-- item id
heldItem.offset.x = slot.x - bmx
heldItem.offset.y = slot.y - bmy
uiMouseDown = true
if bag.items[slotId] and bag.items[slotId] ~= 'none' then
local heldItem = lootBags.client.heldItem
heldItem.bagId = bag.id
heldItem.slotId = slotId
heldItem.offset.x = slot.x - bmx
heldItem.offset.y = slot.y - bmy
end
break
end
end
Expand All @@ -112,22 +126,40 @@ function lootBags.client.mousereleased(x, y, btn)
local mx, my = window2game(x, y)
mx, my = lume.round(mx), lume.round(my)
mx, my = camera:screen2world(mx, my)
local bagId = lootBags.client.closest.id
local closestBag = lootBags.client.closest
local heldItem = lootBags.client.heldItem
if bagId and lootBags.client.closest.dist < 30 then
local bag = client.currentState.lootBags[bagId]
if closestBag.id and closestBag.open and heldItem.bagId then
local bag = client.currentState.lootBags[closestBag.id]
local img = gfx.ui.bag
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
for slotId, 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
-- drop item in slot
client.moveItem{
bagId = heldItem.bagId,
from = heldItem.slotId,
to = slotId
}
-- move clientside before response (will be corrected/affirmed)
local temp = bag.items[slotId]
bag.items[slotId] = bag.items[heldItem.slotId]
bag.items[heldItem.slotId] = temp
break
end
end
end
heldItem.bagId = nil
heldItem.slotId = nil
end

function lootBags.client.keypressed(k, scancode, isrepeat)
if k == 'e' then
local closestBag = lootBags.client.closest
if closestBag.id and closestBag.dist < lootBags.client.openRange then
closestBag.open = not closestBag.open
end
end
end

function lootBags.client.draw()
Expand All @@ -137,34 +169,51 @@ function lootBags.client.draw()
for _, bag in pairs(client.currentState.lootBags) do
scene.add{
draw = function()
local a = 1
local tint = 1
local outlineColor = {0, 0, 0, 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
tint = math.cos(t*5*math.pi)/4 + 1/4 + (1-t)/2
tint = tint/2 + 1/2
outlineColor = {0.8, 0, 0, 1}
end
love.graphics.setColor(1, 1, 1, a)
local closestBag = lootBags.client.closest
if bag.id == closestBag.id and closestBag.dist < lootBags.client.openRange then
outlineColor = {0.8, 0.8, 0.8, 1}
end
love.graphics.setColor(tint, tint, tint)
love.graphics.push()
love.graphics.translate(lume.round(bag.x), lume.round(bag.y))
local img = gfx.items[bag.type]
local _shader = love.graphics.getShader()
love.graphics.setShader(shaders.outline)
shaders.outline:send('stepSize', {1/img:getWidth(), 1/img:getHeight()})
shaders.outline:send('outlineColor', outlineColor)
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
love.graphics.setShader(_shader)
if bag.id == closestBag.id and closestBag.open then
local img = gfx.ui.bag
love.graphics.push()
love.graphics.translate(-lume.round(img:getWidth()/2), -img:getHeight() - 20)
love.graphics.setColor(1, 1, 1)
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
for slotId, 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)
love.graphics.setColor(1, 1, 1, 0.4)
love.graphics.rectangle('fill', slot.x, slot.y, slot.w, slot.h)
end
local heldItem = lootBags.client.heldItem
if not (heldItem.bagId == bag.id and heldItem.slotId == slotId) then
local item = bag.items[slotId]
if item and item ~= 'none' then
love.graphics.setColor(1, 1, 1)
love.graphics.draw(gfx.items[item], slot.x, slot.y)
end
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()
Expand Down
6 changes: 5 additions & 1 deletion main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ function love.keypressed(k, scancode, isrepeat)
end
-- if chat open, esc pressed, chat.active=false & chatActive=true
if not (chatActive or chat.active or chatPanelOpen) then
menu.keypressed(k, scancode, isrepeat)
if gameState == 'menu' then
menu.keypressed(k, scancode, isrepeat)
elseif gameState == 'playing' then
lootBags.client.keypressed(k, scancode, isrepeat)
end
if not isrepeat then
if k == 'escape' then
gameState = 'menu'
Expand Down
52 changes: 25 additions & 27 deletions menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -328,34 +328,32 @@ function menu.textinput(t)
end

function menu.keypressed(k, scancode, isrepeat)
if gameState == 'menu' then
if menu.state == 'play' then
if k == 'escape' and not isrepeat then
menu.state = 'main'
menu.activeInput = nil
end
elseif menu.state == 'connect' then
if k == 'escape' and not isrepeat then
menu.state = 'play'
menu.activeInput = nil
end
elseif menu.state == 'options' then
if k == 'escape' and not isrepeat then
menu.state = 'main'
menu.activeInput = nil
end
if menu.state == 'play' then
if k == 'escape' and not isrepeat then
menu.state = 'main'
menu.activeInput = nil
end
if menu.activeInput then
if k == 'return' and not isrepeat then
menu.activeInput = nil
elseif k == 'backspace' then
menu.activeInput.value = menu.activeInput.value:sub(0, math.max(menu.activeInput.value:len()-1, 0))
elseif k == 'v' and (love.keyboard.isScancodeDown('lctrl') or love.keyboard.isScancodeDown('rctrl')) then
local paste = love.system.getClipboardText()
for v in paste:gmatch('.') do
-- todo: filter for input type (numerical ports etc)
menu.activeInput.value = menu.activeInput.value .. v
end
elseif menu.state == 'connect' then
if k == 'escape' and not isrepeat then
menu.state = 'play'
menu.activeInput = nil
end
elseif menu.state == 'options' then
if k == 'escape' and not isrepeat then
menu.state = 'main'
menu.activeInput = nil
end
end
if menu.activeInput then
if k == 'return' and not isrepeat then
menu.activeInput = nil
elseif k == 'backspace' then
menu.activeInput.value = menu.activeInput.value:sub(0, math.max(menu.activeInput.value:len()-1, 0))
elseif k == 'v' and (love.keyboard.isScancodeDown('lctrl') or love.keyboard.isScancodeDown('rctrl')) then
local paste = love.system.getClipboardText()
for v in paste:gmatch('.') do
-- todo: filter for input type (numerical ports etc)
menu.activeInput.value = menu.activeInput.value .. v
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ function player.draw()
1/canvases.tempGame:getWidth(),
1/canvases.tempGame:getHeight()
})
shaders.outline:send('outlineColor', {0, 0, 0, 1})
love.graphics.draw(canvases.tempGame, 0, 0)
love.graphics.pop()
love.graphics.setShader(_shader)
Expand Down Expand Up @@ -245,6 +246,7 @@ function player.draw()
1/canvases.tempGame:getWidth(),
1/canvases.tempGame:getHeight()
})
shaders.outline:send('outlineColor', {0, 0, 0, 1})
love.graphics.draw(canvases.tempGame, 0, 0)
love.graphics.pop()
love.graphics.setShader(_shader)
Expand Down
1 change: 1 addition & 0 deletions projectiles.lua
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ function projectiles.client.draw()
1/canvases.tempGame:getWidth(),
1/canvases.tempGame:getHeight()
})
shaders.outline:send('outlineColor', {0, 0, 0, 1})
love.graphics.draw(canvases.tempGame, 0, 0)
love.graphics.pop()
love.graphics.setShader(_shader)
Expand Down
Loading

0 comments on commit 96884ae

Please sign in to comment.