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

@@ -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")