Add health bar

This commit is contained in:
Amazed 2021-10-12 23:11:23 +02:00
parent 3cabf08b85
commit feb2bcc410
5 changed files with 50 additions and 16 deletions

View File

@ -31,10 +31,10 @@ shape = SubResource( 1 )
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.611, 0 ) transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.611, 0 )
[node name="lefthand" parent="ReaperGLTF/Armature/Skeleton" index="1"] [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"] [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"] [node name="AnimationTree" parent="ReaperGLTF" index="10"]
parameters/movement/blend_position = Vector2( 0, 0 ) parameters/movement/blend_position = Vector2( 0, 0 )

View File

@ -56,6 +56,7 @@ var ground_type = GroundType.DIRT
var footstep_sounds = {} var footstep_sounds = {}
export var max_health = 100
export var health = 100 export var health = 100
var state = States.Alive var state = States.Alive
@ -91,10 +92,11 @@ func pickup_weapon(weapon: Weapon) -> Weapon:
return weapon return weapon
func take_damage(hitpts: float): func take_damage(hitpts: float):
health -= hitpts if state == States.Alive:
emit_signal("health_changed") health -= hitpts
if health <= 0: emit_signal("health_changed")
die() if health <= 0:
die()
func get_current_weapon(): func get_current_weapon():
return current_weapon return current_weapon
@ -102,6 +104,7 @@ func get_current_weapon():
func die(): func die():
state = States.Dead state = States.Dead
emit_signal("died") emit_signal("died")
print("Me ded")
func set_weapon_slot(slot: int): func set_weapon_slot(slot: int):
if current_weapon: if current_weapon:
@ -185,6 +188,7 @@ func _process(delta):
if Globals.process_3d_inputs: if Globals.process_3d_inputs:
move_axis.x = Input.get_action_strength("move_forward") - Input.get_action_strength("move_backward") 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") move_axis.y = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
take_damage(0.1)
func fire_pressed(): func fire_pressed():
current_weapon.activate(Globals.target_position["position"]) current_weapon.activate(Globals.target_position["position"])

View File

@ -57,25 +57,19 @@ func create(_from: Transform, _to: Vector3, exclude_collisions: Array):
func body_entered(body: Node): func body_entered(body: Node):
if properties & WeaponProperties.TRIGGER_GROUND and body.is_in_group("world"): if properties & WeaponProperties.TRIGGER_GROUND and body.is_in_group("world"):
print("trigger by body_entered TRIGGER_GROUND")
trigger(body) trigger(body)
if properties & WeaponProperties.TRIGGER_OBJECT and body.is_in_group("objects"): if properties & WeaponProperties.TRIGGER_OBJECT and body.is_in_group("objects"):
print("trigger by body_entered TRIGGER_OBJECT")
trigger(body) trigger(body)
func proximity_body_entered(body: Node): func proximity_body_entered(body: Node):
if properties & WeaponProperties.TRIGGER_OBJECT: if properties & WeaponProperties.TRIGGER_OBJECT:
if body.is_in_group("objects"): if body.is_in_group("objects"):
print("trigger by proximity_body_entered")
trigger(body) trigger(body)
func trigger(body: Node): 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: if properties & WeaponProperties.EXPLODE:
# create decal # create decal
var decal: MeshInstance = preload("res://effects/explosion_decal.tscn").instance() var decal: MeshInstance = preload("res://effects/explosion_decal.tscn").instance()
@ -83,6 +77,16 @@ func trigger(body: Node):
decal.scale = Vector3(20, 2, 20) decal.scale = Vector3(20, 2, 20)
get_parent().add_child(decal) get_parent().add_child(decal)
print("Created 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): func _process(delta):
if current_fx_explode and not current_fx_explode.emitting: if current_fx_explode and not current_fx_explode.emitting:

View File

@ -22,6 +22,7 @@ func _ready():
add_child(HUD) add_child(HUD)
character = character_scene.instance() character = character_scene.instance()
add_child(character) add_child(character)
character.connect("health_changed", self, "update_health")
func get_target() -> Dictionary: func get_target() -> Dictionary:
var weapon = character.current_weapon var weapon = character.current_weapon
@ -132,3 +133,13 @@ func update_hud() -> void:
else: else:
hud_weapon_slot.get_node("selected").visible = false 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)

View File

@ -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_mec_energy.tga" type="Texture" id=1]
[ext_resource path="res://assets/all_gbs/textures/st_delphi.tga" type="Texture" id=2] [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_slot3k.tga" type="Texture" id=8]
[ext_resource path="res://assets/all_gbs/textures/st_rp_slot2k.tga" type="Texture" id=9] [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"] [node name="UI" type="Control"]
anchor_right = 1.0 anchor_right = 1.0
anchor_bottom = 1.0 anchor_bottom = 1.0
@ -19,6 +26,14 @@ __meta__ = {
"_edit_use_anchors_": false "_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="."] [node name="healthbar" type="TextureRect" parent="."]
anchor_left = 1.0 anchor_left = 1.0
anchor_top = 1.0 anchor_top = 1.0