feat: 更新 IntoHole 的球体、滑槽和环境资产

- 为铁球、陶瓷球和木球制作风格化材质,归档 CC0 原始参考
- 更新方形球洞和实心滑槽,统一 90° 弯槽并兼容旧存档
- 用顺流泡沫表示水流方向,为冰块制作三种交错的裂纹
- 同步 Blender 源资产、Godot 加载、工坊和试玩预览
This commit is contained in:
Yanchen
2026-11-19 16:18:41 +08:00
parent 39ff1fd7b4
commit d1141b04c7
467 changed files with 67228 additions and 44624 deletions
+21 -14
View File
@@ -1,5 +1,6 @@
extends RefCounted
const Chutes = preload("res://runtime/chute_types.gd")
var lib
var chutes: Array
var entity_ids: Array=[]
@@ -26,33 +27,34 @@ func validate(data: Variant) -> String:
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 "缺少积木、实体或铁球数据"
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 r.get("kind") not in allowed or not valid_vector(r.get("position")):return "无效的方块类型或坐标"
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 度"
if r.kind!="goal_hole" and (r.position[0]!=round(r.position[0]) or r.position[2]!=round(r.position[2]) or r.position[1]*2!=round(r.position[1]*2)):return "积木坐标必须对齐网格"
var d={"id":r.id,"kind":r.kind,"position":lib.vector(r.position),"quarter":int(r.quarter)}
# 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 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 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")!="iron_ball":return "未知的小球类型"
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 ""
@@ -63,7 +65,7 @@ func array_from(v: Vector3) -> Array:
func normalize(data: Dictionary) -> Dictionary:
var records: Array=[]
for r in data.pieces+data.get("entities",[]):
var record={"id":r.id,"kind":r.kind,"position":lib.vector(r.position),"quarter":int(r.quarter)}
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=[]
@@ -73,8 +75,13 @@ func normalize(data: Dictionary) -> Dictionary:
for r in records:
if r.id==b.pieceId:record=r;break
var basis:=Basis(Vector3.UP,-int(record.quarter)*PI/2)
var position: Vector3=lib.record_center(record)+basis*lib.ball_on_surface(record.kind,float(b.pathT),int(b.branch))
var direction: Vector3=basis*lib.path_tangent(record.kind,float(b.pathT),int(b.branch))
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"
@@ -82,13 +89,13 @@ func normalize(data: Dictionary) -> Dictionary:
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":"iron_ball","position":array_from(lib.vector(b.position)),"launchDirection":array_from(lib.vector(b.get("launchDirection",[1,0,0])))})
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":r.kind,"position":array_from(r.position),"quarter":r.quarter}
if r.kind in chutes:pieces.append(item)
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)}
+74 -5
View File
@@ -9,11 +9,12 @@ func bottom_of(record: Dictionary) -> Vector3:
func record_at(kind: String,bottom: Vector3,quarter: int,id: String) -> Dictionary:
var at:=bottom
if kind!="goal_hole":at=Vector3(round(at.x),snappedf(at.y,.5),round(at.z))
# Surface joins can be at e.g. Y=1.82; half-metre rounding recreates a lip.
if kind!="goal_hole":at=Vector3(round(at.x),at.y,round(at.z))
var local_min: float=lib.catalog[kind].boundsBlender.min[2]
return {"id":id,"kind":kind,"position":at-Vector3.UP*local_min,"quarter":quarter}
func on_face(support: Dictionary,point: Vector3,normal: Vector3,kind: String,quarter: int) -> Vector3:
func on_face(support: Dictionary,point: Vector3,normal: Vector3,kind: String,quarter: int,neighbors: Array=[]) -> Vector3:
var bounds: AABB=lib.record_bounds(support)
var size: Vector2i=lib.footprint(kind,quarter)
var height: float=lib.catalog[kind].boundsBlender.size[2]
@@ -26,7 +27,75 @@ func on_face(support: Dictionary,point: Vector3,normal: Vector3,kind: String,qua
else:
result.z=bounds.end.z+.5 if normal.z>=0 else bounds.position.z+.5-size.y
if kind!="goal_hole":
if axis==1:result.y=ceilf(result.y*2-.0001)/2 if normal.y>=0 else floorf(result.y*2+.0001)/2
elif axis==0:result.x=ceilf(result.x-.0001) if normal.x>=0 else floorf(result.x+.0001)
else:result.z=ceilf(result.z-.0001) if normal.z>=0 else floorf(result.z+.0001)
if axis==0:result.x=ceilf(result.x-.0001) if normal.x>=0 else floorf(result.x+.0001)
elif axis==2:result.z=ceilf(result.z-.0001) if normal.z>=0 else floorf(result.z+.0001)
# Top/bottom clicks still mean stacking. Only side joins use rolling surfaces.
if axis!=1:
var nearby: Array=neighbors if not neighbors.is_empty() else [support]
result=bottom_of(align_surfaces(record_at(kind,result,quarter,"preview"),nearby,str(support.id)))
return result
func ports(record: Dictionary) -> Array:
# Catalog ports are authored groove-floor positions in runtime coordinates.
# Their travel vector is not an outward normal (both ends may share travel).
var result: Array=[]
var model: Dictionary=lib.catalog[record.kind]
var basis:=Basis(Vector3.UP,-int(record.quarter)*PI/2)
var center: Vector3=lib.record_center(record)
for port in model.get("ports",[]):
var local: Vector3=lib.vector(port.surface)
var half_x: float=model.boundsBlender.size[0]*.5
var half_z: float=model.boundsBlender.size[1]*.5
var outward:=Vector3(signf(local.x),0,0) if absf(absf(local.x)-half_x)<.001 else Vector3(0,0,signf(local.z))
if absf(absf(local.x)-half_x)>=.001 and absf(absf(local.z)-half_z)>=.001:continue
result.append({"surface":center+basis*local,"normal":basis*outward})
return result
func connections(candidate: Dictionary,neighbors: Array) -> Array:
var matches: Array=[]
for own in ports(candidate):
for other in neighbors:
if other.id==candidate.id:continue
var targets: Array=ports(other)
if other.kind in ["wall","directional_boost","ice","goal_hole"]:
# Flat solid tops and the goal rim have no groove. Use the edge at
# this lane, not the block bottom or the decorative arrow mesh.
var bounds: AABB=lib.record_bounds(other)
var p: Vector3=own.surface
var n: Vector3=own.normal
var axis:=0 if absf(n.x)>.5 else 2
var lateral:=2 if axis==0 else 0
var edge: float=bounds.position[axis] if n[axis]>0 else bounds.end[axis]
if absf(p[axis]-edge)<.001 and p[lateral]>bounds.position[lateral]+.001 and p[lateral]<bounds.end[lateral]-.001:
targets.append({"surface":Vector3(p.x,bounds.end.y,p.z),"normal":-n})
for target in targets:
var delta: Vector3=target.surface-own.surface
if Vector2(delta.x,delta.z).length()>.001 or own.normal.dot(target.normal)>-.999:continue
# Nearby layers only; a distant floor must not capture a grid pick.
var reach: float=maxf(lib.catalog[candidate.kind].boundsBlender.size[2],lib.catalog[other.kind].boundsBlender.size[2])
if absf(delta.y)>reach+.001:continue
matches.append({"offset":delta.y,"support":str(other.id)})
return matches
func align_surfaces(candidate: Dictionary,neighbors: Array,preferred: String="") -> Dictionary:
# Shared by workshop face picking and player grid placement/preview. Keep the
# chosen type, X/Z and rotation; solve only the translation along world Y.
var matches:=connections(candidate,neighbors)
if matches.is_empty():return candidate
var best: Dictionary={}
for match in matches:
var count:=0
for other in matches:
if absf(other.offset-match.offset)<.001:count+=1
var priority:=1 if match.support==preferred else 0
var better: bool=best.is_empty()
if not best.is_empty():
if priority!=best.priority:better=priority>best.priority
elif count!=best.count:better=count>best.count
elif not is_equal_approx(absf(match.offset),absf(best.offset)):better=absf(match.offset)<absf(best.offset)
else:better=match.offset<best.offset # Stable tie-break, independent of record order.
if better:
best={"offset":match.offset,"priority":priority,"count":count}
var result:=candidate.duplicate(true)
result.position.y+=float(best.offset)
return result
+140 -52
View File
@@ -4,13 +4,13 @@ const Library = preload("res://runtime/asset_library.gd")
const LevelData = preload("res://designer/scripts/level_data.gd")
const Placement = preload("res://designer/scripts/placement.gd")
const Booster = preload("res://assets/blocks/directional_boost/behavior.gd")
const IDS = ["straight", "turn_cw", "turn_ccw", "gentle", "steep", "y_merge"]
const TITLES = ["直行", "右转", "左转", "缓坡 ½", "陡坡 1", "Y 汇流"]
const IDS = preload("res://runtime/chute_types.gd").IDS
const TITLES = ["直行", "90°弯槽", "缓坡 ½", "陡坡 1", "Y 汇流"]
const INK = Color("343143")
const PAPER = Color("fff4d9")
const ACCENT = Color("bf6050")
const SAVE_DIR = "res://designer/drafts"
const EXAMPLE_LEVELS = ["res://playtest/showcase/data/meadowexample.json", "res://playtest/underpass/data/underpass_validation.json"]
const EXAMPLE_LEVELS = ["res://playtest/showcase/data/meadowexample.json"]
var editor_mode := false
var lib = Library.new()
var codec = LevelData.new(lib,IDS)
@@ -35,19 +35,23 @@ var contact_visited := {}
var elapsed := 0.0
var started := false
var paused := false
var status := "准备好,让两颗铁球一起出发。"
var status := "准备好,让两颗小球一起出发。"
var world := Node3D.new()
var actors := Node3D.new()
var camera := Camera3D.new()
var grid := Node3D.new()
var selection := Node3D.new()
var cursor := Vector3.ZERO # Bottom-face placement coordinates, ground Y = 0.
var face_pick: Dictionary={} # Retain the clicked face while rotating the preview.
var block_mode := "place"
var last_placed_id := ""
var mode_buttons: Dictionary={}
var place_button: Button
var apply_button: Button
var delete_button: Button
var reset_level_button: Button
var eraser_tool_button: Button
var eraser_hint: Label
var boost_controls: VBoxContainer
var ground_view := MeshInstance3D.new()
var ground_holes: Array=[]
@@ -82,6 +86,8 @@ var category := "chutes"
var ball_position_fields: Array=[]
var ball_cursor := Vector3(0,2,0)
var selected_ball_id := ""
var ball_kind := "iron_ball"
var ball_type_label: Label
var ball_marker := Node3D.new()
var ball_pick_mode: OptionButton
var ball_pick_hint: Label
@@ -194,7 +200,7 @@ func setup_ui() -> void:
col.add_theme_constant_override("separation",10);scroll.add_child(col)
col.add_child(label("INTO / HOLE",28))
col.add_child(label("关卡工坊" if editor_mode else "01 林间汇流",20))
col.add_child(label("无限积木 · 自由编排" if editor_mode else "六种滑槽 · 两颗铁球 · 一个洞",14,Color("7a7366")))
col.add_child(label("无限积木 · 自由编排" if editor_mode else "五种滑槽 · 两颗小球 · 一个洞",14,Color("7a7366")))
count_label=label("",18);col.add_child(count_label)
run_button=button("▶ 同时释放",toggle_run,true);col.add_child(run_button)
col.add_child(button("↺ 重试 / 返回编辑" if editor_mode else "↺ 重新开始",reset_simulation))
@@ -219,6 +225,11 @@ func build_editor_ui(col: VBoxContainer) -> void:
tab.toggle_mode=true;tab.button_group=group;tab.button_pressed=id==category
tab.add_theme_font_size_override("font_size",14);tab.custom_minimum_size.x=126
categories.add_child(tab);category_buttons[id]=tab
eraser_tool_button=button("橡皮擦",func():set_block_mode("place" if block_mode=="erase" else "erase"),false,true)
eraser_tool_button.toggle_mode=true
eraser_tool_button.tooltip_text="选中后单击方块立即删除,可连续擦除;再次点击或选择素材退出。"
col.add_child(eraser_tool_button);mode_buttons["erase"]=eraser_tool_button
eraser_hint=label("单击方块立即删除 · 可连续擦除\n再次点击橡皮擦或选素材退出",13,Color("7a7366"));col.add_child(eraser_hint)
palette_box=GridContainer.new();palette_box.columns=2;col.add_child(palette_box)
block_controls=VBoxContainer.new();block_controls.add_theme_constant_override("separation",10);col.add_child(block_controls)
detail_label=label("",14);block_controls.add_child(detail_label)
@@ -228,6 +239,7 @@ func build_editor_ui(col: VBoxContainer) -> void:
boost_controls.add_child(boost_hint)
var modes:=row(block_controls)
var mode_group:=ButtonGroup.new()
eraser_tool_button.button_group=mode_group
for entry in [["place","放置模式"],["edit","选择 / 移动"]]:
var mode: String=entry[0]
var b:=button(entry[1],func():set_block_mode(mode),false,true)
@@ -237,11 +249,12 @@ func build_editor_ui(col: VBoxContainer) -> void:
var coords:=row(block_controls)
for i in range(3):
var field:=SpinBox.new();field.min_value=-10000;field.max_value=10000
field.step=.5 if i==1 else 1;field.prefix=["X ","Y ","Z "][i]
field.step=.01 if i==1 else 1;field.prefix=["X ","Y ","Z "][i]
field.custom_minimum_size.x=83;field.add_theme_font_size_override("font_size",13)
var axis:=i
field.value_changed.connect(func(v):
if syncing:return
face_pick.clear()
cursor[axis]=v;refresh_selection())
coords.add_child(field);position_fields.append(field);editable.append(field)
var actions:=row(block_controls)
@@ -258,7 +271,7 @@ func build_editor_ui(col: VBoxContainer) -> void:
ball_pick_hint=label("",13,Color("7a7366"))
ball_pick_hint.autowrap_mode=TextServer.AUTOWRAP_WORD_SMART;ball_pick_hint.custom_minimum_size.x=255
ball_controls.add_child(ball_pick_hint);refresh_ball_pick_hint()
ball_controls.add_child(label("铁球 · 球心坐标",17))
ball_type_label=label("小球 · 球心坐标",17);ball_controls.add_child(ball_type_label)
var ball_coords:=row(ball_controls)
for i in range(3):
var field:=SpinBox.new();field.min_value=-10000;field.max_value=10000
@@ -270,30 +283,34 @@ func build_editor_ui(col: VBoxContainer) -> void:
if syncing:return
ball_cursor[axis]=v;refresh_ball_marker())
ball_coords.add_child(field);ball_position_fields.append(field);editable.append(field)
add_ball_button=button("+ 放置铁球",add_spawn,true,true);ball_controls.add_child(add_ball_button)
add_ball_button=button("+ 放置小球",add_spawn,true,true);ball_controls.add_child(add_ball_button)
ball_list=OptionButton.new();ball_controls.add_child(ball_list);editable.append(ball_list)
ball_list.item_selected.connect(select_ball)
actions=row(ball_controls);move_ball_button=button("移动选中铁球",move_spawn,false,true);actions.add_child(move_ball_button)
actions.add_child(button("移除铁球",remove_spawn,false,true))
actions=row(ball_controls);move_ball_button=button("移动选中小球",move_spawn,false,true);actions.add_child(move_ball_button)
actions.add_child(button("移除小球",remove_spawn,false,true))
ball_controls.add_child(label("可悬空;启动后受重力影响。",13,Color("7a7366")))
actions=row(col);actions.add_child(button("撤销",undo,false,true));actions.add_child(button("重做",redo,false,true))
actions=row(col);actions.add_child(button("撤销",undo,false,true))
reset_level_button=button("重置",reset_level)
reset_level_button.tooltip_text="清空本关全部方块、小球和球洞;可撤销恢复。"
actions.add_child(reset_level_button)
col.add_child(label("关卡文件",17))
name_field=LineEdit.new();name_field.text="my_level";name_field.placeholder_text="关卡名称"
col.add_child(name_field);editable.append(name_field)
actions=row(col);actions.add_child(button("保存",save_named,false,true));actions.add_child(button("载入",load_named,false,true))
file_list=OptionButton.new();col.add_child(file_list);refresh_files();editable.append(file_list)
file_list.item_selected.connect(func(i):name_field.text=file_list.get_item_text(i))
col.add_child(button("新建空白",clear_level,false,true))
col.add_child(label("放置模式:点顶面向上叠放,点侧面拼接
选择模式:点方块后修改坐标并应用
铁球 Y 为球心高度
橡皮擦:单击方块立即删除
重置:清空本关全部内容
小球 Y 为球心高度
R 旋转 · Del 删除 · F 全景
空格试跑,T 回到原布局
右键旋转 / 中键平移 / 滚轮缩放",13,Color("7a7366")))
refresh_palette()
func kind_name(id: String) -> String:
if id=="iron_ball":return "铁球"
if id in lib.ball_type_ids():return lib.ball_type(id).get("displayName",id)
if id in IDS:return TITLES[IDS.find(id)]
for group in entity_registry.values():
for item in group:
@@ -301,12 +318,13 @@ func kind_name(id: String) -> String:
return "未选择方块"
func kinds_in_category(id: String) -> Array:
if id=="balls":return ["iron_ball"]
if id=="balls":return lib.ball_type_ids()
if id=="chutes":return IDS.duplicate()
return entity_registry[id].map(func(item):return item.id)
func select_category(id: String) -> void:
if started:return
if block_mode=="erase":block_mode="place"
category=id
if id=="balls":
selected_id=""
@@ -323,14 +341,25 @@ func refresh_palette() -> void:
if options.is_empty():palette_box.add_child(label("暂无交互方块 · 待添加",14,Color("7a7366")))
for id in options:
var chosen: String=id
palette_box.add_child(button(kind_name(id)+" ∞",func():
var option:=button(kind_name(id)+" ∞",func():
if started:return
if category=="balls":refresh_ball_marker()
else:kind=chosen;refresh_selection(),false,true))
if category=="balls":select_ball_type(chosen)
else:
kind=chosen
if block_mode=="erase":block_mode="place"
refresh_palette();refresh_selection(),false,true)
option.toggle_mode=true
option.button_pressed=block_mode!="erase" and (id==ball_kind if category=="balls" else id==kind)
if category=="balls":
option.tooltip_text={"wood_ball":"原木球:容易被风吹动","ceramic_ball":"陶瓷球:330 kg,在当前水域中下沉","iron_ball":"铁球:最重,普通风下难以推动"}[id]
elif id=="water":option.tooltip_text="水体可穿入;泡沫顺流漂动,水速固定 5 m/s。R 顺时针旋转 90°。"
elif id=="ice":option.tooltip_text="低摩擦冰面;强撞击会破碎,静置承重不触发。"
elif id=="wall":option.tooltip_text="实心墙壁;高摩擦、高滚动阻力,也可搭建河床和河岸。"
palette_box.add_child(option)
for id in category_buttons:category_buttons[id].button_pressed=id==category
if block_controls!=null:block_controls.visible=category!="balls"
if ball_controls!=null:ball_controls.visible=category=="balls"
ball_marker.visible=category=="balls"
if block_controls!=null:block_controls.visible=category!="balls" and block_mode!="erase"
if ball_controls!=null:ball_controls.visible=category=="balls" and block_mode!="erase"
ball_marker.visible=category=="balls" and block_mode!="erase"
func category_for(id: String) -> String:
if id in IDS:return "chutes"
@@ -350,15 +379,26 @@ func valid_vector(value: Variant) -> bool:
func apply_data(data: Dictionary) -> void:
var normalized: Dictionary=codec.normalize(data)
records=normalized.records;spawn_specs=normalized.balls
face_pick.clear()
selected_id="";selected_ball_id="";block_mode="place"
if palette_box!=null:refresh_palette()
rebuild();reset_simulation();frame_level()
func reset_level() -> void:
if not editor_mode:return
# Reset stops the current trial and clears authored content in one action.
started=false;paused=false
category="chutes";kind="";quarter=0
clear_level()
func clear_level() -> void:
if started:return
remember();records.clear();spawn_specs.clear()
selected_id="";selected_ball_id="";block_mode="place";cursor=Vector3.ZERO;ball_cursor=Vector3(0,2,0)
face_pick.clear()
selected_id="";selected_ball_id="";last_placed_id="";block_mode="place";cursor=Vector3.ZERO;ball_cursor=Vector3(0,2,0)
if palette_box!=null:refresh_palette()
rebuild();reset_simulation();frame_level()
status="空白关卡:选择方块分类或添加悬空铁球。";refresh_ui()
status="已重置:关卡内容已清空。";refresh_ui()
func remember() -> void:
undo_stack.append(encode());redo_stack.clear()
@@ -391,28 +431,41 @@ func placement_error(candidate: Dictionary, ignore: String="") -> String:
return ""
func place_current() -> void:
if started or not editor_mode or category=="balls":return
if started or not editor_mode or category=="balls" or block_mode=="erase":return
if kind.is_empty():status="此分类暂无可用方块。";refresh_ui();return
# Adding never overwrites or moves an existing record, even if it was selected.
var record: Dictionary=placement.record_at(kind,cursor,quarter,unique_id("piece"))
var issue:=placement_error(record)
if not issue.is_empty():status=issue;refresh_ui();return
remember();records.append(record);last_placed_id=record.id
face_pick.clear()
cursor=placement.bottom_of(record);selected_id="";selected_ball_id="";block_mode="place"
rebuild();reset_simulation()
status="已新增 %s · 底面 Y %.2f;点顶面可继续叠放" % [kind_name(kind),cursor.y];refresh_ui()
func set_block_mode(mode: String) -> void:
if started or not editor_mode:return
if mode not in ["place","edit","erase"]:return
block_mode=mode;selected_id="";selected_ball_id=""
status="点方块顶面或侧面选择新位置,再放置。" if mode=="place" else "点击要修改的方块,再调整坐标并应用修改。"
refresh_selection()
face_pick.clear()
status={"place":"点方块顶面或侧面选择新位置,再放置。", "edit":"点击要修改的方块,再调整坐标并应用修改。", "erase":"橡皮擦已选中:单击方块立即删除,点击素材退出。"}[mode]
refresh_palette();refresh_selection();refresh_ball_marker()
func erase_block(id: String) -> void:
if started or not editor_mode or block_mode!="erase":return
var index:=index_for(id)
if index<0:return
var title:=kind_name(records[index].kind)
selected_id=id;selected_ball_id=""
remove_current()
status="已擦除 %s · 可继续点击其他方块"%title;refresh_ui()
func select_record(id: String) -> void:
if started:return
var index:=index_for(id)
if index<0:return
block_mode="edit";selected_ball_id="";selected_id=id
face_pick.clear()
var record: Dictionary=records[index]
cursor=placement.bottom_of(record);kind=record.kind;quarter=record.quarter
category=category_for(kind);refresh_palette();refresh_selection()
@@ -429,7 +482,7 @@ func apply_selected() -> void:
rebuild();reset_simulation();status="已修改选中方块;其他方块保持原位。";refresh_ui()
func rotate_current() -> void:
if started or not editor_mode or category=="balls":return
if started or not editor_mode or category=="balls" or block_mode=="erase":return
var old:=quarter;quarter=(quarter+1)%4
var idx:=index_for(selected_id) if block_mode=="edit" else -1
if idx>=0:
@@ -445,20 +498,20 @@ func remove_current() -> void:
var idx:=index_for(selected_id)
if idx<0:return
remember();records.remove_at(idx)
selected_id="";rebuild();reset_simulation();status="方块已移除,铁球坐标保持不变。";refresh_ui()
selected_id="";rebuild();reset_simulation();status="方块已移除,小球坐标保持不变。";refresh_ui()
func add_spawn() -> void:
if started or not editor_mode:return
remember();selected_ball_id=unique_id("iron");selected_id=""
spawn_specs.append({"id":selected_ball_id,"ballTypeId":"iron_ball","position":codec.array_from(ball_cursor),"launchDirection":codec.array_from(Vector3.RIGHT)})
reset_simulation();status="已按球心坐标放置铁球,可悬空。";refresh_ui()
if started or not editor_mode or block_mode=="erase":return
remember();selected_ball_id=unique_id(ball_kind);selected_id=""
spawn_specs.append({"id":selected_ball_id,"ballTypeId":ball_kind,"position":codec.array_from(ball_cursor),"launchDirection":codec.array_from(Vector3.RIGHT)})
reset_simulation();status="已按球心坐标放置小球,可悬空。";refresh_ui()
func refresh_ball_pick_hint() -> void:
if ball_pick_hint==null:return
ball_pick_hint.text="点击场景选位置,再点“放置铁球”。\n滑槽会自动对准槽内。" if ball_pick_mode.selected==0 else "先设置球心 Y,再点击场景选 X/Z,\n最后点“放置铁球”。"
ball_pick_hint.text="点击场景选位置,再点“放置小球”。\n滑槽会自动对准槽内。" if ball_pick_mode.selected==0 else "先设置球心 Y,再点击场景选 X/Z,\n最后点“放置小球”。"
func ball_position_is_clear(at: Vector3) -> bool:
var shape:=SphereShape3D.new();shape.radius=float(lib.catalog.iron_ball.radius)
var shape:=SphereShape3D.new();shape.radius=lib.ball_radius(ball_kind)
var query:=PhysicsShapeQueryParameters3D.new();query.shape=shape
query.transform=Transform3D(Basis.IDENTITY,at);query.collision_mask=1;query.margin=0
return get_world_3d().direct_space_state.intersect_shape(query).is_empty()
@@ -497,7 +550,7 @@ func ball_position_on_chute(hit: Dictionary) -> Variant:
func pick_ball_position(origin: Vector3,direction: Vector3,hit: Dictionary) -> void:
var candidate: Variant=null
var radius: float=lib.catalog.iron_ball.radius
var radius: float=lib.ball_radius(ball_kind)
if ball_pick_mode.selected==1:
candidate=Plane(Vector3.UP,ball_cursor.y).intersects_ray(origin,direction)
elif not hit.is_empty():
@@ -512,15 +565,23 @@ func pick_ball_position(origin: Vector3,direction: Vector3,hit: Dictionary) -> v
if candidate==null:return
var rounded: Vector3=Vector3(candidate).snapped(Vector3.ONE*.01)
if not ball_position_is_clear(rounded):
status="这里容不下铁球;请换个位置或提高球心 Y。";refresh_ui();return
status="这里容不下小球;请换个位置或提高球心 Y。";refresh_ui();return
ball_cursor=rounded
refresh_ball_marker()
status="已选球心 (%.2f, %.2f, %.2f) · 点放置新增,或移动选中铁球" % [ball_cursor.x,ball_cursor.y,ball_cursor.z]
status="已选球心 (%.2f, %.2f, %.2f) · 点放置新增,或移动选中小球" % [ball_cursor.x,ball_cursor.y,ball_cursor.z]
refresh_ui()
func select_ball_type(id: String) -> void:
if started or id not in lib.ball_type_ids():return
block_mode="place"
ball_kind=id;selected_ball_id="";selected_id="";category="balls"
refresh_palette();refresh_selection();refresh_ball_marker()
func select_ball(index: int) -> void:
if started or index<0 or index>=spawn_specs.size():return
block_mode="place"
selected_ball_id=spawn_specs[index].id;selected_id=""
ball_kind=spawn_specs[index].get("ballTypeId","iron_ball")
category="balls";refresh_palette()
if sidebar_scroll!=null:sidebar_scroll.scroll_vertical=0
ball_cursor=lib.vector(spawn_specs[index].position)
@@ -537,19 +598,19 @@ func ball_index() -> int:
func move_spawn() -> void:
if started:return
var index:=ball_index()
if index<0:status="先选择一颗铁球。";refresh_ui();return
if index<0:status="先选择一颗小球。";refresh_ui();return
remember();spawn_specs[index].position=codec.array_from(ball_cursor)
reset_simulation();status="已移动铁球球心。";refresh_ui()
reset_simulation();status="已移动小球球心。";refresh_ui()
func refresh_ball_marker() -> void:
clear_children(ball_marker)
if not editor_mode or started or category!="balls":return
if not editor_mode or started or category!="balls" or block_mode=="erase":return
syncing=true
for i in range(ball_position_fields.size()):ball_position_fields[i].value=ball_cursor[i]
syncing=false
var index:=ball_index()
if index>=0 and lib.vector(spawn_specs[index].position).is_equal_approx(ball_cursor):return
var preview: Node3D=load(lib.catalog.iron_ball.path).instantiate()
var preview: Node3D=load(lib.ball_model(ball_kind).path).instantiate()
preview.position=ball_cursor;ball_marker.add_child(preview);ghost(preview)
if absf(ball_cursor.y)>.02:
box(Vector3(.012,absf(ball_cursor.y),.012),Vector3(ball_cursor.x,ball_cursor.y*.5,ball_cursor.z),Color("649e82"),ball_marker)
@@ -578,24 +639,26 @@ func rebuild() -> void:
refresh_ground();refresh_selection()
func reset_simulation() -> void:
for piece in pieces:
if piece.has_method("reset_block"):piece.reset_block()
clear_children(actors);balls.clear();tags.clear();previous.clear()
entered.clear();lost.clear();events.clear();samples.clear();visited.clear();contact_visited.clear();release_ticks.clear();pause_states.clear()
elapsed=0;started=false;paused=false
for spec in spawn_specs:
var ball=lib.create_ball();ball.freeze=true;ball.name=spec.id;ball.set_meta("ball_id",spec.id)
var ball=lib.create_ball(spec.get("ballTypeId","iron_ball"));ball.freeze=true;ball.name=spec.id;ball.set_meta("ball_id",spec.id)
actors.add_child(ball);ball.position=lib.vector(spec.position)
balls.append(ball);previous.append(ball.position)
var tag:=Label3D.new();tag.text=str(balls.size());tag.font=font;tag.font_size=42
tag.pixel_size=.006;tag.modulate=ACCENT;tag.outline_size=3;tag.outline_modulate=PAPER
tag.billboard=BaseMaterial3D.BILLBOARD_ENABLED;actors.add_child(tag);tags.append(tag)
refresh_ball_marker()
status="准备释放 %d 颗铁球。" % balls.size();refresh_ui()
status="准备释放 %d 颗小球。" % balls.size();refresh_ui()
func initial_velocity(_index: int) -> Vector3:
return Vector3.ZERO
func toggle_run() -> void:
if balls.is_empty():status="请先添加至少一个铁球起点。";refresh_ui();return
if balls.is_empty():status="请先添加至少一个小球起点。";refresh_ui();return
if not started:
started=true;paused=false;clear_children(ball_marker)
for i in range(balls.size()):
@@ -639,7 +702,7 @@ func _physics_process(delta: float) -> void:
for b in balls:positions.append([b.position.x,b.position.y,b.position.z])
samples.append({"time":elapsed,"positions":positions,"entered":entered.size()})
if entered.size()==balls.size():status="全部入洞! %.2f 秒" % elapsed
elif not lost.is_empty():status="有铁球离轨。按 T 回到编辑。"
elif not lost.is_empty():status="有小球离轨。按 T 回到编辑。"
else:status="运行中 · %.1f 秒" % elapsed
refresh_ui()
@@ -666,24 +729,36 @@ func refresh_ui() -> void:
elif control is SpinBox or control is LineEdit:control.editable=not started
elif control is Range:control.editable=not started
if editor_mode:
boost_controls.visible=category!="balls" and kind==Booster.KIND
var erasing:=block_mode=="erase"
block_controls.visible=category!="balls" and not erasing
ball_controls.visible=category=="balls" and not erasing
eraser_hint.visible=erasing
ball_marker.visible=category=="balls" and not erasing and not started
boost_controls.visible=category!="balls" and kind==Booster.KIND and not erasing
for mode in mode_buttons:mode_buttons[mode].button_pressed=mode==block_mode
if apply_button!=null:apply_button.disabled=started or block_mode!="edit" or index_for(selected_id)<0
if delete_button!=null:delete_button.disabled=started or (index_for(selected_id)<0 and ball_index()<0)
if place_button!=null:place_button.disabled=started or kind.is_empty()
if place_button!=null:place_button.disabled=started or kind.is_empty() or erasing
if move_ball_button!=null:move_ball_button.disabled=started or ball_index()<0
if add_ball_button!=null:add_ball_button.text="+ 放置"+kind_name(ball_kind)
if ball_type_label!=null:ball_type_label.text="%s · %.0f kg · 球心坐标"%[kind_name(ball_kind),lib.physics.physicsProfiles[lib.ball_type(ball_kind).physicsProfileId].massKg]
ball_list.clear()
for i in range(spawn_specs.size()):
var p: Array=spawn_specs[i].position
ball_list.add_item("铁球 %d · (%.2f, %.2f, %.2f)" % [i+1,p[0],p[1],p[2]])
ball_list.add_item("%s %d · (%.2f, %.2f, %.2f)" % [kind_name(spawn_specs[i].get("ballTypeId","iron_ball")),i+1,p[0],p[1],p[2]])
ball_list.select(ball_index())
if detail_label!=null:detail_label.text="%s · %d° · %d 块" % [kind_name(kind),quarter*90,records.size()]
if detail_label!=null and kind=="water":detail_label.text+=" · 固定水速 5 m/s"
func refresh_selection() -> void:
clear_children(selection)
if not editor_mode:return
if block_mode=="erase":refresh_grid();refresh_ui();return
if category=="balls":refresh_grid();refresh_ui();return
if kind.is_empty():refresh_grid();refresh_ui();return
if block_mode=="place" and not face_pick.is_empty():
var support_index:=index_for(face_pick.id)
if support_index>=0:cursor=placement.on_face(records[support_index],face_pick.point,face_pick.normal,kind,quarter,records)
var candidate: Dictionary=placement.record_at(kind,cursor,quarter,"preview")
var bounds: AABB=lib.record_bounds(candidate)
var center: Vector3=bounds.get_center();center.y=bounds.position.y
@@ -702,12 +777,16 @@ func refresh_selection() -> void:
if issue.is_empty() and not editing_unchanged:
var preview: Node3D=load(lib.catalog[kind].path).instantiate()
preview.position=lib.record_center(candidate);preview.rotation.y=-quarter*PI/2
selection.add_child(preview);ghost(preview)
selection.add_child(preview)
if kind == "water":
Library.WaterSurface.apply(preview, lib.vector(lib.catalog[kind].flowDirectionGodot))
else:
ghost(preview)
if absf(center.y)>.01:
box(Vector3(.012,absf(center.y),.012),Vector3(center.x,center.y*.5,center.z),tint,selection)
syncing=true
for i in range(position_fields.size()):
position_fields[i].step=.01 if kind=="goal_hole" else (.5 if i==1 else 1)
position_fields[i].step=.01 if kind=="goal_hole" or i==1 else 1
position_fields[i].value=cursor[i]
syncing=false
refresh_grid();refresh_ui()
@@ -799,8 +878,15 @@ func update_camera() -> void:
func select_at(screen: Vector2) -> void:
if started or not editor_mode:return
var origin:=camera.project_ray_origin(screen);var dir:=camera.project_ray_normal(screen)
var query:=PhysicsRayQueryParameters3D.create(origin,origin+dir*300,3)
var query:=PhysicsRayQueryParameters3D.create(origin,origin+dir*300,11)
query.collide_with_areas=true
var hit:=get_world_3d().direct_space_state.intersect_ray(query)
if block_mode=="erase":
if not hit.is_empty():
var target_node: Node=hit.collider
while target_node!=null and not target_node.has_meta("record_id"):target_node=target_node.get_parent()
if target_node!=null:erase_block(str(target_node.get_meta("record_id")))
return
if not hit.is_empty():
var node: Node=hit.collider
if node.has_meta("ball_id"):
@@ -812,13 +898,15 @@ func select_at(screen: Vector2) -> void:
var id: String=node.get_meta("record_id")
if block_mode=="edit":select_record(id);return
if kind.is_empty():status="先选择要放置的方块。";refresh_ui();return
cursor=placement.on_face(records[index_for(id)],hit.position,hit.normal,kind,quarter)
face_pick={"id":id,"point":hit.position,"normal":hit.normal}
cursor=placement.on_face(records[index_for(id)],hit.position,hit.normal,kind,quarter,records)
selected_id="";selected_ball_id=""
status="新方块底面 Y %.2f · 原方块保持不动" % cursor.y
refresh_selection();return
if category=="balls":pick_ball_position(origin,dir,{});return
var p=Plane(Vector3.UP,0).intersects_ray(origin,dir)
if p!=null:
face_pick.clear()
cursor=Vector3(round(p.x),0,round(p.z));selected_id="";selected_ball_id=""
refresh_selection()
+1 -1
View File
@@ -5,5 +5,5 @@ func _init() -> void:
func initialize_level() -> void:
apply_data({"schemaVersion":2,"pieces":[],"entities":[],"balls":[]})
status="空白关卡:选择方块分类或添加悬空铁球。"
status="空白关卡:水块/墙壁在非交互方块;冰块在交互方块。"
refresh_ui()