feat(@projects/@magic-civilization): improve camera centering precision

Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
This commit is contained in:
Natalie 2026-05-10 04:08:59 -07:00
parent 1847f41042
commit 9763b4b374

View file

@ -112,18 +112,30 @@ func _ready() -> void:
elif cities.has_method("render_cities"):
cities.call("render_cities")
# Camera centred on the map middle, zoomed out enough to see the
# whole continent.
# Camera centred on the actual pixel-bbox of the rendered tiles. The
# axial→pixel transform isn't a simple linear scale (flat-top hex offset
# rows) so compute the bounding box from the real tile positions.
var min_p: Vector2 = Vector2(INF, INF)
var max_p: Vector2 = Vector2(-INF, -INF)
for pos_key: Vector2i in game_map.tiles:
var p: Vector2 = HexUtilsScript.axial_to_pixel(pos_key)
min_p = min_p.min(p)
max_p = max_p.max(p)
var bbox: Vector2 = max_p - min_p
var center: Vector2 = (min_p + max_p) * 0.5
# Fit the longer axis into the viewport with 5% padding.
var pad: float = 1.10
var fit_x: float = (bbox.x * pad) / float(VIEWPORT_SIZE.x)
var fit_y: float = (bbox.y * pad) / float(VIEWPORT_SIZE.y)
var world_per_screen: float = max(fit_x, fit_y)
var zoom_factor: float = 1.0 / max(world_per_screen, 0.001)
var cam: Camera2D = Camera2D.new()
cam.name = "DemoCamera"
var mid_axial: Vector2i = Vector2i(game_map.width / 2, game_map.height / 2)
var mid_pixel: Vector2 = HexUtilsScript.axial_to_pixel(mid_axial)
cam.position = mid_pixel
# axial_to_pixel scales each tile to ~150px; duel map (40×24) = ~6000×3600 px.
# Zoom 0.18 fits the whole map into 1920×1080.
cam.zoom = Vector2(0.18, 0.18)
cam.position = center
cam.zoom = Vector2(zoom_factor, zoom_factor)
cam.make_current()
add_child(cam)
print("full_game_demo: bbox=%s center=%s zoom=%.4f" % [bbox, center, zoom_factor])
# Wait for the renderers to flush their _draw passes.
for _i: int in range(20):