Doing some more quest stuff
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
class_name EventItem extends Event
|
class_name EventItem extends Event
|
||||||
const Inventory = preload("res://scripts/Item/Inventory.gd")
|
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
|
@export var quantity:int = 1
|
||||||
var inventory:Inventory = null
|
var inventory:Inventory = null
|
||||||
|
|
||||||
|
@@ -19,11 +19,15 @@ const ITEM_STACK_SIZE_MAX = 99;
|
|||||||
|
|
||||||
var contents:Array[ItemStack] = [];
|
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):
|
if !Item.isStackable(item):
|
||||||
# Item cannot be stacked, add each item to inv
|
# Item cannot be stacked, add each item to inv
|
||||||
for i in range(quantity):
|
for i in range(quantity):
|
||||||
contents.append(ItemStack.new(item, 1))
|
contents.append(ItemStack.new(item, 1))
|
||||||
|
_contentsUpdated()
|
||||||
return
|
return
|
||||||
|
|
||||||
# Check for existing stacks
|
# Check for existing stacks
|
||||||
@@ -35,6 +39,7 @@ func addItem(item:Item.ItemType, quantity: int = 1) -> void:
|
|||||||
|
|
||||||
if quantity <= spaceAvailable:
|
if quantity <= spaceAvailable:
|
||||||
stack.quantity += quantity;
|
stack.quantity += quantity;
|
||||||
|
_contentsUpdated()
|
||||||
return
|
return
|
||||||
|
|
||||||
stack.quantity = ITEM_STACK_SIZE_MAX;
|
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);
|
var newStackQuantity = min(quantity, ITEM_STACK_SIZE_MAX);
|
||||||
contents.append(ItemStack.new(item, newStackQuantity));
|
contents.append(ItemStack.new(item, newStackQuantity));
|
||||||
quantity -= newStackQuantity;
|
quantity -= newStackQuantity;
|
||||||
|
_contentsUpdated()
|
||||||
|
|
||||||
func removeItem(item:Item.ItemType, quantity:int) -> void:
|
func removeItem(item:Item.Type, quantity:int) -> void:
|
||||||
var totalQuantity = 0
|
var totalQuantity = 0
|
||||||
|
|
||||||
# Calculate total quantity of the item in the inventory
|
# Calculate total quantity of the item in the inventory
|
||||||
@@ -73,12 +79,13 @@ func removeItem(item:Item.ItemType, quantity:int) -> void:
|
|||||||
contents.erase(stack)
|
contents.erase(stack)
|
||||||
|
|
||||||
if quantity == 0:
|
if quantity == 0:
|
||||||
|
self._contentsUpdated()
|
||||||
return
|
return
|
||||||
|
|
||||||
func removeStack(stack: ItemStack) -> void:
|
func removeStack(stack: ItemStack) -> void:
|
||||||
self.removeItem(stack.item, stack.quantity);
|
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
|
var totalQuantity = 0
|
||||||
|
|
||||||
for stack in contents:
|
for stack in contents:
|
||||||
@@ -100,3 +107,7 @@ func sortBy(by:ItemSortType) -> void:
|
|||||||
contents.sort_custom(ItemStackTypeComparator._sort)
|
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
|
class_name Item
|
||||||
|
|
||||||
enum ItemType {
|
enum Type {
|
||||||
POTION,
|
POTION,
|
||||||
ONION
|
ONION
|
||||||
};
|
};
|
||||||
|
|
||||||
enum ItemCategory {
|
enum Category {
|
||||||
MEDICINE,
|
MEDICINE,
|
||||||
KEY_ITEM,
|
KEY_ITEM,
|
||||||
INGREDIENT
|
INGREDIENT
|
||||||
};
|
};
|
||||||
|
|
||||||
static func isStackable(itemType:ItemType) -> bool:
|
static func isStackable(itemType:Type) -> bool:
|
||||||
match itemType:
|
match itemType:
|
||||||
_:
|
_:
|
||||||
return true
|
return true
|
||||||
|
@@ -1,8 +1,8 @@
|
|||||||
class_name ItemStack
|
class_name ItemStack
|
||||||
|
|
||||||
var item:Item.ItemType;
|
var item:Item.Type;
|
||||||
var quantity:int;
|
var quantity:int;
|
||||||
|
|
||||||
func _init(item:Item.ItemType, quantity:int = 1):
|
func _init(item:Item.Type, quantity:int = 1):
|
||||||
self.item = item;
|
self.item = item;
|
||||||
self.quantity = quantity;
|
self.quantity = quantity;
|
||||||
|
@@ -26,3 +26,6 @@ func isCompleted() -> bool:
|
|||||||
|
|
||||||
func isStarted() -> bool:
|
func isStarted() -> bool:
|
||||||
return questStarted
|
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 objectiveName:String = "Some objective"
|
||||||
@export var objectiveType:Type = Type.Item
|
@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:
|
var completed:bool = false
|
||||||
self.quest = quest
|
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 POTION = preload("res://scripts/Item/Potion.gd").new();
|
||||||
# static var ONION = preload("res://scripts/Item/Ingredient/Onion.gd").new();
|
# static var ONION = preload("res://scripts/Item/Ingredient/Onion.gd").new();
|
||||||
|
|
||||||
enum ItemType {
|
|
||||||
POTION,
|
|
||||||
ONION
|
|
||||||
};
|
|
||||||
|
|
||||||
# Static inventories
|
# Static inventories
|
||||||
static var PLAYER_INVENTORY = Inventory.new();
|
static var PLAYER_INVENTORY = Inventory.new();
|
||||||
|
@@ -9,6 +9,7 @@ var quests:Dictionary[int, Quest]
|
|||||||
signal questStarted(quest:Quest)
|
signal questStarted(quest:Quest)
|
||||||
signal questUpdated(quest:Quest)
|
signal questUpdated(quest:Quest)
|
||||||
signal questCompleted(quest:Quest)
|
signal questCompleted(quest:Quest)
|
||||||
|
signal playerInventoryUpdated()
|
||||||
# signal questObjectiveCompleted(quest:Quest, objective:QuestObjective)
|
# signal questObjectiveCompleted(quest:Quest, objective:QuestObjective)
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
|
Reference in New Issue
Block a user