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

102 lines
5.0 KiB
GDScript

extends RefCounted
# The editor always works in bottom-face coordinates; imported mesh origins stay intact.
var lib
func _init(library) -> void:lib=library
func bottom_of(record: Dictionary) -> Vector3:
return record.position+Vector3.UP*float(lib.catalog[record.kind].boundsBlender.min[2])
func record_at(kind: String,bottom: Vector3,quarter: int,id: String) -> Dictionary:
var at:=bottom
# 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,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]
var result:=Vector3(round(point.x),bounds.position.y,round(point.z))
var axis:=normal.abs().max_axis_index()
if axis==1:
result.y=bounds.end.y if normal.y>=0 else bounds.position.y-height
elif axis==0:
result.x=bounds.end.x+.5 if normal.x>=0 else bounds.position.x+.5-size.x
else:
result.z=bounds.end.z+.5 if normal.z>=0 else bounds.position.z+.5-size.y
if kind!="goal_hole":
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