161 lines
No EOL
7 KiB
C#
161 lines
No EOL
7 KiB
C#
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
public class Controller : MonoBehaviour {
|
|
|
|
[Tooltip("List of cables in the scene. Each cable consists of segments describing the link and last/first can optionally be fixed to points.")]
|
|
[SerializeField]
|
|
public List<Cable> cables = new();
|
|
|
|
//Initialize
|
|
void Awake() {
|
|
var rotatableSprite = Resources.Load<Sprite>("Rotatable");
|
|
var fixedSprite = Resources.Load<Sprite>("Fixed");
|
|
var poweredSprite = Resources.Load<Sprite>("Powered");
|
|
var freeSprite = Resources.Load<Sprite>("Free");
|
|
|
|
//prepare rollers
|
|
var gameObjects = FindObjectsByType<GameObject>(FindObjectsInactive.Exclude, FindObjectsSortMode.None);
|
|
foreach (GameObject go in gameObjects) {
|
|
RollerProperties rp = go.GetComponent<RollerProperties>();
|
|
if (rp != null) {
|
|
if (!rp.useFixedAttachmentPoint) {
|
|
switch (rp.movement) {
|
|
case RollerProperties.RotationType.Rotatable:
|
|
go.GetComponent<SpriteRenderer>().sprite = rotatableSprite;
|
|
break;
|
|
case RollerProperties.RotationType.Fixed:
|
|
go.GetComponent<SpriteRenderer>().sprite = fixedSprite;
|
|
break;
|
|
case RollerProperties.RotationType.Free:
|
|
go.GetComponent<SpriteRenderer>().sprite = freeSprite;
|
|
break;
|
|
case RollerProperties.RotationType.Powered:
|
|
go.GetComponent<SpriteRenderer>().sprite = poweredSprite;
|
|
break;
|
|
}
|
|
}
|
|
|
|
//collisions and physics
|
|
Rigidbody2D rb = go.GetOrAddComponent<Rigidbody2D>();
|
|
// mass is based on area, if not provided
|
|
rb.useAutoMass = rp.mass < 0;
|
|
if(rp.mass >= 0) {
|
|
rb.mass = rp.mass;
|
|
}
|
|
rb.angularDrag = 1;
|
|
rb.drag = 1;
|
|
if (rp.movement == RollerProperties.RotationType.Fixed) {
|
|
rb.bodyType = RigidbodyType2D.Static;
|
|
}
|
|
|
|
go.AddComponent<PolygonCollider2D>();
|
|
|
|
Debug.Log("Initialized roller: " + go.name);
|
|
}
|
|
}
|
|
|
|
//create canvas to display information
|
|
var canvas = gameObject.AddComponent<Canvas>();
|
|
|
|
//prepare joints
|
|
foreach (var go in gameObjects) {
|
|
RollerProperties[] rpArr = go.GetComponents<RollerProperties>();
|
|
foreach (var rp in rpArr) {
|
|
if (rp != null) {
|
|
if (rp.linkTo != null) {
|
|
var dist = go.AddComponent<DistanceJoint2D>();
|
|
dist.enableCollision = true;
|
|
dist.connectedBody = rp.linkTo.GetComponent<Rigidbody2D>();
|
|
dist.maxDistanceOnly = true;
|
|
dist.autoConfigureDistance = false;
|
|
rp.distanceJoint2D = dist;
|
|
|
|
//set fixed points only once
|
|
if (rp.useFixedAttachmentPoint) {
|
|
dist.anchor = rp.attachmentPoint;
|
|
}
|
|
|
|
if (rp.linkTo.useFixedAttachmentPoint) {
|
|
dist.connectedAnchor = rp.linkTo.attachmentPoint;
|
|
}
|
|
|
|
//update roller tangent attachments
|
|
rp.updateDistanceJoints();
|
|
|
|
rp.actualDistance = dist.distance = dist.ActualDistanceInWorld();
|
|
}
|
|
|
|
if (rp.movement is RollerProperties.RotationType.Rotatable
|
|
or RollerProperties.RotationType.Powered) {
|
|
var wheel = go.AddComponent<HingeJoint2D>();
|
|
wheel.anchor = Vector2.zero;
|
|
if (rp.movement == RollerProperties.RotationType.Powered) {
|
|
wheel.useMotor = true;
|
|
wheel.motor = new JointMotor2D()
|
|
{ maxMotorTorque = 10000, motorSpeed = rp.clockwise ? 10 : -10 };
|
|
}
|
|
}
|
|
|
|
//create names on objects
|
|
GameObject text = new GameObject(go.name + "_text");
|
|
text.transform.parent = canvas.transform;
|
|
|
|
var textMeshPro = text.AddComponent<TextMeshPro>();
|
|
// textMeshPro.material.shader.GetComponent<Outline>().enabled = true;
|
|
// textMeshPro.material.shader.GetComponent<Outline>().useGraphicAlpha = false;
|
|
// textMeshPro.material.shader.GetComponent<Outline>().effectDistance = new Vector2(1, -1);
|
|
textMeshPro.fontSize = .3f;
|
|
textMeshPro.color = Color.black;
|
|
textMeshPro.fontStyle = FontStyles.Bold;
|
|
textMeshPro.outlineColor = Color.black;
|
|
textMeshPro.outlineWidth = 0.2f;
|
|
textMeshPro.horizontalAlignment = HorizontalAlignmentOptions.Center;
|
|
textMeshPro.verticalAlignment = VerticalAlignmentOptions.Middle;
|
|
textMeshPro.transform.localScale = new Vector3(100, 100, 1);
|
|
textMeshPro.fontSize = .03f;
|
|
textMeshPro.autoSizeTextContainer = true;
|
|
textMeshPro.fontSizeMin = 0;
|
|
rp.text = text;
|
|
}
|
|
}
|
|
}
|
|
|
|
Debug.Log("Initialized "+cables.Count+" cables");
|
|
|
|
/* Define cables directly instead
|
|
var cableNum = 0;
|
|
//find and report loops of cables
|
|
foreach (var cable in cableStartingPoints) {
|
|
List<RollerProperties> visitedSegments = new();
|
|
for (var segment = cable; segment != null; segment = segment.linkTo) {
|
|
if (visitedSegments.Contains(segment)) {
|
|
cableStartingPoints = null;
|
|
Debug.LogError("Fix cable loop in scene with object "+segment.gameObject.name);
|
|
break;
|
|
}
|
|
visitedSegments.Add(segment);
|
|
}
|
|
}*/
|
|
|
|
}
|
|
|
|
private float waitedFrames = 0;
|
|
// Update is called once per frame
|
|
void Update() {
|
|
if (Input.GetKeyDown(KeyCode.T)) {
|
|
foreach (var go in FindObjectsByType<GameObject>(FindObjectsInactive.Exclude, FindObjectsSortMode.None)) {
|
|
if (go.GetComponent<RollerProperties>() != null && go.GetComponent<RollerProperties>().movement == RollerProperties.RotationType.Powered) {
|
|
go.GetComponent<HingeJoint2D>().useMotor = !go.GetComponent<HingeJoint2D>().useMotor;
|
|
Debug.Log("TOGGLED ALL MOTORS");
|
|
}
|
|
}
|
|
}
|
|
|
|
if (waitedFrames++ > -1) {
|
|
CableJointsAlgorithm.TimeStep(cables);
|
|
waitedFrames = 0;
|
|
}
|
|
}
|
|
} |