116 lines
No EOL
4.6 KiB
C#
116 lines
No EOL
4.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using NUnit.Framework;
|
|
using TMPro;
|
|
using Unity.Collections;
|
|
using Unity.VisualScripting;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using FontWeight = TMPro.FontWeight;
|
|
|
|
public class Controller : MonoBehaviour {
|
|
private List<Cable> cables = new();
|
|
|
|
//Initialize
|
|
void OnEnable() {
|
|
|
|
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) {
|
|
Rigidbody2D settings = go.GetOrAddComponent<Rigidbody2D>();
|
|
|
|
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;
|
|
}
|
|
|
|
if (rp.movement == RollerProperties.RotationType.Fixed) {
|
|
settings.bodyType = RigidbodyType2D.Static;
|
|
}
|
|
|
|
if (rp.start) {
|
|
cables.Add(new Cable { firstRoller = go });
|
|
}
|
|
|
|
Debug.Log("Initialized roller: " + go.name);
|
|
}
|
|
}
|
|
|
|
//create canvas to display information
|
|
var canvas = FindAnyObjectByType<Canvas>();
|
|
|
|
//prepare joints
|
|
foreach (var go in gameObjects) {
|
|
RollerProperties rp = go.GetComponent<RollerProperties>();
|
|
if (rp != null) {
|
|
if (rp.linkTo != null) {
|
|
var dist = go.AddComponent<DistanceJoint2D>();
|
|
dist.enableCollision = true;
|
|
dist.connectedBody = rp.linkTo.GetComponent<Rigidbody2D>();
|
|
dist.maxDistanceOnly = true;
|
|
var (left, right) = CableJointsAlgorithm.TangentCircleCircle(go, rp.linkTo);
|
|
dist.anchor = left;
|
|
dist.connectedAnchor = right;
|
|
rp.currentDistance = dist.distance = dist.DistanceInWorld(left, right);
|
|
}
|
|
|
|
if (rp.movement == RollerProperties.RotationType.Rotatable ||
|
|
rp.movement == 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 = 10 };
|
|
}
|
|
}
|
|
|
|
//create names on objects
|
|
GameObject text = new GameObject(go.name + "_text");
|
|
text.transform.parent = canvas.transform;
|
|
|
|
var textMeshPro = text.AddComponent<TextMeshPro>();
|
|
textMeshPro.text = go.name + "\n" + (rp.clockwise ? "cw" : "ccw") + " - " + rp.movement;
|
|
textMeshPro.fontSize = .3f;
|
|
textMeshPro.color = Color.red;
|
|
textMeshPro.fontWeight = FontWeight.Bold;
|
|
textMeshPro.outlineColor = Color.white;
|
|
textMeshPro.outlineWidth = 1;
|
|
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;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
// Update is called once per frame
|
|
void Update() {
|
|
CableJointsAlgorithm.TimeStep(cables);
|
|
}
|
|
}
|
|
|
|
public class Cable {
|
|
public GameObject firstRoller;
|
|
} |