Skip to content

Commit a01ec8a

Browse files
authored
deathmatch: tiny cleanup (#519)
* Minor cleanup Use generic even/function names, use onPlayerResourceStart event. * Addendum to previous commit
1 parent 8ffc446 commit a01ec8a

File tree

6 files changed

+56
-58
lines changed

6 files changed

+56
-58
lines changed

[gamemodes]/[deathmatch]/deathmatch/client/hud.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
-- TODO: long term - implement new UI resembling original game design
2-
-- more code cleanup?
31
local SCREEN_WIDTH, SCREEN_HEIGHT = guiGetScreenSize()
42

53
--
@@ -25,6 +23,7 @@ end
2523
_hud.loadingScreen.update = function()
2624
_hud.loadingScreen.mapInfoText:text(_mapTitle..(_mapAuthor and ("\n by ".._mapAuthor) or ""))
2725
end
26+
2827
-- score display
2928
_hud.scoreDisplay = {}
3029
_hud.scoreDisplay.roundInfoText = dxText:create("", 0, 0, false, "bankgothic", 1)
@@ -48,6 +47,7 @@ _hud.scoreDisplay.update = function()
4847
.."\nRank: "..getElementData(localPlayer, "Rank").."/"..#getElementsByType("player")
4948
)
5049
end
50+
5151
-- respawn screen
5252
_hud.respawnScreen = {}
5353
-- respawn counter (You will respawn in x seconds)
@@ -65,6 +65,7 @@ _hud.respawnScreen.startCountdown = function()
6565
_hud.respawnScreen.respawnCounter:text("Wasted")
6666
end
6767
end
68+
6869
-- end screen
6970
_hud.endScreen = {}
7071
-- announcement text (x has won the round!)

[gamemodes]/[deathmatch]/deathmatch/client/main.lua

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
--
2-
-- startDeathmatchClient: initializes the deathmatch client
2+
-- startGamemodeClient: initializes the gamemode client
33
--
4-
local function startDeathmatchClient()
4+
local function startGamemodeClient()
55
-- add scoreboard columns
66
exports.scoreboard:scoreboardAddColumn("Score")
77
exports.scoreboard:scoreboardAddColumn("Rank")
@@ -13,27 +13,25 @@ local function startDeathmatchClient()
1313
if getElementData(resourceRoot, "gameState") == GAME_IN_PROGRESS then
1414
setCameraMatrix(unpack(calculateLoadingCameraMatrix()))
1515
end
16-
-- inform server we are ready to play
17-
triggerServerEvent("onDeathmatchPlayerReady", localPlayer)
1816
end
19-
addEventHandler("onClientResourceStart", resourceRoot, startDeathmatchClient)
17+
addEventHandler("onClientResourceStart", resourceRoot, startGamemodeClient)
2018

2119
--
22-
-- stopDeathmatchClient: cleans up the deathmatch client
20+
-- stopGamemodeClient: cleans up the gamemode client
2321
--
24-
local function stopDeathmatchClient()
22+
local function stopGamemodeClient()
2523
-- remove scoreboard columns
2624
exports.scoreboard:scoreboardRemoveColumn("Score")
2725
exports.scoreboard:scoreboardRemoveColumn("Rank")
2826
-- hide scoreboard
2927
exports.scoreboard:setScoreboardForced(false)
3028
end
31-
addEventHandler("onClientResourceStop", resourceRoot, stopDeathmatchClient)
29+
addEventHandler("onClientResourceStop", resourceRoot, stopGamemodeClient)
3230

3331
--
34-
-- startDeathmatchMap: triggered when a deathmatch map starts
32+
-- startGamemodeMap: triggered when a gamemode map starts
3533
--
36-
local function startDeathmatchMap(mapTitle, mapAuthor, fragLimit, respawnTime)
34+
local function startGamemodeMap(mapTitle, mapAuthor, fragLimit, respawnTime)
3735
-- apply the loading camera matrix - used to stream-in map elements
3836
setCameraMatrix(unpack(calculateLoadingCameraMatrix()))
3937
-- hide end screen and scoreboard
@@ -48,13 +46,13 @@ local function startDeathmatchMap(mapTitle, mapAuthor, fragLimit, respawnTime)
4846
_hud.loadingScreen:update()
4947
_hud.loadingScreen:setVisible(true)
5048
end
51-
addEvent("onClientDeathmatchMapStart", true)
52-
addEventHandler("onClientDeathmatchMapStart", resourceRoot, startDeathmatchMap)
49+
addEvent("onClientGamemodeMapStart", true)
50+
addEventHandler("onClientGamemodeMapStart", resourceRoot, startGamemodeMap)
5351

5452
--
55-
-- stopDeathmatchMap: triggered when a deathmatch map stops
53+
-- stopGamemodeMap: triggered when a gamemode map stops
5654
--
57-
local function stopDeathmatchMap()
55+
local function stopGamemodeMap()
5856
-- clear stored map data
5957
_mapTitle = nil
6058
_mapAuthor = nil
@@ -63,13 +61,13 @@ local function stopDeathmatchMap()
6361
-- hide loading text
6462
_hud.loadingScreen:setVisible(false)
6563
end
66-
addEvent("onClientDeathmatchMapStop", true)
67-
addEventHandler("onClientDeathmatchMapStop", resourceRoot, stopDeathmatchMap)
64+
addEvent("onClientGamemodeMapStop", true)
65+
addEventHandler("onClientGamemodeMapStop", resourceRoot, stopGamemodeMap)
6866

6967
--
70-
-- startDeathmatchRound: triggered when a round begins
68+
-- startGamemodeRound: triggered when a round begins
7169
--
72-
local function startDeathmatchRound()
70+
local function startGamemodeRound()
7371
-- attach player wasted handler
7472
addEventHandler("onClientPlayerWasted", localPlayer, _hud.respawnScreen.startCountdown)
7573
-- attach element data change handler
@@ -82,13 +80,13 @@ local function startDeathmatchRound()
8280
_hud.scoreDisplay:update()
8381
_hud.scoreDisplay:setVisible(true)
8482
end
85-
addEvent("onClientDeathmatchRoundStart", true)
86-
addEventHandler("onClientDeathmatchRoundStart", resourceRoot, startDeathmatchRound)
83+
addEvent("onClientGamemodeRoundStart", true)
84+
addEventHandler("onClientGamemodeRoundStart", resourceRoot, startGamemodeRound)
8785

8886
--
89-
-- stopDeathmatchRound: triggered when a round ends
87+
-- stopGamemodeRound: triggered when a round ends
9088
--
91-
local function stopDeathmatchRound(winner, draw, aborted)
89+
local function stopGamemodeRound(winner, draw, aborted)
9290
-- remove player wasted handler and hide respawn screen if active
9391
removeEventHandler("onClientPlayerWasted", localPlayer, _hud.respawnScreen.startCountdown)
9492
_hud.respawnScreen.setVisible(false)
@@ -110,8 +108,8 @@ local function stopDeathmatchRound(winner, draw, aborted)
110108
_hud.endScreen:setVisible(true)
111109
exports.scoreboard:setScoreboardForced(true)
112110
end
113-
addEvent("onClientDeathmatchRoundEnd", true)
114-
addEventHandler("onClientDeathmatchRoundEnd", resourceRoot, stopDeathmatchRound)
111+
addEvent("onClientGamemodeRoundEnd", true)
112+
addEventHandler("onClientGamemodeRoundEnd", resourceRoot, stopGamemodeRound)
115113

116114
--
117115
-- elementDataChange: triggered when element data changes - used to track score changes

[gamemodes]/[deathmatch]/deathmatch/meta.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<meta>
2-
<info name="Deathmatch" author="Talidan, jlillis" version="2.0.0" type="gamemode" compatible-with="tdm" description="A raw deathmatch gamemode." edf:definition="edf/dm.edf" />
2+
<info name="Deathmatch" author="Talidan, jlillis" version="2.0.1" type="gamemode" compatible-with="tdm" description="A raw deathmatch gamemode." edf:definition="edf/dm.edf" />
33

44
<!-- Required resources -->
55
<include resource="killmessages" />

[gamemodes]/[deathmatch]/deathmatch/server/main.lua

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,34 @@ local defaults = {
1010
}
1111

1212
--
13-
-- startDeathmatchMode: initializes the deathmatch gamemode
13+
-- startGamemodeMode: initializes the gamemode
1414
--
15-
local function startDeathmatchMode()
15+
local function startGamemode()
1616
-- update game state
1717
setElementData(resourceRoot, "gameState", GAME_WAITING)
1818
-- set default player state on gamemode start (clients will report in when ready)
1919
for _, player in ipairs(getElementsByType("player")) do
2020
_playerStates[player] = PLAYER_JOINED
2121
end
2222
end
23-
addEventHandler("onGamemodeStart", resourceRoot, startDeathmatchMode)
23+
addEventHandler("onGamemodeStart", resourceRoot, startGamemode)
2424

2525
--
26-
-- stopDeathmatchMode: cleans up the deathmatch gamemode
26+
-- stopGamemodeMode: cleans up the gamemode
2727
--
28-
local function stopDeathmatchMode()
28+
local function stopGamemode()
2929
-- cleanup player score data, make sure scoreboard isn't forced
3030
for _, player in ipairs(getElementsByType("player")) do
3131
removeElementData(player, "Score")
3232
removeElementData(player, "Rank")
3333
end
3434
end
35-
addEventHandler("onGamemodeStop", resourceRoot, stopDeathmatchMode)
35+
addEventHandler("onGamemodeStop", resourceRoot, stopGamemode)
3636

3737
--
38-
-- startDeathmatchMap: initializes a deathmatch map
38+
-- startGamemodeMap: initializes a gamemode map
3939
--
40-
local function startDeathmatchMap(resource)
40+
local function startGamemodeMap(resource)
4141
-- load map settings
4242
_mapResource = resource
4343
local resourceName = getResourceName(resource)
@@ -46,7 +46,7 @@ local function startDeathmatchMap(resource)
4646
_respawnTime = (tonumber(get(resourceName..".respawn_time")) and math.floor(tonumber(get(resourceName..".respawn_time"))) or defaults.respawnTime)*1000
4747
-- use a default frag and time limit if both are zero (infinite)
4848
if _fragLimit == 0 and _timeLimit == 0 then
49-
outputDebugString("deathmatch: map frag_limit and time_limit both disabled; using default values", 2)
49+
outputDebugString("Gamemode: map frag_limit and time_limit both disabled; using default values", 2)
5050
_fragLimit = defaults.fragLimit
5151
_timeLimit = defaults.timeLimit
5252
end
@@ -70,30 +70,30 @@ local function startDeathmatchMap(resource)
7070
-- inform all ready players that the game is about to start
7171
for player, state in pairs(_playerStates) do
7272
if state == PLAYER_READY then
73-
triggerClientEvent(player, "onClientDeathmatchMapStart", resourceRoot, _mapTitle, _mapAuthor, _fragLimit, _respawnTime)
73+
triggerClientEvent(player, "onClientGamemodeMapStart", resourceRoot, _mapTitle, _mapAuthor, _fragLimit, _respawnTime)
7474
end
7575
end
7676
-- schedule round to begin
7777
setTimer(beginRound, CAMERA_LOAD_DELAY, 1)
7878
end
79-
addEventHandler("onGamemodeMapStart", root, startDeathmatchMap)
79+
addEventHandler("onGamemodeMapStart", root, startGamemodeMap)
8080

8181
--
82-
-- stopDeathmatchMap: cleans up a deathmatch map
82+
-- stopGamemodeMap: cleans up a gamemode map
8383
--
84-
local function stopDeathmatchMap(resource)
84+
local function stopGamemodeMap(resource)
8585
-- end the round
8686
endRound(false, false, true)
8787
-- update game state
8888
setElementData(resourceRoot, "gameState", GAME_WAITING)
8989
-- inform all clients that the map was stopped
9090
for player, state in pairs(_playerStates) do
9191
if state ~= PLAYER_JOINED then
92-
triggerClientEvent(player, "onClientDeathmatchMapStop", resourceRoot)
92+
triggerClientEvent(player, "onClientGamemodeMapStop", resourceRoot)
9393
end
9494
end
9595
end
96-
addEventHandler("onGamemodeMapStop", root, stopDeathmatchMap)
96+
addEventHandler("onGamemodeMapStop", root, stopGamemodeMap)
9797

9898
--
9999
-- calculatePlayerRanks: calculates player ranks

[gamemodes]/[deathmatch]/deathmatch/server/player.lua

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,30 @@ end
2525
addEventHandler("onPlayerQuit", root, processPlayerQuit)
2626

2727
--
28-
-- deathmatchPlayerReady: triggered when a client is ready to play
28+
-- gamemodePlayerReady: triggered when a client is ready to play
2929
--
3030
-- triggered by the client post-onClientResourceStart
31-
function deathmatchPlayerReady()
31+
function gamemodePlayerReady()
3232
-- inform client of current game state by triggering certain events
3333
local gameState = getElementData(resourceRoot, "gameState")
3434
if gameState == GAME_STARTING then
35-
triggerClientEvent(client, "onClientDeathmatchMapStart", resourceRoot, _mapTitle, _mapAuthor, _fragLimit, _respawnTime)
35+
triggerClientEvent(source, "onClientGamemodeMapStart", resourceRoot, _mapTitle, _mapAuthor, _fragLimit, _respawnTime)
3636
elseif gameState == GAME_IN_PROGRESS then
37-
triggerClientEvent(client, "onClientDeathmatchMapStart", resourceRoot, _mapTitle, _mapAuthor, _fragLimit, _respawnTime)
38-
triggerClientEvent(client, "onClientDeathmatchRoundStart", resourceRoot)
39-
spawnDeathmatchPlayer(client)
37+
triggerClientEvent(source, "onClientGamemodeMapStart", resourceRoot, _mapTitle, _mapAuthor, _fragLimit, _respawnTime)
38+
triggerClientEvent(source, "onClientGamemodeRoundStart", resourceRoot)
39+
spawnGamemodePlayer(source)
4040
elseif gameState == GAME_FINISHED then
41-
triggerClientEvent(client, "onClientDeathmatchRoundEnd", resourceRoot, false, false)
41+
triggerClientEvent(source, "onClientGamemodeRoundEnd", resourceRoot, false, false)
4242
end
4343
-- update player state
44-
_playerStates[client] = PLAYER_READY
44+
_playerStates[source] = PLAYER_READY
4545
end
46-
addEvent("onDeathmatchPlayerReady", true)
47-
addEventHandler("onDeathmatchPlayerReady", root, deathmatchPlayerReady)
46+
addEventHandler("onPlayerResourceStart", root, gamemodePlayerReady)
4847

4948
--
50-
-- spawnDeathmatchPlayer: spawns a player in deathmatch mode
49+
-- spawnGamemodePlayer: spawns a player in Gamemode mode
5150
--
52-
function spawnDeathmatchPlayer(player)
51+
function spawnGamemodePlayer(player)
5352
if not isElement(player) then
5453
return
5554
end
@@ -105,6 +104,6 @@ function processPlayerWasted(totalAmmo, killer, killerWeapon, bodypart)
105104
calculatePlayerRanks()
106105
-- set timer to respawn player
107106
if _respawnTime > 0 then
108-
_respawnTimers[source] = setTimer(spawnDeathmatchPlayer, _respawnTime, 1, source)
107+
_respawnTimers[source] = setTimer(spawnGamemodePlayer, _respawnTime, 1, source)
109108
end
110109
end

[gamemodes]/[deathmatch]/deathmatch/server/round.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ function beginRound()
1717
setElementData(players[i], "Score", 0)
1818
setElementData(players[i], "Rank", "-")
1919
if _playerStates[players[i]] == PLAYER_READY then
20-
spawnDeathmatchPlayer(players[i])
21-
triggerClientEvent(players[i], "onClientDeathmatchRoundStart", resourceRoot)
20+
spawnGamemodePlayer(players[i])
21+
triggerClientEvent(players[i], "onClientGamemodeRoundStart", resourceRoot)
2222
end
2323
end
2424
end
@@ -63,7 +63,7 @@ function endRound(winner, draw, aborted)
6363
-- update player state
6464
_playerStates[players[i]] = PLAYER_READY
6565
-- inform client round is over
66-
triggerClientEvent(players[i], "onClientDeathmatchRoundEnd", resourceRoot, winner, draw, aborted)
66+
triggerClientEvent(players[i], "onClientGamemodeRoundEnd", resourceRoot, winner, draw, aborted)
6767
end
6868
end
6969
-- don't cycle the map if the round was aborted (map resource was stopped)

0 commit comments

Comments
 (0)