Util.gd 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. extends Object
  2. class_name Util
  3. # Logging
  4. static func PrintLog(logGroup : String, logString : String):
  5. print("[%d.%03d][%s] %s" % [Time.get_ticks_msec() / 1000.0, Time.get_ticks_msec() % 1000, logGroup, logString])
  6. static func PrintInfo(_logGroup : String, _logString : String):
  7. # print("[%d.%03d][%s] %s" % [Time.get_ticks_msec() / 1000.0, Time.get_ticks_msec() % 1000, logGroup, logString])
  8. pass
  9. # Node management
  10. static func RemoveNode(node : Node, parent : Node):
  11. if node != null:
  12. if parent != null:
  13. parent.remove_child(node)
  14. node.queue_free()
  15. # Screenshot
  16. static func GetScreenCapture() -> Image:
  17. return Launcher.get_viewport().get_texture().get_image()
  18. # Math
  19. static func UnrollPathLength(path : PackedVector2Array) -> float:
  20. var pathSize : int = path.size()
  21. if pathSize < 2:
  22. return INF
  23. var unrolledPos : Vector2 = Vector2.ZERO
  24. for i in (pathSize-1):
  25. unrolledPos += (path[i] - path[i+1]).abs()
  26. return unrolledPos.length_squared()
  27. static func IsReachableSquared(pos1 : Vector2, pos2 : Vector2, threshold : float) -> bool:
  28. return pos1.distance_squared_to(pos2) < threshold
  29. # Fade
  30. static func FadeInOutRatio(value : float, maxValue : float, fadeIn : float, fadeOut : float) -> float:
  31. var ratio : float = 1.0
  32. if value < fadeIn:
  33. ratio = min(value / fadeIn, ratio)
  34. if value > maxValue - fadeOut:
  35. ratio = min((fadeOut - (value - (maxValue - fadeOut))) / fadeOut, ratio)
  36. return ratio
  37. # Text
  38. static func GetFormatedText(value : String, numberAfterComma : int = 0) -> String:
  39. var commaLocation : int = value.find(".")
  40. var charCounter : int = 0
  41. if commaLocation > 0:
  42. charCounter = commaLocation - 3
  43. else:
  44. charCounter = value.length() - 3
  45. while charCounter > 0:
  46. value = value.insert(charCounter, ",")
  47. charCounter = charCounter - 3
  48. commaLocation = value.find(".")
  49. if commaLocation == -1:
  50. commaLocation = value.length()
  51. if numberAfterComma > 0:
  52. value += "."
  53. if numberAfterComma > 0:
  54. for i in range(value.length() - 1, commaLocation + numberAfterComma):
  55. value += "0"
  56. else:
  57. value = value.substr(0, commaLocation)
  58. return value
  59. # Dictionary
  60. static func DicCheckOrAdd(dic : Dictionary[Variant, Variant], key : Variant, value : Variant):
  61. if not key in dic:
  62. dic[key] = value
  63. elif dic[key] == null:
  64. dic[key] = value