extends Node3D ## Peça reaproveitável: lancha de pesca (~8 m) low-poly, com balanço e afundamento. ## ## const Boat := preload("res://kits/boat.gd") ## var b := Boat.new() ## b.build({"hull": "#f2f0ea", "stripe": "#1f5fa8", "cabin": "#e9e6df", "lights": true}) ## add_child(b) ## # em apply(t): ## b.float_on(t, 1.0) # balanço no mar (intensidade 0 = calmo, 1 = normal, 2 = agitado) ## b.sink(0.6) # 0 = flutuando; 1 = quase submerso (popa afunda primeiro, adernado) ## ## Origem no centro do casco na linha d'água (y = 0 é o nível do mar), proa em -Z. const LENGTH := 8.0 const BEAM := 2.6 var _body: Node3D var _sink := 0.0 var _materials := {} var _lights: Array[MeshInstance3D] = [] func build(opts: Dictionary = {}) -> void: var hull := Color(str(opts.get("hull", "#f2f0ea"))) var stripe := Color(str(opts.get("stripe", "#1f5fa8"))) var cabin := Color(str(opts.get("cabin", "#e9e6df"))) _body = Node3D.new() _body.name = "Body" add_child(_body) _build_hull(hull, stripe) # Convés. var deck := BoxMesh.new() deck.size = Vector3(BEAM * 0.86, 0.08, LENGTH * 0.78) _add(_body, deck, Color("#9a7b55"), Vector3(0, 0.62, 0.35)) # Cabine com janelas. var box := BoxMesh.new() box.size = Vector3(BEAM * 0.62, 1.3, 2.2) _add(_body, box, cabin, Vector3(0, 1.3, -0.4)) var roof := BoxMesh.new() roof.size = Vector3(BEAM * 0.7, 0.1, 2.5) _add(_body, roof, stripe, Vector3(0, 1.99, -0.4)) for i in 3: var win := BoxMesh.new() win.size = Vector3(BEAM * 0.63, 0.34, 0.5) _add(_body, win, Color("#1d2c3a"), Vector3(0, 1.5, -1.15 + i * 0.68)) var front := BoxMesh.new() front.size = Vector3(BEAM * 0.55, 0.4, 0.05) _add(_body, front, Color("#1d2c3a"), Vector3(0, 1.5, -1.52)) # Antena e mastro. var mast := CylinderMesh.new() mast.top_radius = 0.03 mast.bottom_radius = 0.04 mast.height = 2.0 _add(_body, mast, Color("#d8d8d8"), Vector3(0, 3.0, -0.2)) # Grade (amurada) dos dois lados. for sx in [-1.0, 1.0]: var rail := BoxMesh.new() rail.size = Vector3(0.04, 0.04, LENGTH * 0.6) _add(_body, rail, Color("#cfcfcf"), Vector3(sx * BEAM * 0.42, 1.0, 0.9)) # Luzes de navegação (bombordo vermelha, boreste verde, branca no mastro). if bool(opts.get("lights", true)): _lights.append(_light(Vector3(-BEAM * 0.36, 2.05, -1.2), Color("#ff3b30"))) _lights.append(_light(Vector3(BEAM * 0.36, 2.05, -1.2), Color("#34c759"))) _lights.append(_light(Vector3(0, 4.02, -0.2), Color("#fff6d8"))) ## Altura do convés no corpo do barco (pés de quem está a bordo). const DECK_Y := 0.66 ## Corpo do barco (balança e afunda): pessoas e objetos a bordo entram como filhos dele, ## com o pé em y = DECK_Y; popa em +Z (z de 0,8 a 3,4 livre atrás da cabine). func deck() -> Node3D: return _body ## Balanço determinístico no mar (sobe/desce, arfagem e jogo). func float_on(t: float, intensity := 1.0) -> void: var k := intensity _body.position = Vector3(0, sin(t * 1.3) * 0.18 * k - _sink * 1.9, 0) _body.rotation = Vector3( sin(t * 0.9 + 0.7) * 0.05 * k + _sink * 0.42, # popa afunda primeiro sin(t * 0.35) * 0.03 * k, sin(t * 1.1) * 0.07 * k + _sink * 0.35) # adernando ## 0 = flutuando; 1 = quase submerso. Chame antes de float_on no mesmo quadro. func sink(amount: float) -> void: _sink = clampf(amount, 0.0, 1.0) for l in _lights: l.visible = _sink < 0.75 func _build_hull(hull: Color, stripe: Color) -> void: # Casco: seções em "V" ao longo do comprimento, fechando na proa. var st := SurfaceTool.new() st.begin(Mesh.PRIMITIVE_TRIANGLES) var sections := 14 var rows: Array = [] for i in sections + 1: var u := float(i) / sections # 0 = popa, 1 = proa var z := LENGTH * 0.5 - u * LENGTH var width := BEAM * 0.5 * (1.0 - pow(maxf(u - 0.55, 0.0) / 0.45, 1.6)) width = maxf(width, 0.02) var keel := -0.75 + u * 0.15 var top := 0.65 + pow(u, 3.0) * 0.45 # proa mais alta rows.append([Vector3(-width, top, z), Vector3(-width * 0.8, 0.0, z), Vector3(0, keel, z), Vector3(width * 0.8, 0.0, z), Vector3(width, top, z)]) for i in sections: for k in 4: var a: Vector3 = rows[i][k] var b: Vector3 = rows[i][k + 1] var c: Vector3 = rows[i + 1][k] var d: Vector3 = rows[i + 1][k + 1] var col := stripe if (k == 0 or k == 3) and i < sections - 1 else hull for v in [a, c, b, b, c, d]: st.set_color(col) st.add_vertex(v) # Popa fechada. var r0: Array = rows[0] for k in [[0, 1, 2], [0, 2, 4], [2, 3, 4]]: for idx in k: st.set_color(hull) st.add_vertex(r0[idx]) st.generate_normals() var mat := StandardMaterial3D.new() mat.vertex_color_use_as_albedo = true mat.roughness = 0.55 mat.cull_mode = BaseMaterial3D.CULL_DISABLED var mi := MeshInstance3D.new() mi.mesh = st.commit() mi.material_override = mat _body.add_child(mi) func _light(pos: Vector3, color: Color) -> MeshInstance3D: var mesh := SphereMesh.new() mesh.radius = 0.07 mesh.height = 0.14 var mat := StandardMaterial3D.new() mat.albedo_color = color mat.emission_enabled = true mat.emission = color mat.emission_energy_multiplier = 4.0 var mi := MeshInstance3D.new() mi.mesh = mesh mi.material_override = mat mi.position = pos _body.add_child(mi) return mi func _add(parent: Node3D, mesh: Mesh, color: Color, pos: Vector3) -> MeshInstance3D: var key := color.to_html() if not _materials.has(key): var m := StandardMaterial3D.new() m.albedo_color = color m.roughness = 0.7 _materials[key] = m var mi := MeshInstance3D.new() mi.mesh = mesh mi.material_override = _materials[key] mi.position = pos parent.add_child(mi) return mi