Minimap.gd 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. extends WindowPanel
  2. @onready var textureRect : TextureRect = $ScrollContainer/TextureRect
  3. @onready var scrollContainer : ScrollContainer = $ScrollContainer
  4. #
  5. func Warped():
  6. if not textureRect or not Launcher.Map:
  7. return
  8. if Launcher.Map.currentMapID == DB.UnknownHash:
  9. assert(false, "Could not fetch the active map name")
  10. return
  11. var mapData : FileData = DB.MapsDB.get(Launcher.Map.currentMapID, null)
  12. if not mapData:
  13. assert(false, "Could not retrieve the map ID from our map daabase")
  14. return
  15. var resource : Texture2D = FileSystem.LoadMinimap(mapData._path)
  16. if not resource:
  17. assert(false, "Could not load the minimap resource")
  18. return
  19. textureRect.set_texture(resource)
  20. #
  21. func _ready():
  22. scrollContainer.get_h_scroll_bar().scale = Vector2.ZERO
  23. scrollContainer.get_v_scroll_bar().scale = Vector2.ZERO
  24. func _process(_delta : float):
  25. if visible and Launcher.Camera != null && Launcher.Camera.mainCamera != null:
  26. var screenCenter : Vector2 = Launcher.Camera.mainCamera.get_target_position()
  27. var mapSize : Vector2 = Vector2(Launcher.Camera.mainCamera.get_limit(SIDE_RIGHT), Launcher.Camera.mainCamera.get_limit(SIDE_BOTTOM))
  28. if not mapSize.is_zero_approx():
  29. var posRatio : Vector2 = screenCenter / mapSize
  30. var minimapWindowSize : Vector2 = textureRect.size
  31. var scrollPos : Vector2i = Vector2i(minimapWindowSize * posRatio - size / 2)
  32. maxSize = minimapWindowSize
  33. scrollContainer.set_h_scroll(scrollPos.x)
  34. scrollContainer.set_v_scroll(scrollPos.y)
  35. func _on_visibility_changed():
  36. if Launcher.Map and not Launcher.Map.PlayerWarped.is_connected(Warped):
  37. Launcher.Map.PlayerWarped.connect(Warped)
  38. Warped()