From aa3595babd35bfee44d133ee829f21c1f1997963 Mon Sep 17 00:00:00 2001 From: Nathan Lovato Date: Mon, 26 Nov 2018 21:30:53 +0900 Subject: [PATCH] Refactor the Inventory and Item classes To have add/remove functions as part of the api Changed some names for more common English words --- godot/items/Item.gd | 11 ++--- godot/party/Inventory.gd | 97 ++++++++++++++-------------------------- 2 files changed, 37 insertions(+), 71 deletions(-) diff --git a/godot/items/Item.gd b/godot/items/Item.gd index 18bb0e7..7384353 100644 --- a/godot/items/Item.gd +++ b/godot/items/Item.gd @@ -1,14 +1,9 @@ extends Resource class_name Item -enum ItemCategory { CONSUMABLE, MATERIAL, KEY, EQUIPMENT } +enum Category { CONSUMABLE, MATERIAL, KEY, EQUIPMENT } export var name : String -# menu description export var description : String -# key items are used typically as flags or one time use items in RPGs -# if an item is a key item, the inventory will detect and only allow possession -# of one of that type of item. -export var is_key : bool = false -# sell value of the item -export(int, 0, 999) var msrp +export var unique : bool = false +export(int) var sell_value diff --git a/godot/party/Inventory.gd b/godot/party/Inventory.gd index 97a5007..16d4fb8 100644 --- a/godot/party/Inventory.gd +++ b/godot/party/Inventory.gd @@ -2,82 +2,53 @@ extends Reference class_name Inventory -enum Operator { - ADD, - SET -} +export(int) var MAX_COINS = 999 +var coins : int = 0 +var content = {} -# Currency acquired for use in shopping -var balance = 500 -# Counters for items contained in the inventory -var buckets = {} +signal item_count_changed(item, amount) +signal coins_count_changed(amount) -signal item_count(item, amount) -signal balance_changed(new_amount, old_amount) +func add(item: Item, amount: int = 1) -> void: + if not content.has(item): + content[item] = 1 if item.is_unique else amount + else: + content[item] += amount -func has_item(item: Item): - return buckets.has(item) - -func adjust_held(item: Item, amount: int = 1, operator = Operator.ADD) -> bool: - match operator: - ADD: - # safely add the item into the inventory - if buckets.has(item): - # prevent taking out more than the player has - if buckets[item] + amount < 0: - return false - # ignore picking up more than one of a key item - if item.is_key and buckets[item] + amount > 1: - return false - buckets[item] += amount - else: - buckets[item] = amount - SET: - if amount < 0: - return false - buckets[item] = amount - - if buckets[item] <= 0: - buckets.erase(item) +func remove(item: Item, amount: int = 1): + assert amount <= content[item] + content[item] -= amount + if content[item] == 0: + content.erase(item) emit_signal("item_count", item, 0) else: - emit_signal("item_count", item, buckets[item]) - return true + emit_signal("item_count", item, content[item]) -func adjust_balance(amount: int, operator = Operator.ADD) -> bool: - var old = balance - - # prevent overcharging a user when shopping - match operator: - ADD: - if balance + amount < 0: - return false - balance += amount - SET: - if amount < 0: - return false - balance = amount - - emit_signal("balance_changed", balance, old) - return true +func add_coins(amount: int) -> void: + coins = min(coins + amount, MAX_COINS) + emit_signal("coins_changed", coins) + +func remove_coins(amount: int) -> void: + coins = max(0, coins - amount) + emit_signal("coins_changed", coins) func get_consumables() -> Array: - var usable = [] - for item in buckets.keys(): + var consumable: Array + for item in content.keys(): if item is ConsumableItem: - usable.append(item) - return usable + consumable.append(item) + return consumable func get_equipment() -> Array: - var equipment = [] - for item in buckets.keys(): + var equipment: Array + for item in content.keys(): if item is Equipment: equipment.append(item) return equipment -func get_key_items() -> Array: - var keys = [] - for item in buckets.keys(): +func get_unique_items() -> Array: + var unique: Array + for item in content.keys(): if item is ConsumableItem: - keys.append(item) - return keys + unique.append(item) + return unique -- GitLab