From 921ebc01f2760a41b1b037ded34436fabe750fed Mon Sep 17 00:00:00 2001 From: Daniel Snider Date: Tue, 16 Aug 2022 00:30:37 -0700 Subject: [PATCH] Make broken state for oven --- scenes/oven.tscn | 28 ++++++++++++++++++++--- scripts/oven/oven.gd | 14 ++++++++++-- scripts/oven/state/broken.gd | 36 ++++++++++++++++++++++++++++++ scripts/oven/state/cooking.gd | 13 +++++++++++ scripts/oven/state/holding_item.gd | 13 +++++++++++ scripts/oven/state/idle.gd | 10 +++++++++ 6 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 scripts/oven/state/broken.gd diff --git a/scenes/oven.tscn b/scenes/oven.tscn index 079f05c..e00f014 100644 --- a/scenes/oven.tscn +++ b/scenes/oven.tscn @@ -1,8 +1,11 @@ -[gd_scene load_steps=7 format=2] +[gd_scene load_steps=11 format=2] [ext_resource path="res://scenes/item_holder.tscn" type="PackedScene" id=1] [ext_resource path="res://scripts/oven/oven.gd" type="Script" id=2] [ext_resource path="res://scenes/coin_machine.tscn" type="PackedScene" id=3] +[ext_resource path="res://scenes/item_dump.tscn" type="PackedScene" id=4] +[ext_resource path="res://scenes/status_light.tscn" type="PackedScene" id=5] +[ext_resource path="res://item_types/repair_kit.tres" type="Resource" id=6] [sub_resource type="SpatialMaterial" id=1] albedo_color = Color( 0, 0, 0, 1 ) @@ -14,14 +17,18 @@ bottom_radius = 0.5 height = 0.1 radial_segments = 9 +[sub_resource type="BoxShape" id=4] +extents = Vector3( 0.648623, 1, 0.516904 ) + [sub_resource type="CubeMesh" id=3] size = Vector3( 1.362, 0.49, 0.081 ) -[node name="oven" type="Spatial"] +[node name="oven" type="Spatial" groups=["damageable"]] transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -0.0063355, 0.00319374, -0.00456095 ) script = ExtResource( 2 ) [node name="CSGBox" type="CSGBox" parent="."] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.497031, 0 ) use_collision = true width = 1.36825 height = 1.04121 @@ -42,7 +49,10 @@ skeleton = NodePath("../..") [node name="item_holder" parent="CSGBox/coil" instance=ExtResource( 1 )] unique_name_in_owner = true -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.00424552, 0.29638, 0.00761187 ) +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.00424552, 0.0865635, 0.00761187 ) + +[node name="CollisionShape" type="CollisionShape" parent="CSGBox/coil/item_holder"] +shape = SubResource( 4 ) [node name="backsplash" type="MeshInstance" parent="CSGBox"] transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -0.00424552, 0.556925, -0.575066 ) @@ -50,10 +60,22 @@ mesh = SubResource( 3 ) skeleton = NodePath("../..") [node name="coin_machine" parent="." instance=ExtResource( 3 )] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.621351, 0 ) + +[node name="item_dump" parent="." instance=ExtResource( 4 )] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.733121, 0 ) +collision_layer = 8 +collision_mask = 0 +item_type_0 = ExtResource( 6 ) +item_count_0 = 1 + +[node name="status_light" parent="." instance=ExtResource( 5 )] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.232042, 0.967385 ) [node name="cook_timer" type="Timer" parent="."] one_shot = true [connection signal="item_changed" from="CSGBox/coil/item_holder" to="." method="_on_item_holder_item_changed"] [connection signal="coin_requirement_met" from="coin_machine" to="." method="_on_coin_machine_coin_requirement_met"] +[connection signal="item_dump_completed" from="item_dump" to="." method="_on_item_dump_completed"] [connection signal="timeout" from="cook_timer" to="." method="_on_cook_timer_timeout"] diff --git a/scripts/oven/oven.gd b/scripts/oven/oven.gd index 5cf0462..02779d7 100644 --- a/scripts/oven/oven.gd +++ b/scripts/oven/oven.gd @@ -3,14 +3,18 @@ extends Spatial const StateIdle = preload("res://scripts/oven/state/idle.gd") const StateCooking = preload("res://scripts/oven/state/cooking.gd") const StateHoldingItem = preload("res://scripts/oven/state/holding_item.gd") +const StateBroken = preload("res://scripts/oven/state/broken.gd") export var cook_time: float = 2.0 +export var start_broken: bool = false +export var max_hp: int = 1 +var hp: int = max_hp + var state = null func _ready(): $cook_timer.wait_time = cook_time - - state = StateIdle.new() + state = StateBroken.new() if start_broken else StateIdle.new() state.ctx = self state.enter_from(null) print("oven: NULL -> ", state.NAME) @@ -30,3 +34,9 @@ func _on_cook_timer_timeout(): func _on_item_holder_item_changed(item): state.on_item_holder_item_changed(item) + +func _on_item_dump_completed(): + state.on_item_dump_completed() + +func take_damage(): + state.take_damage() diff --git a/scripts/oven/state/broken.gd b/scripts/oven/state/broken.gd new file mode 100644 index 0000000..5d1c466 --- /dev/null +++ b/scripts/oven/state/broken.gd @@ -0,0 +1,36 @@ +extends Reference + +const NAME = "broken" + +var ctx = null + +func _init(): + pass + +func enter_from(state): + ctx.remove_from_group("damageable") + ctx.get_node("item_dump").enabled = true + ctx.get_node("item_dump").reset() + ctx.get_node("coin_machine").enabled = false + ctx.get_node("%item_holder").add_enabled = false + ctx.get_node("%item_holder").remove_enabled = false + ctx.get_node("status_light").fail() + +func exit_to(state): + assert(state.NAME != "broken") + ctx.add_to_group("damageable") + +func on_coin_machine_coin_requirement_met(player): + assert(false, "should be impossible while broken") + +func on_cook_timer_timeout(): + assert(false, "should be impossible while broken") + +func on_item_holder_item_changed(item): + assert(false, "should be impossible while broken") + +func on_item_dump_completed(): + ctx.change_state(ctx.StateIdle.new()) + +func take_damage(): + pass # already broken diff --git a/scripts/oven/state/cooking.gd b/scripts/oven/state/cooking.gd index c6ac4a3..8612c72 100644 --- a/scripts/oven/state/cooking.gd +++ b/scripts/oven/state/cooking.gd @@ -10,12 +10,15 @@ func _init(): mat.albedo_color = Color.red func enter_from(state): + ctx.get_node("item_dump").enabled = false ctx.get_node("coin_machine").enabled = false ctx.get_node("%item_holder").add_enabled = false ctx.get_node("%item_holder").remove_enabled = false ctx.get_node("cook_timer").start() ctx.get_node("%coil").set_surface_material(0, mat) + ctx.get_node("status_light").light_color = Color.green + ctx.get_node("status_light").activate() func exit_to(state): ctx.get_node("%coil").set_surface_material(0, null) @@ -31,3 +34,13 @@ func on_cook_timer_timeout(): func on_item_holder_item_changed(item): assert(item == null || item.item_type == cooked_yam) + +func on_item_dump_completed(): + assert(false, "item dump should be disabled") + +func take_damage(): + ctx.hp -= 1 + if ctx.hp <= 0: + assert(ctx.get_node("%item_holder").has_item()) + ctx.get_node("%item_holder").destroy_item() + ctx.change_state(ctx.StateBroken.new()) diff --git a/scripts/oven/state/holding_item.gd b/scripts/oven/state/holding_item.gd index 42dea1c..4df3c38 100644 --- a/scripts/oven/state/holding_item.gd +++ b/scripts/oven/state/holding_item.gd @@ -9,14 +9,17 @@ func _init(): pass func enter_from(state): + ctx.get_node("item_dump").enabled = false if ctx.get_node("%item_holder").item_in_hold.item_type == raw_yam: ctx.get_node("coin_machine").enabled = true ctx.get_node("%item_holder").add_enabled = false ctx.get_node("%item_holder").remove_enabled = false + ctx.get_node("status_light").prime() else: ctx.get_node("coin_machine").enabled = false ctx.get_node("%item_holder").add_enabled = true ctx.get_node("%item_holder").remove_enabled = true + ctx.get_node("status_light").warn() func exit_to(state): pass @@ -33,3 +36,13 @@ func on_item_holder_item_changed(item): ctx.change_state(ctx.StateIdle.new()) else: ctx.change_state(ctx.StateHoldingItem.new()) + +func on_item_dump_completed(): + assert(false, "item dump should be disabled") + +func take_damage(): + ctx.hp -= 1 + if ctx.hp <= 0: + if ctx.get_node("%item_holder").has_item(): + ctx.get_node("%item_holder").destroy_item() + ctx.change_state(ctx.StateBroken.new()) diff --git a/scripts/oven/state/idle.gd b/scripts/oven/state/idle.gd index b04a82b..78dd6cc 100644 --- a/scripts/oven/state/idle.gd +++ b/scripts/oven/state/idle.gd @@ -8,9 +8,11 @@ func _init(): pass func enter_from(state): + ctx.get_node("item_dump").enabled = false 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 func exit_to(state): pass @@ -24,3 +26,11 @@ func on_cook_timer_timeout(): func on_item_holder_item_changed(item): assert(item != null, "there should be no item to remove while idle") ctx.change_state(ctx.StateHoldingItem.new()) + +func on_item_dump_completed(): + assert(false, "item dump should be disabled") + +func take_damage(): + ctx.hp -= 1 + if ctx.hp <= 0: + ctx.change_state(ctx.StateBroken.new())