sm64coopdx/mods/character-select-coop/n-hud.lua

257 lines
9.3 KiB
Lua
Raw Normal View History

2024-03-09 19:23:25 +00:00
------------------------------------------------------
-- Custom HUD Rendering by Agent X and xLuigiGamerx --
------------------------------------------------------
2024-07-01 00:09:46 +00:00
if incompatibleClient then return 0 end
local defaultIcons = {
[CT_MARIO] = gTextures.mario_head,
[CT_LUIGI] = gTextures.luigi_head,
[CT_TOAD] = gTextures.toad_head,
[CT_WALUIGI] = gTextures.waluigi_head,
[CT_WARIO] = gTextures.wario_head
}
2024-07-17 19:08:38 +00:00
local sHudElements = {
[HUD_DISPLAY_FLAG_LIVES] = true,
[HUD_DISPLAY_FLAG_STAR_COUNT] = true,
[HUD_DISPLAY_FLAG_CAMERA] = true
}
---Hides the specified custom hud element
---@param hudElement HUDDisplayFlag
function hud_hide_element(hudElement)
if sHudElements[hudElement] == nil then return false end
sHudElements[hudElement] = false
return true
end
---Shows the specified custom hud element
---@param hudElement HUDDisplayFlag
function hud_show_element(hudElement)
if sHudElements[hudElement] == nil then return false end
sHudElements[hudElement] = true
return true
end
---Gets the specified custom hud element's state
---@param hudElement HUDDisplayFlag
function hud_get_element(hudElement)
if sHudElements[hudElement] == nil then return false end
return sHudElements[hudElement]
end
2024-07-01 00:09:46 +00:00
local MATH_DIVIDE_16 = 1/16
-- Localize Functions to improve performence
local texture_override_set,texture_override_reset,djui_hud_print_text,djui_hud_render_texture,tostring,hud_set_value,hud_get_value,hud_get_flash,djui_hud_get_screen_width,djui_hud_render_rect,math_ceil = texture_override_set,texture_override_reset,djui_hud_print_text,djui_hud_render_texture,tostring,hud_set_value,hud_get_value,hud_get_flash,djui_hud_get_screen_width,djui_hud_render_rect,math.ceil
2024-03-09 19:23:25 +00:00
--- @param localIndex integer
--- @return TextureInfo|nil
2024-07-01 00:09:46 +00:00
--- This assumes multiple characters will not have the same model,
--- Icons can only be seen by users who have the character avalible to them.
--- This function can return nil. if this is the case, render `djui_hud_print_text("?", x, y, 1)`
2024-03-09 19:23:25 +00:00
function life_icon_from_local_index(localIndex)
for i = 1, #characterTable do
2024-07-01 00:09:46 +00:00
if i == 1 and characterTable[i].model == gPlayerSyncTable[localIndex].modelId then
return defaultIcons[gMarioStates[localIndex].character.type]
end
2024-03-09 19:23:25 +00:00
if characterTable[i].model == gPlayerSyncTable[localIndex].modelId then
return characterTable[i].lifeIcon
end
end
end
2024-07-01 00:09:46 +00:00
--- @param localIndex integer
--- @return TextureInfo
--- This assumes multiple characters will not have the same model,
--- Icons can only be seen by users who have the character avalible to them
function star_icon_from_local_index(localIndex)
for i = 1, #characterTable do
if characterTable[i].model == gPlayerSyncTable[localIndex].modelId then
return characterTable[i].starIcon
end
end
return gTextures.star
end
local pieTextureNames = {
"one_segments",
"two_segments",
"three_segments",
"four_segments",
"five_segments",
"six_segments",
"seven_segments",
"full",
}
local function render_hud_health()
local textureTable = characterTable[currChar].healthTexture
if textureTable then -- sets health HUD to custom textures
if textureTable.label.left and textureTable.label.right then -- if left and right label textures exist. BOTH have to exist to be set!
texture_override_set("texture_power_meter_left_side", textureTable.label.left)
texture_override_set("texture_power_meter_right_side", textureTable.label.right)
end
for i = 1, 8 do
texture_override_set("texture_power_meter_" .. pieTextureNames[i], textureTable.pie[i])
end
else -- resets the health HUD
texture_override_reset("texture_power_meter_left_side")
texture_override_reset("texture_power_meter_right_side")
for i = 1, 8 do
texture_override_reset("texture_power_meter_" .. pieTextureNames[i])
end
end
end
2024-03-09 19:23:25 +00:00
local function render_hud_mario_lives()
hud_set_value(HUD_DISPLAY_FLAGS, hud_get_value(HUD_DISPLAY_FLAGS) & ~HUD_DISPLAY_FLAG_LIVES)
2024-07-17 19:08:38 +00:00
if not hud_get_element(HUD_DISPLAY_FLAG_LIVES) then return end
2024-03-09 19:23:25 +00:00
local x = 22
local y = 15 -- SCREEN_HEIGHT - 209 - 16
local lifeIcon = characterTable[currChar].lifeIcon
if lifeIcon == nil then
djui_hud_print_text("?", x, y, 1)
else
2024-07-01 00:09:46 +00:00
djui_hud_render_texture(lifeIcon, x, y, 1 / (lifeIcon.width * MATH_DIVIDE_16), 1 / (lifeIcon.height * MATH_DIVIDE_16))
2024-03-09 19:23:25 +00:00
end
djui_hud_print_text("@", x + 16, y, 1)
djui_hud_print_text(tostring(hud_get_value(HUD_DISPLAY_LIVES)):gsub("-", "M"), x + 32, y, 1)
end
local function render_hud_stars()
hud_set_value(HUD_DISPLAY_FLAGS, hud_get_value(HUD_DISPLAY_FLAGS) & ~HUD_DISPLAY_FLAG_STAR_COUNT)
2024-07-17 19:08:38 +00:00
if not hud_get_element(HUD_DISPLAY_FLAG_STAR_COUNT) then return end
if hud_get_flash ~= nil then
2024-03-09 19:23:25 +00:00
-- prevent star count from flashing outside of castle
if gNetworkPlayers[0].currCourseNum ~= COURSE_NONE then hud_set_flash(0) end
if hud_get_flash() == 1 and (get_global_timer() & 0x08) == 0 then
return
end
end
2024-07-01 00:09:46 +00:00
local x = math_ceil(djui_hud_get_screen_width() - 76)
if x % 2 ~= 0 then
x = x - 1
end
local y = math_ceil(240 - 209 - 16)
2024-03-09 19:23:25 +00:00
local starIcon = characterTable[currChar].starIcon and characterTable[currChar].starIcon or gTextures.star
local showX = 0
local hudDisplayStars = hud_get_value(HUD_DISPLAY_STARS)
if hudDisplayStars < 100 then showX = 1 end
2024-07-01 00:09:46 +00:00
djui_hud_render_texture(starIcon, x, y, 1 / (starIcon.width*MATH_DIVIDE_16), 1 / (starIcon.height*MATH_DIVIDE_16))
2024-03-09 19:23:25 +00:00
if showX == 1 then
djui_hud_print_text("@", x + 16, y, 1)
end
djui_hud_print_text(tostring(hudDisplayStars):gsub("-", "M"), (showX * 14) + x + 16, y, 1)
end
local function render_hud_camera_status()
2024-07-17 19:08:38 +00:00
if not HUD_DISPLAY_CAMERA_STATUS then return end
2024-03-09 19:23:25 +00:00
hud_set_value(HUD_DISPLAY_FLAGS, hud_get_value(HUD_DISPLAY_FLAGS) & ~HUD_DISPLAY_FLAG_CAMERA)
2024-07-17 19:08:38 +00:00
if not hud_get_element(HUD_DISPLAY_FLAG_CAMERA) then return end
2024-03-09 19:23:25 +00:00
local x = djui_hud_get_screen_width() - 54
local y = 205
local cameraHudStatus = hud_get_value(HUD_DISPLAY_CAMERA_STATUS)
if cameraHudStatus == CAM_STATUS_NONE then return end
djui_hud_render_texture(gTextures.camera, x, y, 1, 1)
switch(cameraHudStatus & CAM_STATUS_MODE_GROUP, {
[CAM_STATUS_MARIO] = function()
local lifeIcon = characterTable[currChar].lifeIcon
if lifeIcon == nil then
djui_hud_print_text("?", x + 16, y, 1)
else
2024-07-01 00:09:46 +00:00
djui_hud_render_texture(lifeIcon, x + 16, y, 1 / (lifeIcon.width * MATH_DIVIDE_16), 1 / (lifeIcon.height * MATH_DIVIDE_16))
2024-03-09 19:23:25 +00:00
end
end,
[CAM_STATUS_LAKITU] = function()
djui_hud_render_texture(gTextures.lakitu, x + 16, y, 1, 1)
end,
[CAM_STATUS_FIXED] = function()
djui_hud_render_texture(gTextures.no_camera, x + 16, y, 1, 1)
end
})
switch(cameraHudStatus & CAM_STATUS_C_MODE_GROUP, {
[CAM_STATUS_C_DOWN] = function()
djui_hud_render_texture(gTextures.arrow_down, x + 4, y + 16, 1, 1)
end,
[CAM_STATUS_C_UP] = function()
djui_hud_render_texture(gTextures.arrow_up, x + 4, y - 8, 1, 1)
end
})
end
2024-07-17 19:08:38 +00:00
-- Act Select Hud --
2024-03-09 19:23:25 +00:00
local function render_act_select_hud()
2024-07-17 19:08:38 +00:00
local course, starBhvCount, sVisibleStars -- Localizing variables
course = gNetworkPlayers[0].currCourseNum
2024-03-09 19:23:25 +00:00
if gServerSettings.enablePlayersInLevelDisplay == 0 or course == 0 or obj_get_first_with_behavior_id(id_bhvActSelector) == nil then return end
2024-07-17 19:08:38 +00:00
starBhvCount = count_objects_with_behavior(get_behavior_from_id(id_bhvActSelectorStarType))
sVisibleStars = starBhvCount < 7 and starBhvCount or 6
for a = 1, sVisibleStars do
local x = (139 - sVisibleStars * 17 + a * 34) + (djui_hud_get_screen_width() / 2) - 160 + 0.5
2024-07-01 00:09:46 +00:00
for j = 1, MAX_PLAYERS - 1 do -- 0 is not needed due to the due to the fact that you are never supposed to see yourself in the act
2024-03-09 19:23:25 +00:00
local np = gNetworkPlayers[j]
if np and np.connected and np.currCourseNum == course and np.currActNum == a then
djui_hud_render_rect(x - 4, 17, 16, 16)
local displayHead = life_icon_from_local_index(j)
if displayHead == nil then
djui_hud_print_text("?", x - 4, 17, 1)
else
2024-07-01 00:09:46 +00:00
djui_hud_render_texture(displayHead, x - 4, 17, 1 / (displayHead.width/16), 1 / (displayHead.height * MATH_DIVIDE_16))
2024-03-09 19:23:25 +00:00
end
2024-07-01 00:09:46 +00:00
break
2024-03-09 19:23:25 +00:00
end
end
end
end
local function on_hud_render_behind()
djui_hud_set_resolution(RESOLUTION_N64)
djui_hud_set_font(FONT_HUD)
djui_hud_set_color(255, 255, 255, 255)
2024-07-01 00:09:46 +00:00
if gNetworkPlayers[0].currActNum == 99 or gMarioStates[0].action == ACT_INTRO_CUTSCENE or hud_is_hidden() or obj_get_first_with_behavior_id(id_bhvActSelector) ~= nil then
2024-03-09 19:23:25 +00:00
return
end
render_hud_mario_lives()
render_hud_stars()
render_hud_camera_status()
2024-07-01 00:09:46 +00:00
render_hud_health()
end
local function on_hud_render()
djui_hud_set_resolution(RESOLUTION_N64)
djui_hud_set_font(FONT_HUD)
djui_hud_set_color(255, 255, 255, 255)
if obj_get_first_with_behavior_id(id_bhvActSelector) ~= nil then
render_act_select_hud()
end
2024-03-09 19:23:25 +00:00
end
2024-07-01 00:09:46 +00:00
hook_event(HOOK_ON_HUD_RENDER_BEHIND, on_hud_render_behind)
hook_event(HOOK_ON_HUD_RENDER, on_hud_render)