Files
Diabolo/Demon_v0.19/mesh_walking.gd
T

65 lines
3.2 KiB
GDScript

extends RefCounted
# Physics uses the rendered triangles; the grid is only a route planner.
var g
var probe: CharacterBody3D
var shape: CapsuleShape3D
var shape_node: CollisionShape3D
var outlines=[]
func setup(game):
g=game
probe=CharacterBody3D.new();g.add_child(probe)
probe.collision_layer=0;probe.collision_mask=2
probe.motion_mode=CharacterBody3D.MOTION_MODE_FLOATING
shape=CapsuleShape3D.new();shape.radius=.30;shape.height=1.65
shape_node=CollisionShape3D.new();shape_node.shape=shape;shape_node.position.y=1.02;probe.add_child(shape_node)
func build(root):
for mesh in root.find_children("*","MeshInstance3D",true,false):
# Internal lining/embossing is enclosed by the real exterior shell.
if mesh.get_meta("no_walk_collision",false):continue
if not mesh.is_visible_in_tree() or mesh.mesh==null:continue
if mesh.mesh is QuadMesh:continue
if mesh.material_override is ShaderMaterial and mesh.material_override.shader in [preload("res://surface_water.gdshader"),preload("res://boiling.gdshader"),preload("res://leather.gdshader")]:continue
var bounds=mesh.get_aabb()
if bounds.size.length()<.24:continue
var body=StaticBody3D.new();body.name="MeshWalkingCollider";body.collision_layer=2;body.collision_mask=0
mesh.add_child(body)
var collider=CollisionShape3D.new()
collider.shape=mesh.get_meta("walk_collision_shape") if mesh.has_meta("walk_collision_shape") else mesh.mesh.create_trimesh_shape()
body.add_child(collider)
collider.shape.backface_collision=true
func rebuild(stations):
outlines.clear()
for s in stations:
for mesh in s.device.find_children("*","MeshInstance3D",true,false):
if not mesh.has_node("MeshWalkingCollider"):continue
var points=PackedVector2Array()
var faces=mesh.mesh.get_faces()
for i in range(0,faces.size(),3):
var a=mesh.global_transform*faces[i];var b=mesh.global_transform*faces[i+1];var c=mesh.global_transform*faces[i+2]
if maxf(a.y,maxf(b.y,c.y))<.20 or minf(a.y,minf(b.y,c.y))>1.85:continue
for v in [a,b,c]:points.append(Vector2(v.x,v.z))
if points.size()>2:outlines.append(Geometry2D.convex_hull(points))
func blocked(point,radius=.32):
for polygon in outlines:
if Geometry2D.is_point_in_polygon(point,polygon):return true
for i in range(polygon.size()-1):
if point.distance_to(Geometry2D.get_closest_point_to_segment(point,polygon[i],polygon[i+1]))<radius:return true
return false
func move_actor(node,motion,radius=.30,height=1.65):
if motion.length_squared()<.0000001:return
if not is_equal_approx(shape.radius,radius):shape.radius=radius
var capsule_height=maxf(height,2*radius)
if not is_equal_approx(shape.height,capsule_height):shape.height=capsule_height
var offset=(.195 if height>1.4 else .10)+height/2
if not is_equal_approx(shape_node.position.y,offset):shape_node.position.y=offset
probe.position=node.position;probe.force_update_transform()
var remaining=motion
for i in range(4):
if remaining.length_squared()<.0000001:break
var hit=probe.move_and_collide(remaining,false,.005)
if not hit:break
var normal=hit.get_normal();normal.y=0
if normal.length_squared()<.0001:break
remaining=hit.get_remainder().slide(normal.normalized());remaining.y=0
node.position=Vector3(probe.position.x,node.position.y,probe.position.z)