From feb2bcc4102f66483f0adc942f37751211288e20 Mon Sep 17 00:00:00 2001 From: Hipstercat Date: Tue, 12 Oct 2021 23:11:23 +0200 Subject: [PATCH] Add health bar --- characters/reaper.tscn | 4 ++-- entities/character.gd | 12 ++++++++---- entities/genericprojectile.gd | 22 +++++++++++++--------- player/TPSController.gd | 11 +++++++++++ ui/rp_ui.tscn | 17 ++++++++++++++++- 5 files changed, 50 insertions(+), 16 deletions(-) diff --git a/characters/reaper.tscn b/characters/reaper.tscn index bde87001..cb0fd8b0 100644 --- a/characters/reaper.tscn +++ b/characters/reaper.tscn @@ -31,10 +31,10 @@ shape = SubResource( 1 ) transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.611, 0 ) [node name="lefthand" parent="ReaperGLTF/Armature/Skeleton" index="1"] -transform = Transform( 0.532954, 0.0959757, -0.840684, -0.651193, 0.680928, -0.335089, 0.540284, 0.726034, 0.425402, -1.56225, 5.06134, 1.12064 ) +transform = Transform( 0.524316, 0.123817, -0.842473, -0.658921, 0.685675, -0.309309, 0.539366, 0.717299, 0.441096, -1.57618, 5.07854, 1.16485 ) [node name="righthand" parent="ReaperGLTF/Armature/Skeleton" index="2"] -transform = Transform( 0.227042, -0.513301, -0.827631, 0.0755038, -0.837989, 0.540437, -0.970953, -0.185191, -0.151503, 1.15032, 5.00594, -0.0259017 ) +transform = Transform( 0.232014, -0.551311, -0.801389, 0.0601252, -0.814167, 0.577508, -0.970851, -0.182174, -0.155751, 1.20283, 5.04613, 0.0143257 ) [node name="AnimationTree" parent="ReaperGLTF" index="10"] parameters/movement/blend_position = Vector2( 0, 0 ) diff --git a/entities/character.gd b/entities/character.gd index f0e9f859..c9aa49a5 100644 --- a/entities/character.gd +++ b/entities/character.gd @@ -56,6 +56,7 @@ var ground_type = GroundType.DIRT var footstep_sounds = {} +export var max_health = 100 export var health = 100 var state = States.Alive @@ -91,10 +92,11 @@ func pickup_weapon(weapon: Weapon) -> Weapon: return weapon func take_damage(hitpts: float): - health -= hitpts - emit_signal("health_changed") - if health <= 0: - die() + if state == States.Alive: + health -= hitpts + emit_signal("health_changed") + if health <= 0: + die() func get_current_weapon(): return current_weapon @@ -102,6 +104,7 @@ func get_current_weapon(): func die(): state = States.Dead emit_signal("died") + print("Me ded") func set_weapon_slot(slot: int): if current_weapon: @@ -185,6 +188,7 @@ func _process(delta): if Globals.process_3d_inputs: move_axis.x = Input.get_action_strength("move_forward") - Input.get_action_strength("move_backward") move_axis.y = Input.get_action_strength("move_right") - Input.get_action_strength("move_left") + take_damage(0.1) func fire_pressed(): current_weapon.activate(Globals.target_position["position"]) diff --git a/entities/genericprojectile.gd b/entities/genericprojectile.gd index c10e69e9..20b7f32e 100644 --- a/entities/genericprojectile.gd +++ b/entities/genericprojectile.gd @@ -57,25 +57,19 @@ func create(_from: Transform, _to: Vector3, exclude_collisions: Array): func body_entered(body: Node): if properties & WeaponProperties.TRIGGER_GROUND and body.is_in_group("world"): + print("trigger by body_entered TRIGGER_GROUND") trigger(body) if properties & WeaponProperties.TRIGGER_OBJECT and body.is_in_group("objects"): + print("trigger by body_entered TRIGGER_OBJECT") trigger(body) func proximity_body_entered(body: Node): if properties & WeaponProperties.TRIGGER_OBJECT: if body.is_in_group("objects"): + print("trigger by proximity_body_entered") trigger(body) func trigger(body: Node): - if fx_explode: - var fx: Particles = fx_explode.instance() - fx.global_transform = global_transform - fx.emitting = true - get_parent().add_child(fx) - current_fx_explode = fx - visible = false # needed because _process must still destroy the fx - else: - queue_free() if properties & WeaponProperties.EXPLODE: # create decal var decal: MeshInstance = preload("res://effects/explosion_decal.tscn").instance() @@ -83,6 +77,16 @@ func trigger(body: Node): decal.scale = Vector3(20, 2, 20) get_parent().add_child(decal) print("Created decal") + if fx_explode: + var fx: Particles = fx_explode.instance() + fx.global_transform = global_transform + fx.emitting = true + get_parent().add_child(fx) + current_fx_explode = fx + visible = false # needed because _process must still destroy the fx + sleeping = true + else: + queue_free() func _process(delta): if current_fx_explode and not current_fx_explode.emitting: diff --git a/player/TPSController.gd b/player/TPSController.gd index 0c4231e0..ebfcdca8 100644 --- a/player/TPSController.gd +++ b/player/TPSController.gd @@ -22,6 +22,7 @@ func _ready(): add_child(HUD) character = character_scene.instance() add_child(character) + character.connect("health_changed", self, "update_health") func get_target() -> Dictionary: var weapon = character.current_weapon @@ -131,4 +132,14 @@ func update_hud() -> void: hud_weapon_slot.get_node("selected").visible = true else: hud_weapon_slot.get_node("selected").visible = false + + +func update_health() -> void: + var current_health = character.health + var max_health = character.max_health + var hp_percent = current_health/max_health + + # UI/hp polygon: 0 means 100% hp, 250 means 0% + var val = 250 - 250*hp_percent + HUD.find_node("hp").texture_offset = Vector2(val, 0) diff --git a/ui/rp_ui.tscn b/ui/rp_ui.tscn index 3311fa3f..f094be4a 100644 --- a/ui/rp_ui.tscn +++ b/ui/rp_ui.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=10 format=2] +[gd_scene load_steps=12 format=2] [ext_resource path="res://assets/all_gbs/textures/st_mec_energy.tga" type="Texture" id=1] [ext_resource path="res://assets/all_gbs/textures/st_delphi.tga" type="Texture" id=2] @@ -10,6 +10,13 @@ [ext_resource path="res://assets/all_gbs/textures/st_rp_slot3k.tga" type="Texture" id=8] [ext_resource path="res://assets/all_gbs/textures/st_rp_slot2k.tga" type="Texture" id=9] +[sub_resource type="Gradient" id=1] +colors = PoolColorArray( 1, 0, 0, 1, 1, 1, 1, 0.046729 ) + +[sub_resource type="GradientTexture" id=2] +gradient = SubResource( 1 ) +width = 10 + [node name="UI" type="Control"] anchor_right = 1.0 anchor_bottom = 1.0 @@ -19,6 +26,14 @@ __meta__ = { "_edit_use_anchors_": false } +[node name="hp" type="Polygon2D" parent="."] +position = Vector2( 957.76, 325.233 ) +color = Color( 1, 0, 0, 1 ) +offset = Vector2( -31.7015, 28.4404 ) +texture = SubResource( 2 ) +texture_rotation_degrees = 90.0 +polygon = PoolVector2Array( 4.07648, -12.0231, 5.19122, -17.9239, 8.9613, -23.6782, 12.533, -26.3569, 15.8069, -28.0435, 20.966, -27.5475, 23.9424, -24.3727, 27.1172, -19.6105, 29.1014, -14.5507, 29.9944, -10.1853, 31.2841, -7.10971, 34.4589, -12.7648, 35.3519, -16.7333, 37.9314, -21.1979, 40.0148, -25.0672, 42.6935, -27.4483, 47.0589, -28.4404, 52.8132, -27.1506, 54.103, -25.8609, 57.8731, -20.1066, 59.7581, -14.6499, 60.9487, -7.70499, 61.3455, 2.51392, 61.2463, 12.336, 60.155, 18.5864, 48.9894, 80.8738, 39.2252, 135.995, 31.9805, 202.456, 25.051, 136.625, 13.7117, 70.7945, 2.68756, 13.468, 2.05756, -1.33612 ) + [node name="healthbar" type="TextureRect" parent="."] anchor_left = 1.0 anchor_top = 1.0