Added broken state to hydroponic station and fixed a bug with all the brokens tates where they wouldn't heal the object back up on repair

This commit is contained in:
akshay 2022-08-18 20:31:07 -04:00
parent 6e6da14975
commit ef4b8bd7cf
10 changed files with 105 additions and 7 deletions

View File

@ -1,8 +1,11 @@
[gd_scene load_steps=6 format=2] [gd_scene load_steps=9 format=2]
[ext_resource path="res://scenes/item_holder.tscn" type="PackedScene" id=1] [ext_resource path="res://scenes/item_holder.tscn" type="PackedScene" id=1]
[ext_resource path="res://scripts/hydroponics_station/hydroponics_station.gd" type="Script" id=2] [ext_resource path="res://scripts/hydroponics_station/hydroponics_station.gd" type="Script" id=2]
[ext_resource path="res://scenes/coin_machine.tscn" type="PackedScene" id=3] [ext_resource path="res://scenes/coin_machine.tscn" type="PackedScene" id=3]
[ext_resource path="res://scenes/status_light.tscn" type="PackedScene" id=4]
[ext_resource path="res://scenes/item_dump.tscn" type="PackedScene" id=5]
[ext_resource path="res://item_types/repair_kit.tres" type="Resource" id=6]
[sub_resource type="CylinderMesh" id=1] [sub_resource type="CylinderMesh" id=1]
height = 0.936 height = 0.936
@ -13,6 +16,7 @@ points = PoolVector3Array( -0.0608763, 0.449247, 0.974783, -0.0608763, -0.464612
[node name="hydroponics_station" type="Spatial"] [node name="hydroponics_station" type="Spatial"]
script = ExtResource( 2 ) script = ExtResource( 2 )
start_broken = true
[node name="MeshInstance" type="MeshInstance" parent="."] [node name="MeshInstance" type="MeshInstance" parent="."]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.372095, 0 ) transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.372095, 0 )
@ -38,6 +42,16 @@ transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.991012, 0 )
[node name="grow_timer" type="Timer" parent="."] [node name="grow_timer" type="Timer" parent="."]
one_shot = true one_shot = true
[node name="status_light" parent="." instance=ExtResource( 4 )]
unique_name_in_owner = true
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 1.0767, -0.147681, 0 )
[node name="item_dump" parent="." instance=ExtResource( 5 )]
unique_name_in_owner = true
item_type_0 = ExtResource( 6 )
item_count_0 = 1
[connection signal="coin_requirement_met" from="coin_machine" to="." method="_on_coin_machine_coin_requirement_met"] [connection signal="coin_requirement_met" from="coin_machine" to="." method="_on_coin_machine_coin_requirement_met"]
[connection signal="item_changed" from="item_holder" to="." method="_on_item_holder_item_changed"] [connection signal="item_changed" from="item_holder" to="." method="_on_item_holder_item_changed"]
[connection signal="timeout" from="grow_timer" to="." method="_on_grow_timer_timeout"] [connection signal="timeout" from="grow_timer" to="." method="_on_grow_timer_timeout"]
[connection signal="item_dump_completed" from="item_dump" to="." method="_on_item_dump_item_dump_completed"]

View File

@ -37,6 +37,7 @@ func on_item_holder_item_changed(item):
pass pass
func on_item_dump_item_dump_completed(): func on_item_dump_item_dump_completed():
ctx.full_heal()
if ctx.item_holder.item_in_hold == null: if ctx.item_holder.item_in_hold == null:
ctx.change_state(ctx.States[ctx.EState.IDLE].new()) ctx.change_state(ctx.States[ctx.EState.IDLE].new())
else: else:

View File

@ -9,14 +9,14 @@ export var start_broken : bool = false
var current_hp : int = 1 setget set_hp var current_hp : int = 1 setget set_hp
func _init(): func _init():
pass
func _ready():
var starting_hp : int = max_hp var starting_hp : int = max_hp
if start_broken: if start_broken:
starting_hp = 0 starting_hp = 0
set_hp(starting_hp) set_hp(starting_hp)
func _ready():
pass
func take_damage(damage : int = 1): func take_damage(damage : int = 1):
set_hp(current_hp - max_hp) set_hp(current_hp - max_hp)
@ -26,3 +26,6 @@ func set_hp(hp_to_set : int):
remove_from_group(GROUP_STRING) remove_from_group(GROUP_STRING)
if current_hp > 0 and !is_in_group(GROUP_STRING): if current_hp > 0 and !is_in_group(GROUP_STRING):
add_to_group(GROUP_STRING) add_to_group(GROUP_STRING)
func full_heal():
set_hp(max_hp)

View File

@ -1,16 +1,20 @@
extends Spatial extends Damageable
const StateIdle = preload("res://scripts/hydroponics_station/state/idle.gd") const StateIdle = preload("res://scripts/hydroponics_station/state/idle.gd")
const StateGrowing = preload("res://scripts/hydroponics_station/state/growing.gd") const StateGrowing = preload("res://scripts/hydroponics_station/state/growing.gd")
const StateBlocked = preload("res://scripts/hydroponics_station/state/blocked.gd") const StateBlocked = preload("res://scripts/hydroponics_station/state/blocked.gd")
const StateBroken = preload("res://scripts/hydroponics_station/state/broken.gd")
export var grow_time: float = 2.0 export var grow_time: float = 2.0
var state = null var state = null
onready var status_light = $"%status_light"
onready var item_dump = $"%item_dump"
func _ready(): func _ready():
$grow_timer.wait_time = grow_time $grow_timer.wait_time = grow_time
state = StateIdle.new() state = StateBroken.new() if start_broken else StateIdle.new()
state.ctx = self state.ctx = self
state.enter_from(null) state.enter_from(null)
print("hydroponics_station: NULL -> ", state.NAME) print("hydroponics_station: NULL -> ", state.NAME)
@ -30,3 +34,10 @@ func _on_grow_timer_timeout():
func _on_item_holder_item_changed(item): func _on_item_holder_item_changed(item):
state.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)
state.on_damage_taken()

View File

@ -10,6 +10,9 @@ func _init():
func enter_from(state): func enter_from(state):
ctx.get_node("coin_machine").enabled = false ctx.get_node("coin_machine").enabled = false
ctx.get_node("item_holder").add_enabled = true ctx.get_node("item_holder").add_enabled = true
ctx.get_node("item_holder").remove_enabled = true
ctx.status_light.warn()
ctx.item_dump.enabled = false
func exit_to(state): func exit_to(state):
pass pass
@ -23,3 +26,10 @@ func on_grow_timer_timeout():
func on_item_holder_item_changed(item): func on_item_holder_item_changed(item):
if item == null: if item == null:
ctx.change_state(ctx.StateIdle.new()) ctx.change_state(ctx.StateIdle.new())
func on_item_dump_item_dump_completed():
assert(false)
func on_damage_taken():
if ctx.current_hp <= 0:
ctx.change_state(ctx.StateBroken.new())

View File

@ -0,0 +1,36 @@
extends Reference
const NAME = "broken"
var ctx = null
func _init():
pass
func enter_from(state):
ctx.get_node("coin_machine").enabled = false
ctx.get_node("item_holder").add_enabled = false
ctx.get_node("item_holder").remove_enabled = false
ctx.status_light.fail()
ctx.item_dump.enabled = true
ctx.item_dump.reset()
func exit_to(state):
pass
func on_coin_machine_coin_requirement_met(player):
assert(false)
func on_grow_timer_timeout():
assert(false)
func on_item_holder_item_changed(item):
if item == null:
ctx.change_state(ctx.StateIdle.new())
func on_item_dump_item_dump_completed():
ctx.full_heal()
ctx.change_state(ctx.StateIdle.new())
func on_damage_taken():
assert(false, "cant take damage when broken")

View File

@ -12,8 +12,11 @@ func _init():
func enter_from(state): func enter_from(state):
ctx.get_node("coin_machine").enabled = false ctx.get_node("coin_machine").enabled = false
ctx.get_node("item_holder").add_enabled = false ctx.get_node("item_holder").add_enabled = false
ctx.get_node("item_holder").remove_enabled = false
ctx.get_node("grow_timer").start() ctx.get_node("grow_timer").start()
ctx.get_node("MeshInstance").set_surface_material(0, mat) ctx.get_node("MeshInstance").set_surface_material(0, mat)
ctx.status_light.activate()
ctx.item_dump.enabled = false
func exit_to(state): func exit_to(state):
ctx.get_node("MeshInstance").set_surface_material(0, null) ctx.get_node("MeshInstance").set_surface_material(0, null)
@ -28,3 +31,11 @@ func on_grow_timer_timeout():
func on_item_holder_item_changed(item): func on_item_holder_item_changed(item):
assert(item.item_type == raw_yam) assert(item.item_type == raw_yam)
func on_item_dump_item_dump_completed():
assert(false)
func on_damage_taken():
ctx.get_node("grow_timer").stop()
if ctx.current_hp <= 0:
ctx.change_state(ctx.StateBroken.new())

View File

@ -10,6 +10,9 @@ func _init():
func enter_from(state): func enter_from(state):
ctx.get_node("coin_machine").enabled = true ctx.get_node("coin_machine").enabled = true
ctx.get_node("item_holder").add_enabled = true ctx.get_node("item_holder").add_enabled = true
ctx.get_node("item_holder").remove_enabled = true
ctx.status_light.idle()
ctx.item_dump.enabled = false
func exit_to(state): func exit_to(state):
pass pass
@ -23,3 +26,10 @@ func on_grow_timer_timeout():
func on_item_holder_item_changed(item): func on_item_holder_item_changed(item):
assert(item != null) assert(item != null)
ctx.change_state(ctx.StateBlocked.new()) ctx.change_state(ctx.StateBlocked.new())
func on_item_dump_item_dump_completed():
assert(false)
func on_damage_taken():
if ctx.current_hp <= 0:
ctx.change_state(ctx.StateBroken.new())

View File

@ -33,6 +33,7 @@ func on_item_slot_item_changed(item):
assert(false, "item dump should be disabled while broken") assert(false, "item dump should be disabled while broken")
func on_item_dump_item_dump_completed(): func on_item_dump_item_dump_completed():
ctx.full_heal()
for item_slot in ctx.item_slots: for item_slot in ctx.item_slots:
if item_slot.has_item(): if item_slot.has_item():
ctx.change_state(ctx.States[ctx.EState.BLOCKED].new("Blocked!")) ctx.change_state(ctx.States[ctx.EState.BLOCKED].new("Blocked!"))

View File

@ -30,6 +30,7 @@ func on_item_holder_item_changed(item):
assert(false, "should be impossible while broken") assert(false, "should be impossible while broken")
func on_item_dump_completed(): func on_item_dump_completed():
ctx.full_heal()
ctx.change_state(ctx.StateIdle.new()) ctx.change_state(ctx.StateIdle.new())
func take_damage(): func take_damage():