Implement cubemap skybox, multi-active skyboxes, and 2D scene backgrounds
- Server: Add SkyboxType + 6 CubeUrl fields to SkyboxDefinition entity - Server: EF migration AddSkyboxCubemapType - Server: GetSkyboxConfigResponse now returns array of all active skyboxes - Admin: SkyboxManagerPage supports Panoramic/AnimatedPanorama/Cubemap types, 6 face URL fields for cubemap, and independent per-skybox active toggle - Client: ContentCache fetches + caches skyboxes; adds get_random_skybox, get_skybox_bg_url, load_texture_async, apply_scene_background helpers - Client: game_screen replaces _fetch_skybox with ContentCache-based approach; adds _load_cubemap_skybox / _apply_cubemap_images / _apply_cubemap_material - Client: 12 2D scene scripts call ContentCache.apply_scene_background(self) in _ready() to replace solid-color backgrounds with CDN skybox images Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
@@ -21,6 +21,7 @@ const CACHE_DIR := "user://content"
|
||||
const CACHE_PATH := "user://content/defs.json"
|
||||
const TILE_MODELS_PATH := "user://content/tile_models.json"
|
||||
const TILE_BOTTOMS_PATH := "user://content/tile_bottoms.json"
|
||||
const SKYBOXES_PATH := "user://content/skyboxes.json"
|
||||
const PAGE_SIZE := 200
|
||||
const REFRESH_INTERVAL_SECS := 300.0 # re-sync every 5 minutes during live play
|
||||
|
||||
@@ -42,12 +43,16 @@ var _tile_textures: Dictionary = {}
|
||||
## Array of CDN URL strings for tile bottom (stalagmite) models.
|
||||
var _tile_bottom_urls: Array = []
|
||||
|
||||
## Array of Dictionary (SkyboxDefinitionDto) for all active skyboxes.
|
||||
var _skyboxes: Array = []
|
||||
|
||||
# ── Lifecycle ─────────────────────────────────────────────────────────────────
|
||||
|
||||
func _ready() -> void:
|
||||
_load_from_disk()
|
||||
_load_tile_models_from_disk()
|
||||
_load_tile_bottoms_from_disk()
|
||||
_load_skyboxes_from_disk()
|
||||
|
||||
# ── Public API ────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -91,6 +96,7 @@ func start_sync() -> void:
|
||||
_fetch_page(_sync_version)
|
||||
fetch_tile_models()
|
||||
fetch_tile_bottoms()
|
||||
fetch_skyboxes()
|
||||
|
||||
|
||||
## Return the GLB animation track name for a card action.
|
||||
@@ -141,6 +147,87 @@ func fetch_tile_bottoms() -> void:
|
||||
)
|
||||
|
||||
|
||||
## Fetch active skybox definitions from the server and update the local cache.
|
||||
func fetch_skyboxes() -> void:
|
||||
Network.request(
|
||||
"content.get_skybox_config",
|
||||
{},
|
||||
func(_type: String, p: Variant): _on_skyboxes(p),
|
||||
func(code: String, msg: String):
|
||||
push_warning("ContentCache: skyboxes fetch failed — %s %s" % [code, msg])
|
||||
)
|
||||
|
||||
|
||||
## Returns a random active skybox Dictionary, or {} if none are cached.
|
||||
func get_random_skybox() -> Dictionary:
|
||||
if _skyboxes.is_empty():
|
||||
return {}
|
||||
return _skyboxes[randi() % _skyboxes.size()] as Dictionary
|
||||
|
||||
|
||||
## Returns the best URL to use as a flat 2D background from this skybox dict.
|
||||
## For Cubemap: returns the +Z (Front) face URL.
|
||||
## For Panoramic/AnimatedPanorama: returns the cdnPath.
|
||||
func get_skybox_bg_url(skybox: Dictionary) -> String:
|
||||
var sky_type := str(skybox.get("type", "Panoramic"))
|
||||
if sky_type == "Cubemap":
|
||||
return str(skybox.get("cubePzUrl", ""))
|
||||
return str(skybox.get("cdnPath", ""))
|
||||
|
||||
|
||||
## Downloads an image from url and calls callback(Texture2D).
|
||||
## Adds a temporary HTTPRequest child; frees itself on completion.
|
||||
func load_texture_async(url: String, callback: Callable) -> void:
|
||||
var http := HTTPRequest.new()
|
||||
add_child(http)
|
||||
http.request_completed.connect(func(result: int, code: int, _hdrs: PackedStringArray, body: PackedByteArray):
|
||||
http.queue_free()
|
||||
if result != HTTPRequest.RESULT_SUCCESS or code != 200:
|
||||
callback.call(null)
|
||||
return
|
||||
var img := Image.new()
|
||||
var ext := url.split("?")[0].get_extension().to_lower()
|
||||
var err: Error
|
||||
if ext in ["jpg", "jpeg"]:
|
||||
err = img.load_jpg_from_buffer(body)
|
||||
elif ext == "webp":
|
||||
err = img.load_webp_from_buffer(body)
|
||||
else:
|
||||
err = img.load_png_from_buffer(body)
|
||||
if err != OK:
|
||||
callback.call(null)
|
||||
return
|
||||
callback.call(ImageTexture.create_from_image(img)))
|
||||
http.request(url)
|
||||
|
||||
|
||||
## Hides the first direct-child ColorRect of `parent` and injects a full-rect
|
||||
## TextureRect (from a random active skybox) as the new first child.
|
||||
## Call from _ready() in any 2D scene that has a solid-color background.
|
||||
func apply_scene_background(parent: Control) -> void:
|
||||
var skybox := get_random_skybox()
|
||||
if skybox.is_empty():
|
||||
return
|
||||
var url := get_skybox_bg_url(skybox)
|
||||
if url.is_empty():
|
||||
return
|
||||
load_texture_async(url, func(tex: Texture2D):
|
||||
if not is_instance_valid(parent) or tex == null:
|
||||
return
|
||||
for child in parent.get_children():
|
||||
if child is ColorRect:
|
||||
child.visible = false
|
||||
break
|
||||
var tr := TextureRect.new()
|
||||
tr.texture = tex
|
||||
tr.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
|
||||
tr.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_COVERED
|
||||
tr.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
|
||||
tr.mouse_filter = Control.MOUSE_FILTER_IGNORE
|
||||
parent.add_child(tr)
|
||||
parent.move_child(tr, 0))
|
||||
|
||||
|
||||
## Fetch tile model bindings from the server and update the local cache.
|
||||
## Emits tile_models_ready when done (even on error, using cached data).
|
||||
func fetch_tile_models() -> void:
|
||||
@@ -226,6 +313,15 @@ func _on_tile_bottoms(payload: Variant) -> void:
|
||||
_tile_bottom_urls.append(u as String)
|
||||
_save_tile_bottoms_to_disk()
|
||||
|
||||
|
||||
func _on_skyboxes(payload: Variant) -> void:
|
||||
var p := payload as Dictionary
|
||||
_skyboxes.clear()
|
||||
for item in p.get("skyboxes", []):
|
||||
if typeof(item) == TYPE_DICTIONARY:
|
||||
_skyboxes.append(item as Dictionary)
|
||||
_save_skyboxes_to_disk()
|
||||
|
||||
# ── Persistence ───────────────────────────────────────────────────────────────
|
||||
|
||||
func _load_from_disk() -> void:
|
||||
@@ -298,3 +394,24 @@ func _save_tile_bottoms_to_disk() -> void:
|
||||
return
|
||||
file.store_string(JSON.stringify(_tile_bottom_urls))
|
||||
file.close()
|
||||
|
||||
|
||||
func _load_skyboxes_from_disk() -> void:
|
||||
if not FileAccess.file_exists(SKYBOXES_PATH):
|
||||
return
|
||||
var file := FileAccess.open(SKYBOXES_PATH, FileAccess.READ)
|
||||
if not file:
|
||||
return
|
||||
var parsed: Variant = JSON.parse_string(file.get_as_text())
|
||||
file.close()
|
||||
if typeof(parsed) == TYPE_ARRAY:
|
||||
_skyboxes = parsed as Array
|
||||
|
||||
|
||||
func _save_skyboxes_to_disk() -> void:
|
||||
DirAccess.make_dir_recursive_absolute(CACHE_DIR)
|
||||
var file := FileAccess.open(SKYBOXES_PATH, FileAccess.WRITE)
|
||||
if not file:
|
||||
return
|
||||
file.store_string(JSON.stringify(_skyboxes))
|
||||
file.close()
|
||||
|
||||
Reference in New Issue
Block a user