Files
mmo/systems/crafting_system.gd
Marek Le 2d4002bd3f refactor
2026-05-09 23:37:26 +02:00

50 lines
1.6 KiB
GDScript

extends Node
@onready var inventory_system: Node = get_node("../InventorySystem")
var recipes: Array = []
func _ready() -> void:
recipes = [
{"id": &"wood", "name": "Wood", "out_count": 1, "inputs": {&"essence": 1}},
{"id": &"stone", "name": "Stone", "out_count": 1, "inputs": {&"essence": 2}},
{"id": &"iron", "name": "Iron", "out_count": 1, "inputs": {&"essence": 5}},
{"id": &"sword", "name": "Sword", "out_count": 1, "inputs": {&"iron": 3, &"wood": 1}},
]
func get_recipes() -> Array:
return recipes
func can_craft(player: Node, recipe: Dictionary) -> bool:
for input_id in recipe.inputs.keys():
if inventory_system.get_amount(player, input_id) < recipe.inputs[input_id]:
return false
return true
func craft(player: Node, recipe_id: StringName) -> bool:
if not (multiplayer.is_server() or multiplayer.multiplayer_peer == null):
request_craft.rpc_id(1, player.get_path(), recipe_id)
return false
var recipe: Dictionary = _find_recipe(recipe_id)
if recipe.is_empty():
return false
if not can_craft(player, recipe):
return false
for input_id in recipe.inputs.keys():
inventory_system.remove_item(player, input_id, recipe.inputs[input_id])
inventory_system.add_item(player, recipe.id, recipe.out_count)
EventBus.item_crafted.emit(player, recipe.id)
return true
@rpc("any_peer", "reliable")
func request_craft(path: NodePath, recipe_id: StringName) -> void:
var player: Node = get_node_or_null(path)
if player:
craft(player, recipe_id)
func _find_recipe(id: StringName) -> Dictionary:
for r in recipes:
if r.id == id:
return r
return {}