Quest menu closeable

This commit is contained in:
2025-05-07 11:16:08 -05:00
parent f940247a48
commit 55081f777a
11 changed files with 168 additions and 141 deletions

View File

@@ -4,7 +4,7 @@ class_name EventShowQuest extends "res://scripts/Event/Event.gd"
@export var waitUntilClosed:bool = true
func start():
UI.showQuestsMenu(quest)
UI.QUEST_MENU.open(quest)
pass
func isDone() -> bool:
@@ -12,6 +12,6 @@ func isDone() -> bool:
return false
if waitUntilClosed:
return false
return !UI.QUEST_MENU.isOpen()
return true
return true

102
scripts/Item/Inventory.gd Normal file
View File

@@ -0,0 +1,102 @@
class_name Inventory
const Item = preload("res://scripts/Item/Item.gd");
const ItemStack = preload("res://scripts/Item/ItemStack.gd");
enum ItemSortType {
NAME,
TYPE
};
class ItemStackNameComparator:
static func _sort(a, b):
return a.item.getName().to_lower() < b.item.getName().to_lower()
class ItemStackTypeComparator:
static func _sort(a, b):
return a.item.getCategory() < b.item.getCategory()
const ITEM_STACK_SIZE_MAX = 99;
var contents:Array[ItemStack] = [];
func addItem(item: Item, quantity: int = 1) -> void:
if !item.isStackable():
# Item cannot be stacked, add each item to inv
for i in range(quantity):
contents.append(ItemStack.new(item, 1))
return
# Check for existing stacks
for stack in contents:
if stack.item != item or stack.quantity >= ITEM_STACK_SIZE_MAX:
continue
var spaceAvailable = ITEM_STACK_SIZE_MAX - stack.quantity
if quantity <= spaceAvailable:
stack.quantity += quantity;
return
stack.quantity = ITEM_STACK_SIZE_MAX;
quantity -= spaceAvailable;
# Add any remaining inventory as new stack.
while quantity > 0:
var newStackQuantity = min(quantity, ITEM_STACK_SIZE_MAX);
contents.append(ItemStack.new(item, newStackQuantity));
quantity -= newStackQuantity;
func removeItem(item: Item, quantity: int) -> void:
var totalQuantity = 0
# Calculate total quantity of the item in the inventory
for stack in contents:
if stack.item != item:
continue
totalQuantity += stack.quantity
if totalQuantity < quantity:
push_error("Not enough quantity to remove");
return
# Remove the quantity from the stacks
for stack in contents:
if stack.item != item:
continue
if stack.quantity < quantity:
quantity -= stack.quantity
contents.erase(stack)
stack.quantity -= quantity
if stack.quantity == 0:
contents.erase(stack)
if quantity == 0:
return
func removeStack(stack: ItemStack) -> void:
self.removeItem(stack.item, stack.quantity);
func hasItem(item: Item, quantity: int = 1) -> bool:
var totalQuantity = 0
for stack in contents:
if stack.item != item:
continue
totalQuantity += stack.quantity
if totalQuantity >= quantity:
return true
return false
func sortBy(by:ItemSortType) -> void:
match by:
ItemSortType.NAME:
contents.sort_custom(ItemStackNameComparator._sort)
ItemSortType.TYPE:
contents.sort_custom(ItemStackTypeComparator._sort)
_:
assert(false, "Invalid sort type: %s" % by)

View File

@@ -0,0 +1 @@
uid://dgunweso54t2t

View File

@@ -1,22 +0,0 @@
extends Node
var debugMenu:DebugMenu
func _ready() -> void:
debugMenu = $SubsceneUI/DebugMenu;
debugMenu.hide()
func _process(delta: float) -> void:
if Input.is_action_just_pressed("debug"):
print("Debug key pressed")
if debugMenu.is_visible():
hideMenu()
else:
showMenu()
func showMenu() -> void:
debugMenu.show()
print("Debug menu shown")
func hideMenu() -> void:
debugMenu.hide()

View File

@@ -1 +0,0 @@
uid://d36d3fnpi8y30

View File

@@ -1,106 +1,8 @@
extends Node
const Inventory = preload("res://scripts/Item/Inventory.gd")
const Item = preload("res://scripts/Item/Item.gd");
const ItemStack = preload("res://scripts/Item/ItemStack.gd");
# Item Constants
static var POTION = preload("res://scripts/Item/Potion.gd").new();
enum ItemSortType {
NAME,
TYPE
};
class ItemStackNameComparator:
static func _sort(a, b):
return a.item.getName().to_lower() < b.item.getName().to_lower()
class ItemStackTypeComparator:
static func _sort(a, b):
return a.item.getCategory() < b.item.getCategory()
# Constants
const ITEM_STACK_SIZE_MAX = 99;
static var ITEM_POTION = preload("res://scripts/Item/Potion.gd").new();
# Class
var inventory:Array[ItemStack] = [];
# Methods
func addItem(item: Item, quantity: int = 1) -> void:
print("Adding ", quantity, "x ", item.getName());
if !item.isStackable():
# Item cannot be stacked, add each item to inv
for i in range(quantity):
inventory.append(ItemStack.new(item, 1))
return
# Check for existing stacks
for stack in inventory:
if stack.item != item or stack.quantity >= ITEM_STACK_SIZE_MAX:
continue
var spaceAvailable = ITEM_STACK_SIZE_MAX - stack.quantity
if quantity <= spaceAvailable:
stack.quantity += quantity;
return
stack.quantity = ITEM_STACK_SIZE_MAX;
quantity -= spaceAvailable;
# Add any remaining inventory as new stack.
while quantity > 0:
var newStackQuantity = min(quantity, ITEM_STACK_SIZE_MAX);
inventory.append(ItemStack.new(item, newStackQuantity));
quantity -= newStackQuantity;
func removeItem(item: Item, quantity: int) -> void:
var totalQuantity = 0
# Calculate total quantity of the item in the inventory
for stack in inventory:
if stack.item != item:
continue
totalQuantity += stack.quantity
if totalQuantity < quantity:
push_error("Not enough quantity to remove");
return
# Remove the quantity from the stacks
for stack in inventory:
if stack.item != item:
continue
if stack.quantity < quantity:
quantity -= stack.quantity
inventory.erase(stack)
stack.quantity -= quantity
if stack.quantity == 0:
inventory.erase(stack)
if quantity == 0:
return
func removeStack(stack: ItemStack) -> void:
self.removeItem(stack.item, stack.quantity);
func hasItem(item: Item, quantity: int = 1) -> bool:
var totalQuantity = 0
for stack in inventory:
if stack.item != item:
continue
totalQuantity += stack.quantity
if totalQuantity >= quantity:
return true
return false
func sortBy(by:ItemSortType) -> void:
match by:
ItemSortType.NAME:
inventory.sort_custom(ItemStackNameComparator._sort)
ItemSortType.TYPE:
inventory.sort_custom(ItemStackTypeComparator._sort)
# Static inventories
static var PLAYER_INVENTORY = Inventory.new();

View File

@@ -1,9 +1,12 @@
class_name UISystem extends Control
var QUEST_MENU:QuestMenu
var DEBUG_MENU:DebugMenu
func _ready() -> void:
QUEST_MENU = $QuestMenu
DEBUG_MENU = $DebugMenu
func _process(delta: float) -> void:
# This needs to always be at the end of the parent node's tree
get_parent().move_child(self, get_parent().get_child_count() - 1)
func showQuestsMenu(questKey = null) -> void:
$QuestMenu.setQuest(questKey)
$QuestMenu.show()

View File

@@ -10,6 +10,7 @@ func _ready() -> void:
$Cooking.connect("pressed", _on_Cooking_pressed);
$Battle.connect("pressed", _on_Battle_pressed);
# Overworld List
var i:int = 0;
for map in OVERWORLD.MAPS.keys():
$OverworldOption/MapDropdown.add_item(map, i);
@@ -18,23 +19,27 @@ func _ready() -> void:
func _process(delta: float) -> void:
if Input.is_action_just_pressed("debug"):
print("Debug key pressed")
if is_visible():
hide()
if isOpen():
close()
else:
show()
open()
func _on_MainMenu_pressed():
SCENE_MANAGER.setScene("MainMenu");
hide()
close()
func _on_Overworld_pressed():
var keys:Array[String] = OVERWORLD.MAPS.keys()
OVERWORLD.setMap(keys[$OverworldOption/MapDropdown.selected])
SCENE_MANAGER.setScene("Overworld");
hide()
close()
func _on_Quests_pressed():
UI.showQuestsMenu()
close()
if UI.QUEST_MENU.isOpen():
UI.QUEST_MENU.close()
else:
UI.QUEST_MENU.open()
func _on_Custscene_pressed():
print("Cutscene pressed")
@@ -44,3 +49,12 @@ func _on_Cooking_pressed():
func _on_Battle_pressed():
print("Battle pressed")
func open() -> void:
show()
func close() -> void:
hide()
func isOpen() -> bool:
return visible

View File

@@ -2,6 +2,7 @@ class_name QuestMenu extends Panel
@export var questList:ItemList
@export var questName:Label
@export var closeButton:Button
func _ready() -> void:
hide()
@@ -14,6 +15,7 @@ func _ready() -> void:
# Connect signals
questList.item_selected.connect(_onQuestSelected)
closeButton.pressed.connect(_onCloseClicked)
func _onQuestSelected(index:int) -> void:
setQuest(index)
@@ -29,3 +31,16 @@ func setQuest(questKey = null):
questList.select(questKey)
questName.text = quest.questName
pass
func _onCloseClicked() -> void:
self.close()
func open(questKey = null) -> void:
setQuest(questKey)
self.show()
func close() -> void:
self.hide()
func isOpen() -> bool:
return self.visible