PINGS.gd 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. extends VBoxContainer
  2. # Declare member variables here. Examples:
  3. # var a = 2
  4. # var b = "text"
  5. onready var MAIN = find_parent("MAIN")
  6. onready var SHIP = find_parent("SHIP")
  7. var pings_max = 3
  8. var labels = []
  9. var ping_per_dist = 1 #ms
  10. # Called when the node enters the scene tree for the first time.
  11. func _ready():
  12. for i in range(pings_max):
  13. var lab = Label.new()
  14. add_child(lab)
  15. labels.append(lab)
  16. func _process(delta):
  17. var planetlist = MAIN.planets.duplicate()
  18. planetlist.sort_custom(self, "ping_sort")
  19. update_pings(planetlist)
  20. func update_pings(planetlist):
  21. var i = 0
  22. for l in labels:
  23. var planet = planetlist[i]
  24. l.text = str(int(get_ping(planet)))+" ms"
  25. l.modulate = planet.color
  26. i += 1
  27. func get_ping(planet):
  28. var ping = (planet.global_position - SHIP.global_position).length() - planet.radius
  29. ping *= ping_per_dist
  30. return ping
  31. func ping_sort(planet_a, planet_b):
  32. return get_ping(planet_a) < get_ping(planet_b)
  33. # Called every frame. 'delta' is the elapsed time since the previous frame.
  34. #func _process(delta):
  35. # pass