11extends Control
22
33
4+ # TODO: this script needs some work.. just an early prototype
45
5-
6- # var healthbar_x = 500
7- # var healthbar_y =40
86var my_health_bar_size : Vector2 = Vector2 (500.0 , 40.0 )
9- #
10- # var otherhealthbar_x = 250
11- # var otherhealthbar_y =20
127var other_health_bar_size : Vector2 = Vector2 (250.0 ,20.0 )
13-
148var player_id
15- @onready var player = get_node ("../" )
16- var lbl_nametag
17-
189var health_bar_size : Vector2
1910
20- func _ready ():
11+ @onready var player = get_node ( "../" )
2112
13+ func _ready ():
2214 # if my player
2315 if str (get_parent ().name ) == str ($ "/root/main/net" .multiplayer .get_unique_id ()):
2416 health_bar_size = my_health_bar_size
25-
26- print ("its me :) ---------------" )
27- else : # if other pplayer
28- # add label
29- # lbl_nametag = Label.new()
30- # lbl_nametag.set_name("nametag")
31- # lbl_nametag.text = "testtext"
32- # lbl_nametag.set_position(Vector2(0,-20))
33- # lbl_nametag.hide()
34- # add_child(lbl_nametag)
17+ # if other player
18+ else :
3519 health_bar_size = other_health_bar_size
36-
37- # hide()
20+
3821 var image = Image .create (health_bar_size .x ,health_bar_size .y ,false ,Image .FORMAT_RGB8 )
3922 image .fill (Color .DIM_GRAY )
4023 var image_texture = ImageTexture .new ()
@@ -44,44 +27,54 @@ func _ready():
4427 $ vbox/health_bar .set_tint_progress (Color .RED )
4528 set_name ("healthbar_" + str (player_id ))
4629
30+
4731func _physics_process (delta ):
48- pass
4932 if str (get_parent ().name ) == str ($ "/root/main/net" .multiplayer .get_unique_id ()):
5033 set_anchors_and_offsets_preset (Control .PRESET_FULL_RECT )
5134 $ vbox .set_anchors_and_offsets_preset (Control .PRESET_CENTER_BOTTOM )
52-
5335 else : # if not network master (if otherplayer)
5436 var healthbar_max_distance = 50
5537
38+ # TODO..
5639 var test_id = str (multiplayer .get_unique_id ())
5740 var test = get_node ("/root/main/players/" + str (test_id ) + "/" + str (test_id ))
5841
5942 var distance = player .transform .origin .distance_to (test .transform .origin )
6043# #TODO offset based on distance??????????????????????????????????????????????????????????????
6144 if distance < healthbar_max_distance :
6245 var camera : Camera3D = test .get_node ("SpringArm3D/Camera3D" )
46+
47+ # TODO offset based on distance
48+ # var healthbar_offset_y=clamp(healthbar_max_offset_y-distance*4,50,180) #I dont know how to calculate the offset based on distance...TODO pls help xD
6349 var healthbar_max_offset_y = 180
64- var healthbar_offset_y = healthbar_max_offset_y - distance
65- ## var nametag_healthbar_offset = 5
66- var offset = Vector2 (size .x / 2 * get_scale ().x / 2 , healthbar_offset_y ) # TODO offset based on distance
67- ## var offset_nametag = Vector2(lbl_nametag.get_size().x * lbl_nametag.get_scale().x/2, healthbar_offset_y + get_node("healthbar_" + get_name()).get_size().y*get_node("healthbar_" + get_name()).get_scale().y/2 + nametag_healthbar_offset) #TODO offset based on distance
68- # # if( get_node("../../ui/healthbar_" + get_name()).get_position().x in (camera.unproject_position(get_translation()) - offset).x):
50+ var zwischenresultat1 = float (1 ) / healthbar_max_distance
51+ var distanceinprozent1 = distance * zwischenresultat1 # dreisatz 1=100%
52+ var healthbar_offset_y = clamp (180 - 180 * distanceinprozent1 ,50 ,180 )
53+ var offset = Vector2 (size .x / 2 * get_scale ().x / 2 , healthbar_offset_y )
54+
55+ # TODO: only show if player is infront of my players camera
56+ # if( get_node("../../ui/healthbar_" + get_name()).get_position().x in (camera.unproject_position(get_translation()) - offset).x):
6957 var fovx = get_node ("../SpringArm3D/Camera3D" ).position .x
7058 var fovy = get_node ("../SpringArm3D/Camera3D" ).position .y
7159 var hbx = (get_node ("../SpringArm3D/Camera3D" ).unproject_position (test .position ) - offset ).x
7260 var hby = (get_node ("../SpringArm3D/Camera3D" ).unproject_position (test .position ) - offset ).y
73- if (hby > 0 and hby > fovy ) or (hbx > 0 and hbx > fovx ) :
61+
62+ if (hby > 0 and hby > fovy ) or (hbx > 0 and hbx > fovx ):
7463## # healthbar.set_scale(Vector2(0.5, 0.5)) #scale 50%
7564 if distance < 40 :
7665 var zwischenresultat = float (1 ) / healthbar_max_distance
7766 var distanceinprozent = distance * zwischenresultat # dreisatz 1=100%
7867 set_scale (Vector2 (1 ,1 ) - Vector2 (distanceinprozent , distanceinprozent )) # Set scale based on distance
7968 show ()
69+
70+ # show label
8071# lbl_nametag.text="<" + str(player.charname) + "@" + str(player.username) + ">"
8172 $ vbox/name_tag .text = "<" + str (player .player_name ) + ">"
8273 $ vbox/name_tag .show ()
83- # set_position(camera.unproject_position(player.position) - offset)
84- set_position (camera .unproject_position (player .position ))
74+
75+ # set position with offset
76+ set_position (camera .unproject_position (player .position ) - offset )
77+ # set_position(camera.unproject_position(player.position))
8578 else :
8679 hide ()
8780 else :
0 commit comments