diff --git a/data/coin_flower.tres b/data/coin_flower.tres index 5608935..1eed4f4 100644 --- a/data/coin_flower.tres +++ b/data/coin_flower.tres @@ -8,5 +8,5 @@ script = ExtResource("3_fe16f") icon = ExtResource("1_vrj0d") name = "Coin Flower" id = &"coin_flower" -stackable = false +stackable = true stack_limit = 8 diff --git a/src/ingame/player.gd b/src/ingame/player.gd index 90dc056..3cb6842 100644 --- a/src/ingame/player.gd +++ b/src/ingame/player.gd @@ -59,13 +59,17 @@ func _physics_process(delta: float) -> void: ## Process interactivity selection. if id == multiplayer.get_unique_id(): var collider: Object = null - if empty_handed() and _line_of_sight.is_colliding(): - collider = _line_of_sight.get_collider(0) + if _line_of_sight.is_colliding(): + var possible_collider = _line_of_sight.get_collider(0) + if empty_handed(): collider = possible_collider + elif possible_collider and \ + GameState.are_bundles_stackable(held_thing, possible_collider.owner.item_bundle): + collider = possible_collider if collider != _interaction_selection: if _interaction_selection != null: _interaction_selection.get_parent().mark_non_interactive() if collider != null: - collider.get_parent().mark_interactive() + collider.owner.mark_interactive() _interaction_selection = collider # Add the gravity. @@ -114,7 +118,7 @@ func hold_thing(p_bundle: Dictionary) -> void: base_node.add_child(model) else: # Create a icon sprite based one instead. - var model := preload("res://src/quad_viewmodel.tscn").instantiate() + var model := preload("res://src/ingame/quad_viewmodel.tscn").instantiate() model.reflect_bundle(p_bundle) base_node.add_child(model) @@ -151,10 +155,15 @@ func _unhandled_input(event: InputEvent) -> void: _camera_pivot.rotation.x = clamp(_camera_pivot.rotation.x, -1.2, 1.2) return - if event.is_action_pressed("pick") and empty_handed(): + if event.is_action_pressed("pick"): if _interaction_selection != null: - _interaction_selection.owner.get_picked_up.rpc() - hold_thing.rpc(_interaction_selection.owner.item_bundle) + if empty_handed(): + _interaction_selection.owner.get_picked_up.rpc() + hold_thing.rpc(_interaction_selection.owner.item_bundle) + elif GameState.are_bundles_stackable(held_thing, _interaction_selection.owner.item_bundle): + _interaction_selection.owner.get_picked_up.rpc() + var bundle := GameState.combine_bundles(held_thing, _interaction_selection.owner.item_bundle) + hold_thing.rpc(bundle) if event.is_action_pressed("fire") and not empty_handed(): var new_projectile: Node3D = _projectile_scene.instantiate() diff --git a/src/lib/game_state.gd b/src/lib/game_state.gd index e8030b8..3a278de 100644 --- a/src/lib/game_state.gd +++ b/src/lib/game_state.gd @@ -23,3 +23,18 @@ static func save() -> void: static func fetch() -> GameState: return _instance + + +# TODO: better place for those? +static func are_bundles_stackable(p_a_bundle: Dictionary, p_b_bundle: Dictionary) -> bool: + var item = GameState.fetch().INVENTORY_ITEM_DB[p_a_bundle["item_id"]] + return p_a_bundle["item_id"] == p_b_bundle["item_id"] and \ + item.stackable and p_a_bundle["count"] + p_b_bundle["count"] <= item.stack_limit + + +static func combine_bundles(p_a_bundle: Dictionary, p_b_bundle: Dictionary) -> Dictionary: + assert(are_bundles_stackable(p_a_bundle, p_b_bundle)) + return { + "item_id": p_a_bundle["item_id"], + "count": p_a_bundle["count"] + p_b_bundle["count"], + } diff --git a/src/lib/inventory_item.gd b/src/lib/inventory_item.gd index b981271..1050727 100644 --- a/src/lib/inventory_item.gd +++ b/src/lib/inventory_item.gd @@ -1,5 +1,4 @@ -class_name InventoryItem -extends Resource +class_name InventoryItem extends Resource @export var icon: Texture2D = preload("res://icon.svg") @export var model: PackedScene diff --git a/src/quad_viewmodel.gd b/src/quad_viewmodel.gd deleted file mode 100644 index 1d74589..0000000 --- a/src/quad_viewmodel.gd +++ /dev/null @@ -1,5 +0,0 @@ -extends Node3D - -func reflect_bundle(p_bundle: Dictionary) -> void: - $Sprite3D.texture = GameState.fetch().INVENTORY_ITEM_DB[p_bundle["item_id"]].icon - $CountLabel.text = str(p_bundle["count"]) diff --git a/src/quad_viewmodel.tscn b/src/quad_viewmodel.tscn deleted file mode 100644 index 3687ebc..0000000 --- a/src/quad_viewmodel.tscn +++ /dev/null @@ -1,19 +0,0 @@ -[gd_scene load_steps=3 format=3 uid="uid://dxb5f3il2h1ur"] - -[ext_resource type="Script" path="res://src/quad_viewmodel.gd" id="1_vidqb"] -[ext_resource type="Texture2D" uid="uid://dw3x3h3f34sy3" path="res://assets/coin_flower.png" id="2_3hbqi"] - -[node name="QuadViewmodel" type="Node3D"] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.179242, 0) -script = ExtResource("1_vidqb") - -[node name="CountLabel" type="Label3D" parent="."] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.181474, 0) -billboard = 1 -text = "3" - -[node name="Sprite3D" type="Sprite3D" parent="."] -pixel_size = 0.015 -billboard = 1 -texture_filter = 0 -texture = ExtResource("2_3hbqi")