Player health UI is bound to singleton

This commit is contained in:
karl 2020-02-01 12:22:18 +01:00
parent c8acaf0988
commit a773e5900d
6 changed files with 65 additions and 6 deletions

View File

@ -7,6 +7,7 @@ var player_count: int
signal player_win(player_id) signal player_win(player_id)
signal player_count_updated(new_player_count) signal player_count_updated(new_player_count)
signal player_health_updated(player_id, new_player_health)
func set_player_count(new_player_count: int): func set_player_count(new_player_count: int):
@ -16,16 +17,23 @@ func set_player_count(new_player_count: int):
player_healths = [] player_healths = []
for i in range(0, player_count): 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) emit_signal("player_count_updated", new_player_count)
func subtract_player_health(player_id, subtractor): 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 < player_count)
assert(player_id >= 0) assert(player_id >= 0)
player_healths[player_id] -= subtractor player_healths[player_id] = new_value
if player_healths[player_id] < 0.0: if player_healths[player_id] < 0.0:
emit_signal("player_win", player_id) emit_signal("player_win", player_id)
else:
emit_signal("player_health_updated", player_id, player_healths[player_id])

16
Ingame/Ingame.gd Normal file
View File

@ -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

View File

@ -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://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://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] [sub_resource type="PlaneMesh" id=1]
size = Vector2( 10, 10 ) size = Vector2( 10, 10 )
@ -9,7 +11,8 @@ size = Vector2( 10, 10 )
[sub_resource type="BoxShape" id=2] [sub_resource type="BoxShape" id=2]
extents = Vector3( 5, 1, 5 ) extents = Vector3( 5, 1, 5 )
[node name="Spatial" type="Spatial"] [node name="InGame" type="Spatial"]
script = ExtResource( 4 )
[node name="MeshInstance" type="MeshInstance" parent="."] [node name="MeshInstance" type="MeshInstance" parent="."]
transform = Transform( 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0 ) 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 )] [node name="Arm" parent="Torso/FrontRight" index="1" instance=ExtResource( 1 )]
key = 65 key = 65
[node name="UI" parent="." instance=ExtResource( 2 )]
[editable path="Torso"] [editable path="Torso"]

21
UI/PlayerHealth.gd Normal file
View File

@ -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

View File

@ -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"] [node name="UI" type="Control"]
anchor_right = 1.0 anchor_right = 1.0
@ -33,13 +35,14 @@ __meta__ = {
[node name="HSplitContainer" type="HSplitContainer" parent="MarginContainer/PlayerHealthController/VSplitContainer"] [node name="HSplitContainer" type="HSplitContainer" parent="MarginContainer/PlayerHealthController/VSplitContainer"]
margin_right = 984.0 margin_right = 984.0
margin_bottom = 546.0 margin_bottom = 560.0
split_offset = 492 split_offset = 492
dragger_visibility = 1 dragger_visibility = 1
[node name="Player1Health" type="ProgressBar" parent="MarginContainer/PlayerHealthController/VSplitContainer/HSplitContainer"] [node name="Player1Health" type="ProgressBar" parent="MarginContainer/PlayerHealthController/VSplitContainer/HSplitContainer"]
margin_right = 496.0 margin_right = 496.0
margin_bottom = 14.0 margin_bottom = 14.0
script = ExtResource( 1 )
__meta__ = { __meta__ = {
"_edit_use_anchors_": false "_edit_use_anchors_": false
} }
@ -48,11 +51,13 @@ __meta__ = {
margin_left = 508.0 margin_left = 508.0
margin_right = 984.0 margin_right = 984.0
margin_bottom = 14.0 margin_bottom = 14.0
script = ExtResource( 1 )
__meta__ = { __meta__ = {
"_edit_use_anchors_": false "_edit_use_anchors_": false
} }
[node name="HSplitContainer2" type="HSplitContainer" parent="MarginContainer/PlayerHealthController/VSplitContainer"] [node name="HSplitContainer2" type="HSplitContainer" parent="MarginContainer/PlayerHealthController/VSplitContainer"]
visible = false
margin_top = 546.0 margin_top = 546.0
margin_right = 984.0 margin_right = 984.0
margin_bottom = 560.0 margin_bottom = 560.0

View File

@ -36,6 +36,10 @@ config/name="BodyPartFighter"
run/main_scene="res://Ingame/Testing.tscn" run/main_scene="res://Ingame/Testing.tscn"
config/icon="res://icon.png" config/icon="res://icon.png"
[autoload]
InGameState="*res://Global/InGameState.gd"
[rendering] [rendering]
environment/default_environment="res://default_env.tres" environment/default_environment="res://default_env.tres"