extends Node3D ## Peça reaproveitável: pessoa estilizada (low-poly) com juntas e animações por tempo. ## ## const Person := preload("res://kits/person.gd") ## var p := Person.new() ## p.build({"shirt": "#2f6fb3", "pants": "#3a3a3a", "skin": "#c68e6a", "hair": "#2b1d14", "height": 1.75}) ## # opções: "sleeveless": true (regata, braço de fora), "shorts": true (bermuda), "vest", "shoes" ## add_child(p) ## # em apply(t): ## p.pose("walk", t) # parado | andar | correr | nadar | sentar | acenar | ajoelhar | deitar ## p.blend("walk", "run", t, 0.3) # mistura duas poses (0 = a primeira, 1 = a segunda) ## ## Origem nos pés (y = 0 é o chão), frente em -Z. Tudo depende só de t: determinístico. ## Poses: "idle", "walk", "run", "swim", "sit", "wave", "kneel", "lie", "exhausted". var joints := {} # nome → Node3D (hips, spine, chest, neck, head, shoulder_l/r, elbow_l/r, hip_l/r, knee_l/r) var height := 1.75 var _s := 1.0 # escala (altura / 1,75) var _materials := {} func build(opts: Dictionary = {}) -> void: height = float(opts.get("height", 1.75)) _s = height / 1.75 var shirt := Color(str(opts.get("shirt", "#2f6fb3"))) var pants := Color(str(opts.get("pants", "#34363a"))) var skin := Color(str(opts.get("skin", "#c68e6a"))) var hair := Color(str(opts.get("hair", "#2b1d14"))) var shoes := Color(str(opts.get("shoes", "#1c1c1c"))) var vest = opts.get("vest") # colete salva-vidas, ex.: "#ff6a00" (opcional) var arm := skin if bool(opts.get("sleeveless", false)) else shirt # regata: braço de fora var shin := skin if bool(opts.get("shorts", false)) else pants # bermuda: canela de fora var hips := _joint("hips", self, Vector3(0, 0.95, 0)) var spine := _joint("spine", hips, Vector3(0, 0.08, 0)) var chest := _joint("chest", spine, Vector3(0, 0.28, 0)) var neck := _joint("neck", chest, Vector3(0, 0.22, 0)) var head := _joint("head", neck, Vector3(0, 0.1, 0)) # Tronco e quadril. _capsule(hips, 0.17, 0.32, pants, Vector3(0, -0.02, 0), Vector3(0, 0, PI / 2)) _capsule(spine, 0.16, 0.42, shirt, Vector3(0, 0.16, 0)) _capsule(chest, 0.19, 0.36, shirt, Vector3(0, 0.06, 0), Vector3(0, 0, PI / 2)) if vest != null: _box(chest, Vector3(0.44, 0.36, 0.26), Color(str(vest)), Vector3(0, -0.02, 0)) _capsule(neck, 0.05, 0.12, skin, Vector3(0, 0.02, 0)) _sphere(head, 0.12, skin, Vector3(0, 0.1, 0)) _sphere(head, 0.125, hair, Vector3(0, 0.15, 0.02), Vector3(1.0, 0.62, 1.0)) # cabelo _sphere(head, 0.02, Color("#1a1a1a"), Vector3(-0.045, 0.11, -0.11)) # olhos _sphere(head, 0.02, Color("#1a1a1a"), Vector3(0.045, 0.11, -0.11)) # Braços: ombro → cotovelo → mão. for side in ["l", "r"]: var sx := -1.0 if side == "l" else 1.0 var shoulder := _joint("shoulder_" + side, chest, Vector3(0.22 * sx, 0.14, 0)) _capsule(shoulder, 0.055, 0.3, arm, Vector3(0, -0.14, 0)) var elbow := _joint("elbow_" + side, shoulder, Vector3(0, -0.29, 0)) _capsule(elbow, 0.047, 0.27, skin, Vector3(0, -0.13, 0)) _sphere(elbow, 0.05, skin, Vector3(0, -0.28, 0)) # Pernas: quadril → joelho → pé. var hip := _joint("hip_" + side, hips, Vector3(0.1 * sx, -0.05, 0)) _capsule(hip, 0.08, 0.44, pants, Vector3(0, -0.21, 0)) var knee := _joint("knee_" + side, hip, Vector3(0, -0.43, 0)) _capsule(knee, 0.065, 0.42, shin, Vector3(0, -0.2, 0)) _box(knee, Vector3(0.1, 0.07, 0.24), shoes, Vector3(0, -0.43, -0.05)) scale = Vector3.ONE * _s pose("idle", 0.0) ## Aplica uma pose no instante t (segundos). Ver lista no topo. func pose(action: String, t: float, amount := 1.0) -> void: var r := _pose_values(action, t) for k in r.keys(): if k == "root_y" or k == "root_rx" or k == "root_rz": continue var j: Node3D = joints[k] j.rotation = r[k] * amount joints.hips.position = Vector3(0, 0.95 + float(r.get("root_y", 0.0)) * amount, 0) joints.hips.rotation.x = float(r.get("root_rx", 0.0)) * amount joints.hips.rotation.z = float(r.get("root_rz", 0.0)) * amount ## Mistura duas poses (k de 0 a 1), por exemplo "walk" → "kneel" quando o personagem chega. func blend(a: String, b: String, t: float, k: float) -> void: var ra := _pose_values(a, t) var rb := _pose_values(b, t) var keys := {} for key in ra.keys(): keys[key] = true for key in rb.keys(): keys[key] = true for key in keys.keys(): var va = ra.get(key, Vector3.ZERO if not key.begins_with("root") else 0.0) var vb = rb.get(key, Vector3.ZERO if not key.begins_with("root") else 0.0) var v = lerp(va, vb, clampf(k, 0.0, 1.0)) if key == "root_y": joints.hips.position = Vector3(0, 0.95 + float(v), 0) elif key == "root_rx": joints.hips.rotation.x = float(v) elif key == "root_rz": joints.hips.rotation.z = float(v) else: (joints[key] as Node3D).rotation = v func _pose_values(action: String, t: float) -> Dictionary: var v := {} for k in joints.keys(): v[k] = Vector3.ZERO match action: "walk", "run": var run := action == "run" var w := t * (8.5 if run else 5.6) var amp := 0.85 if run else 0.5 v.hip_l = Vector3(sin(w) * amp, 0, 0) v.hip_r = Vector3(-sin(w) * amp, 0, 0) v.knee_l = Vector3(maxf(0.0, -sin(w + 0.9)) * (1.4 if run else 0.9), 0, 0) v.knee_r = Vector3(maxf(0.0, sin(w + 0.9)) * (1.4 if run else 0.9), 0, 0) v.shoulder_l = Vector3(-sin(w) * amp * 0.9, 0, 0.08) v.shoulder_r = Vector3(sin(w) * amp * 0.9, 0, -0.08) v.elbow_l = Vector3(-(1.3 if run else 0.35), 0, 0) v.elbow_r = Vector3(-(1.3 if run else 0.35), 0, 0) v.spine = Vector3(-(0.22 if run else 0.05), 0, 0) v.root_y = absf(sin(w)) * (0.06 if run else 0.025) - (0.04 if run else 0.0) "swim": # Crawl: corpo deitado na água, braços em círculo alternado, pernas batendo. var w := t * 3.2 v.root_rx = -PI / 2.0 + 0.1 v.root_y = -0.85 v.shoulder_l = Vector3(w, 0, 0.15) v.shoulder_r = Vector3(w + PI, 0, -0.15) v.elbow_l = Vector3(-0.3, 0, 0) v.elbow_r = Vector3(-0.3, 0, 0) v.hip_l = Vector3(sin(w * 3.0) * 0.3, 0, 0) v.hip_r = Vector3(-sin(w * 3.0) * 0.3, 0, 0) v.neck = Vector3(0.5, sin(w) * 0.5, 0) v.root_rz = sin(w) * 0.25 "sit": v.root_y = -0.5 v.hip_l = Vector3(-PI / 2.0 + 0.1, 0, 0.08) v.hip_r = Vector3(-PI / 2.0 + 0.1, 0, -0.08) v.knee_l = Vector3(PI / 2.0 - 0.2, 0, 0) v.knee_r = Vector3(PI / 2.0 - 0.2, 0, 0) v.shoulder_l = Vector3(-0.4, 0, 0.1) v.shoulder_r = Vector3(-0.4, 0, -0.1) v.elbow_l = Vector3(-0.8, 0, 0) v.elbow_r = Vector3(-0.8, 0, 0) v.spine = Vector3(-0.25 + sin(t * 1.6) * 0.03, 0, 0) v.neck = Vector3(0.25, 0, 0) "wave": v.shoulder_r = Vector3(-PI + 0.3, 0, -0.3) v.elbow_r = Vector3(-0.5 + sin(t * 9.0) * 0.45, 0, 0) v.shoulder_l = Vector3(0.05, 0, 0.08) v.spine = Vector3(0.04, 0, 0) v.root_y = absf(sin(t * 4.5)) * 0.02 "kneel": v.root_y = -0.42 v.hip_l = Vector3(-PI / 2.0 + 0.2, 0, 0.05) v.knee_l = Vector3(PI / 2.0 - 0.1, 0, 0) v.hip_r = Vector3(0.2, 0, -0.05) v.knee_r = Vector3(PI / 2.0 + 0.3, 0, 0) v.spine = Vector3(-0.35, 0, 0) v.shoulder_l = Vector3(-0.6, 0, 0.1) v.shoulder_r = Vector3(-0.6, 0, -0.1) v.elbow_l = Vector3(-0.6, 0, 0) v.elbow_r = Vector3(-0.6, 0, 0) "exhausted": # Curvado, mãos nos joelhos, respirando fundo. var b := sin(t * 2.4) * 0.04 v.spine = Vector3(-0.75 + b, 0, 0) v.chest = Vector3(-0.15, 0, 0) v.hip_l = Vector3(-0.35, 0, 0.06) v.hip_r = Vector3(-0.35, 0, -0.06) v.knee_l = Vector3(0.55, 0, 0) v.knee_r = Vector3(0.55, 0, 0) v.shoulder_l = Vector3(-0.55, 0, 0.12) v.shoulder_r = Vector3(-0.55, 0, -0.12) v.elbow_l = Vector3(-0.25, 0, 0) v.elbow_r = Vector3(-0.25, 0, 0) v.neck = Vector3(0.45, 0, 0) v.root_y = -0.14 "lie": # Deitado de costas, exausto (respiração visível). v.root_rx = PI / 2.0 v.root_y = -0.82 v.chest = Vector3(sin(t * 2.0) * 0.04, 0, 0) v.shoulder_l = Vector3(0.2, 0, 0.6) v.shoulder_r = Vector3(0.2, 0, -0.6) v.hip_r = Vector3(-0.3, 0, 0) v.knee_r = Vector3(0.6, 0, 0) _: # idle: respiração e peso mudando de perna. v.chest = Vector3(sin(t * 1.8) * 0.025, 0, 0) v.spine = Vector3(0, sin(t * 0.6) * 0.04, 0) v.shoulder_l = Vector3(0.05, 0, 0.08) v.shoulder_r = Vector3(0.05, 0, -0.08) v.elbow_l = Vector3(-0.15, 0, 0) v.elbow_r = Vector3(-0.15, 0, 0) return v # ------------------------------------------------------------------ montagem func _joint(joint_name: String, parent: Node3D, offset: Vector3) -> Node3D: var j := Node3D.new() j.name = joint_name j.position = offset parent.add_child(j) joints[joint_name] = j return j func _material(color: Color) -> StandardMaterial3D: var key := color.to_html() if not _materials.has(key): var m := StandardMaterial3D.new() m.albedo_color = color m.roughness = 0.8 _materials[key] = m return _materials[key] func _capsule(parent: Node3D, radius: float, length: float, color: Color, pos: Vector3, rot := Vector3.ZERO) -> MeshInstance3D: var mesh := CapsuleMesh.new() mesh.radius = radius mesh.height = maxf(length, radius * 2.0) mesh.radial_segments = 10 mesh.rings = 4 return _add(parent, mesh, color, pos, rot) func _sphere(parent: Node3D, radius: float, color: Color, pos: Vector3, scl := Vector3.ONE) -> MeshInstance3D: var mesh := SphereMesh.new() mesh.radius = radius mesh.height = radius * 2.0 mesh.radial_segments = 12 mesh.rings = 6 var mi := _add(parent, mesh, color, pos) mi.scale = scl return mi func _box(parent: Node3D, size: Vector3, color: Color, pos: Vector3) -> MeshInstance3D: var mesh := BoxMesh.new() mesh.size = size return _add(parent, mesh, color, pos) func _add(parent: Node3D, mesh: Mesh, color: Color, pos: Vector3, rot := Vector3.ZERO) -> MeshInstance3D: var mi := MeshInstance3D.new() mi.mesh = mesh mi.material_override = _material(color) mi.position = pos mi.rotation = rot parent.add_child(mi) return mi