diff --git a/scripts/Event/Item/EventItem.gd b/scripts/Event/Item/EventItem.gd index 92ad396..5389201 100644 --- a/scripts/Event/Item/EventItem.gd +++ b/scripts/Event/Item/EventItem.gd @@ -1,7 +1,7 @@ class_name EventItem extends Event const Inventory = preload("res://scripts/Item/Inventory.gd") -@export var itemType:Item.ItemType = Item.ItemType.POTION +@export var itemType:Item.Type = Item.Type.POTION @export var quantity:int = 1 var inventory:Inventory = null diff --git a/scripts/Item/Inventory.gd b/scripts/Item/Inventory.gd index 811fec5..9cf8d99 100644 --- a/scripts/Item/Inventory.gd +++ b/scripts/Item/Inventory.gd @@ -19,11 +19,15 @@ const ITEM_STACK_SIZE_MAX = 99; var contents:Array[ItemStack] = []; -func addItem(item:Item.ItemType, quantity: int = 1) -> void: +func isPlayerInventory() -> bool: + return self == ITEM.PLAYER_INVENTORY + +func addItem(item:Item.Type, quantity: int = 1) -> void: if !Item.isStackable(item): # Item cannot be stacked, add each item to inv for i in range(quantity): contents.append(ItemStack.new(item, 1)) + _contentsUpdated() return # Check for existing stacks @@ -35,6 +39,7 @@ func addItem(item:Item.ItemType, quantity: int = 1) -> void: if quantity <= spaceAvailable: stack.quantity += quantity; + _contentsUpdated() return stack.quantity = ITEM_STACK_SIZE_MAX; @@ -45,8 +50,9 @@ func addItem(item:Item.ItemType, quantity: int = 1) -> void: var newStackQuantity = min(quantity, ITEM_STACK_SIZE_MAX); contents.append(ItemStack.new(item, newStackQuantity)); quantity -= newStackQuantity; + _contentsUpdated() -func removeItem(item:Item.ItemType, quantity:int) -> void: +func removeItem(item:Item.Type, quantity:int) -> void: var totalQuantity = 0 # Calculate total quantity of the item in the inventory @@ -73,12 +79,13 @@ func removeItem(item:Item.ItemType, quantity:int) -> void: contents.erase(stack) if quantity == 0: + self._contentsUpdated() return func removeStack(stack: ItemStack) -> void: self.removeItem(stack.item, stack.quantity); -func hasItem(item:Item.ItemType, quantity: int = 1) -> bool: +func hasItem(item:Item.Type, quantity: int = 1) -> bool: var totalQuantity = 0 for stack in contents: @@ -99,4 +106,8 @@ func sortBy(by:ItemSortType) -> void: ItemSortType.TYPE: contents.sort_custom(ItemStackTypeComparator._sort) _: - assert(false, "Invalid sort type: %s" % by) \ No newline at end of file + assert(false, "Invalid sort type: %s" % by) + +func _contentsUpdated() -> void: + if isPlayerInventory(): + QUEST.playerInventoryUpdated.emit() diff --git a/scripts/Item/Item.gd b/scripts/Item/Item.gd index 05ce220..65fdac6 100644 --- a/scripts/Item/Item.gd +++ b/scripts/Item/Item.gd @@ -1,17 +1,17 @@ class_name Item -enum ItemType { +enum Type { POTION, ONION }; -enum ItemCategory { +enum Category { MEDICINE, KEY_ITEM, INGREDIENT }; -static func isStackable(itemType:ItemType) -> bool: +static func isStackable(itemType:Type) -> bool: match itemType: _: return true diff --git a/scripts/Item/ItemStack.gd b/scripts/Item/ItemStack.gd index 8837123..bfad7ad 100644 --- a/scripts/Item/ItemStack.gd +++ b/scripts/Item/ItemStack.gd @@ -1,8 +1,8 @@ class_name ItemStack -var item:Item.ItemType; +var item:Item.Type; var quantity:int; -func _init(item:Item.ItemType, quantity:int = 1): +func _init(item:Item.Type, quantity:int = 1): self.item = item; self.quantity = quantity; diff --git a/scripts/Quest/Quest.gd b/scripts/Quest/Quest.gd index c58b17a..63e64f0 100644 --- a/scripts/Quest/Quest.gd +++ b/scripts/Quest/Quest.gd @@ -26,3 +26,6 @@ func isCompleted() -> bool: func isStarted() -> bool: return questStarted + +func objectiveUpdated(objective:QuestObjective) -> void: + QUEST.questUpdated.emit(self) \ No newline at end of file diff --git a/scripts/Quest/QuestObjective.gd b/scripts/Quest/QuestObjective.gd index 0efbec2..518380c 100644 --- a/scripts/Quest/QuestObjective.gd +++ b/scripts/Quest/QuestObjective.gd @@ -7,7 +7,34 @@ enum Type { @export var objectiveName:String = "Some objective" @export var objectiveType:Type = Type.Item -var quest:Quest +@export var itemType:Item.Type = Item.Type.POTION +@export var quantity:int = 1 -func onQuestReady(quest:Quest) -> void: - self.quest = quest \ No newline at end of file +var completed:bool = false +var quest:Quest = null + +func onQuestReady(_quest:Quest) -> void: + self.quest = _quest + if objectiveType == Type.Item: + QUEST.playerInventoryUpdated.connect(_onPlayerInventoryUpdated) + _onPlayerInventoryUpdated() + +func _exit_tree() -> void: + QUEST.playerInventoryUpdated.disconnect(_onPlayerInventoryUpdated) + +func _onPlayerInventoryUpdated() -> void: + if !quest.isStarted(): + return + + # Ensure player has the item + var hasItem = ITEM.PLAYER_INVENTORY.hasItem(itemType, quantity) + + if hasItem && !completed: + self.completed = true + quest.objectiveUpdated(self) + else: + self.completed = false + quest.objectiveUpdated(self) + +func isCompleted() -> bool: + return completed \ No newline at end of file diff --git a/scripts/Singleton/Item.gd b/scripts/Singleton/Item.gd index 77f7635..badc24b 100644 --- a/scripts/Singleton/Item.gd +++ b/scripts/Singleton/Item.gd @@ -5,10 +5,5 @@ const Inventory = preload("res://scripts/Item/Inventory.gd") # static var POTION = preload("res://scripts/Item/Potion.gd").new(); # static var ONION = preload("res://scripts/Item/Ingredient/Onion.gd").new(); -enum ItemType { - POTION, - ONION -}; - # Static inventories static var PLAYER_INVENTORY = Inventory.new(); diff --git a/scripts/Singleton/Quest.gd b/scripts/Singleton/Quest.gd index 5656045..84bd08f 100644 --- a/scripts/Singleton/Quest.gd +++ b/scripts/Singleton/Quest.gd @@ -9,6 +9,7 @@ var quests:Dictionary[int, Quest] signal questStarted(quest:Quest) signal questUpdated(quest:Quest) signal questCompleted(quest:Quest) +signal playerInventoryUpdated() # signal questObjectiveCompleted(quest:Quest, objective:QuestObjective) func _ready() -> void: @@ -23,4 +24,4 @@ func _updateQuests() -> void: quests[quest.questKey] = quest for quest in QuestKey: - assert(quests.has(QuestKey[quest]), "Quest with key %s does not exist" % quest) + assert(quests.has(QuestKey[quest]), "Quest with key %s does not exist" % quest) \ No newline at end of file