-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentities.lua
146 lines (134 loc) · 5.7 KB
/
entities.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
entities = {
server = {
defs = {},
container = {},
activeChunks = {}
},
client = {
defs = {},
}
}
-- 306px visible on screen, 465 visible on map
entities.activeRadius = 500
entities.chunkSize = 8
local entityDefs = {}
for _, v in ipairs{'player', 'slime', 'sorcerer', 'spoder', 'stingy', 'zombie', 'ant',
'newMonster1', 'newMonster2', 'mudskipper', 'mudskipperEvolved', 'godex',
'tree', 'wall', 'bush', 'bigRock', 'smallRock', 'questBlock'} do
table.insert(entityDefs, require('entityDefs.' .. v))
end
for _, sc in ipairs{'server', 'client'} do
-- entities.server.defs.slime = slime.server
for _, v in pairs(entityDefs) do
entities[sc].defs[v.server.type] = v[sc]
end
end
function entities.server.reset()
for etype, _ in pairs(entities.server.defs) do
for _, v in pairs(entities.server.container[etype] or {}) do
v:destroy()
end
end
end
function entities.server.update(dt)
-- 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
-- spawn enemies
local choices = {none=80, slime=2, sorcerer=2, spoder=2, stingy=2, zombie=2, ant=2,
newMonster1=2, newMonster2=2, mudskipper=1, mudskipperEvolved=1, godex=2}
for _=1, 3 do
choice = lume.weightedchoice(choices)
if choice ~= 'none' then
local x = (cx*entities.chunkSize + math.random()*entities.chunkSize)*15
local y = (cy*entities.chunkSize + math.random()*entities.chunkSize)*15
-- if not in spawn area or wall
if x^2 + y^2 > 192^2 and serverRealm.world:getTile(x, y) ~= tile2id['wall'] then
entities.server.defs[choice]:new{x=x, y=y}:spawn()
end
end
end
-- spawn walls, quest block
for i=1, entities.chunkSize do
for j=1, entities.chunkSize do
local x = (cx*entities.chunkSize + (i-1))*15
local y = (cy*entities.chunkSize + (j-1))*15
if x == 0 and y == 0 then
entities.server.defs.questBlock:new{x=x, y=y}:spawn()
elseif serverRealm.world:getTile(x, y) == tile2id['wall'] then
entities.server.defs.wall:new{x=x, y=y}:spawn()
end
end
end
-- spawn trees, bush, rocks
for _=1, 3 do
if math.random() < 0.5 then
local x = (cx*entities.chunkSize + math.random()*entities.chunkSize)*15
local y = (cy*entities.chunkSize + math.random()*entities.chunkSize)*15
-- if on grass and grass surrounding
local onGrass = true
for i=-1, 1 do
for j=-1, 1 do
if serverRealm.world:getTile(x + i*15, y + j*15) ~= tile2id['grass'] then
onGrass = false
break
end
end
if not onGrass then break end
end
if onGrass then
local choices = {tree=40, bush=30, bigRock=10, smallRock=20}
choice = lume.weightedchoice(choices)
entities.server.defs[choice]:new{x=x, y=y}:spawn()
end
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
function entities.client.reset()
for _, v in pairs(client.currentState.entities) do
v:destroy()
end
end
function entities.client.update(dt)
-- todo: local cull
for _, v in pairs(client.currentState.entities) do
v:update(dt)
end
end
function entities.client.draw()
for _, v in pairs(client.currentState.entities) do
if not v.destroyed and v.draw and v.id ~= playerController.serverId then
scene.add{
draw = function()
v:draw()
end,
y = v.y
}
end
end
end