Files
IntoHole/designer/scripts/level_data.gd
T
Yanchen d1141b04c7 feat: 更新 IntoHole 的球体、滑槽和环境资产
- 为铁球、陶瓷球和木球制作风格化材质,归档 CC0 原始参考
- 更新方形球洞和实心滑槽,统一 90° 弯槽并兼容旧存档
- 用顺流泡沫表示水流方向,为冰块制作三种交错的裂纹
- 同步 Blender 源资产、Godot 加载、工坊和试玩预览
2026-11-19 16:18:41 +08:00

102 lines
5.8 KiB
GDScript

extends RefCounted
const Chutes = preload("res://runtime/chute_types.gd")
var lib
var chutes: Array
var entity_ids: Array=[]
func _init(library, chute_ids: Array) -> void:
lib=library;chutes=chute_ids
var registry=JSON.parse_string(FileAccess.get_file_as_string("res://assets/collections/storybook/entities.json"))
for group in ["interactive","nonInteractive"]:
for item in registry[group]:entity_ids.append(item.id)
func valid_vector(value: Variant) -> bool:
if not value is Array or value.size()!=3:return false
for n in value:
if not (n is float or n is int) or not is_finite(float(n)):return false
return true
func overlaps(a: Dictionary,b: Dictionary) -> bool:
var ab: AABB=lib.record_bounds(a);var bb: AABB=lib.record_bounds(b)
var extent: Vector3=ab.end.min(bb.end)-ab.position.max(bb.position)
return extent.x>.001 and extent.y>.001 and extent.z>.001
func validate(data: Variant) -> String:
if not data is Dictionary:return "关卡文件必须是 JSON 对象"
var version=data.get("schemaVersion")
if not (version is int or version is float):return "缺少关卡版本"
if (version!=1 and version!=2) or data.get("assetFamily")!="railbound_inspired":return "关卡版本或资产系列不匹配"
if not data.get("pieces") is Array or not data.get("balls") is Array or not data.get("entities",[]) is Array:return "缺少积木、实体或小球数据"
var legacy: bool=data.schemaVersion==1
if legacy and (not valid_vector(data.get("goal")) or not data.get("entities",[]).is_empty()):return "旧版洞口或实体数据无效"
var ids: Array=[];var decoded: Array=[]
for group in ["pieces","entities"]:
for r in data.get(group,[]):
var allowed: Array=chutes if group=="pieces" else entity_ids
if not r is Dictionary or not r.get("kind") is String or Chutes.canonical_kind(r.kind) not in allowed or not valid_vector(r.get("position")):return "无效的方块类型或坐标"
if not r.get("id") is String or r.id.is_empty() or r.id in ids:return "方块编号缺失或重复"
if not (r.get("quarter") is float or r.get("quarter") is int):return "无效的旋转值"
if r.quarter!=int(r.quarter) or r.quarter<0 or r.quarter>3:return "旋转必须为 0 / 90 / 180 / 270 度"
# X/Z remain grid-based; exact Y is required for groove-to-flat joins.
if r.kind!="goal_hole" and (r.position[0]!=round(r.position[0]) or r.position[2]!=round(r.position[2])):return "积木 X / Z 坐标必须对齐网格"
var d={"id":r.id,"kind":Chutes.canonical_kind(r.kind),"position":lib.vector(r.position),"quarter":Chutes.canonical_quarter(r.kind,int(r.quarter))}
for other in decoded:
if overlaps(d,other):return "关卡中有重叠方块"
decoded.append(d);ids.append(r.id)
var ball_ids: Array=[]
for b in data.balls:
if not b is Dictionary or not b.get("id") is String or b.id.is_empty() or b.id in ball_ids:return "小球编号缺失或重复"
if legacy:
if b.get("pieceId") not in ids:return "旧版小球起点引用无效"
if not (b.get("pathT") is float or b.get("pathT") is int) or not is_finite(float(b.pathT)) or b.pathT<0 or b.pathT>1:return "旧版起点位置必须位于槽内"
var r: Dictionary=decoded[ids.find(b.pieceId)]
if not (b.get("branch") is float or b.get("branch") is int) or b.branch!=int(b.branch) or b.branch<0 or b.branch>=lib.catalog[r.kind].upperPaths.size():return "旧版起点分支无效"
else:
if not valid_vector(b.get("position")):return "小球球心坐标无效"
if b.get("ballTypeId","iron_ball") not in lib.ball_type_ids():return "未知的小球类型"
if b.has("launchDirection") and not valid_vector(b.launchDirection):return "初速度方向无效"
ball_ids.append(b.id)
return ""
func array_from(v: Vector3) -> Array:
return [v.x,v.y,v.z]
func normalize(data: Dictionary) -> Dictionary:
var records: Array=[]
for r in data.pieces+data.get("entities",[]):
var record={"id":r.id,"kind":Chutes.canonical_kind(r.kind),"position":lib.vector(r.position),"quarter":Chutes.canonical_quarter(r.kind,int(r.quarter))}
# Obsolete per-block speedGain fields are intentionally discarded on load.
records.append(record)
var balls: Array=[]
if data.schemaVersion==1:
for b in data.balls:
var record: Dictionary={}
for r in records:
if r.id==b.pieceId:record=r;break
var basis:=Basis(Vector3.UP,-int(record.quarter)*PI/2)
var old_kind:String=""
for original in data.pieces:
if original.id==b.pieceId:old_kind=original.kind;break
var path_t:float=1.0-float(b.pathT) if old_kind=="turn_ccw" else float(b.pathT)
var position: Vector3=lib.record_center(record)+basis*lib.ball_on_surface(record.kind,path_t,int(b.branch))
var direction: Vector3=basis*lib.path_tangent(record.kind,path_t,int(b.branch))
if old_kind=="turn_ccw":direction=-direction
if direction.y>0:direction=-direction
balls.append({"id":b.id,"ballTypeId":"iron_ball","position":array_from(position),"launchDirection":array_from(direction.normalized())})
var goal_id: String="legacy_goal"
while records.any(func(r):return r.id==goal_id):goal_id+="_"
records.append({"id":goal_id,"kind":"goal_hole","position":lib.vector(data.goal),"quarter":0})
else:
for b in data.balls:
balls.append({"id":b.id,"ballTypeId":b.get("ballTypeId","iron_ball"),"position":array_from(lib.vector(b.position)),"launchDirection":array_from(lib.vector(b.get("launchDirection",[1,0,0])))})
return {"records":records,"balls":balls}
func encode(records: Array,balls: Array,title: String) -> Dictionary:
var pieces: Array=[];var entities: Array=[]
for r in records:
var item={"id":r.id,"kind":Chutes.canonical_kind(r.kind),"position":array_from(r.position),"quarter":Chutes.canonical_quarter(r.kind,int(r.quarter))}
if item.kind in chutes:pieces.append(item)
else:entities.append(item)
return {"schemaVersion":2,"assetFamily":"railbound_inspired","title":title,"pieces":pieces,"entities":entities,"balls":balls.duplicate(true)}