diff --git a/Global/InGameState.gd b/Global/InGameState.gd index d0cf413..4dbceeb 100644 --- a/Global/InGameState.gd +++ b/Global/InGameState.gd @@ -7,6 +7,7 @@ var player_count: int signal player_win(player_id) signal player_count_updated(new_player_count) +signal player_health_updated(player_id, new_player_health) func set_player_count(new_player_count: int): @@ -16,16 +17,23 @@ func set_player_count(new_player_count: int): player_healths = [] for i in range(0, player_count): - player_healths.append(max_health) + player_healths.append(0.0) + set_player_health(i, max_health) emit_signal("player_count_updated", new_player_count) func subtract_player_health(player_id, subtractor): + set_player_health(player_id, player_healths[player_id] - subtractor) + + +func set_player_health(player_id, new_value): assert(player_id < player_count) assert(player_id >= 0) - player_healths[player_id] -= subtractor + player_healths[player_id] = new_value if player_healths[player_id] < 0.0: emit_signal("player_win", player_id) + else: + emit_signal("player_health_updated", player_id, player_healths[player_id]) diff --git a/Ingame/Ingame.gd b/Ingame/Ingame.gd new file mode 100644 index 0000000..2983361 --- /dev/null +++ b/Ingame/Ingame.gd @@ -0,0 +1,16 @@ +extends Spatial + + +# Declare member variables here. Examples: +# var a: int = 2 +# var b: String = "text" + + +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + InGameState.set_player_count(2) + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +#func _process(delta: float) -> void: +# pass diff --git a/Ingame/Testing.tscn b/Ingame/Testing.tscn index d50581e..6d46412 100644 --- a/Ingame/Testing.tscn +++ b/Ingame/Testing.tscn @@ -1,7 +1,9 @@ -[gd_scene load_steps=5 format=2] +[gd_scene load_steps=7 format=2] [ext_resource path="res://BodyParts/Arm.tscn" type="PackedScene" id=1] +[ext_resource path="res://UI/UI.tscn" type="PackedScene" id=2] [ext_resource path="res://BodyParts/BodyBase/BodyBase.tscn" type="PackedScene" id=3] +[ext_resource path="res://Ingame/Ingame.gd" type="Script" id=4] [sub_resource type="PlaneMesh" id=1] size = Vector2( 10, 10 ) @@ -9,7 +11,8 @@ size = Vector2( 10, 10 ) [sub_resource type="BoxShape" id=2] extents = Vector3( 5, 1, 5 ) -[node name="Spatial" type="Spatial"] +[node name="InGame" type="Spatial"] +script = ExtResource( 4 ) [node name="MeshInstance" type="MeshInstance" parent="."] transform = Transform( 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0 ) @@ -38,4 +41,6 @@ key = 83 [node name="Arm" parent="Torso/FrontRight" index="1" instance=ExtResource( 1 )] key = 65 +[node name="UI" parent="." instance=ExtResource( 2 )] + [editable path="Torso"] diff --git a/UI/PlayerHealth.gd b/UI/PlayerHealth.gd new file mode 100644 index 0000000..bc42463 --- /dev/null +++ b/UI/PlayerHealth.gd @@ -0,0 +1,21 @@ +extends ProgressBar + + +export(int) var player_id + + +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + InGameState.connect("player_health_updated", self, "_on_player_health_updated") + + max_value = InGameState.max_health + + +func _on_player_health_updated(updated_id, updated_health): + if player_id == updated_id: + value = updated_health + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +#func _process(delta: float) -> void: +# pass diff --git a/UI/UI.tscn b/UI/UI.tscn index 5a3e938..1b33837 100644 --- a/UI/UI.tscn +++ b/UI/UI.tscn @@ -1,4 +1,6 @@ -[gd_scene format=2] +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://UI/PlayerHealth.gd" type="Script" id=1] [node name="UI" type="Control"] anchor_right = 1.0 @@ -33,13 +35,14 @@ __meta__ = { [node name="HSplitContainer" type="HSplitContainer" parent="MarginContainer/PlayerHealthController/VSplitContainer"] margin_right = 984.0 -margin_bottom = 546.0 +margin_bottom = 560.0 split_offset = 492 dragger_visibility = 1 [node name="Player1Health" type="ProgressBar" parent="MarginContainer/PlayerHealthController/VSplitContainer/HSplitContainer"] margin_right = 496.0 margin_bottom = 14.0 +script = ExtResource( 1 ) __meta__ = { "_edit_use_anchors_": false } @@ -48,11 +51,13 @@ __meta__ = { margin_left = 508.0 margin_right = 984.0 margin_bottom = 14.0 +script = ExtResource( 1 ) __meta__ = { "_edit_use_anchors_": false } [node name="HSplitContainer2" type="HSplitContainer" parent="MarginContainer/PlayerHealthController/VSplitContainer"] +visible = false margin_top = 546.0 margin_right = 984.0 margin_bottom = 560.0 diff --git a/project.godot b/project.godot index c3958d8..4b61df9 100644 --- a/project.godot +++ b/project.godot @@ -36,6 +36,10 @@ config/name="BodyPartFighter" run/main_scene="res://Ingame/Testing.tscn" config/icon="res://icon.png" +[autoload] + +InGameState="*res://Global/InGameState.gd" + [rendering] environment/default_environment="res://default_env.tres"