Updated crafting_station to have states
This commit is contained in:
1 parent
a4c7dcf21a
commit
383e868b2b
10 files changed
+228
-71
No files matched your search
@@ -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())
|
||||
@@ -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
|
||||
@@ -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")
|
||||
Reference in new issue
Block a user