Skip to content

Commit

Permalink
Final touches
Browse files Browse the repository at this point in the history
  • Loading branch information
NancokPS2 committed Feb 11, 2024
1 parent e6f6575 commit a073a89
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 37 deletions.
11 changes: 9 additions & 2 deletions classes/Item.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extends StaticBody2D

class_name Item

signal amount_changed(this: Item, new_amount: int)
signal amount_changed(new_amount: int)

enum MODE { LOOT, ITEMSLOT }

Expand Down Expand Up @@ -30,8 +30,8 @@ var amount_max: int = 1
var amount: int = 1:
set(val):
amount = val
amount_changed.emit(amount)

amount_changed.emit(self, amount)
var price: int = 0
var value: int = 0

Expand Down Expand Up @@ -100,10 +100,17 @@ func server_use(player: Player) -> bool:
ITEM_TYPE.CONSUMABLE:
if boost.hp > 0:
player.stats.heal(self.name, boost.hp)

if amount <= 1:
player.inventory.remove_item(uuid)
else:
player.inventory.set_item_amount(uuid, amount - 1)
return true

ITEM_TYPE.EQUIPMENT:
if player.equipment and player.equipment.server_equip_item(self):
return true

else:
GodotLogger.info("%s could not equip item %s" % [player.name, item_class])
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var _target_node: Node

var _equipment_synchronizer_rpc: EquipmentSynchronizerRPC = null

var items = {
var items: Dictionary = {
"Head": null,
"Body": null,
"Legs": null,
Expand Down Expand Up @@ -144,7 +144,7 @@ func _unequip_item(item_uuid: String) -> Item:


func get_item(item_uuid: String) -> Item:
for equipment_slot in items:
for equipment_slot: String in items:
var item: Item = items[equipment_slot]
if item != null and item.uuid == item_uuid:
return item
Expand All @@ -156,7 +156,7 @@ func get_boost() -> Boost:
var boost: Boost = Boost.new()
boost.identifier = "equipment"

for equipment_slot in items:
for equipment_slot: String in items:
var item: Item = items[equipment_slot]
if item != null:
boost.combine_boost(item.boost)
Expand All @@ -167,7 +167,7 @@ func get_boost() -> Boost:
func to_json() -> Dictionary:
var output: Dictionary = {}

for slot in items:
for slot: String in items:
if items[slot] != null:
var item: Item = items[slot]
output[slot] = item.to_json()
Expand All @@ -176,7 +176,7 @@ func to_json() -> Dictionary:


func from_json(data: Dictionary) -> bool:
for slot in data:
for slot: String in data:
if not slot in items:
GodotLogger.warn("Slot=[%s] does not exist in equipment items" % slot)
return false
Expand Down
30 changes: 14 additions & 16 deletions scenes/player/inventory/Inventory.gd
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func get_panel_at_pos(pos: Vector2) -> InventoryPanel:

func get_panels(occupied_only: bool) -> Array[InventoryPanel]:
var output: Array[InventoryPanel] = []
var temp_arr: Array = []
var temp_arr: Array = []

if occupied_only:
for row: Array in panels:
for panel: InventoryPanel in row:
Expand All @@ -82,7 +82,7 @@ func get_panels(occupied_only: bool) -> Array[InventoryPanel]:
else:
for row: Array in panels:
temp_arr += row

output.assign(temp_arr)
return output

Expand All @@ -98,41 +98,39 @@ func update_inventory():
var occupied_panels: Array[InventoryPanel] = get_panels(true)
var panel_item_uuid_dictionary: Dictionary = {}
var panels_with_invalid_items: Dictionary = {}

#Ensure all panels have an item
assert(
occupied_panels.all(
func(panel_occupied: InventoryPanel):
return panel_occupied.item is Item
)
occupied_panels.all(
func(panel_occupied: InventoryPanel): return panel_occupied.item is Item
)

)

#Store the uuid of the item that each panel has to accelerate the rest of the update.
for panel: InventoryPanel in occupied_panels:
panel_item_uuid_dictionary[panel.item.uuid] = panel

#All panels are assumed invalid until proven otherwise.
panels_with_invalid_items = panel_item_uuid_dictionary.duplicate()

#Check every inventory item
for inv_item: Item in player.inventory.items:

#Unmark as invalid those who have been found in the inventory
panels_with_invalid_items.erase(inv_item.uuid)

#No item with this uuid has been found in the displayed inventory, add it.
if not inv_item.uuid in panel_item_uuid_dictionary.keys():
place_item_at_free_slot(inv_item)

else:
#Item found, update it.
panel_item_uuid_dictionary[inv_item.uuid].item = inv_item
panel_item_uuid_dictionary[inv_item.uuid].queue_redraw()

#Clear any panels with items that are NOT in the inventory
for item_uuid: String in panels_with_invalid_items:
panels_with_invalid_items[item_uuid].item = null


func place_item_at_free_slot(item: Item) -> bool:
for y in range(SIZE.y):
Expand Down
31 changes: 17 additions & 14 deletions scenes/player/inventory/InventoryPanel.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@ const DEFAULT_FONT: Font = preload("res://addons/gut/fonts/LobsterTwo-Regular.tt
@export var item: Item:
set(new_item):
if item is Item and item.amount_changed.is_connected(on_item_amount_changed):
item.amount_changed.disconnect(on_item_amount_changed)
item.amount_changed.disconnect(on_item_amount_changed)

item = new_item

if item is Item:
if not item.amount_changed.is_connected(on_item_amount_changed):
item.amount_changed.connect(on_item_amount_changed)
item.amount_changed.connect(on_item_amount_changed)
$TextureRect.texture = item.get_node("Icon").texture
else:

$TextureRect.texture = null

queue_redraw()

@onready var inventory: Inventory = $"../.."
Expand Down Expand Up @@ -52,14 +51,18 @@ func _ready():
mouse_exited.connect(_on_mouse_exited)





func _draw():
if item is Item:
if item is Item and item.amount != 1:
var font_height: int = 14
draw_string(DEFAULT_FONT, Vector2(0, size.y - font_height), str(item.amount),HORIZONTAL_ALIGNMENT_CENTER, -1, font_height)

draw_string(
DEFAULT_FONT,
Vector2(0, size.y - font_height),
str(item.amount),
HORIZONTAL_ALIGNMENT_CENTER,
-1,
font_height
)


func _gui_input(event: InputEvent):
if event.is_action_pressed("j_left_click"):
Expand Down Expand Up @@ -87,7 +90,7 @@ func _gui_input(event: InputEvent):
drag_panel.hide()


func _physics_process(_delta):
func _physics_process(_delta: float):
if selected:
drag_panel.position = get_local_mouse_position() + drag_panel_offset

Expand All @@ -100,5 +103,5 @@ func _on_mouse_exited():
inventory.mouse_above_this_panel = null


func on_item_amount_changed(item: Item, amount: int):
func on_item_amount_changed(_amount: int):
queue_redraw()

0 comments on commit a073a89

Please sign in to comment.