Added Damageable class that crating_station inherits from. Added new broken state to crafting_station with status light
This commit is contained in:
1 parent
63c0199d77
commit
7289eba670
15 files changed
+180
-72
No files matched your search
@@ -1,16 +1,25 @@
|
||||
extends Spatial
|
||||
extends Damageable
|
||||
|
||||
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")
|
||||
enum EState {IDLE, WORKING, BLOCKED, BROKEN, NUM_STATES}
|
||||
|
||||
const States = {
|
||||
EState.IDLE : preload("res://scripts/crafting_station/state/idle.gd"),
|
||||
EState.WORKING : preload("res://scripts/crafting_station/state/working.gd"),
|
||||
EState.BLOCKED : preload("res://scripts/crafting_station/state/blocked.gd"),
|
||||
EState.BROKEN : preload("res://scripts/crafting_station/state/broken.gd")
|
||||
}
|
||||
|
||||
const RepairKit : Resource = preload("res://item_types/repair_kit.tres")
|
||||
|
||||
onready var item_holder = $"%item_holder"
|
||||
onready var coin_machine = $"%coin_machine"
|
||||
onready var gen_timer = $"%gen_timer"
|
||||
onready var progress_text = $"%progress_text"
|
||||
onready var item_dump = $"%item_dump"
|
||||
onready var status_light = $"%status_light"
|
||||
|
||||
var item_slots = null
|
||||
var state = null
|
||||
@@ -25,19 +34,22 @@ func _ready():
|
||||
|
||||
gen_timer.wait_time = gen_time
|
||||
|
||||
state = StateIdle.new()
|
||||
state = States[EState.BROKEN].new() if start_broken else States[EState.IDLE].new()
|
||||
state.ctx = self
|
||||
state.enter_from(null)
|
||||
print("crafting_station: NULL -> ", state.NAME)
|
||||
|
||||
if start_broken:
|
||||
item_holder.spawn_item(RepairKit)
|
||||
|
||||
func _process(delta):
|
||||
state.update(delta)
|
||||
|
||||
func change_state(new_state, user_data = null):
|
||||
func change_state(new_state):
|
||||
print("crafting_station: ", state.NAME, " -> ", new_state.NAME)
|
||||
new_state.ctx = self
|
||||
state.exit_to(new_state)
|
||||
new_state.enter_from(state, user_data)
|
||||
new_state.enter_from(state)
|
||||
state = new_state
|
||||
|
||||
func _on_coin_machine_coin_requirement_met(player):
|
||||
@@ -51,3 +63,11 @@ func _on_slot_item_changed(item):
|
||||
|
||||
func _on_item_holder_item_changed(item):
|
||||
state.on_item_holder_item_changed(item)
|
||||
|
||||
func _on_item_dump_item_dump_completed():
|
||||
state.on_item_dump_item_dump_completed()
|
||||
|
||||
func take_damage(damage : int = 1):
|
||||
.take_damage(damage)
|
||||
if state != null:
|
||||
state.on_damage_taken()
|
||||
@@ -3,17 +3,20 @@ extends Reference
|
||||
const NAME = "blocked"
|
||||
|
||||
var ctx = null
|
||||
var label : String = "Blocked!"
|
||||
|
||||
func _init():
|
||||
pass
|
||||
func _init(block_string : String):
|
||||
label = block_string
|
||||
|
||||
func enter_from(state, block_string = null):
|
||||
ctx.progress_text.text = block_string
|
||||
func enter_from(state):
|
||||
ctx.progress_text.text = label
|
||||
ctx.coin_machine.enabled = false
|
||||
ctx.item_dump.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
|
||||
ctx.status_light.warn()
|
||||
|
||||
func exit_to(state):
|
||||
pass
|
||||
@@ -32,4 +35,11 @@ func on_slot_item_changed(item):
|
||||
|
||||
func on_item_holder_item_changed(item):
|
||||
if item == null:
|
||||
ctx.change_state(ctx.StateIdle.new())
|
||||
ctx.change_state(ctx.States[ctx.EState.IDLE].new())
|
||||
|
||||
func on_item_dump_item_dump_completed():
|
||||
pass
|
||||
|
||||
func on_damage_taken():
|
||||
if ctx.current_hp <= 0:
|
||||
ctx.change_state(ctx.States[ctx.EState.BROKEN].new())
|
||||
@@ -0,0 +1,46 @@
|
||||
extends Reference
|
||||
|
||||
const NAME = "broken"
|
||||
|
||||
var ctx = null
|
||||
|
||||
func _init():
|
||||
pass
|
||||
|
||||
func enter_from(state):
|
||||
ctx.coin_machine.enabled = false
|
||||
ctx.item_dump.enabled = true
|
||||
ctx.item_dump.reset()
|
||||
ctx.item_holder.add_enabled = true
|
||||
ctx.progress_text.text = "Broken"
|
||||
for item_slot in ctx.item_slots:
|
||||
item_slot.add_enabled = true
|
||||
item_slot.remove_enabled = true
|
||||
ctx.status_light.fail()
|
||||
|
||||
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):
|
||||
pass
|
||||
|
||||
func on_item_dump_item_dump_completed():
|
||||
if ctx.item_holder.item_in_hold == null:
|
||||
ctx.change_state(ctx.States[ctx.EState.IDLE].new())
|
||||
else:
|
||||
ctx.change_state(ctx.States[ctx.EState.BLOCKED].new("Blocked!"))
|
||||
|
||||
func on_damage_taken():
|
||||
assert(false, "cant take damage if already broken")
|
||||
@@ -8,11 +8,12 @@ var item_to_craft = null
|
||||
func _init():
|
||||
pass
|
||||
|
||||
func enter_from(state, user_data = null):
|
||||
func enter_from(state):
|
||||
for item_slot in ctx.item_slots:
|
||||
item_slot.add_enabled = true
|
||||
item_slot.remove_enabled = true
|
||||
ctx.item_holder.add_enabled = true
|
||||
ctx.item_dump.enabled = false
|
||||
ctx.progress_text.text = "Ready!"
|
||||
update_coin_machine_status()
|
||||
|
||||
@@ -25,7 +26,7 @@ func update(delta):
|
||||
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)
|
||||
ctx.change_state(ctx.States[ctx.EState.WORKING].new(item_to_craft) )
|
||||
|
||||
func on_gen_timer_timeout():
|
||||
assert(false, "gen timer shouldn't trigger while idle")
|
||||
@@ -37,8 +38,15 @@ func on_slot_item_changed(item):
|
||||
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!")
|
||||
ctx.change_state(ctx.States[ctx.EState.BLOCKED].new("Blocked!"))
|
||||
|
||||
func on_item_dump_item_dump_completed():
|
||||
pass
|
||||
|
||||
func on_damage_taken():
|
||||
if ctx.current_hp <= 0:
|
||||
ctx.change_state(ctx.States[ctx.EState.BROKEN].new())
|
||||
|
||||
func get_craft_recipe_item():
|
||||
var items_in_slots = []
|
||||
for item_slot in ctx.item_slots:
|
||||
@@ -49,4 +57,9 @@ func get_craft_recipe_item():
|
||||
return ctx.recipes.find_recipe(items_in_slots)
|
||||
|
||||
func update_coin_machine_status():
|
||||
ctx.coin_machine.enabled = get_craft_recipe_item() != null
|
||||
var can_craft_recipe : bool = get_craft_recipe_item() != null
|
||||
ctx.coin_machine.enabled = can_craft_recipe
|
||||
if can_craft_recipe:
|
||||
ctx.status_light.prime()
|
||||
else:
|
||||
ctx.status_light.idle()
|
||||
@@ -5,18 +5,19 @@ const NAME = "working"
|
||||
var ctx = null
|
||||
var crafting_item = null
|
||||
|
||||
func _init():
|
||||
pass
|
||||
|
||||
func enter_from(state, item_to_craft = null):
|
||||
func _init(item_to_craft : Resource):
|
||||
crafting_item = item_to_craft
|
||||
assert(crafting_item != null)
|
||||
|
||||
func enter_from(state):
|
||||
ctx.coin_machine.enabled = false
|
||||
ctx.item_dump.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.status_light.activate()
|
||||
ctx.progress_text.text = "Progress: 0%"
|
||||
|
||||
func exit_to(state):
|
||||
@@ -31,15 +32,22 @@ func update(delta):
|
||||
func on_coin_machine_coin_requirement_met(player):
|
||||
assert(false, "coin machine should be disabled while crafting")
|
||||
|
||||
func on_item_dump_item_dump_completed():
|
||||
pass
|
||||
|
||||
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)
|
||||
ctx.change_state(ctx.States[ctx.EState.BLOCKED].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")
|
||||
|
||||
func on_damage_taken():
|
||||
if ctx.current_hp <= 0:
|
||||
ctx.change_state(ctx.States[ctx.EState.BROKEN].new())
|
||||
@@ -0,0 +1,28 @@
|
||||
extends Spatial
|
||||
class_name Damageable
|
||||
|
||||
const GROUP_STRING : String = "damageable"
|
||||
|
||||
export var max_hp : int = 1
|
||||
export var start_broken : bool = false
|
||||
|
||||
var current_hp : int = 1 setget set_hp
|
||||
|
||||
func _init():
|
||||
var starting_hp : int = max_hp
|
||||
if start_broken:
|
||||
starting_hp = 0
|
||||
set_hp(starting_hp)
|
||||
|
||||
func _ready():
|
||||
pass
|
||||
|
||||
func take_damage(damage : int = 1):
|
||||
set_hp(current_hp - max_hp)
|
||||
|
||||
func set_hp(hp_to_set : int):
|
||||
current_hp = clamp(hp_to_set,0,max_hp)
|
||||
if current_hp == 0 and is_in_group(GROUP_STRING):
|
||||
remove_from_group(GROUP_STRING)
|
||||
if current_hp > 0 and !is_in_group(GROUP_STRING):
|
||||
add_to_group(GROUP_STRING)
|
||||
@@ -16,7 +16,6 @@ func enter_from(state):
|
||||
ctx.get_node("%item_holder").remove_enabled = false
|
||||
|
||||
ctx.get_node("cook_timer").start()
|
||||
ctx.get_node("status_light").light_color = Color.green
|
||||
ctx.get_node("status_light").activate()
|
||||
|
||||
func exit_to(state):
|
||||
|
||||
@@ -12,7 +12,7 @@ func enter_from(state):
|
||||
ctx.get_node("coin_machine").enabled = false
|
||||
ctx.get_node("%item_holder").add_enabled = true
|
||||
ctx.get_node("%item_holder").remove_enabled = false # not possible
|
||||
ctx.get_node("status_light").light_color = Color.white
|
||||
ctx.get_node("status_light").idle()
|
||||
|
||||
func exit_to(state):
|
||||
pass
|
||||
|
||||
Reference in new issue
Block a user