Updated crafting_station to have states

This commit is contained in:
akshay committed 2022-08-15 23:47:01 -04:00
1 parent a4c7dcf21a
commit 383e868b2b
10 files changed
+228 -71

No files matched your search

+15
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:
-51
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()
@@ -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)
+35
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())
+52
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
+45
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")
+2 -2
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()