diff --git a/recipes/repair_kit_recipe.tres b/recipes/repair_kit_recipe.tres index 089e3c2..2f3be0d 100644 --- a/recipes/repair_kit_recipe.tres +++ b/recipes/repair_kit_recipe.tres @@ -2,7 +2,7 @@ [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/raw_yam.tres" type="Resource" id=3] +[ext_resource path="res://item_types/repair_kit.tres" type="Resource" id=3] [resource] script = ExtResource( 1 ) diff --git a/scenes/crafting_machine.tscn b/scenes/crafting_machine.tscn new file mode 100644 index 0000000..8097196 --- /dev/null +++ b/scenes/crafting_machine.tscn @@ -0,0 +1,47 @@ +[gd_scene load_steps=6 format=2] + +[ext_resource path="res://scenes/item_holder.tscn" type="PackedScene" id=1] +[ext_resource path="res://scripts/crafting_machine.gd" type="Script" id=2] +[ext_resource path="res://scenes/coin_machine.tscn" type="PackedScene" id=3] + +[sub_resource type="CylinderMesh" id=1] +height = 0.936 +radial_segments = 8 + +[sub_resource type="ConvexPolygonShape" id=2] +points = PoolVector3Array( -0.0608763, 0.449247, 0.974783, -0.0608763, -0.464612, -0.974783, -0.0608763, 0.449247, -0.974783, 0.974783, -0.464612, -0.0608763, -0.974783, -0.464612, 0.0608763, 0.974783, 0.449247, 0.0608763, 0.0606902, -0.464648, 0.97486, -0.974783, 0.449247, -0.0608763, 0.7071, 0.453517, -0.7071, -0.705748, -0.468, -0.705748, -0.7071, 0.453517, 0.7071, 0.7071, 0.453517, 0.7071, 0.705748, -0.468, 0.705748, -0.705748, -0.468, 0.705748, 0.705748, -0.468, -0.705748, -0.7071, 0.453517, -0.7071, 0.0606902, -0.464648, -0.97486, -0.974783, -0.464612, -0.0608763, 0.974783, -0.464612, 0.0608763, -0.0608763, -0.464612, 0.974783, 0.0606902, 0.449283, 0.97486, 0.0606902, 0.449283, -0.97486, 0.974783, 0.449247, -0.0608763, -0.974783, 0.449247, 0.0608763, -0.642208, -0.46642, 0.73398, -0.642208, -0.46642, -0.73398, 0.578116, -0.464071, -0.760529, 0.578116, -0.464071, 0.760529, 0.760589, -0.464107, 0.577971, -0.760589, -0.464107, 0.577971, -0.760589, -0.464107, -0.577971, 0.760589, -0.464107, -0.577971 ) + +[node name="crafting_machine" type="Spatial"] +script = ExtResource( 2 ) + +[node name="MeshInstance" type="MeshInstance" parent="."] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.372095, 0 ) +mesh = SubResource( 1 ) + +[node name="status_label" type="Label3D" parent="MeshInstance"] +unique_name_in_owner = true +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.737837, 0 ) +billboard = 1 + +[node name="StaticBody" type="StaticBody" parent="."] + +[node name="CollisionShape2" type="CollisionShape" parent="StaticBody"] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.372095, 0 ) +shape = SubResource( 2 ) + +[node name="slot_1" parent="." instance=ExtResource( 1 )] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 3 ) + +[node name="slot_2" parent="." instance=ExtResource( 1 )] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 6 ) + +[node name="craft_item_holder" parent="." instance=ExtResource( 1 )] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -3 ) +add_enabled = false + +[node name="coin_machine" parent="." instance=ExtResource( 3 )] + +[connection signal="item_changed" from="slot_1" to="." method="_on_slot_1_item_changed"] +[connection signal="item_changed" from="slot_2" to="." method="_on_slot_2_item_changed"] +[connection signal="item_changed" from="craft_item_holder" to="." method="_on_craft_item_holder_item_changed"] +[connection signal="coin_requirement_met" from="coin_machine" to="." method="_on_coin_machine_coin_requirement_met"] diff --git a/scenes/dev-akshay.tscn b/scenes/dev-akshay.tscn index 24fb365..9c6236c 100644 --- a/scenes/dev-akshay.tscn +++ b/scenes/dev-akshay.tscn @@ -1,11 +1,12 @@ -[gd_scene load_steps=7 format=2] +[gd_scene load_steps=8 format=2] [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/power_station.tscn" type="PackedScene" id=3] -[ext_resource path="res://item_types/raw_yam.tres" type="Resource" id=4] +[ext_resource path="res://scenes/crafting_machine.tscn" type="PackedScene" id=4] [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] [node name="dev_akshay" type="Spatial"] @@ -17,13 +18,14 @@ time_to_generate = 5.0 [node name="Player" parent="." instance=ExtResource( 1 )] -[node name="item_holder" parent="." instance=ExtResource( 5 )] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -4.04269, 0, -4.32055 ) -start_with_item = ExtResource( 4 ) +[node name="crafting_machine" parent="." instance=ExtResource( 4 )] +transform = Transform( -4.37114e-08, 0, -1, 0, 1, 0, 1, 0, -4.37114e-08, 0.974658, 0.0662553, -4.70336 ) +recipes = ExtResource( 7 ) -[node name="item_holder2" parent="." instance=ExtResource( 5 )] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 3.179, 0, -4.17727 ) +[node name="item_holder" parent="." instance=ExtResource( 5 )] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 1.334, 0, 1 ) start_with_item = ExtResource( 6 ) -[node name="item_holder3" parent="." instance=ExtResource( 5 )] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -0.0480788, 0, -7.2244 ) +[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 ) diff --git a/scripts/RecipeDB.gd b/scripts/RecipeDB.gd index d638001..429802d 100644 --- a/scripts/RecipeDB.gd +++ b/scripts/RecipeDB.gd @@ -4,7 +4,38 @@ 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) + 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 recipe in recipes: - pass - # TODO, find recipe - return recipes[0] # This is just stubbed; remove later + if recipe.item_type_in_1 == null or recipe.item_type_in_2 == null or recipe.item_type_out == null: + continue + + var items = [recipe.item_type_in_1, recipe.item_type_in_2] + # build cost map + var total_cost = {} + for n in 2: + if total_cost.has(items[n]): + total_cost[items[n]] += 1 + else: + total_cost[items[n]] = 1 + + # check if you can afford the cost + var cost_met : bool = true + for item_type in total_cost.keys(): + if not in_items.has(item_type): + cost_met = false + break + if in_items[item_type] < total_cost[item_type]: + cost_met = false + break + + if cost_met: + return recipe.item_type_out + return null diff --git a/scripts/crafting_machine.gd b/scripts/crafting_machine.gd new file mode 100644 index 0000000..de3d181 --- /dev/null +++ b/scripts/crafting_machine.gd @@ -0,0 +1,40 @@ +extends Spatial + +export var recipes: Resource + +# Called when the node enters the scene tree for the first time. +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) + if crafted_item == null: + return + $slot_1.destroy_item() + $slot_2.destroy_item() + $craft_item_holder.spawn_item(crafted_item) + +func _on_coin_machine_coin_requirement_met(player): + if not $slot_1.has_item(): + print("Slot 1 item missing") + return + if not $slot_2.has_item(): + print("Slot 2 item missing") + return + craft_item() + +func should_enable_coin_machine(): + if $craft_item_holder.has_item(): + return false + if not $slot_1.has_item() or not $slot_2.has_item(): + return false + return true + +func _on_craft_item_holder_item_changed(item): + $coin_machine.enabled = should_enable_coin_machine() + +func _on_slot_1_item_changed(item): + $coin_machine.enabled = should_enable_coin_machine() + +func _on_slot_2_item_changed(item): + $coin_machine.enabled = should_enable_coin_machine()