Editor/WallPieces.cs

A static utility class for the editor providing functions to create and configure wall pieces and general model objects within the game environment. It handles the complex setup for kit model walls, including calculating shear and dimensions.

using Sandbox;
using System;

/// <summary>
/// What the two wall tools share: loading a kit model, standing a model object on a point, and the
/// Kit Model Wall piece that runs a wall model between two points (sheared on a slope). The tools
/// themselves are separate: Add bounds on Golf Hole Collision builds the tall perimeter around a
/// hole's tiles, the Course Wall Builder draws the low rails inside it.
/// </summary>
internal static class WallPieces
{
	/// <summary>The kit's concrete: the material on the Holekit rails and posts, and on Kit-style bounds.</summary>
	internal const string ConcretePath = "textures/tcom_concrete_base8_2x2_1k.vmat";

	/// <summary>A kit model read from disk, or null when it is missing or broken. Model.Load reads it from disk; ResourceLibrary only knows models something already loaded.</summary>
	internal static Model LoadModel( string path )
	{
		var model = Model.Load( path );
		return model is null || model.IsError ? null : model;
	}

	/// <summary>The kit concrete, or null (with a warning) when it is missing; a null material keeps a model's own.</summary>
	internal static Material LoadConcrete()
	{
		var material = Material.Load( ConcretePath );
		if ( material is null ) Log.Warning( $"Kit wall material {ConcretePath} not found; walls and posts keep the model's own material." );
		return material;
	}

	/// <summary>Bottom centre of a model's bounds.</summary>
	internal static Vector3 BottomCentre( BBox bounds ) => new( bounds.Center.x, bounds.Center.y, bounds.Mins.z );

	/// <summary>
	/// The point of a model that stands on the floor: the origin's height when the model was built
	/// around z 0 (the trench post reaches 20 below its origin, to sink into the floor), else the
	/// bounds' bottom centre. Always the xy centre of the bounds.
	/// </summary>
	internal static Vector3 StandOffset( BBox bounds )
		=> bounds.Mins.z <= 0f && bounds.Maxs.z > 0f ? new Vector3( bounds.Center.x, bounds.Center.y, 0f ) : BottomCentre( bounds );

	/// <summary>
	/// One wall model piece from one point to another, always a Kit Model Wall: the model's mesh
	/// rebuilt at the run's length (so the wall material repeats along the run instead of
	/// stretching with a scaled model) and, on a slope, sheared so the ends stay vertical and the
	/// top follows the ground line. Line Y is the model's local Y (in the wall frame, length along
	/// X) that sits on the line between the points: its bounds centre when not given. A material
	/// replaces the model's own; null keeps them. Along Y is on for a model built along Y (the
	/// trench wall). Returns the new object, or null for no run.
	/// </summary>
	internal static GameObject PlaceModelWall( GameObject parent, Model model, Vector3 from, Vector3 to, float? lineY, Material material, bool alongY = false, string name = "Wall" )
	{
		var run = to - from;
		var flat = run.WithZ( 0f );
		if ( flat.Length < 1f ) return null;
		var rotation = Rotation.LookAt( flat.Normal, Vector3.Up );

		var wall = new GameObject( true, name );
		if ( parent.IsValid() ) wall.SetParent( parent, false );
		wall.WorldPosition = from;
		wall.WorldRotation = rotation;
		var shear = wall.AddComponent<KitModelWall>();
		shear.Source = model;
		shear.Length = flat.Length;
		shear.Rise = run.z;
		shear.SourceAlongY = alongY;
		shear.LineY = lineY ?? KitModelWall.WallBounds( model, alongY ).Center.y;
		shear.Material = material;
		shear.Rebuild();
		return wall;
	}

	/// <summary>A plain model object: Model Renderer (with the material as override, or the model's own when null) and a static Model Collider on the golf surface.</summary>
	internal static GameObject CreateModelObject( GameObject parent, string name, Model model, Vector3 position, Rotation rotation, Vector3 scale, Material material )
	{
		var go = new GameObject( true, name );
		if ( parent.IsValid() ) go.SetParent( parent, false );
		go.WorldPosition = position;
		go.WorldRotation = rotation;
		go.LocalScale = scale;

		var renderer = go.AddComponent<ModelRenderer>();
		renderer.Model = model;
		renderer.MaterialOverride = material;

		var collider = go.AddComponent<ModelCollider>();
		collider.Model = model;
		collider.Static = true;
		collider.Surface = Surface.FindByName( GolfHoleCollision.SurfaceName ) ?? collider.Surface;
		return go;
	}
}