4
0

Compare commits

...

2 Commits

Author SHA1 Message Date
94a33e6e40 inventory actions
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2025-12-08 22:57:05 +01:00
7fa148d329 inventory 2025-12-08 22:54:19 +01:00

View File

@@ -48,6 +48,8 @@ local GAME_STATE_INTRO = 1
local GAME_STATE_MENU = 2
local GAME_STATE_GAME = 3
local GAME_STATE_DIALOG = 4
local GAME_STATE_INVENTORY = 5
local GAME_STATE_INVENTORY_ACTION = 6
--------------------------------------------------------------------------------
-- Modules
@@ -58,6 +60,7 @@ local Menu = {}
local Game = {}
local UI = {}
local Input = {}
local Inventory = {}
local NpcActions = {}
local ItemActions = {}
local MenuActions = {}
@@ -67,6 +70,7 @@ local MenuActions = {}
--------------------------------------------------------------------------------
local State = {
game_state = GAME_STATE_SPLASH,
inventory = {},
intro = {
y = Config.screen.height,
speed = 0.5,
@@ -93,6 +97,7 @@ local State = {
},
menu_items = {},
selected_menu_item = 1,
selected_inventory_item = 1,
dialog_menu_items = {},
selected_dialog_menu_item = 1,
active_entity = nil,
@@ -144,6 +149,49 @@ local State = {
}
}
--------------------------------------------------------------------------------
-- Inventory Module
--------------------------------------------------------------------------------
function Inventory.draw()
cls(Config.colors.dark_grey)
UI.draw_top_bar("Inventory")
if #State.inventory == 0 then
print("Inventory is empty.", 70, 70, Config.colors.light_grey)
else
for i, item in ipairs(State.inventory) do
local color = Config.colors.light_grey
if i == State.selected_inventory_item then
color = Config.colors.green
print(">", 60, 20 + i * 10, color)
end
print(item.name, 70, 20 + i * 10, color)
end
end
end
function Inventory.update()
State.selected_inventory_item = UI.update_menu(State.inventory, State.selected_inventory_item)
if Input.action() and #State.inventory > 0 then
local selected_item = State.inventory[State.selected_inventory_item]
State.active_entity = selected_item
State.dialog_text = selected_item.name
State.game_state = GAME_STATE_INVENTORY_ACTION
State.dialog_menu_items = {
{label = "Use", action = ItemActions.use},
{label = "Drop", action = ItemActions.drop},
{label = "Look at", action = ItemActions.look_at},
{label = "Goodbye", action = ItemActions.inventory_goodbye}
}
State.selected_dialog_menu_item = 1
end
if Input.back() then
State.game_state = GAME_STATE_GAME
end
end
--------------------------------------------------------------------------------
-- Menu Actions
--------------------------------------------------------------------------------
@@ -180,13 +228,57 @@ end
--------------------------------------------------------------------------------
-- Item Actions
--------------------------------------------------------------------------------
function ItemActions.use() end
function ItemActions.look_at() end
function ItemActions.take_away() end
function ItemActions.use()
print("Used item: " .. State.active_entity.name)
State.game_state = GAME_STATE_INVENTORY
end
function ItemActions.look_at()
print("Looked at item: " .. State.active_entity.name)
State.game_state = GAME_STATE_INVENTORY
end
function ItemActions.put_away()
-- Add item to inventory
table.insert(State.inventory, State.active_entity)
-- Remove item from screen
local currentScreenData = State.screens[State.current_screen]
for i, item in ipairs(currentScreenData.items) do
if item == State.active_entity then
table.remove(currentScreenData.items, i)
break
end
end
-- Go back to game
State.game_state = GAME_STATE_GAME
end
function ItemActions.goodbye()
State.game_state = GAME_STATE_GAME
end
function ItemActions.inventory_goodbye()
State.game_state = GAME_STATE_INVENTORY
end
function ItemActions.drop()
-- Remove item from inventory
for i, item in ipairs(State.inventory) do
if item == State.active_entity then
table.remove(State.inventory, i)
break
end
end
-- Add item to screen
local currentScreenData = State.screens[State.current_screen]
State.active_entity.x = State.player.x
State.active_entity.y = State.player.y
table.insert(currentScreenData.items, State.active_entity)
-- Go back to inventory
State.game_state = GAME_STATE_INVENTORY
end
--------------------------------------------------------------------------------
-- Input Module
@@ -412,24 +504,30 @@ function Game.update()
end
end
if interaction_found then return end
-- Item interaction
for _, item in ipairs(currentScreenData.items) do
if math.abs(State.player.x - item.x) < 8 and math.abs(State.player.y - item.y) < 8 then
State.active_entity = item
State.dialog_text = item.name
State.game_state = GAME_STATE_DIALOG
State.dialog_menu_items = {
{label = "Use", action = ItemActions.use},
{label = "Look at", action = ItemActions.look_at},
{label = "Take away", action = ItemActions.take_away},
{label = "Goodbye", action = ItemActions.goodbye}
}
State.selected_dialog_menu_item = 1
break
if not interaction_found then
-- Item interaction
for _, item in ipairs(currentScreenData.items) do
if math.abs(State.player.x - item.x) < 8 and math.abs(State.player.y - item.y) < 8 then
State.active_entity = item
State.dialog_text = item.name
State.game_state = GAME_STATE_DIALOG
State.dialog_menu_items = {
{label = "Use", action = ItemActions.use},
{label = "Look at", action = ItemActions.look_at},
{label = "Put away", action = ItemActions.put_away},
{label = "Goodbye", action = ItemActions.goodbye}
}
State.selected_dialog_menu_item = 1
interaction_found = true
break
end
end
end
-- If no interaction happened, open inventory
if not interaction_found then
State.game_state = GAME_STATE_INVENTORY
end
end
end
@@ -473,6 +571,15 @@ local STATE_HANDLERS = {
UI.draw_dialog()
Game.update_dialog()
end,
[GAME_STATE_INVENTORY] = function()
Inventory.update()
Inventory.draw()
end,
[GAME_STATE_INVENTORY_ACTION] = function()
Inventory.draw() -- Draw inventory behind dialog
UI.draw_dialog()
Game.update_dialog()
end,
}
function TIC()