Doing some more quest stuff
This commit is contained in:
@@ -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
|
||||
|
||||
|
@@ -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)
|
||||
assert(false, "Invalid sort type: %s" % by)
|
||||
|
||||
func _contentsUpdated() -> void:
|
||||
if isPlayerInventory():
|
||||
QUEST.playerInventoryUpdated.emit()
|
||||
|
@@ -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
|
||||
|
@@ -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;
|
||||
|
@@ -26,3 +26,6 @@ func isCompleted() -> bool:
|
||||
|
||||
func isStarted() -> bool:
|
||||
return questStarted
|
||||
|
||||
func objectiveUpdated(objective:QuestObjective) -> void:
|
||||
QUEST.questUpdated.emit(self)
|
@@ -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
|
||||
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
|
@@ -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();
|
||||
|
@@ -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)
|
Reference in New Issue
Block a user