Play sound and highlight hotbar if full

This commit is contained in:
Wuzzy 2024-08-11 11:02:53 +02:00
parent a37538db4b
commit 78b21bbb38
9 changed files with 115 additions and 19 deletions

View File

@ -125,6 +125,9 @@ All levels by Wuzzy.
- `lzr_sounds_footstep_stone.*.ogg`, `default_dirt_footstep.*.ogg`, `lzr_sounds_footstep_leaves.*.ogg`, `lzr_sounds_dug_grass.*.ogg`:
- by Wuzzy
- License: MIT
- `lzr_protection_inventory_full.ogg`:
- by Wuzzy
- License: MIT
- `lzr_treasure_chest_open.ogg`:
- by JUDITH136 <https://freesound.org/people/JUDITH136/sounds/408001/>
- License: CC BY 3.0

View File

@ -5,6 +5,17 @@ lzr_gui = {}
local hud_ids = {}
local hotbar_highlight = {}
local HOTBAR_SIZE_PLAY = 3
local HOTBAR_IMG_PLAY = "lzr_gui_hotbar_3.png"
local HOTBAR_IMG_PLAY_HIGHLIGHT = "lzr_gui_hotbar_highlight_3.png"
local HOTBAR_SIZE_EDITOR = 8
local HOTBAR_IMG_EDITOR = "lzr_gui_hotbar_8.png"
local HOTBAR_IMG_EDITOR_HIGHLIGHT = "lzr_gui_hotbar_highlight_8.png"
local HOTBAR_IMG_SELECTED = "lzr_gui_hotbar_selected.png"
local HOTBAR_IMG_SELECTED_HIGHLIGHT = "lzr_gui_hotbar_selected_highlight.png"
lzr_gui.hide_treasure_status = function(player)
local name = player:get_player_name()
if hud_ids[name].treasures_img then
@ -60,9 +71,13 @@ end
lzr_gui.set_play_gui = function(player)
player:hud_set_flags({hotbar=true})
player:hud_set_hotbar_itemcount(3)
player:hud_set_hotbar_image("lzr_gui_hotbar_3.png")
player:hud_set_hotbar_itemcount(HOTBAR_SIZE_PLAY)
local name = player:get_player_name()
if hotbar_highlight[name] then
player:hud_set_hotbar_image(HOTBAR_IMG_PLAY_HIGHLIGHT)
else
player:hud_set_hotbar_image(HOTBAR_IMG_PLAY)
end
if hud_ids[name].editor_mode then
player:hud_remove(hud_ids[name].editor_mode)
hud_ids[name].editor_mode = nil
@ -72,8 +87,13 @@ lzr_gui.set_play_gui = function(player)
end
lzr_gui.set_editor_gui = function(player)
player:hud_set_flags({hotbar=true})
player:hud_set_hotbar_itemcount(8)
player:hud_set_hotbar_image("lzr_gui_hotbar_8.png")
player:hud_set_hotbar_itemcount(HOTBAR_SIZE_EDITOR)
local name = player:get_player_name()
if hotbar_highlight[name] then
player:hud_set_hotbar_image(HOTBAR_IMG_EDITOR_HIGHLIGHT)
else
player:hud_set_hotbar_image(HOTBAR_IMG_EDITOR)
end
local id = player:hud_add({
hud_elem_type = "text",
position = { x = 0, y = 1 },
@ -213,7 +233,7 @@ lzr_gui.set_menu_gui = function(player)
player:hud_set_flags({hotbar=false})
-- Same itemcount as in editor
-- NOTE: Setting this to 1 might lead to problems!
player:hud_set_hotbar_itemcount(8)
player:hud_set_hotbar_itemcount(HOTBAR_SIZE_EDITOR)
local name = player:get_player_name()
if hud_ids[name].editor_mode then
player:hud_remove(hud_ids[name].editor_mode)
@ -224,6 +244,30 @@ lzr_gui.set_menu_gui = function(player)
lzr_gui.hide_treasure_status(player)
end
-- Set the 'highlight' state of the hotbar for player.
-- * player: Player to set hotbar state for
-- * highlight: true to activate highlight, false to disable it
lzr_gui.set_hotbar_highlight = function(player, highlight)
local name = player:get_player_name()
hotbar_highlight[name] = highlight
local state = lzr_gamestate.get_state()
if highlight then
if state == lzr_gamestate.EDITOR then
player:hud_set_hotbar_image(HOTBAR_IMG_EDITOR_HIGHLIGHT)
elseif state == lzr_gamestate.LEVEL then
player:hud_set_hotbar_image(HOTBAR_IMG_PLAY_HIGHLIGHT)
end
player:hud_set_hotbar_selected_image(HOTBAR_IMG_SELECTED_HIGHLIGHT)
else
if state == lzr_gamestate.EDITOR then
player:hud_set_hotbar_image(HOTBAR_IMG_EDITOR)
elseif state == lzr_gamestate.LEVEL then
player:hud_set_hotbar_image(HOTBAR_IMG_PLAY)
end
player:hud_set_hotbar_selected_image(HOTBAR_IMG_SELECTED)
end
end
minetest.register_on_joinplayer(function(player)
player:hud_set_flags({minimap = false, minimap_radar = false, healthbar = false, breathbar = false})
lzr_player.set_menu_inventory(player)
@ -235,10 +279,11 @@ minetest.register_on_joinplayer(function(player)
style_type[button:hovered;bgimg=lzr_gui_button_hover.png]
style_type[button:pressed;bgimg=lzr_gui_button_pressed.png]
style_type[button,image_button,item_image_button;sound=lzr_sounds_button]]=])
player:hud_set_hotbar_selected_image("lzr_gui_hotbar_selected.png")
player:hud_set_hotbar_selected_image(HOTBAR_IMG_SELECTED)
local name = player:get_player_name()
hud_ids[name] = {}
hotbar_highlight[name] = false
lzr_gui.set_menu_gui(player)
end)
@ -246,4 +291,5 @@ end)
minetest.register_on_leaveplayer(function(player)
local name = player:get_player_name()
hud_ids[name] = nil
hotbar_highlight[name] = nil
end)

View File

@ -1,2 +1,2 @@
name = lzr_gui
depends = lzr_player
depends = lzr_player, lzr_gamestate

Binary file not shown.

After

Width:  |  Height:  |  Size: 697 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 457 B

View File

@ -3,6 +3,25 @@
-- This can be bypassed with the server or protection_bypass privileges.
-- This also disallows taking blocks in the game when the inventory is full.
-- How many seconds to flash the hotbar if the inventory was full
local HOTBAR_FLASH_TIME = 0.5
local function has_room_for_node_drops(player, node)
local tool = player:get_wielded_item()
local inv = player:get_inventory()
local def = minetest.registered_nodes[node.name]
-- This assumes drops aren't random (which wouldn't
-- make sense in this game anyway)
local drops = minetest.get_node_drops(node, tool)
for d=1, #drops do
local drop = drops[d]
if not inv:room_for_item("main", drop) then
return false
end
end
return true
end
local old_is_protected = minetest.is_protected
minetest.is_protected = function(pos, name)
@ -10,18 +29,9 @@ minetest.is_protected = function(pos, name)
-- Can't dig if inventory is full! (unless in editor)
if state ~= lzr_gamestate.EDITOR then
local player = minetest.get_player_by_name(name)
local tool = player:get_wielded_item()
local inv = player:get_inventory()
local node = minetest.get_node(pos)
local def = minetest.registered_nodes[node.name]
-- This assumes drops aren't random (which wouldn't
-- make sense in this game anyway)
local drops = minetest.get_node_drops(node, tool)
for d=1, #drops do
local drop = drops[d]
if not inv:room_for_item("main", drop) then
return true
end
if not has_room_for_node_drops(player, node) then
return true
end
end
@ -35,3 +45,40 @@ minetest.is_protected = function(pos, name)
end
return old_is_protected(pos, name)
end
local flash_jobs = {}
minetest.register_on_protection_violation(function(pos, name)
local state = lzr_gamestate.get_state()
if state ~= lzr_gamestate.EDITOR then
local player = minetest.get_player_by_name(name)
local node = minetest.get_node(pos)
if not has_room_for_node_drops(player, node) then
-- If there is already a job running, cancel it
if flash_jobs[name] then
flash_jobs[name]:cancel()
flash_jobs[name] = nil
end
-- Play warning sound and highlight the hotbar
minetest.sound_play({name = "lzr_protection_inventory_full", gain = 0.3}, {to_player=name}, true)
lzr_gui.set_hotbar_highlight(player, true)
-- Disable flashing after a brief moment
local job = minetest.after(HOTBAR_FLASH_TIME, function(player)
if player and player:is_player() then
lzr_gui.set_hotbar_highlight(player, false)
end
end, player)
-- Remember this 'after' job
flash_jobs[name] = job
end
end
end)
minetest.register_on_leaveplayer(function(player)
local pname = player:get_player_name()
if flash_jobs[pname] then
flash_jobs[pname]:cancel()
flash_jobs[pname] = nil
end
end)

View File

@ -1,2 +1,2 @@
name = lzr_protection
depends = lzr_gamestate, lzr_editor
depends = lzr_gamestate, lzr_editor, lzr_gui