broken_seals_2d/game/ui/loot_window/LootWindow.gd

99 lines
2.9 KiB
GDScript3
Raw Normal View History

extends Control
2020-07-08 23:25:40 +02:00
# Copyright (c) 2019-2020 Péter Magyar
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
export(PackedScene) var entry_scene : PackedScene
export(NodePath) var container_path : NodePath
var container : Node
var player : Entity
var target_bag : Bag
func _ready():
2021-02-21 17:39:36 +01:00
get_node("../../../").loot_window = self
container = get_node(container_path)
connect("visibility_changed", self, "on_visibility_changed")
if entry_scene == null:
2021-02-21 17:39:36 +01:00
Logger.error("LootWindow: entry_scene is null")
func refresh():
for child in container.get_children():
child.queue_free()
if target_bag == null:
return
for i in range(target_bag.get_size()):
var ii : ItemInstance = target_bag.get_item(i)
if ii:
var e : Node = entry_scene.instance()
container.add_child(e)
e.owner = container
e.set_item(i, ii, player)
func set_player(p_player : Entity) -> void:
player = p_player
player.connect("ctarget_bag_changed", self, "ctarget_bag_changed")
2021-02-21 17:39:36 +01:00
player.connect("onc_open_winow_request", self, "onc_open_loot_winow_request")
func on_player_moved():
if visible:
hide()
func on_visibility_changed():
if visible:
refresh()
else:
2020-07-08 23:25:40 +02:00
if target_bag != null:
target_bag.disconnect("item_removed", self, "on_item_removed")
target_bag = null
2021-02-21 17:39:36 +01:00
func on_item_removed(bag: Bag, item: ItemInstance, slot_id: int) -> void:
refresh()
func ctarget_bag_changed(entity: Entity, bag: Bag) -> void:
if target_bag != null:
target_bag.disconnect("item_removed", self, "on_item_removed")
target_bag = null
target_bag = bag
if target_bag == null:
return
target_bag.connect("item_removed", self, "on_item_removed")
2021-02-21 17:39:36 +01:00
func onc_open_loot_winow_request(window_id) -> void:
if window_id != EntityEnums.ENTITY_WINDOW_LOOT:
return
2020-07-08 23:25:40 +02:00
show()
2021-02-21 17:39:36 +01:00
if player.has_signal("player_moved") && !player.is_connected("player_moved", self, "on_player_moved"):
player.connect("player_moved", self, "on_player_moved", [], CONNECT_ONESHOT)