From 7289eba670863d491f739146e1002fd459e67f15 Mon Sep 17 00:00:00 2001 From: akshay Date: Thu, 18 Aug 2022 01:56:07 -0400 Subject: [PATCH] Added Damageable class that crating_station inherits from. Added new broken state to crafting_station with status light --- project.godot | 8 +++- scenes/crafting_station.tscn | 20 ++++++++- scenes/dev-akshay.tscn | 5 ++- scenes/dinner_table.tscn | 41 +---------------- scenes/item_dump.tscn | 2 + scenes/oven.tscn | 4 +- scenes/player.tscn | 2 +- scripts/crafting_station/crafting_station.gd | 34 ++++++++++++--- scripts/crafting_station/state/blocked.gd | 20 ++++++--- scripts/crafting_station/state/broken.gd | 46 ++++++++++++++++++++ scripts/crafting_station/state/idle.gd | 21 +++++++-- scripts/crafting_station/state/working.gd | 18 +++++--- scripts/damageable_object.gd | 28 ++++++++++++ scripts/oven/state/cooking.gd | 1 - scripts/oven/state/idle.gd | 2 +- 15 files changed, 180 insertions(+), 72 deletions(-) create mode 100644 scripts/crafting_station/state/broken.gd create mode 100644 scripts/damageable_object.gd diff --git a/project.godot b/project.godot index 2435988..2feefd8 100644 --- a/project.godot +++ b/project.godot @@ -14,6 +14,11 @@ _global_script_classes=[ { "language": "GDScript", "path": "res://addons/dialogic/Localization/translation_service.gd" }, { +"base": "Spatial", +"class": "Damageable", +"language": "GDScript", +"path": "res://scripts/damageable_object.gd" +}, { "base": "Node", "class": "Dialogic", "language": "GDScript", @@ -81,6 +86,7 @@ _global_script_classes=[ { } ] _global_script_class_icons={ "DTS": "", +"Damageable": "", "Dialogic": "", "DialogicAnimaPropertiesHelper": "", "DialogicAnimaResources": "", @@ -99,7 +105,7 @@ _global_script_class_icons={ [application] config/name="project-dung" -run/main_scene="res://scenes/main.tscn" +run/main_scene="res://scenes/dev-akshay.tscn" config/icon="res://icon.png" [display] diff --git a/scenes/crafting_station.tscn b/scenes/crafting_station.tscn index 302a5fb..6dcfc3f 100644 --- a/scenes/crafting_station.tscn +++ b/scenes/crafting_station.tscn @@ -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://scripts/crafting_station/crafting_station.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="CylinderMesh" id=1] height = 0.936 @@ -11,8 +14,9 @@ radial_segments = 8 [sub_resource type="ConvexPolygonShape" id=2] points = PoolVector3Array( -0.0608763, 0.449247, 0.974783, -0.0608763, -0.464612, -0.974783, -0.0608763, 0.449247, -0.974783, 0.974783, -0.464612, -0.0608763, -0.974783, -0.464612, 0.0608763, 0.974783, 0.449247, 0.0608763, 0.0606902, -0.464648, 0.97486, -0.974783, 0.449247, -0.0608763, 0.7071, 0.453517, -0.7071, -0.705748, -0.468, -0.705748, -0.7071, 0.453517, 0.7071, 0.7071, 0.453517, 0.7071, 0.705748, -0.468, 0.705748, -0.705748, -0.468, 0.705748, 0.705748, -0.468, -0.705748, -0.7071, 0.453517, -0.7071, 0.0606902, -0.464648, -0.97486, -0.974783, -0.464612, -0.0608763, 0.974783, -0.464612, 0.0608763, -0.0608763, -0.464612, 0.974783, 0.0606902, 0.449283, 0.97486, 0.0606902, 0.449283, -0.97486, 0.974783, 0.449247, -0.0608763, -0.974783, 0.449247, 0.0608763, -0.642208, -0.46642, 0.73398, -0.642208, -0.46642, -0.73398, 0.578116, -0.464071, -0.760529, 0.578116, -0.464071, 0.760529, 0.760589, -0.464107, 0.577971, -0.760589, -0.464107, 0.577971, -0.760589, -0.464107, -0.577971, 0.760589, -0.464107, -0.577971 ) -[node name="crafting_station" type="Spatial"] +[node name="crafting_station" type="Spatial" groups=["damageable"]] script = ExtResource( 2 ) +start_broken = true [node name="MeshInstance" type="MeshInstance" parent="."] transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.372095, 0 ) @@ -48,6 +52,18 @@ unique_name_in_owner = true unique_name_in_owner = true one_shot = true +[node name="item_dump" parent="." instance=ExtResource( 4 )] +unique_name_in_owner = true +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.366242, 0, 0 ) +item_type_0 = ExtResource( 6 ) +item_count_0 = 1 + +[node name="status_light" parent="." instance=ExtResource( 5 )] +unique_name_in_owner = true +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 1.1, 0, 0 ) + [connection signal="item_changed" from="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="timeout" from="gen_timer" to="." method="_on_gen_timer_timeout"] +[connection signal="item_dump_completed" from="item_dump" to="." method="_on_item_dump_item_dump_completed"] +[connection signal="item_dumped" from="item_dump" to="." method="_on_item_dump_item_dumped"] diff --git a/scenes/dev-akshay.tscn b/scenes/dev-akshay.tscn index 4de08ca..d940fde 100644 --- a/scenes/dev-akshay.tscn +++ b/scenes/dev-akshay.tscn @@ -295,10 +295,11 @@ modulate = Color( 0, 0, 0, 1 ) text = "Galley" [node name="dinner_table" parent="Galley_Room" instance=ExtResource( 12 )] -transform = Transform( 1, 0, -1.49012e-08, 0, 1, 0, 1.49012e-08, 0, 1, 1.54321, 0, -7.74583 ) +transform = Transform( 1, 0, -1.11759e-08, 0, 1, 0, 1.11759e-08, 0, 1, 1.77669, 0, -8.88646 ) [node name="oven" parent="Galley_Room" instance=ExtResource( 14 )] -transform = Transform( 1, 0, -7.45058e-09, 0, 1, 0, 7.45058e-09, 0, 1, -2.08601, 0, -7.98964 ) +transform = Transform( 1.00001, 0, 3.72529e-09, 0, 1, 0, -3.72529e-09, 0, 1.00001, -2.828, 0, -10.6561 ) +start_broken = true [node name="Hydroponics_Room" type="Spatial" parent="."] transform = Transform( 0.690862, 0.000404371, 0.722986, -0.000832856, 1, 0.000236545, -0.722985, -0.000765563, 0.690862, -6.25959, -7.15916e-05, -5.86512 ) diff --git a/scenes/dinner_table.tscn b/scenes/dinner_table.tscn index a88267e..e6d2090 100644 --- a/scenes/dinner_table.tscn +++ b/scenes/dinner_table.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=8 format=2] +[gd_scene load_steps=5 format=2] [ext_resource path="res://scenes/item_holder.tscn" type="PackedScene" id=1] [ext_resource path="res://scripts/dinner_table.gd" type="Script" id=2] @@ -6,19 +6,6 @@ [sub_resource type="SphereShape" id=4] -[sub_resource type="CylinderMesh" id=1] -height = 0.1 -radial_segments = 8 - -[sub_resource type="CylinderMesh" id=2] -top_radius = 0.1 -bottom_radius = 0.1 -height = 0.738 -radial_segments = 16 - -[sub_resource type="CylinderShape" id=3] -height = 0.87498 - [node name="dinner_table" type="Area"] collision_layer = 8 collision_mask = 4 @@ -29,32 +16,6 @@ all_food = [ ExtResource( 3 ) ] transform = Transform( 1.2, 0, 0, 0, 1.2, 0, 0, 0, 1.2, 0, 0, 0 ) shape = SubResource( 4 ) -[node name="MeshInstance" type="MeshInstance" parent="."] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.648164, 0 ) -mesh = SubResource( 1 ) - -[node name="MeshInstance2" type="MeshInstance" parent="."] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.274821, 0.806474 ) -mesh = SubResource( 2 ) - -[node name="MeshInstance3" type="MeshInstance" parent="."] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.831715, 0.274821, 0.00579381 ) -mesh = SubResource( 2 ) - -[node name="MeshInstance4" type="MeshInstance" parent="."] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -0.012414, 0.274821, -0.832128 ) -mesh = SubResource( 2 ) - -[node name="MeshInstance5" type="MeshInstance" parent="."] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -0.809991, 0.274821, 0.00269032 ) -mesh = SubResource( 2 ) - -[node name="StaticBody" type="StaticBody" parent="."] - -[node name="CollisionShape" type="CollisionShape" parent="StaticBody"] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.439889, 0 ) -shape = SubResource( 3 ) - [node name="item_holder" parent="." instance=ExtResource( 1 )] unique_name_in_owner = true transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.776869, 0 ) diff --git a/scenes/item_dump.tscn b/scenes/item_dump.tscn index 2b1ab34..2336940 100644 --- a/scenes/item_dump.tscn +++ b/scenes/item_dump.tscn @@ -5,6 +5,8 @@ [sub_resource type="BoxShape" id=1] [node name="item_dump" type="Area"] +collision_layer = 8 +collision_mask = 4 script = ExtResource( 1 ) [node name="CollisionShape" type="CollisionShape" parent="."] diff --git a/scenes/oven.tscn b/scenes/oven.tscn index 0dfd9c5..b8bea67 100644 --- a/scenes/oven.tscn +++ b/scenes/oven.tscn @@ -11,7 +11,6 @@ extents = Vector3( 0.648623, 1, 0.516904 ) [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="coin_machine" parent="." instance=ExtResource( 3 )] @@ -19,7 +18,6 @@ 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 @@ -32,7 +30,7 @@ transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.14052, 0 ) shape = SubResource( 4 ) [node name="status_light" parent="." instance=ExtResource( 5 )] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.232042, 0.967385 ) +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0.868397 ) [node name="cook_timer" type="Timer" parent="."] one_shot = true diff --git a/scenes/player.tscn b/scenes/player.tscn index 1c26b9c..681b6d1 100644 --- a/scenes/player.tscn +++ b/scenes/player.tscn @@ -697,7 +697,7 @@ mesh = SubResource( 1 ) skin = SubResource( 2 ) [node name="head_bone" type="BoneAttachment" parent="model_transform/DungMan/Armature/Skeleton" index="1"] -transform = Transform( 1, -2.21502e-16, 1.62129e-17, -2.37146e-19, 0.0719347, 0.997409, -2.22095e-16, -0.997409, 0.0719347, -3.31708e-16, -0.0484204, -1.49945 ) +transform = Transform( 1, -2.22045e-16, 5.12852e-23, 1.32349e-23, 2.83122e-07, 1, -2.22045e-16, -1, 2.83122e-07, -3.33067e-16, 5.21541e-08, -1.5 ) bone_name = "Item" [node name="ItemSpawn" type="Spatial" parent="model_transform/DungMan/Armature/Skeleton/head_bone"] diff --git a/scripts/crafting_station/crafting_station.gd b/scripts/crafting_station/crafting_station.gd index 2d89d82..776b0cc 100644 --- a/scripts/crafting_station/crafting_station.gd +++ b/scripts/crafting_station/crafting_station.gd @@ -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() diff --git a/scripts/crafting_station/state/blocked.gd b/scripts/crafting_station/state/blocked.gd index fd4790e..ca79982 100644 --- a/scripts/crafting_station/state/blocked.gd +++ b/scripts/crafting_station/state/blocked.gd @@ -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()) diff --git a/scripts/crafting_station/state/broken.gd b/scripts/crafting_station/state/broken.gd new file mode 100644 index 0000000..6646832 --- /dev/null +++ b/scripts/crafting_station/state/broken.gd @@ -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") diff --git a/scripts/crafting_station/state/idle.gd b/scripts/crafting_station/state/idle.gd index 98bf1d8..c09fe20 100644 --- a/scripts/crafting_station/state/idle.gd +++ b/scripts/crafting_station/state/idle.gd @@ -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() diff --git a/scripts/crafting_station/state/working.gd b/scripts/crafting_station/state/working.gd index bb80c23..95c0da6 100644 --- a/scripts/crafting_station/state/working.gd +++ b/scripts/crafting_station/state/working.gd @@ -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()) diff --git a/scripts/damageable_object.gd b/scripts/damageable_object.gd new file mode 100644 index 0000000..76295ce --- /dev/null +++ b/scripts/damageable_object.gd @@ -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) diff --git a/scripts/oven/state/cooking.gd b/scripts/oven/state/cooking.gd index 3cc333c..0104fc1 100644 --- a/scripts/oven/state/cooking.gd +++ b/scripts/oven/state/cooking.gd @@ -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): diff --git a/scripts/oven/state/idle.gd b/scripts/oven/state/idle.gd index 78dd6cc..4eae896 100644 --- a/scripts/oven/state/idle.gd +++ b/scripts/oven/state/idle.gd @@ -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