-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameView.lua
570 lines (467 loc) · 16.8 KB
/
GameView.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
-- GameView
NetworkSyncedObjects = {
TargetDummy = true,
Npc = true,
Barrier = true,
Ressource = true,
ValidPosition = true,
Cover = true,
}
GameView = View:extend
{
layers = {
management = Group:new(),
ground = Group:new(),
particles = Group:new(),
characters = Group:new(),
projectiles = Group:new(),
above = Group:new(),
lineOfSight = Group:new(),
ui = Group:new(),
debug = Group:new(),
topmost = Group:new(),
},
game_start_time = 0,
fogEnabled = nil,
-- GridIndex
gridIndexMovable = nil,
-- GridIndex
gridIndexCollision = nil,
loadMap = function (self, file, filter)
local ok, data = pcall(loadstring(Cached:text(file)))
if ok then
for _, layer in pairs(data.layers) do
if layer.name == "objects" and layer.type == 'objectgroup' then
for _, obj in pairs(layer.objects) do
-- roll in tile properties if based on a tile
if obj.gid and tileProtos[obj.gid] then
local tile = tileProtos[obj.gid]
obj.name = tile.properties.name
obj.width = tile.width
obj.height = tile.height
for key, value in pairs(tile.properties) do
obj.properties[key] = tovalue(value)
end
end
-- create a new object if the class does exist
local spr
if not filter or filter(obj) then
if obj.name and rawget(_G, obj.name) then
obj.properties.x = obj.x
obj.properties.y = obj.y
obj.properties.width = obj.width
obj.properties.height = obj.height
spr = _G[obj.name]:new(obj.properties)
else
spr = Class:new{ x = obj.x, y = obj.y, width = obj.width, height = obj.height, fill = { 128, 128, 128 } }
end
if obj.properties._the then
the[obj.properties._the] = spr
end
end
end
end
end
else
error('could not load view data from file: ' .. data)
end
end,
onNew = function (self)
the.app.view = self
print("the.app.view", the.app.view)
self.gridIndexMovable = GridIndex:new{}
the.gridIndexMovable = self.gridIndexMovable
self.gridIndexCollision = GridIndex:new{}
the.gridIndexCollision = self.gridIndexCollision
self.gridIndexTargetDummys = GridIndex:new{}
the.gridIndexTargetDummys = self.gridIndexTargetDummys
-- object -> true map for easy remove, key contains projectile references
the.projectiles = {}
the.targetDummies = {}
the.footsteps = {}
the.ressources = {}
the.ressourceObjects = {}
the.validPositions = {}
the.covers = {}
the.characters = {}
the.blockers = {}
local mapIdx = 1 + (network.seed % config.numberOfMaps)
if config.mapNumber ~= 0 and config.mapNumber then
mapIdx = config.mapNumber
end
the.mapFile = '/assets/maps/desert/desert' .. mapIdx .. '.lua'
print("using map", the.mapFile)
self:loadLayers(the.mapFile, true, {objects = true, })
local is_server = network.is_first and network.connected_client_count == 1
print("startup", network.is_first, network.connected_client_count)
self:loadMap(the.mapFile, function (o) return not (o.name and NetworkSyncedObjects[o.name]) end)
-- first client -> setup "new" world
if is_server then
PhaseManager:new{}
Score:new{}
end
self.collision.visible = false
self.collision.static = true
if self.cover then
self.cover.visible = false
self.cover.static = true
end
-- specify render order
self:add(self.layers.management)
self:add(self.layers.ground)
self:add(self.layers.particles)
self:add(self.layers.characters)
self:add(self.layers.projectiles)
self:add(self.layers.above)
self:add(self.layers.ui)
self:add(self.layers.lineOfSight)
self:add(self.layers.debug)
the.hud = UiGroup:new()
self:add(the.hud)
self:add(self.layers.topmost)
-- setup player
the.player = Player:new{ x = the.app.width / 2, y = the.app.height / 2,
name = localconfig.playerName,
armor = localconfig.armor,
weapon = localconfig.weapon,
team = localconfig.team,
}
-- place ontop
self:remove(self.trees)
self:remove(self.buildings)
self:remove(self.vegetation)
self.layers.above:add(self.trees)
self.layers.above:add(self.buildings)
self.layers.above:add(self.vegetation)
-- set spawn position
the.player.x = the.spawnpoint.x
the.player.y = the.spawnpoint.y
the.focusSprite = FocusSprite:new{ x = 0, y = 0 }
self:add(the.focusSprite)
the.arrow = Arrow:new{ x = 0, y = 0 }
if the.player and the.player.class ~= "Ghost" then
self.focus = the.focusSprite
else
self.focus = the.player
end
-- topmost, cursor + loveframes
the.cursor = Cursor:new{ x = 0, y = 0 }
self.layers.topmost:add(LoveFramesCaller:new{})
self.layers.topmost:add(the.cursor)
the.timerDisplay = TimerDisplay:new{ x = 0, y = 0 }
the.hud:add(the.timerDisplay)
the.xpTimerDisplay = XpTimerDisplay:new{ x = 0, y = 0 }
the.hud:add(the.xpTimerDisplay)
the.playtestTimerDisplay = PlaytestTimerDisplay:new{ x = 0, y = 0 }
the.hud:add(the.playtestTimerDisplay)
the.networkDisplay = NetworkDisplay:new{ x = 0, y = 0 }
the.hud:add(the.networkDisplay)
--~ the.debuffDisplay = DebuffDisplay:new{}
--~ the.hud:add(the.debuffDisplay)
the.ignorePlayerCharacterInputs = false
the.chatText = ChatText:new{}
the.hud:add(the.chatText)
the.chatText.x = 10
the.chatText.y = love.graphics.getHeight() - 120
the.skillbar = SkillBar:new()
-- set skillbar images
local skills = {}
for k,v in pairs(the.player.skills) do
table.insert(skills, action_definitions[v.id].icon)
end
the.skillbar:setSkills(skills)
--the.playerDetails = PlayerDetails:new{ x = 0, y = 0 }
--self.layers.ui:add(the.playerDetails)
the.controlUI = ControlUI:new{}
the.hud:add(the.controlUI)
the.energyUIBG = EnergyUIBG:new{}
the.hud:add(the.energyUIBG)
the.energyUI = EnergyUI:new{}
the.hud:add(the.energyUI)
the.painUIBG = PainUIBG:new{}
the.hud:add(the.painUIBG)
the.painUI = PainUI:new{}
the.hud:add(the.painUI)
the.experienceUIBG = ExperienceUIBG:new{}
the.hud:add(the.experienceUIBG)
the.experienceUI = ExperienceUI:new{}
the.hud:add(the.experienceUI)
the.levelUI = {}
for i = 0, config.levelCap - 1 do
local width = (love.graphics.getWidth() / 2 - the.controlUI.width / 2) / config.levelCap
local ui = LevelUI:new{width = width, x = (love.graphics.getWidth() + the.controlUI.width) / 2 + width * i}
ui.level = i + 1
the.hud:add(ui)
table.insert(the.levelUI, ui)
end
audio.init()
self:setupNetworkHandler()
-- auth
network.send_request({channel = "server", cmd = "auth",
name = localconfig.playerName, pass = localconfig.accountPassword, }, function(fin, result)
end)
-- set admin flag, totally not tamper proved, just prevent accidential admin commands
network.send_request({channel = "server", cmd = "is_admin", password = localconfig.adminPassword}, function(fin, result)
network.is_admin = result.is_admin
print("is admin", network.is_admin)
end)
-- send revision to server
local revision = storage.load_content("revision.txt")
print("REV", revision)
network.send({channel = "server", cmd = "revision", rev = revision})
local chatInfo = Text:new{
x = 5, y = love.graphics.getHeight() - 85, width = 500, tint = config.textColor,
text = "Press enter to chat",
}
the.hud:add(chatInfo)
-- text chat input
the.frameChatInput = loveframes.Create("textinput")
the.frameChatInput:SetPos(5, love.graphics.getHeight() - 90)
the.frameChatInput:SetWidth(love.graphics.getWidth() - 10)
the.frameChatInput:SetVisible(false)
the.frameChatInput:SetFocus(false)
the.frameChatInput.OnEnter = function (self, text)
if text:len() > 0 then
if not runAsLocalChatCommand(text) then
--~ print("CHAT", self.visible, text)
network.send({channel = "chat", cmd = "text", from = localconfig.playerName, text = text, time = network.time})
showChatText(localconfig.playerName, text, network.time)
end
end
the.app.view.timer:after(0.1, function()
self:SetVisible(false)
self:SetFocus(false)
self:SetText("")
the.ignorePlayerCharacterInputs = false
-- TODO keep focus if one clicks of close on click on screen
end)
end
switchToGhost()
the.lineOfSight = LineOfSight:new{}
self.layers.lineOfSight:add(the.lineOfSight)
-- prepare collision layer
for k,v in pairs(self.collision.sprites) do
self.gridIndexCollision:insertAt(v.x,v.y,v)
end
end,
setFogEnabled = function (self, enabled)
if self.fogEnabled ~= enabled then
self.fogEnabled = enabled
-- TODO update fog or line of sight
self.fogEnabled = enabled
end
end,
onUpdate = function (self, elapsed)
collectgarbage("step", 1)
profile.start("gameview.onupdate")
-- handle chat
if the.keys:justPressed("return") then
print(the.frameChatInput.visible, the.frameChatInput.focus)
if not the.frameChatInput.visible then
the.frameChatInput:SetVisible(true)
the.frameChatInput:SetFocus(true)
the.ignorePlayerCharacterInputs = true
end
end
-- show debug geometry?
self.layers.debug.visible = config.draw_debug_info
if the.player and the.player.class ~= "Ghost" then
profile.start("update.skillbar")
the.skillbar:onUpdate(elapsed)
profile.stop()
else
the.player:onUpdate(elapsed)
end
profile.start("update.displace")
local characterDisplaceRange = 100
for dummy,v in pairs(the.targetDummies) do
profile.start("update.displace.collision") self.gridIndexCollision:visitInRange(dummy.x, dummy.y, characterDisplaceRange, function(o) o:displace(dummy) end) profile.stop()
profile.start("update.displace.landscape") self.landscape:subdisplace(dummy) profile.stop()
profile.start("update.displace.water") self.water:subdisplace(dummy) profile.stop()
profile.start("update.displace.characters") self.gridIndexMovable:visitInRange(dummy.x, dummy.y, characterDisplaceRange, function(o) o:displace(dummy) end) profile.stop()
profile.start("update.displace.dummy") self.gridIndexTargetDummys:visitInRange(dummy.x, dummy.y, characterDisplaceRange, function(o) o:displace(dummy) end) profile.stop()
end
for blocker,v in pairs(the.blockers) do
profile.start("update.displace.collision") self.gridIndexCollision:visitInRange(blocker.x, blocker.y, characterDisplaceRange, function(o) o:displace(blocker) end) profile.stop()
--self.layers.characters:displace(blocker)
profile.start("update.displace.landscape") self.landscape:subdisplace(blocker) profile.stop()
profile.start("update.displace.water") self.water:subdisplace(blocker) profile.stop()
end
if the.player and the.player.class ~= "Ghost" then
profile.start("update.displace.collision") self.gridIndexCollision:visitInRange(the.player.x, the.player.y, characterDisplaceRange, function(o) o:displace(the.player) end) profile.stop()
profile.start("update.displace.landscape") self.landscape:subdisplace(the.player) profile.stop()
profile.start("update.displace.water") self.water:subdisplace(the.player) profile.stop()
profile.start("update.displace.characters") self.gridIndexMovable:visitInRange(the.player.x, the.player.y, characterDisplaceRange, function(o) o:displace(the.player) end) profile.stop()
end
if the.barrier then
profile.start("update.displace.collision") self.gridIndexCollision:visitInRange(the.barrier.x, the.barrier.y, characterDisplaceRange, function(o) o:displace(the.barrier) end) profile.stop()
profile.start("update.displace.landscape") self.landscape:subdisplace(the.barrier) profile.stop()
profile.start("update.displace.water") self.water:subdisplace(the.barrier) profile.stop()
profile.start("update.displace.characters") self.gridIndexMovable:visitInRange(the.barrier.x, the.barrier.y, characterDisplaceRange, function(o) o:displace(the.barrier) end) profile.stop()
end
profile.stop()
profile.start("update.projectile")
for projectile,v in pairs(the.projectiles) do
self.landscape:subcollide(projectile)
if projectile.image ~= "/assets/graphics/action_projectiles/scythe_jump.png" then
self.collision:collide(projectile)
self.layers.characters:collide(projectile)
end
end
profile.stop()
--~ local s = ""
--~ for k, v in pairs(the.ressources) do
--~ s = s .. k .. ": " .. v .. "\n"
--~ end
--~ the.ressourceDisplay.text = s
profile.start("AUDIO")
audio.update()
profile.stop()
profile.stop()
for k,v in pairs(self.gridIndexTargetDummys.grid) do
for kk,vv in pairs(v) do print(">", k,kk,vv,kk.x,kk.y,kk.class) end
end
if config.show_profile_info then profile.print() end
profile.clear()
end,
resyncAllLocalObjects = function (self)
local s,c = 0,0
object_manager.visit(function(oid,o)
if o:isLocal() then
if o.sendResync then o:sendResync() s = s + 1 end
if o.netCreate then o:netCreate() c = c + 1 end
end
end)
print("RESYNC", "sync", s, "create", c)
end,
setupNetworkHandler = function (self)
table.insert(network.on_message, function(m)
--~ print ("RECEIVED", json.encode(m))
if m.channel == "chat" then
if m.cmd == "text" then
showChatText(m.from, m.text, m.time)
end
end
if m.channel == "game" then
if m.cmd == "create" then
local o = object_manager.get(m.oid)
if not o then
--~ print("NEW REMOTE OBJECT", m.oid, m.owner, m.class)
m.created_via_network = true
o = _G[m.class]:new(m)
end
elseif m.cmd == "delete" then
--~ print("DELETE OBJ REQUEST", m.oid)
local o = object_manager.get(m.oid)
if o then
if o.active then o:die() end
object_manager.delete(o)
end
elseif m.cmd == "request" then
local o = object_manager.get(m.oid)
if o and o.netCreate then
--~ print("NEW OBJECT REQUESTED")
o:netCreate()
end
elseif m.cmd == "msg" then
local o = object_manager.get(m.oid)
if o and o.receiveWithoutResendingToNet then
o:receiveWithoutResendingToNet(m.name, unpack(m.params or {}))
end
elseif m.cmd == "sync" then
local o = object_manager.get(m.oid)
if o then
-- sync
for k,v in pairs(m) do o[k] = v end
if m.nils then
for _,v in pairs(m.nils) do o[v] = nil end
end
--~ print("SYNC REMOTE OBJECT", o.oid)
o.last_net_sync_time = love.timer.getTime()
else
--~ print("SYNC REQUEST REMOTE OBJECT", m.oid)
network.send ({ channel = "game", cmd = "request", oid = m.oid })
end
end
elseif m.channel == "server" then
if m.cmd == "join" then
-- new player so send obj create messages
print("new player send objects#############")
self:resyncAllLocalObjects();
print("DONE new player send objects#############")
showChatText("SYSTEM", "player joined the game")
elseif m.cmd == "disconnect" then
print("DISCONNECTED BY SERVER")
os.exit()
elseif m.cmd == "left" then
-- player left so kill all objects from the player
showChatText("SYSTEM", "player left the game")
for oid,obj in pairs(object_manager.objects) do
--~ print("LEFT", oid, obj.owner, m.id)
if obj.owner == m.id then
if obj.active then obj:die() end
object_manager.delete(obj)
end
end
end
end
end)
end,
}
function switchToPlayer()
if the.player then
if the.player.class == "Ghost" then
the.player:die()
the.player = Player:new{ x = the.app.width / 2, y = the.app.height / 2,
name = localconfig.playerName,
armor = localconfig.armor,
weapon = localconfig.weapon,
team = localconfig.team,
}
-- set spawn position
the.player.x = the.spawnpoint.x
the.player.y = the.spawnpoint.y
the.app.view:setFogEnabled(true)
end
end
end
function switchToGhost()
if the.player then the.player:die() end
the.player.deaths = (the.player.deaths or 0) + 1
the.player = Ghost:new{}
the.player.x = the.spawnpoint.x
the.player.y = the.spawnpoint.y
the.app.view:setFogEnabled(false)
end
function switchBetweenGhostAndPlayer()
if the.player then
if the.player.class == "Ghost" then
switchToPlayer()
else
switchToGhost()
end
end
end
function runAsLocalChatCommand(text)
if text == "/exit" or text == "/quit" then
quitClient()
return true
elseif text == "/help" then
showChatText("LOCAL", "/exit - closes the game")
showChatText("LOCAL", "/quit - closes the game")
showChatText("LOCAL", "/list - shows how is online")
showChatText("LOCAL", "/revs - shows version of the connected clients")
return true
elseif text == "/revs" then
network.send ({ channel = "server", cmd = "list_revisions" })
return true
elseif text == "/list" then
object_manager.visit(function(oid,o)
if o.class == "Character" then
showChatText("LOCAL", (o.name or "?") .. " [" .. (o.team or "?") .. "]")
end
end)
return true
end
return false
end