first commit

This commit is contained in:
2026-04-07 03:14:32 +03:00
commit b3992fec6b
1026 changed files with 366769 additions and 0 deletions

View File

@@ -0,0 +1,122 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace JusticeScale.Scripts.Scales
{
public class OverlapScale : Scale
{
[Header("Capsule Config")]
[Min(0)] [SerializeField]
private float capsuleLength = 1.5f; // The vertical length of the capsule used to detect objects above the scale
[Min(0)] [SerializeField]
private float capsuleRadius = 0.5f; // The radius of the capsule, determining its width
[Min(0)] [SerializeField]
private float capsuleOffsetHeight = 0.5f; // The vertical offset from the scales position to the start of the capsule
private Vector3 _startPoint, _endPoint; // The calculated start and end points of the capsule
private readonly HashSet<Rigidbody> _detectedObjects = new(); // Set of detected rigidbodies to avoid duplicates
private readonly List<Rigidbody> _objectsInContainer = new(); // List of objects currently inside the capsule area
private GameObject _objectContainer; // A container object to parent detected objects
private float _previousWeight; // Stores the previous total weight to optimize calculations
// Returns the calculated total weight of objects detected inside the capsule
public override float TotalWeight => CalculateWeightInCapsule();
private float CalculateWeightInCapsule()
{
weight = 0f;
_detectedObjects.Clear();
// Adjust the capsule variables by the scale of the GameObject
var scaledCapsuleLength = capsuleLength * transform.lossyScale.normalized.magnitude;
var scaledCapsuleRadius = capsuleRadius * transform.lossyScale.normalized.magnitude;
var scaledCapsuleOffsetHeight = capsuleOffsetHeight * transform.lossyScale.normalized.magnitude;
// Define capsule start and end points based on configuration, adjusted for the scale
_startPoint = transform.position + Vector3.up * scaledCapsuleOffsetHeight;
_endPoint = _startPoint + Vector3.up * scaledCapsuleLength;
// Perform the OverlapCapsule detection, gathering all colliders within the volume
// ReSharper disable once Unity.PreferNonAllocApi
Collider[] colliders = Physics.OverlapCapsule(_startPoint, _endPoint, scaledCapsuleRadius, layerMask);
foreach (var collider in colliders)
{
var rb = collider.GetComponent<Rigidbody>();
if (rb != null && !_detectedObjects.Contains(rb))
{
_detectedObjects.Add(rb);
ManageObjectContainer(rb);
weight += rb.mass;
if (!_objectsInContainer.Contains(rb)) _objectsInContainer.Add(rb);
}
}
RemoveDetectedObjectsNotInCapsule(_detectedObjects);
// Exit early if the weight hasn't changed significantly
if (Mathf.Abs(weight - _previousWeight) < 0.001f) return _previousWeight;
_previousWeight = weight;
return weight;
}
private void ManageObjectContainer(Rigidbody rb)
{
if (_objectContainer == null)
{
_objectContainer = new GameObject("Objects Container");
_objectContainer.transform.parent = transform;
}
rb.transform.SetParent(_objectContainer.transform, true);
}
private void RemoveDetectedObjectsNotInCapsule(HashSet<Rigidbody> detectedObjects)
{
foreach (var obj in _objectsInContainer.ToList())
if (!detectedObjects.Contains(obj))
{
obj.transform.parent = null;
_objectsInContainer.Remove(obj);
}
if (detectedObjects.Count == 0) Destroy(_objectContainer);
}
private void OnDrawGizmosSelected()
{
if (transform == null) return;
// Adjust the capsule variables by the scale of the GameObject
float scaledCapsuleLength = capsuleLength * 0.1f * transform.lossyScale.magnitude;
float scaledCapsuleRadius = capsuleRadius * 0.1f * transform.lossyScale.magnitude;
float scaledCapsuleOffsetHeight = capsuleOffsetHeight * 0.1f * transform.lossyScale.magnitude;
// Define the start and end points, adjusted for the object's scale
_startPoint = transform.position + Vector3.up * scaledCapsuleOffsetHeight;
_endPoint = _startPoint + Vector3.up * scaledCapsuleLength;
// Draw the capsule in the editor (with scaling applied)
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere(_startPoint, scaledCapsuleRadius);
Gizmos.DrawWireSphere(_endPoint, scaledCapsuleRadius);
Gizmos.DrawLine(
_startPoint + Vector3.up * scaledCapsuleRadius,
_endPoint + Vector3.up * scaledCapsuleRadius
);
Gizmos.DrawLine(
_startPoint - Vector3.up * scaledCapsuleRadius,
_endPoint - Vector3.up * scaledCapsuleRadius
);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 8b64e6584f3a49a5b30287827ab3f516
timeCreated: 1731300775

View File

@@ -0,0 +1,18 @@
using UnityEngine;
namespace JusticeScale.Scripts.Scales
{
public abstract class Scale : MonoBehaviour
{
public abstract float TotalWeight { get; }
[SerializeField] protected LayerMask layerMask = -1; // LayerMask specifying which layers to include in the scale detection
[SerializeField] [Tooltip("The current weight of the scale (not visible in the Inspector), for easy developer testing.")]
protected float weight; // Current weight for inspector display; TotalWeight is the actual calculated value.
private void Update()
{
weight = TotalWeight;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3a958ddeab399d44eb4e129be0ea1cc8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,80 @@
using System.Collections.Generic;
using UnityEngine;
namespace JusticeScale.Scripts.Scales
{
[RequireComponent(typeof(MeshCollider))]
public class TriggerScale : Scale
{
[Tooltip("The total weight of the objects currently on this scale trigger.")]
public override float TotalWeight => weight;
// Container for objects detected by the scale
private GameObject _objectContainer;
// HashSet to track objects on the scale, ensuring each is only counted once
// (useful if the GameObject has multiple triggers that could detect the same object).
private HashSet<Transform> _detectedObjects = new HashSet<Transform>();
private void Start()
{
// Initialize the object container at the start
_objectContainer = new GameObject("Objects Container");
_objectContainer.transform.parent = transform;
}
private void OnTriggerEnter(Collider other)
{
var rb = other.GetComponent<Rigidbody>();
if (rb != null && IsInDetectableLayer(other.gameObject) && _detectedObjects.Add(rb.transform))
{
AddObjectToContainer(rb.transform);
weight += rb.mass;
// Round the total weight to 2 decimal places
weight = Mathf.Round(weight * 100f) / 100f;
}
}
private void OnTriggerExit(Collider other)
{
var rb = other.GetComponent<Rigidbody>();
if (rb != null && _detectedObjects.Remove(rb.transform))
{
weight -= rb.mass;
// Ensure total weight doesn't drop below zero
weight = Mathf.Max(0, weight);
weight = Mathf.Round(weight * 100f) / 100f;
RemoveObjectFromContainer(rb.transform);
}
}
private void AddObjectToContainer(Transform objectTransform)
{
if (_objectContainer == null)
{
// Create a new container for the objects if it doesn't exist
_objectContainer = new GameObject("Objects Container");
_objectContainer.transform.parent = transform;
}
// Set the parent of the object to the container
objectTransform.SetParent(_objectContainer.transform, true);
}
private void RemoveObjectFromContainer(Transform objectTransform)
{
// Unparent the object from the container
if (_objectContainer != null) objectTransform.parent = null;
// Destroy the container if it has no child objects left
if (_objectContainer.transform.childCount == 0) Destroy(_objectContainer);
}
private bool IsInDetectableLayer(GameObject obj)
{
// Check if the object is in the correct layer
return ((layerMask.value & (1 << obj.layer)) != 0);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 76c938acc67ae6746ad167809e6b86e8
timeCreated: 1728533272