Skip to content

Commit

Permalink
chunk based entity spawn/despawn
Browse files Browse the repository at this point in the history
  • Loading branch information
parameterized committed Sep 30, 2018
1 parent 9979e85 commit 3faa57c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 52 deletions.
42 changes: 40 additions & 2 deletions entities.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
entities = {
server = {
defs = {},
container = {}
container = {},
activeChunks = {}
},
client = {
defs = {},
}
}

-- 306px visible on screen, 465 visible on map
entities.activeRadius = 500
entities.chunkSize = 8

local entityDefs = {
require 'entityDefs.player',
require 'entityDefs.slime'
Expand All @@ -21,13 +26,15 @@ for _, sc in pairs{'server', 'client'} do
end
end

--[[
function entities.server.load()
-- todo: load chunks
for i=1, 8 do
local x, y = (math.random()*2-1)*256, (math.random()*2-1)*256
entities.server.defs.slime:new{x=x, y=y}:spawn()
end
end
]]

function entities.server.reset()
for etype, _ in pairs(entities.server.defs) do
Expand All @@ -38,11 +45,42 @@ function entities.server.reset()
end

function entities.server.update(dt)
-- todo: load chunks, cull/uncull
-- spawn if new chunks active
local newActiveChunks = {}
for _, v in pairs(server.currentState.players) do
local cx1 = math.floor((v.x - entities.activeRadius)/15/entities.chunkSize)
local cx2 = math.floor((v.x + entities.activeRadius)/15/entities.chunkSize)
local cy1 = math.floor((v.y - entities.activeRadius)/15/entities.chunkSize)
local cy2 = math.floor((v.y + entities.activeRadius)/15/entities.chunkSize)
for cx=cx1, cx2 do
for cy=cy1, cy2 do
if newActiveChunks[cx] == nil then newActiveChunks[cx] = {} end
newActiveChunks[cx][cy] = true
if not entities.server.activeChunks[cx] or not entities.server.activeChunks[cx][cy] then
local choices = {none=90, slime=10}
for _=1, 3 do
choice = lume.weightedchoice(choices)
if choice ~= 'none' then
local x = cx*entities.chunkSize*15 + math.random()*entities.chunkSize*15
local y = cy*entities.chunkSize*15 + math.random()*entities.chunkSize*15
entities.server.defs[choice]:new{x=x, y=y}:spawn()
end
end
end
end
end
end
entities.server.activeChunks = newActiveChunks

-- update, destroy if not in active chunk
for etype, _ in pairs(entities.server.defs) do
for _, v in pairs(entities.server.container[etype] or {}) do
v:update(dt)
local cx = math.floor(v.x/15/entities.chunkSize)
local cy = math.floor(v.y/15/entities.chunkSize)
if not entities.server.activeChunks[cx] or not entities.server.activeChunks[cx][cy] then
v:destroy()
end
end
end
end
Expand Down
35 changes: 0 additions & 35 deletions entityDefs/_base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ function base.server:spawn()
local id = self.id
container[type] = container[type] or {}
container[type][id] = self
--self:freeze()
server.currentState.entities[self.id] = self:serialize()
server.added.entities[self.id] = self:serialize()
return self
Expand Down Expand Up @@ -68,40 +67,6 @@ function base.server:destroy()
server.removed.entities[self.id] = self.id
end

--[[
function base.server:freeze()
self.frozen = true
end
function base.server:unfreeze()
self.frozen = false
end
function base.server:cull()
self:destroy()
self.culled = true
local container = self.static and entities.server.static.culledContainer
or entities.server.dynamic.culledContainer
local cs = entities.chunkSize
local chunk = math.floor(self.x/cs) .. ',' .. math.floor(self.y/cs)
self.culledChunk = chunk
local type = self.type
local id = self.id
container[chunk] = container[chunk] or {}
container[chunk][type] = container[chunk][type] or {}
container[chunk][type][id] = self
end
function base.server:uncull()
local container = self.static and entities.server.static.culledContainer
or entities.server.dynamic.culledContainer
container[self.culledChunk][self.type][self.id] = nil
self.culled = false
self.culledChunk = nil
self:spawn()
end
]]



function base.client:new(o)
Expand Down
16 changes: 2 additions & 14 deletions entityDefs/slime.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ function slime.server:damage(d, clientId)
self.hp = self.hp - d
if self.hp <= 0 and not self.destroyed then
server.addXP(clientId, math.random(3, 5))
for i=1, math.random(1, 2) do
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
self:new{x=x, y=y}:spawn()
end
local items = {}
local choices = {none=50, sword=25, shield=25}
for i=1, 3 do
for _=1, 3 do
choice = lume.weightedchoice(choices)
if choice ~= 'none' then table.insert(items, choice) end
end
Expand Down Expand Up @@ -121,18 +121,6 @@ function slime.server:destroy()
base.server.destroy(self)
end

--[[
function slime.server:freeze()
self.body:setType('static')
base.server.freeze(self)
end
function slime.server:unfreeze()
self.body:setType('dynamic')
base.server.unfreeze(self)
end
]]



function slime.client:new(o)
Expand Down
2 changes: 1 addition & 1 deletion server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ function server.start(port, singleplayer)
server.currentState = server.newState()

physics.server.load()
entities.server.load()
--entities.server.load()
end

function server.newState()
Expand Down

0 comments on commit 3faa57c

Please sign in to comment.