Updated RecipesDB to store an array of tem_type resources instead of hardcoded slots

This commit is contained in:
akshay 2022-08-14 17:56:29 -04:00
parent a336411c84
commit 46817025a6
5 changed files with 22 additions and 23 deletions

View File

@ -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://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/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/repair_kit.tres" type="Resource" id=3]
[ext_resource path="res://item_types/raw_yam.tres" type="Resource" id=4]
[resource] [resource]
script = ExtResource( 1 ) script = ExtResource( 1 )
item_type_in_1 = ExtResource( 2 ) item_type_slots = [ ExtResource( 2 ), ExtResource( 4 ) ]
item_type_in_2 = ExtResource( 2 )
item_type_out = ExtResource( 3 ) item_type_out = ExtResource( 3 )

View File

@ -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/player.tscn" type="PackedScene" id=1]
[ext_resource path="res://scenes/moon.tscn" type="PackedScene" id=2] [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://scenes/item_holder.tscn" type="PackedScene" id=5]
[ext_resource path="res://item_types/ore.tres" type="Resource" id=6] [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://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"] [node name="dev_akshay" type="Spatial"]
@ -28,4 +29,4 @@ start_with_item = ExtResource( 6 )
[node name="item_holder2" parent="." instance=ExtResource( 5 )] [node name="item_holder2" parent="." instance=ExtResource( 5 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -1.33, 0, 1 ) 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 )

View File

@ -1,6 +1,5 @@
extends Resource extends Resource
class_name Recipe class_name Recipe
export var item_type_in_1: Resource export(Array, Resource) var item_type_slots
export var item_type_in_2: Resource
export var item_type_out: Resource export var item_type_out: Resource

View File

@ -3,28 +3,26 @@ class_name RecipeDB
export(Array, Resource) var recipes export(Array, Resource) var recipes
func find_recipe(item_type_1: Resource, item_type_2: Resource) -> Resource: func find_recipe(in_item_types) -> Resource:
assert(item_type_1 != null) assert(in_item_types.size() != 0)
assert(item_type_2 != null)
var in_items = {} var in_items = {}
if item_type_1 == item_type_2: for in_item_type in in_item_types:
in_items[item_type_1] = 2 if in_items.has(in_item_type):
else: in_items[in_item_type] += 1
in_items[item_type_1] = 1 else:
in_items[item_type_2] = 1 in_items[in_item_type] = 1
for recipe in recipes: 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 continue
var items = recipe.item_type_slots
var items = [recipe.item_type_in_1, recipe.item_type_in_2]
# build cost map # build cost map
var total_cost = {} var total_cost = {}
for n in 2: for item_type in items:
if total_cost.has(items[n]): if total_cost.has(item_type):
total_cost[items[n]] += 1 total_cost[item_type] += 1
else: else:
total_cost[items[n]] = 1 total_cost[item_type] = 1
# check if you can afford the cost # check if you can afford the cost
var cost_met : bool = true var cost_met : bool = true

View File

@ -7,7 +7,8 @@ func _ready():
assert(recipes != null) assert(recipes != null)
func craft_item(): 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: if crafted_item == null:
return return
$slot_1.destroy_item() $slot_1.destroy_item()