Skip to content

Commit

Permalink
deathmatch: tiny cleanup (#519)
Browse files Browse the repository at this point in the history
* Minor cleanup

Use generic even/function names, use onPlayerResourceStart event.

* Addendum to previous commit
  • Loading branch information
jlillis committed Jul 17, 2024
1 parent 8ffc446 commit a01ec8a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 58 deletions.
5 changes: 3 additions & 2 deletions [gamemodes]/[deathmatch]/deathmatch/client/hud.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
-- TODO: long term - implement new UI resembling original game design
-- more code cleanup?
local SCREEN_WIDTH, SCREEN_HEIGHT = guiGetScreenSize()

--
Expand All @@ -25,6 +23,7 @@ end
_hud.loadingScreen.update = function()
_hud.loadingScreen.mapInfoText:text(_mapTitle..(_mapAuthor and ("\n by ".._mapAuthor) or ""))
end

-- score display
_hud.scoreDisplay = {}
_hud.scoreDisplay.roundInfoText = dxText:create("", 0, 0, false, "bankgothic", 1)
Expand All @@ -48,6 +47,7 @@ _hud.scoreDisplay.update = function()
.."\nRank: "..getElementData(localPlayer, "Rank").."/"..#getElementsByType("player")
)
end

-- respawn screen
_hud.respawnScreen = {}
-- respawn counter (You will respawn in x seconds)
Expand All @@ -65,6 +65,7 @@ _hud.respawnScreen.startCountdown = function()
_hud.respawnScreen.respawnCounter:text("Wasted")
end
end

-- end screen
_hud.endScreen = {}
-- announcement text (x has won the round!)
Expand Down
46 changes: 22 additions & 24 deletions [gamemodes]/[deathmatch]/deathmatch/client/main.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--
-- startDeathmatchClient: initializes the deathmatch client
-- startGamemodeClient: initializes the gamemode client
--
local function startDeathmatchClient()
local function startGamemodeClient()
-- add scoreboard columns
exports.scoreboard:scoreboardAddColumn("Score")
exports.scoreboard:scoreboardAddColumn("Rank")
Expand All @@ -13,27 +13,25 @@ local function startDeathmatchClient()
if getElementData(resourceRoot, "gameState") == GAME_IN_PROGRESS then
setCameraMatrix(unpack(calculateLoadingCameraMatrix()))
end
-- inform server we are ready to play
triggerServerEvent("onDeathmatchPlayerReady", localPlayer)
end
addEventHandler("onClientResourceStart", resourceRoot, startDeathmatchClient)
addEventHandler("onClientResourceStart", resourceRoot, startGamemodeClient)

--
-- stopDeathmatchClient: cleans up the deathmatch client
-- stopGamemodeClient: cleans up the gamemode client
--
local function stopDeathmatchClient()
local function stopGamemodeClient()
-- remove scoreboard columns
exports.scoreboard:scoreboardRemoveColumn("Score")
exports.scoreboard:scoreboardRemoveColumn("Rank")
-- hide scoreboard
exports.scoreboard:setScoreboardForced(false)
end
addEventHandler("onClientResourceStop", resourceRoot, stopDeathmatchClient)
addEventHandler("onClientResourceStop", resourceRoot, stopGamemodeClient)

--
-- startDeathmatchMap: triggered when a deathmatch map starts
-- startGamemodeMap: triggered when a gamemode map starts
--
local function startDeathmatchMap(mapTitle, mapAuthor, fragLimit, respawnTime)
local function startGamemodeMap(mapTitle, mapAuthor, fragLimit, respawnTime)
-- apply the loading camera matrix - used to stream-in map elements
setCameraMatrix(unpack(calculateLoadingCameraMatrix()))
-- hide end screen and scoreboard
Expand All @@ -48,13 +46,13 @@ local function startDeathmatchMap(mapTitle, mapAuthor, fragLimit, respawnTime)
_hud.loadingScreen:update()
_hud.loadingScreen:setVisible(true)
end
addEvent("onClientDeathmatchMapStart", true)
addEventHandler("onClientDeathmatchMapStart", resourceRoot, startDeathmatchMap)
addEvent("onClientGamemodeMapStart", true)
addEventHandler("onClientGamemodeMapStart", resourceRoot, startGamemodeMap)

--
-- stopDeathmatchMap: triggered when a deathmatch map stops
-- stopGamemodeMap: triggered when a gamemode map stops
--
local function stopDeathmatchMap()
local function stopGamemodeMap()
-- clear stored map data
_mapTitle = nil
_mapAuthor = nil
Expand All @@ -63,13 +61,13 @@ local function stopDeathmatchMap()
-- hide loading text
_hud.loadingScreen:setVisible(false)
end
addEvent("onClientDeathmatchMapStop", true)
addEventHandler("onClientDeathmatchMapStop", resourceRoot, stopDeathmatchMap)
addEvent("onClientGamemodeMapStop", true)
addEventHandler("onClientGamemodeMapStop", resourceRoot, stopGamemodeMap)

--
-- startDeathmatchRound: triggered when a round begins
-- startGamemodeRound: triggered when a round begins
--
local function startDeathmatchRound()
local function startGamemodeRound()
-- attach player wasted handler
addEventHandler("onClientPlayerWasted", localPlayer, _hud.respawnScreen.startCountdown)
-- attach element data change handler
Expand All @@ -82,13 +80,13 @@ local function startDeathmatchRound()
_hud.scoreDisplay:update()
_hud.scoreDisplay:setVisible(true)
end
addEvent("onClientDeathmatchRoundStart", true)
addEventHandler("onClientDeathmatchRoundStart", resourceRoot, startDeathmatchRound)
addEvent("onClientGamemodeRoundStart", true)
addEventHandler("onClientGamemodeRoundStart", resourceRoot, startGamemodeRound)

--
-- stopDeathmatchRound: triggered when a round ends
-- stopGamemodeRound: triggered when a round ends
--
local function stopDeathmatchRound(winner, draw, aborted)
local function stopGamemodeRound(winner, draw, aborted)
-- remove player wasted handler and hide respawn screen if active
removeEventHandler("onClientPlayerWasted", localPlayer, _hud.respawnScreen.startCountdown)
_hud.respawnScreen.setVisible(false)
Expand All @@ -110,8 +108,8 @@ local function stopDeathmatchRound(winner, draw, aborted)
_hud.endScreen:setVisible(true)
exports.scoreboard:setScoreboardForced(true)
end
addEvent("onClientDeathmatchRoundEnd", true)
addEventHandler("onClientDeathmatchRoundEnd", resourceRoot, stopDeathmatchRound)
addEvent("onClientGamemodeRoundEnd", true)
addEventHandler("onClientGamemodeRoundEnd", resourceRoot, stopGamemodeRound)

--
-- elementDataChange: triggered when element data changes - used to track score changes
Expand Down
2 changes: 1 addition & 1 deletion [gamemodes]/[deathmatch]/deathmatch/meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<meta>
<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" />
<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" />

<!-- Required resources -->
<include resource="killmessages" />
Expand Down
30 changes: 15 additions & 15 deletions [gamemodes]/[deathmatch]/deathmatch/server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,34 @@ local defaults = {
}

--
-- startDeathmatchMode: initializes the deathmatch gamemode
-- startGamemodeMode: initializes the gamemode
--
local function startDeathmatchMode()
local function startGamemode()
-- update game state
setElementData(resourceRoot, "gameState", GAME_WAITING)
-- set default player state on gamemode start (clients will report in when ready)
for _, player in ipairs(getElementsByType("player")) do
_playerStates[player] = PLAYER_JOINED
end
end
addEventHandler("onGamemodeStart", resourceRoot, startDeathmatchMode)
addEventHandler("onGamemodeStart", resourceRoot, startGamemode)

--
-- stopDeathmatchMode: cleans up the deathmatch gamemode
-- stopGamemodeMode: cleans up the gamemode
--
local function stopDeathmatchMode()
local function stopGamemode()
-- cleanup player score data, make sure scoreboard isn't forced
for _, player in ipairs(getElementsByType("player")) do
removeElementData(player, "Score")
removeElementData(player, "Rank")
end
end
addEventHandler("onGamemodeStop", resourceRoot, stopDeathmatchMode)
addEventHandler("onGamemodeStop", resourceRoot, stopGamemode)

--
-- startDeathmatchMap: initializes a deathmatch map
-- startGamemodeMap: initializes a gamemode map
--
local function startDeathmatchMap(resource)
local function startGamemodeMap(resource)
-- load map settings
_mapResource = resource
local resourceName = getResourceName(resource)
Expand All @@ -46,7 +46,7 @@ local function startDeathmatchMap(resource)
_respawnTime = (tonumber(get(resourceName..".respawn_time")) and math.floor(tonumber(get(resourceName..".respawn_time"))) or defaults.respawnTime)*1000
-- use a default frag and time limit if both are zero (infinite)
if _fragLimit == 0 and _timeLimit == 0 then
outputDebugString("deathmatch: map frag_limit and time_limit both disabled; using default values", 2)
outputDebugString("Gamemode: map frag_limit and time_limit both disabled; using default values", 2)
_fragLimit = defaults.fragLimit
_timeLimit = defaults.timeLimit
end
Expand All @@ -70,30 +70,30 @@ local function startDeathmatchMap(resource)
-- inform all ready players that the game is about to start
for player, state in pairs(_playerStates) do
if state == PLAYER_READY then
triggerClientEvent(player, "onClientDeathmatchMapStart", resourceRoot, _mapTitle, _mapAuthor, _fragLimit, _respawnTime)
triggerClientEvent(player, "onClientGamemodeMapStart", resourceRoot, _mapTitle, _mapAuthor, _fragLimit, _respawnTime)
end
end
-- schedule round to begin
setTimer(beginRound, CAMERA_LOAD_DELAY, 1)
end
addEventHandler("onGamemodeMapStart", root, startDeathmatchMap)
addEventHandler("onGamemodeMapStart", root, startGamemodeMap)

--
-- stopDeathmatchMap: cleans up a deathmatch map
-- stopGamemodeMap: cleans up a gamemode map
--
local function stopDeathmatchMap(resource)
local function stopGamemodeMap(resource)
-- end the round
endRound(false, false, true)
-- update game state
setElementData(resourceRoot, "gameState", GAME_WAITING)
-- inform all clients that the map was stopped
for player, state in pairs(_playerStates) do
if state ~= PLAYER_JOINED then
triggerClientEvent(player, "onClientDeathmatchMapStop", resourceRoot)
triggerClientEvent(player, "onClientGamemodeMapStop", resourceRoot)
end
end
end
addEventHandler("onGamemodeMapStop", root, stopDeathmatchMap)
addEventHandler("onGamemodeMapStop", root, stopGamemodeMap)

--
-- calculatePlayerRanks: calculates player ranks
Expand Down
25 changes: 12 additions & 13 deletions [gamemodes]/[deathmatch]/deathmatch/server/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,30 @@ end
addEventHandler("onPlayerQuit", root, processPlayerQuit)

--
-- deathmatchPlayerReady: triggered when a client is ready to play
-- gamemodePlayerReady: triggered when a client is ready to play
--
-- triggered by the client post-onClientResourceStart
function deathmatchPlayerReady()
function gamemodePlayerReady()
-- inform client of current game state by triggering certain events
local gameState = getElementData(resourceRoot, "gameState")
if gameState == GAME_STARTING then
triggerClientEvent(client, "onClientDeathmatchMapStart", resourceRoot, _mapTitle, _mapAuthor, _fragLimit, _respawnTime)
triggerClientEvent(source, "onClientGamemodeMapStart", resourceRoot, _mapTitle, _mapAuthor, _fragLimit, _respawnTime)
elseif gameState == GAME_IN_PROGRESS then
triggerClientEvent(client, "onClientDeathmatchMapStart", resourceRoot, _mapTitle, _mapAuthor, _fragLimit, _respawnTime)
triggerClientEvent(client, "onClientDeathmatchRoundStart", resourceRoot)
spawnDeathmatchPlayer(client)
triggerClientEvent(source, "onClientGamemodeMapStart", resourceRoot, _mapTitle, _mapAuthor, _fragLimit, _respawnTime)
triggerClientEvent(source, "onClientGamemodeRoundStart", resourceRoot)
spawnGamemodePlayer(source)
elseif gameState == GAME_FINISHED then
triggerClientEvent(client, "onClientDeathmatchRoundEnd", resourceRoot, false, false)
triggerClientEvent(source, "onClientGamemodeRoundEnd", resourceRoot, false, false)
end
-- update player state
_playerStates[client] = PLAYER_READY
_playerStates[source] = PLAYER_READY
end
addEvent("onDeathmatchPlayerReady", true)
addEventHandler("onDeathmatchPlayerReady", root, deathmatchPlayerReady)
addEventHandler("onPlayerResourceStart", root, gamemodePlayerReady)

--
-- spawnDeathmatchPlayer: spawns a player in deathmatch mode
-- spawnGamemodePlayer: spawns a player in Gamemode mode
--
function spawnDeathmatchPlayer(player)
function spawnGamemodePlayer(player)
if not isElement(player) then
return
end
Expand Down Expand Up @@ -105,6 +104,6 @@ function processPlayerWasted(totalAmmo, killer, killerWeapon, bodypart)
calculatePlayerRanks()
-- set timer to respawn player
if _respawnTime > 0 then
_respawnTimers[source] = setTimer(spawnDeathmatchPlayer, _respawnTime, 1, source)
_respawnTimers[source] = setTimer(spawnGamemodePlayer, _respawnTime, 1, source)
end
end
6 changes: 3 additions & 3 deletions [gamemodes]/[deathmatch]/deathmatch/server/round.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ function beginRound()
setElementData(players[i], "Score", 0)
setElementData(players[i], "Rank", "-")
if _playerStates[players[i]] == PLAYER_READY then
spawnDeathmatchPlayer(players[i])
triggerClientEvent(players[i], "onClientDeathmatchRoundStart", resourceRoot)
spawnGamemodePlayer(players[i])
triggerClientEvent(players[i], "onClientGamemodeRoundStart", resourceRoot)
end
end
end
Expand Down Expand Up @@ -63,7 +63,7 @@ function endRound(winner, draw, aborted)
-- update player state
_playerStates[players[i]] = PLAYER_READY
-- inform client round is over
triggerClientEvent(players[i], "onClientDeathmatchRoundEnd", resourceRoot, winner, draw, aborted)
triggerClientEvent(players[i], "onClientGamemodeRoundEnd", resourceRoot, winner, draw, aborted)
end
end
-- don't cycle the map if the round was aborted (map resource was stopped)
Expand Down

0 comments on commit a01ec8a

Please sign in to comment.