Updated RecipesDB to store an array of tem_type resources instead of hardcoded slots
This commit is contained in:
parent
a336411c84
commit
46817025a6
@ -1,11 +1,11 @@
|
||||
[gd_resource type="Resource" load_steps=4 format=2]
|
||||
[gd_resource type="Resource" load_steps=5 format=2]
|
||||
|
||||
[ext_resource path="res://scripts/Recipe.gd" type="Script" id=1]
|
||||
[ext_resource path="res://item_types/ore.tres" type="Resource" id=2]
|
||||
[ext_resource path="res://item_types/repair_kit.tres" type="Resource" id=3]
|
||||
[ext_resource path="res://item_types/raw_yam.tres" type="Resource" id=4]
|
||||
|
||||
[resource]
|
||||
script = ExtResource( 1 )
|
||||
item_type_in_1 = ExtResource( 2 )
|
||||
item_type_in_2 = ExtResource( 2 )
|
||||
item_type_slots = [ ExtResource( 2 ), ExtResource( 4 ) ]
|
||||
item_type_out = ExtResource( 3 )
|
||||
|
@ -1,4 +1,4 @@
|
||||
[gd_scene load_steps=8 format=2]
|
||||
[gd_scene load_steps=9 format=2]
|
||||
|
||||
[ext_resource path="res://scenes/player.tscn" type="PackedScene" id=1]
|
||||
[ext_resource path="res://scenes/moon.tscn" type="PackedScene" id=2]
|
||||
@ -7,6 +7,7 @@
|
||||
[ext_resource path="res://scenes/item_holder.tscn" type="PackedScene" id=5]
|
||||
[ext_resource path="res://item_types/ore.tres" type="Resource" id=6]
|
||||
[ext_resource path="res://recipes/all_recipes.tres" type="Resource" id=7]
|
||||
[ext_resource path="res://item_types/raw_yam.tres" type="Resource" id=8]
|
||||
|
||||
[node name="dev_akshay" type="Spatial"]
|
||||
|
||||
@ -28,4 +29,4 @@ start_with_item = ExtResource( 6 )
|
||||
|
||||
[node name="item_holder2" parent="." instance=ExtResource( 5 )]
|
||||
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -1.33, 0, 1 )
|
||||
start_with_item = ExtResource( 6 )
|
||||
start_with_item = ExtResource( 8 )
|
||||
|
@ -1,6 +1,5 @@
|
||||
extends Resource
|
||||
class_name Recipe
|
||||
|
||||
export var item_type_in_1: Resource
|
||||
export var item_type_in_2: Resource
|
||||
export(Array, Resource) var item_type_slots
|
||||
export var item_type_out: Resource
|
||||
|
@ -3,28 +3,26 @@ class_name RecipeDB
|
||||
|
||||
export(Array, Resource) var recipes
|
||||
|
||||
func find_recipe(item_type_1: Resource, item_type_2: Resource) -> Resource:
|
||||
assert(item_type_1 != null)
|
||||
assert(item_type_2 != null)
|
||||
func find_recipe(in_item_types) -> Resource:
|
||||
assert(in_item_types.size() != 0)
|
||||
var in_items = {}
|
||||
if item_type_1 == item_type_2:
|
||||
in_items[item_type_1] = 2
|
||||
else:
|
||||
in_items[item_type_1] = 1
|
||||
in_items[item_type_2] = 1
|
||||
for in_item_type in in_item_types:
|
||||
if in_items.has(in_item_type):
|
||||
in_items[in_item_type] += 1
|
||||
else:
|
||||
in_items[in_item_type] = 1
|
||||
|
||||
for recipe in recipes:
|
||||
if recipe.item_type_in_1 == null or recipe.item_type_in_2 == null or recipe.item_type_out == null:
|
||||
if recipe.item_type_out == null or recipe.item_type_slots.size() == 0:
|
||||
continue
|
||||
|
||||
var items = [recipe.item_type_in_1, recipe.item_type_in_2]
|
||||
var items = recipe.item_type_slots
|
||||
# build cost map
|
||||
var total_cost = {}
|
||||
for n in 2:
|
||||
if total_cost.has(items[n]):
|
||||
total_cost[items[n]] += 1
|
||||
for item_type in items:
|
||||
if total_cost.has(item_type):
|
||||
total_cost[item_type] += 1
|
||||
else:
|
||||
total_cost[items[n]] = 1
|
||||
total_cost[item_type] = 1
|
||||
|
||||
# check if you can afford the cost
|
||||
var cost_met : bool = true
|
||||
|
@ -7,7 +7,8 @@ func _ready():
|
||||
assert(recipes != null)
|
||||
|
||||
func craft_item():
|
||||
var crafted_item = recipes.find_recipe($slot_1.item_in_hold.item_type, $slot_2.item_in_hold.item_type)
|
||||
var items_in_slots = [$slot_1.item_in_hold.item_type, $slot_2.item_in_hold.item_type]
|
||||
var crafted_item = recipes.find_recipe(items_in_slots)
|
||||
if crafted_item == null:
|
||||
return
|
||||
$slot_1.destroy_item()
|
||||
|
Loading…
x
Reference in New Issue
Block a user