diff --git a/godot/addons/iterator/iterator.gd b/godot/addons/iterator/iterator.gd index bcdf8e2..f2bc24f 100644 --- a/godot/addons/iterator/iterator.gd +++ b/godot/addons/iterator/iterator.gd @@ -1,33 +1,33 @@ class_name Iterator extends RefCounted func _to_string() -> String: - return "Iterator" + return "Iterator" func clone() -> Iterator: - assert(false, "can't clone a abstract base class") - return null + assert(false, "can't clone a abstract base class") + return null func next() -> Option: - return Option.none + return Option.none func collect() -> Array: - var result = [] - for item in self: - result.append(item) - return result + var result = [] + for item in self: + result.append(item) + return result func into_peekable() -> PeekableIter: - return PeekableIter.new(self) + return PeekableIter.new(self) func into_indexed() -> IndexedIterator: - return IndexedIterator.new(self) + return IndexedIterator.new(self) func _iter_init(iter: Array) -> bool: - return _iter_next(iter) + return _iter_next(iter) func _iter_next(iter: Array) -> bool: - iter[0] = next() - return iter[0].is_some() + iter[0] = next() + return iter[0].is_some() func _iter_get(iter: Variant) -> Variant: - return iter.unwrap() + return iter.unwrap() diff --git a/godot/addons/range/integer.gd.uid b/godot/addons/range/integer.gd.uid new file mode 100644 index 0000000..0a1765d --- /dev/null +++ b/godot/addons/range/integer.gd.uid @@ -0,0 +1 @@ +uid://dtjqmx0kj66bx diff --git a/godot/addons/utils/utils.gd b/godot/addons/utils/utils.gd index 325be72..0c20827 100644 --- a/godot/addons/utils/utils.gd +++ b/godot/addons/utils/utils.gd @@ -1,49 +1,49 @@ -class_name Utils +class_name KCUtils static func remove_children(node: Node): - for i in range(node.get_child_count()): - node.remove_child(node.get_child(i)) + for i in range(node.get_child_count()): + node.remove_child(node.get_child(i)) static func get_class_name(value: Object) -> String: - match value.get_script(): - var script: - match script.get_global_name(): - var name: return name - _: return script.get_instance_base_type() - _: return value.get_class() + match value.get_script(): + var script when script != null: + match script.get_global_name(): + var name when name != null: return name + _: return script.get_instance_base_type() + _: return value.get_class() static func is_string_like(value: Variant) -> bool: - match typeof(value): - TYPE_STRING: return true - TYPE_STRING_NAME: return true - TYPE_NODE_PATH: return true - _: return false + match typeof(value): + TYPE_STRING: return true + TYPE_STRING_NAME: return true + TYPE_NODE_PATH: return true + _: return false static func to_str(value: Variant) -> String: - match typeof(value): - TYPE_OBJECT: - if value.has_method('_to_string'): - return value.to_string() - else: - var name = get_class_name(value) - value = value as Object - var props: Dictionary = inst_to_dict(value) - return "%s %s" % [name, to_str(props)] - _: return str(value) + match typeof(value): + TYPE_OBJECT: + if value.has_method('_to_string'): + return value.to_string() + else: + var name = get_class_name(value) + value = value as Object + var props: Dictionary = inst_to_dict(value) + return "%s %s" % [name, to_str(props)] + _: return str(value) static func propagate(node: Node, fn: StringName, args: Array, call_on_self: bool = true): - if call_on_self and node.has_method(fn): - node.callv(fn, args) + if call_on_self and node.has_method(fn): + node.callv(fn, args) - for child in node.get_children(): - propagate(child, fn, args) + for child in node.get_children(): + propagate(child, fn, args) static func propagate_input_event(node: Node, fn: StringName, event: InputEvent, call_on_self: bool = true): - if node.get_viewport().is_input_handled() or event.is_canceled(): - return + if node.get_viewport().is_input_handled() or event.is_canceled(): + return - if call_on_self and node.has_method(fn): - node.callv(fn, [event]) + if call_on_self and node.has_method(fn): + node.callv(fn, [event]) - for child in node.get_children(): - propagate_input_event(child, fn, event) + for child in node.get_children(): + propagate_input_event(child, fn, event) diff --git a/godot/project.godot b/godot/project.godot index edebd9c..befdda0 100644 --- a/godot/project.godot +++ b/godot/project.godot @@ -23,6 +23,10 @@ PhantomCameraManager="*res://addons/phantom_camera/scripts/managers/phantom_came enabled=PackedStringArray("res://addons/FreeControl/plugin.cfg", "res://addons/godot_object_serializer/plugin.cfg", "res://addons/phantom_camera/plugin.cfg") +[filesystem] + +import/blender/enabled=false + [global_group] persist="" diff --git a/godot/resources/player_inventory.tres b/godot/resources/player_inventory.tres index efeade5..d13c4cf 100644 --- a/godot/resources/player_inventory.tres +++ b/godot/resources/player_inventory.tres @@ -1,8 +1,17 @@ -[gd_resource type="Resource" script_class="Inventory" load_steps=2 format=3 uid="uid://bllq6ri54q3ne"] +[gd_resource type="Resource" script_class="Inventory" load_steps=5 format=3 uid="uid://bllq6ri54q3ne"] +[ext_resource type="Script" uid="uid://db4nrsnmkv3h0" path="res://src/item_instance.gd" id="1_uptie"] [ext_resource type="Script" uid="uid://dh4ytedxidq0x" path="res://src/inventory.gd" id="2_1njko"] +[ext_resource type="Resource" uid="uid://cqfnwpmo4fyv4" path="res://resources/items/key.tres" id="2_85a8j"] + +[sub_resource type="Resource" id="Resource_hfe8y"] +script = ExtResource("1_uptie") +item = ExtResource("2_85a8j") +quantity = 1 +metadata/_custom_type_script = "uid://1rjq1ttxq4nd" [resource] script = ExtResource("2_1njko") -max_capacity = null +initial_items = Array[ExtResource("1_uptie")]([SubResource("Resource_hfe8y")]) +max_capacity = 6 metadata/_custom_type_script = "uid://dh4ytedxidq0x" diff --git a/godot/scenes/inventory2.tscn b/godot/scenes/inventory2.tscn index 36fed91..85dd624 100644 --- a/godot/scenes/inventory2.tscn +++ b/godot/scenes/inventory2.tscn @@ -73,7 +73,6 @@ script = ExtResource("4_usnyx") display_range = 4 snap_behavior = 2 paging_requirement = 100 -metadata/_custom_type_script = "uid://13lxe4c4fmrp" [node name="ItemDetails" type="HBoxContainer" parent="Contents"] layout_mode = 2 diff --git a/godot/scenes/inventory_item.tscn b/godot/scenes/inventory_item.tscn index 22a88cc..eb105a3 100644 --- a/godot/scenes/inventory_item.tscn +++ b/godot/scenes/inventory_item.tscn @@ -12,13 +12,13 @@ size_flags_horizontal = 3 size_flags_vertical = 3 alignment = 1 script = ExtResource("1_letey") -name_label = NodePath("VBoxContainer/Name") +name_label = NodePath("VBoxContainer/Label") icon_texture = NodePath("VBoxContainer/Icon") [node name="VBoxContainer" type="VBoxContainer" parent="."] layout_mode = 2 -[node name="Name" type="Label" parent="VBoxContainer"] +[node name="Label" type="Label" parent="VBoxContainer"] layout_mode = 2 text = "Empty" diff --git a/godot/scenes/level.tscn b/godot/scenes/level.tscn index 8de5c1b..f8857fd 100644 --- a/godot/scenes/level.tscn +++ b/godot/scenes/level.tscn @@ -2,11 +2,11 @@ [ext_resource type="PackedScene" uid="uid://crbrniwi6kd3p" path="res://scenes/player.tscn" id="1_2q6dc"] [ext_resource type="Script" uid="uid://csjccrhj5wnx7" path="res://addons/phantom_camera/scripts/phantom_camera/phantom_camera_3d.gd" id="2_klq6b"] -[ext_resource type="Script" uid="uid://ds8lef4lc6xuj" path="res://src/door.gd" id="2_w8frs"] +[ext_resource type="Script" uid="uid://r8orotw16wn8" path="res://src/door.gd" id="2_w8frs"] [ext_resource type="Script" uid="uid://8umksf8e80fw" path="res://addons/phantom_camera/scripts/resources/tween_resource.gd" id="3_b121j"] -[ext_resource type="Script" uid="uid://dyghf5fq7s72x" path="res://src/interactable.gd" id="3_w8frs"] +[ext_resource type="Script" uid="uid://b5eitlv3jchk" path="res://src/interactable.gd" id="3_w8frs"] [ext_resource type="Script" uid="uid://bd046eokvcnu2" path="res://addons/phantom_camera/scripts/phantom_camera_host/phantom_camera_host.gd" id="4_8c41q"] -[ext_resource type="Script" uid="uid://pdpejp2xor23" path="res://src/persistence.gd" id="4_mx8sn"] +[ext_resource type="Script" uid="uid://cdvtrsqkkxtdd" path="res://src/persistence.gd" id="4_mx8sn"] [ext_resource type="PackedScene" uid="uid://bshvduqysqivm" path="res://scenes/inventory.tscn" id="8_b121j"] [sub_resource type="PlaneMesh" id="PlaneMesh_rd3vj"] @@ -50,7 +50,6 @@ dead_zone_width = 0.5 dead_zone_height = 0.5 show_viewfinder_in_play = true spring_length = 10.0 -metadata/_custom_type_script = "uid://csjccrhj5wnx7" [node name="Camera3D" type="Camera3D" parent="."] physics_interpolation_mode = 1 @@ -62,7 +61,6 @@ size = 15.0 process_priority = 300 process_physics_priority = 300 script = ExtResource("4_8c41q") -metadata/_custom_type_script = "uid://bd046eokvcnu2" [node name="DirectionalLight3D" type="DirectionalLight3D" parent="."] transform = Transform3D(1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 10, 0) @@ -94,7 +92,6 @@ offset_bottom = 40.0 [node name="Persistence" type="Node" parent="CanvasLayer/Control"] script = ExtResource("4_mx8sn") -metadata/_custom_type_script = "uid://pdpejp2xor23" [node name="HBoxContainer" type="HBoxContainer" parent="CanvasLayer/Control"] layout_mode = 0 diff --git a/godot/src/inventory.gd b/godot/src/inventory.gd index 8b78816..37fadfd 100644 --- a/godot/src/inventory.gd +++ b/godot/src/inventory.gd @@ -1,63 +1,71 @@ class_name Inventory extends Resource -var initial_items: Array[ItemInstance] -var items: Dictionary[RID, ItemInstance] +class InventoryIterator extends Iterator: + var values: Array[ItemInstance] + var size: int + var index: int = 0 + + @warning_ignore("shadowed_variable") + func _init(values: Array[ItemInstance], size: int = values.size()) -> void: + self.values = values + self.size = size + + func next() -> Option: + if index < size: + var value = Option.some(values[index]) + index += 1 + return value + else: + return Option.none + +@export var initial_items: Array[ItemInstance] @export var max_capacity: int = 6 +var items: Dictionary[int, ItemInstance] var size: int: - get: return items.size() + get: return items.size() signal item_added(item: Item, quantity: int) signal item_removed(item: Item, remaining: int) signal updated func _init(): - call_deferred("_ready") + call_deferred("_ready") func _ready(): - print(initial_items.size()) - for item in initial_items: - items[item.get_rid()] = item - print('setting', item) + for item in initial_items: + items[_get_id(item.item)] = item + updated.emit() + +func _get_id(item: Item) -> int: + print(item.get_instance_id()) + return item.get_instance_id() func _item_eq(a: Item, b: ItemInstance) -> bool: - return a.item == b + return a.item == b func has_item(item: Item) -> bool: - return items.has(item.get_rid()) + return items.has(_get_id(item)) func find(item: Item) -> Option: - match items.get(item.get_rid()): - null: return Option.none - var found: return Option.some(found) + match items.get(_get_id(item)): + null: return Option.none + var found: return Option.some(found) func add_item(item: Item, quantity: int = 1): - var rid = item.get_rid() - var inst = items.get_or_add(rid) - inst.quantity += quantity - item_added.emit(item, inst.quantity) - updated.emit() + var id = _get_id(item) + var inst = items.get_or_add(id) + inst.quantity += quantity + item_added.emit(item, inst.quantity) + updated.emit() func remove_item(item: Item, quantity: int = 1): - if find(item): - item.quantity -= quantity - if item.quantity <= 0: - items.erase(item.get_rid()) - item_removed.emit(item, item.quantity) - updated.emit() + if find(item): + item.quantity -= quantity + if item.quantity <= 0: + items.erase(_get_id(item)) + item_removed.emit(item, item.quantity) + updated.emit() -func _iter_continue(iter: Array) -> bool: - return iter[0].size() - -func _iter_init(iter: Array) -> bool: - iter[0] = items.keys() - return _iter_continue(iter) - -func _iter_next(iter: Array) -> bool: - iter[0] += 1 - return _iter_continue(iter) - -func _iter_get(iter: Variant) -> ItemInstance: - var rid = iter[0] - iter.remove_at(0) - return items.get(rid) +func iter() -> InventoryIterator: + return InventoryIterator.new(items.values(), max_capacity) diff --git a/godot/src/inventory_ui.gd b/godot/src/inventory_ui.gd index 115bd15..9e7bea5 100644 --- a/godot/src/inventory_ui.gd +++ b/godot/src/inventory_ui.gd @@ -5,37 +5,40 @@ class_name InventoryUI extends Control @export var carousel: Carousel func _ready() -> void: - _build_carousel() - inventory.updated.connect(_build_carousel) + inventory.updated.connect(_build_carousel) func _build_carousel(): - Utils.remove_children(carousel) + KCUtils.remove_children(carousel) - for instance in inventory: - print('pussy', instance) - var scene = create_item() - bind_item(scene, Option.from(instance.item)) - carousel.add_child(scene) + var items = inventory.items.values() + var count = items.size() + for i in range(inventory.max_capacity): + var scene = create_item() + var value = Option.none + if i < count: + value = Option.some(items[i].item) + bind_item(scene, value) + carousel.add_child(scene) func create_item() -> Node: - return item_scene.instantiate() + return item_scene.instantiate() func bind_item(node: Node, item: Option): - if node.has_method('bind'): - node.bind(item) + if node.has_method('bind'): + node.bind(item) func _gui_input(event: InputEvent) -> void: - if event.is_action_pressed(PlayerInput.UIAction.Right): - move_right() - elif event.is_action_pressed(PlayerInput.UIAction.Left): - move_left() + if event.is_action_pressed(PlayerInput.UIAction.Right): + move_right() + elif event.is_action_pressed(PlayerInput.UIAction.Left): + move_left() func move_by(delta: int): - var next_index = carousel.get_carousel_index() + delta - carousel.go_to_index(next_index) + var next_index = carousel.get_carousel_index() + delta + carousel.go_to_index(next_index) func move_right(): - move_by(1) + move_by(1) func move_left(): - move_by(-1) + move_by(-1) diff --git a/godot/src/item_ui.gd b/godot/src/item_ui.gd index 5f803d3..9a203f2 100644 --- a/godot/src/item_ui.gd +++ b/godot/src/item_ui.gd @@ -6,19 +6,18 @@ class_name ItemUI extends Control var _default_text: String var _default_icon: Texture2D -func _init() -> void: - _default_text = name_label.text - _default_icon = icon_texture.texture +func _ready() -> void: + _default_text = name_label.text + _default_icon = icon_texture.texture func bind(_item: Option): - if _item.is_none(): - unbind() - else: - var item = _item.unwrap() - name_label.text = item.name - icon_texture.texture = item.icon + if _item.is_none(): + unbind() + else: + var item = _item.unwrap() + name_label.text = item.name + icon_texture.texture = item.icon func unbind(): - print(_default_text) - name_label.text = _default_text - icon_texture.texture = _default_icon + name_label.text = _default_text + icon_texture.texture = _default_icon diff --git a/godot/src/root_control.gd b/godot/src/root_control.gd index bf7c0d7..be0cd41 100644 --- a/godot/src/root_control.gd +++ b/godot/src/root_control.gd @@ -20,4 +20,4 @@ func close(): func _gui_input(event: InputEvent) -> void: super(event) - Utils.propagate_input_event(self, "_gui_input", event, false) + KCUtils.propagate_input_event(self, "_gui_input", event, false)