PING_RECT.gd 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. extends ColorRect
  2. class_name PingRect
  3. # Declare member variables here. Examples:
  4. # var a = 2
  5. # var b = "text"
  6. var ring_index = 2
  7. onready var TOP_PANEL = find_parent("top_panel")
  8. onready var SHIP = find_parent("SHIP")
  9. var ping
  10. var ping_label
  11. var planet
  12. var active = false
  13. var ping_per_dist = 1 #ms
  14. # Called when the node enters the scene tree for the first time.
  15. func _ready():
  16. color = ColorN("black")
  17. ping_label = FontedLabel.new()
  18. ping_label.rect_size = rect_size
  19. ping_label.font_size = rect_size.y * 0.3
  20. ping_label.align = Label.ALIGN_CENTER
  21. ping_label.valign = Label.ALIGN_CENTER
  22. ping_label.text = "ping label"
  23. call_deferred("add_child",ping_label)
  24. func activate():
  25. planet = TOP_PANEL.planet
  26. active = true
  27. func deactivate():
  28. active = false
  29. func _process(delta):
  30. if active:
  31. var ring
  32. var dist
  33. for r in range(len(planet.rings)):
  34. dist = (SHIP.global_position - planet.global_position).length()
  35. if dist <= planet.rings[r].radius:
  36. ring_index = r
  37. ring = planet.rings[r]
  38. break
  39. if !ring:
  40. TOP_PANEL.jam_progress_bar.reward = 0
  41. planet.jam_missed()
  42. return
  43. if SHIP.gameover:
  44. planet.finish_jam()
  45. return
  46. color = ring.default_color
  47. ping = dist - planet.radius
  48. ping *= ping_per_dist
  49. ping_label.text = "PING: " + str(floor(ping)) + " ms"
  50. if color.r + color.g > 1.2:
  51. ping_label.modulate = ColorN("brown")
  52. else:
  53. ping_label.modulate = ColorN("white")
  54. print(ring_index)