Skip to content

Commit

Permalink
apples, encapsulation for future multi-realms, placeholder portals
Browse files Browse the repository at this point in the history
  • Loading branch information
parameterized committed Dec 27, 2018
1 parent ccb3f2d commit 6b751d8
Show file tree
Hide file tree
Showing 19 changed files with 775 additions and 508 deletions.
42 changes: 29 additions & 13 deletions client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ function client.connect(ip, port)
client.currentState.projectiles[v.id] = v
end
for _, v in pairs(data.entities) do
local ent = entities.client.defs[v.type]:new(v):spawn()
entities.client.defs[v.type]:new(v):spawn()
end
for _, v in pairs(data.lootBags) do
client.currentState.lootBags[v.id] = v
lootBag.client:new(v):spawn()
end
for _, v in pairs(data.portals) do
client.currentState.portals[v.id] = v
end
end,
remove = function(self, data)
Expand All @@ -42,10 +45,13 @@ function client.connect(ip, port)
for _, id in pairs(data.entities) do
local ent = client.currentState.entities[id]
if ent then ent:destroy() end
client.currentState.entities[id] = nil
end
for _, id in pairs(data.lootBags) do
client.currentState.lootBags[id] = nil
local bag = clientRealm.lootBags[id]
if bag then bag:destroy() end
end
for _, id in pairs(data.portals) do
client.currentState.portals[id] = nil
end
end,
stateUpdate = function(self, data)
Expand All @@ -65,10 +71,22 @@ function client.connect(ip, port)
items.client.requested[data.id] = nil
end,
bagUpdate = function(self, data)
client.currentState.lootBags[data.id] = data
local bag = clientRealm.lootBags[data.id]
if bag then
for k, v in pairs(data) do
bag[k] = v
end
end
end,
setWorldChunk = function(self, data)
world.client.setChunk(data.x, data.y, data.chunk)
clientRealm.world:setChunk(data.x, data.y, data.chunk)
end,
teleportPlayer = function(self, data)
playerController.player.body:setPosition(data.x, data.y)
end,
healPlayer = function(self, data)
local p = playerController.player
p.hp = math.min(p.hp + data.hp, p.hpMax)
end
}
for k, v in pairs(bitserRPCs) do
Expand Down Expand Up @@ -98,7 +116,7 @@ function client.connect(ip, port)
-- cleanup previous connection
if client.currentState then
entities.client.reset()
world.client.reset()
clientRealm:destroy()
slimeBalls.reset()
items.client.reset()
collectgarbage()
Expand All @@ -107,12 +125,12 @@ function client.connect(ip, port)

menu.writeDefaults()

physics.client.load()
clientRealm:load()
playerController.load()
end

function client.newState()
return {entities={}, projectiles={}, lootBags={}}
return {entities={}, projectiles={}, lootBags={}, portals={}}
end

function client.startGame(data)
Expand Down Expand Up @@ -200,9 +218,7 @@ function client.update(dt)
if not (server.running and server.paused) then
gameTime = gameTime + dt
hud.update(dt)
lootBags.client.update(dt)
physics.client.update(dt)
world.client.update(dt)
clientRealm:update(dt)
playerController.update(dt)
entities.client.update(dt)
slimeBalls.update(dt)
Expand All @@ -215,7 +231,7 @@ function client.sendMessage(msg)
end
end

for _, v in pairs{'spawnProjectile', 'moveItem', 'dropItem'} do
for _, v in pairs{'spawnProjectile', 'moveItem', 'dropItem', 'useItem', 'usePortal'} do
client[v] = function(data)
client.nutClient:sendRPC(v, bitser.dumps(data))
end
Expand Down
1 change: 1 addition & 0 deletions entityDefs/_base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ end

function base.client:destroy()
self.destroyed = true
client.currentState.entities[self.id] = nil
end


Expand Down
16 changes: 8 additions & 8 deletions entityDefs/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function player.server:new(o)
end

function player.server:spawn()
self.body = love.physics.newBody(physics.server.world, self.x, self.y, 'dynamic')
self.body = love.physics.newBody(serverRealm.physics.world, self.x, self.y, 'dynamic')
self.shapes = {
love.physics.newCircleShape(6)
}
Expand Down Expand Up @@ -142,8 +142,8 @@ function player.server:update(dt)
dy = dy + (self.inputState.keyboard.w and -1 or 0)
dy = dy + (self.inputState.keyboard.s and 1 or 0)
local spd = self.spd*(self.inputState.keyboard.lshift and 2.5 or 1)
if world.server.getTile(self.x, self.y) == 5 then spd = spd * 1.5 end -- platform
if world.server.getTile(self.x, self.y) == 4 then spd = spd / 2 end -- water
if serverRealm.world:getTile(self.x, self.y) == 5 then spd = spd * 1.5 end -- platform
if serverRealm.world:getTile(self.x, self.y) == 4 then spd = spd / 2 end -- water
local attackItem = items.server.getItem(self.inventory.items[2])
if not (dx == 0 and dy == 0)
and not (self.swinging or self.automaticSwing
Expand Down Expand Up @@ -205,7 +205,7 @@ function player.client:new(o)
end

function player.client:spawn()
self.body = love.physics.newBody(physics.client.world, self.x, self.y, 'dynamic')
self.body = love.physics.newBody(clientRealm.physics.world, self.x, self.y, 'dynamic')
self.shapes = {
love.physics.newCircleShape(6)
}
Expand Down Expand Up @@ -309,8 +309,8 @@ function player.client:update(dt)
dy = dy + (self.inputState.keyboard.w and -1 or 0)
dy = dy + (self.inputState.keyboard.s and 1 or 0)
local spd = self.spd*(self.inputState.keyboard.lshift and 2.5 or 1)
if world.client.getTile(self.x, self.y) == 5 then spd = spd * 1.5 end -- platform
if world.client.getTile(self.x, self.y) == 4 then spd = spd / 2 end -- water
if clientRealm.world:getTile(self.x, self.y) == 5 then spd = spd * 1.5 end -- platform
if clientRealm.world:getTile(self.x, self.y) == 4 then spd = spd / 2 end -- water
local attackItem = items.client.getItem(self.inventory.items[2])
if not (dx == 0 and dy == 0)
and not (self.swinging or self.automaticSwing
Expand Down Expand Up @@ -370,7 +370,7 @@ function player.client:draw()
love.graphics.push()

-- offset if on platform
if world.client.getTile(self.x, self.y) == 5 then
if clientRealm.world:getTile(self.x, self.y) == 5 then
love.graphics.translate(0, -2)
end

Expand All @@ -391,7 +391,7 @@ function player.client:draw()
love.graphics.clear()

-- offset/clip feet if in water
if world.client.getTile(self.x, self.y) == 4 then
if clientRealm.world:getTile(self.x, self.y) == 4 then
love.graphics.translate(0, 4)
love.graphics.stencil(function()
love.graphics.rectangle('fill', self.x - 50, self.y - 4, 100, 100)
Expand Down
16 changes: 9 additions & 7 deletions entityDefs/slime.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function slime.server:new(o)
end

function slime.server:spawn()
self.body = love.physics.newBody(physics.server.world, self.x, self.y, 'dynamic')
self.body = love.physics.newBody(serverRealm.physics.world, self.x, self.y, 'dynamic')
self.polys = {
{0.36, 0.64, 0.08, 0.36, 0.08, 0.2, 0.2, 0.08, 0.8, 0.08, 0.92, 0.2, 0.92, 0.36, 0.64, 0.64}
}
Expand Down Expand Up @@ -85,12 +85,12 @@ function slime.server:damage(d, clientId)
if self.hp <= 0 and not self.destroyed then
server.addXP(clientId, math.random(3, 5))
for _=1, math.random(1, 2) do
local x = self.x + (math.random()*2-1)*64
local y = self.y + (math.random()*2-1)*64
local x = self.x + lume.random(-64, 64)
local y = self.y + lume.random(-64, 64)
self:new{x=x, y=y}:spawn()
end
local bagItems = {}
local choices = {none=50, sword=25, shield=25}
local choices = {none=50, sword=15, shield=15, apple=20}
for _=1, 3 do
choice = lume.weightedchoice(choices)
if choice ~= 'none' then
Expand All @@ -105,13 +105,15 @@ function slime.server:damage(d, clientId)
local numItems = #bagItems
if numItems ~= 0 then
local type = lume.randomchoice{'lootBag', 'lootBag1', 'lootBagFuse'}
lootBags.server.spawn{
lootBag.server:new{
realm = serverRealm,
x = self.x, y = self.y,
items = bagItems,
type = type,
life = 30
}
}:spawn()
end
--if math.random() < 0.5 then portals.server.spawn{x=self.x, y=self.y, life=10} end
self:destroy()
end
end
Expand Down Expand Up @@ -145,7 +147,7 @@ end


function slime.client:spawn()
self.body = love.physics.newBody(physics.client.world, self.x, self.y, 'dynamic')
self.body = love.physics.newBody(clientRealm.physics.world, self.x, self.y, 'dynamic')
self.polys = {
{0.36, 0.64, 0.08, 0.36, 0.08, 0.2, 0.2, 0.08, 0.8, 0.08, 0.92, 0.2, 0.92, 0.36, 0.64, 0.64}
}
Expand Down
Binary file added gfx/items/apple.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 19 additions & 11 deletions hud.lua
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ function hud.update(dt)
end
end

function hud.mousepressed(x, y, btn)
function hud.mousepressed(x, y, btn, isTouch, presses)
mx, my = window2game(x, y)
mx, my = lume.round(mx), lume.round(my)

Expand All @@ -194,17 +194,25 @@ function hud.mousepressed(x, y, btn)
and pmy >= slot.y and pmy <= slot.y + slot.h and panel.open then
uiMouseDown = true
if bag.items[slotId] then
-- use item
if btn == 1 and (love.keyboard.isScancodeDown('lshift') or presses > 1) then
client.useItem{
bagId = bag.id,
slotId = slotId
}
end
-- move items
if btn == 1 then
local heldItem = lootBags.client.heldItem
local heldItem = playerController.heldItem
heldItem.bagId = bag.id
heldItem.slotId = slotId
heldItem.offset.x = slot.x - pmx
heldItem.offset.y = slot.y - pmy
elseif btn == 2 then
local closestBag = lootBags.client.closest
local closestBag = playerController.closestBag
if closestBag.id and closestBag.open then
local bagTo = client.currentState.lootBags[closestBag.id]
for bagSlotId, _ in ipairs(lootBags.client.slots) do
for bagSlotId, _ in ipairs(lootBagSlots) do
if bagTo.items[bagSlotId] == nil then
client.moveItem{
from = {
Expand All @@ -226,10 +234,10 @@ function hud.mousepressed(x, y, btn)
end
end

function hud.mousereleased(x, y, btn)
function hud.mousereleased(x, y, btn, isTouch, presses)
local mx, my = window2game(x, y)
mx, my = lume.round(mx), lume.round(my)
local heldItem = lootBags.client.heldItem
local heldItem = playerController.heldItem
if heldItem.bagId then
local bagFrom = client.currentState.lootBags[heldItem.bagId]
if heldItem.bagId == 'inventory' then
Expand Down Expand Up @@ -321,7 +329,7 @@ function hud.draw()
love.graphics.rectangle('fill', 11, 18, 131, 25)
love.graphics.setShader(_shader)

world.client.drawMinimap()
clientRealm.world:drawMinimap()

-- panels/buttons
love.graphics.setColor(1, 1, 1)
Expand Down Expand Up @@ -393,12 +401,12 @@ function hud.draw()
and pmy >= slot.y and pmy <= slot.y + slot.h and panel.open then
if item then
cursor.cursor = cursor.hand
lootBags.client.hoveredItem = item
playerController.hoveredItem = item
end
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
local heldItem = playerController.heldItem
if not (heldItem.bagId == 'inventory' and heldItem.slotId == slotId) then
if item then
love.graphics.setColor(1, 1, 1)
Expand All @@ -409,7 +417,7 @@ function hud.draw()
love.graphics.pop()

-- item info
local item = lootBags.client.hoveredItem
local item = playerController.hoveredItem
if item then
love.graphics.setColor(1, 1, 1)
love.graphics.setShader(shaders.panel)
Expand Down Expand Up @@ -442,7 +450,7 @@ function hud.draw()
end

-- held item
local heldItem = lootBags.client.heldItem
local heldItem = playerController.heldItem
if heldItem.bagId then
local bag = client.currentState.lootBags[heldItem.bagId]
if heldItem.bagId == 'inventory' then
Expand Down
3 changes: 2 additions & 1 deletion loadassets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ gfx = {
lootBag1 = love.graphics.newImage('gfx/items/loot1.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')
shield = love.graphics.newImage('gfx/items/shield.png'),
apple = love.graphics.newImage('gfx/items/apple.png')
},
slimeBall = love.graphics.newImage('gfx/slime_ball.png')
}
Expand Down
Loading

0 comments on commit 6b751d8

Please sign in to comment.