Updated crafting_station to have states

This commit is contained in:
akshay 2022-08-15 23:47:01 -04:00
parent a4c7dcf21a
commit 383e868b2b
10 changed files with 228 additions and 71 deletions

View File

@ -1,7 +1,7 @@
[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://scripts/crafting_station/crafting_station.gd" type="Script" id=2]
[ext_resource path="res://scenes/coin_machine.tscn" type="PackedScene" id=3]
[sub_resource type="CylinderMesh" id=1]
@ -11,17 +11,18 @@ 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"]
[node name="crafting_station" 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"]
[node name="progress_text" 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
transform = Transform( -8.74228e-08, 0, 2, 0, 2, 0, -2, 0, -8.74228e-08, -0.455111, 1.28108, 1.98935e-08 )
modulate = Color( 0, 0, 0, 1 )
text = "Progress: 20%"
[node name="StaticBody" type="StaticBody" parent="."]
@ -29,19 +30,24 @@ billboard = 1
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 )]
[node name="item_slot_1" parent="." groups=["crafting_item_slots"] 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 )]
[node name="item_slot_2" parent="." groups=["crafting_item_slots"] 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 )]
[node name="item_holder" parent="." instance=ExtResource( 1 )]
unique_name_in_owner = true
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 )]
unique_name_in_owner = true
[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"]
[node name="gen_timer" type="Timer" parent="."]
unique_name_in_owner = true
one_shot = true
[connection signal="item_changed" from="item_holder" to="." method="_on_item_holder_item_changed"]
[connection signal="coin_requirement_met" from="coin_machine" to="." method="_on_coin_machine_coin_requirement_met"]
[connection signal="timeout" from="gen_timer" to="." method="_on_gen_timer_timeout"]

View File

@ -3,7 +3,7 @@
[ext_resource path="res://scenes/player.tscn" type="PackedScene" id=1]
[ext_resource path="res://assets/moonbase_whitebox/Moonbase_Whitebox.tscn" type="PackedScene" id=2]
[ext_resource path="res://scenes/power_station.tscn" type="PackedScene" id=3]
[ext_resource path="res://scenes/crafting_machine.tscn" type="PackedScene" id=4]
[ext_resource path="res://scenes/crafting_station.tscn" type="PackedScene" id=4]
[ext_resource path="res://scenes/item_generator.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]
@ -137,6 +137,7 @@ text = "Ore Conveyor"
[node name="item_generator" parent="Ore_Conveyor_Room" instance=ExtResource( 5 )]
transform = Transform( 1, 0, 3.27826e-07, 0, 1, 0, -3.27826e-07, 0, 1, -1.69954, 0, -7.67125 )
gen_item = ExtResource( 6 )
gen_time = 2.0
[node name="Machine_Label" type="Label3D" parent="Ore_Conveyor_Room/item_generator"]
transform = Transform( 3, 0, 0, 0, 3, 0, 0, 0, 3, 1.88, 3, -1.043 )
@ -151,12 +152,13 @@ transform = Transform( 4, 0, 0, 0, 4, 0, 0, 0, 4, 0, 4.309, 0 )
modulate = Color( 0, 0, 0, 1 )
text = "Crafting"
[node name="crafting_machine" parent="Crafting_Room" instance=ExtResource( 4 )]
transform = Transform( -0.00331603, 0, 0.999994, 0, 1, 0, -0.999994, 0, -0.00331603, -2.251, 0, -7.486 )
[node name="crafting_station" parent="Crafting_Room" instance=ExtResource( 4 )]
transform = Transform( -2.98023e-08, 0, -0.999999, 0, 1, 0, 0.999999, 0, -2.98023e-08, 1.01718, 0, -7.486 )
recipes = ExtResource( 7 )
gen_time = 2.0
[node name="Machine_Label" type="Label3D" parent="Crafting_Room/crafting_machine"]
transform = Transform( -1.11759e-07, 0, -3, 0, 3, 0, 3, 0, -1.11759e-07, 1.29471, 3, 1.998 )
[node name="Machine_Label" type="Label3D" parent="Crafting_Room/crafting_station"]
transform = Transform( -1.3411e-07, 0, 3, 0, 3, 0, -3, 0, -1.3411e-07, -1.295, 3, 1.998 )
modulate = Color( 0, 0, 0, 1 )
text = "TRANSFORMATRON"

View File

@ -18,10 +18,10 @@ transform = Transform( 2, 0, 0, 0, 2, 0, 0, 0, 2, 0, 2.11515, -0.712185 )
modulate = Color( 0, 0, 0, 1 )
text = "Progress: 20%"
[node name="item_slot_1" parent="." groups=["item_slots"] instance=ExtResource( 2 )]
[node name="item_slot_1" parent="." groups=["conveyor_item_slots"] instance=ExtResource( 2 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 2.5, 0, 0 )
[node name="item_slot_2" parent="." groups=["item_slots"] instance=ExtResource( 2 )]
[node name="item_slot_2" parent="." groups=["conveyor_item_slots"] instance=ExtResource( 2 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 5, 0, 0 )
[node name="coin_machine" parent="." instance=ExtResource( 1 )]

View File

@ -3,6 +3,17 @@ class_name RecipeDB
export(Array, Resource) var recipes
func validate():
for recipe in recipes:
if recipe == null:
assert(0)
return false
for item_type in recipe.item_type_slots:
if item_type == null:
assert(0)
return false
return true
func find_recipe(in_item_types) -> Resource:
assert(in_item_types.size() != 0)
var in_items = {}
@ -13,12 +24,16 @@ func find_recipe(in_item_types) -> Resource:
in_items[in_item_type] = 1
for recipe in recipes:
assert(recipe != null)
if recipe.item_type_out == null or recipe.item_type_slots.size() == 0:
continue
var items = recipe.item_type_slots
# build cost map
var total_cost = {}
for item_type in items:
assert(item_type != null)
if item_type == null:
continue
if total_cost.has(item_type):
total_cost[item_type] += 1
else:

View File

@ -1,51 +0,0 @@
extends Spatial
export var recipes: Resource
# Called when the node enters the scene tree for the first time.
func _ready():
assert(recipes != null)
update_coin_machine_status()
func get_craft_recipe_item():
var items_in_slots = [$slot_1.item_in_hold.item_type, $slot_2.item_in_hold.item_type]
return recipes.find_recipe(items_in_slots)
func can_craft_recipe():
return get_craft_recipe_item() != null
func craft_item():
var crafted_item = get_craft_recipe_item()
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 can_craft_recipe()
func update_coin_machine_status():
$coin_machine.enabled = should_enable_coin_machine()
func _on_craft_item_holder_item_changed(item):
update_coin_machine_status()
func _on_slot_1_item_changed(item):
update_coin_machine_status()
func _on_slot_2_item_changed(item):
update_coin_machine_status()

View File

@ -0,0 +1,53 @@
extends Spatial
export var recipes: Resource
export var gen_time : float = 10.0
const StateIdle = preload("res://scripts/crafting_station/state/idle.gd")
const StateWorking = preload("res://scripts/crafting_station/state/working.gd")
const StateBlocked = preload("res://scripts/crafting_station/state/blocked.gd")
onready var item_holder = $"%item_holder"
onready var coin_machine = $"%coin_machine"
onready var gen_timer = $"%gen_timer"
onready var progress_text = $"%progress_text"
var item_slots = null
var state = null
func _ready():
assert(recipes != null)
recipes.validate()
item_slots = get_tree().get_nodes_in_group("crafting_item_slots")
assert(item_slots != null)
for item_slot in item_slots:
item_slot.connect("item_changed", self, "_on_slot_item_changed")
gen_timer.wait_time = gen_time
state = StateIdle.new()
state.ctx = self
state.enter_from(null)
print("crafting_station: NULL -> ", state.NAME)
func _process(delta):
state.update(delta)
func change_state(new_state, user_data = null):
print("crafting_station: ", state.NAME, " -> ", new_state.NAME)
new_state.ctx = self
state.exit_to(new_state)
new_state.enter_from(state, user_data)
state = new_state
func _on_coin_machine_coin_requirement_met(player):
state.on_coin_machine_coin_requirement_met(player)
func _on_gen_timer_timeout():
state.on_gen_timer_timeout()
func _on_slot_item_changed(item):
state.on_slot_item_changed(item)
func _on_item_holder_item_changed(item):
state.on_item_holder_item_changed(item)

View File

@ -0,0 +1,35 @@
extends Reference
const NAME = "blocked"
var ctx = null
func _init():
pass
func enter_from(state, block_string = null):
ctx.progress_text.text = block_string
ctx.coin_machine.enabled = false
ctx.item_holder.add_enabled = true
for item_slot in ctx.item_slots:
item_slot.add_enabled = true
item_slot.remove_enabled = true
func exit_to(state):
pass
func update(delta):
pass
func on_coin_machine_coin_requirement_met(player):
assert(false)
func on_gen_timer_timeout():
assert(false)
func on_slot_item_changed(item):
pass
func on_item_holder_item_changed(item):
if item == null:
ctx.change_state(ctx.StateIdle.new())

View File

@ -0,0 +1,52 @@
extends Reference
const NAME = "idle"
var ctx = null
var item_to_craft = null
func _init():
pass
func enter_from(state, user_data = null):
for item_slot in ctx.item_slots:
item_slot.add_enabled = true
item_slot.remove_enabled = true
ctx.item_holder.add_enabled = true
ctx.progress_text.text = "Ready!"
update_coin_machine_status()
func exit_to(state):
pass
func update(delta):
pass
func on_coin_machine_coin_requirement_met(player):
item_to_craft = get_craft_recipe_item()
assert(item_to_craft != null)
ctx.change_state(ctx.StateWorking.new(), item_to_craft)
func on_gen_timer_timeout():
assert(false, "gen timer shouldn't trigger while idle")
func on_slot_item_changed(item):
# enable coin machine if you have a valid combo of items
update_coin_machine_status()
func on_item_holder_item_changed(item):
# if target spot is occupied, go to blocked state
if item != null:
ctx.change_state(ctx.StateBlocked.new(), "Blocked!")
func get_craft_recipe_item():
var items_in_slots = []
for item_slot in ctx.item_slots:
if item_slot.has_item():
items_in_slots.append(item_slot.item_in_hold.item_type)
if items_in_slots.empty():
return null
return ctx.recipes.find_recipe(items_in_slots)
func update_coin_machine_status():
ctx.coin_machine.enabled = get_craft_recipe_item() != null

View File

@ -0,0 +1,45 @@
extends Reference
const NAME = "working"
var ctx = null
var crafting_item = null
func _init():
pass
func enter_from(state, item_to_craft = null):
crafting_item = item_to_craft
assert(crafting_item != null)
ctx.coin_machine.enabled = false
ctx.item_holder.add_enabled = false
for item_slot in ctx.item_slots:
item_slot.add_enabled = false
item_slot.remove_enabled = false
ctx.gen_timer.start()
ctx.progress_text.text = "Progress: 0%"
func exit_to(state):
pass
func update(delta):
var time_left : float = ctx.gen_timer.time_left
var time_passed : float = ctx.gen_time - time_left
var percent_complete : float = (time_passed / ctx.gen_time) * 100.0
ctx.progress_text.text = "Progress: %d%%" % percent_complete
func on_coin_machine_coin_requirement_met(player):
assert(false, "coin machine should be disabled while crafting")
func on_gen_timer_timeout():
for item_slot in ctx.item_slots:
if item_slot.has_item():
item_slot.destroy_item()
ctx.item_holder.spawn_item(crafting_item)
ctx.change_state(ctx.StateBlocked.new(), "Finished: %s" % crafting_item.name)
func on_slot_item_changed(item):
assert(item == null, "slot items can only be destroyed at this point")
func on_item_holder_item_changed(item):
assert(item.item_type == crafting_item, "only the crafted item can be added to the item holder")

View File

@ -8,7 +8,7 @@ var item_slots
func _ready():
assert(gen_item != null)
item_slots = get_tree().get_nodes_in_group("item_slots")
item_slots = get_tree().get_nodes_in_group("conveyor_item_slots")
assert(item_slots != null)
for item_slot in item_slots:
item_slot.connect("item_changed", self, "_on_item_slot_item_changed")
@ -22,7 +22,7 @@ func update_progress_viz():
var time_left : float = $"%gen_timer".time_left
var time_passed : float = gen_time - time_left
var percent_complete : float = (time_passed / gen_time) * 100.0
$"%progress_text".text = "Progress: %.2f " % percent_complete
$"%progress_text".text = "Progress: %d%%" % percent_complete
func _process(delta):
update_progress_viz()