处理plug和线缆以及几个bug
This commit is contained in:
+955
-813
File diff suppressed because it is too large
Load Diff
@@ -53,7 +53,7 @@ namespace AibisDream.FixSystem
|
||||
_targetSpriteRenderer = transform.Find("Module Pic").GetComponent<SpriteRenderer>();
|
||||
_originalMaterial = _targetSpriteRenderer.GetComponent<SpriteRenderer>().material;
|
||||
|
||||
_cutSpriteRenderer = transform.Find("Cut Pic").GetComponent<SpriteRenderer>();
|
||||
_cutSpriteRenderer = transform.Find("Cut Pic")?.GetComponent<SpriteRenderer>();
|
||||
|
||||
_highlightMaterial = Resources.Load<Material>("Materials/HighlightMaterial");
|
||||
_alarmMaterial = Resources.Load<Material>("Materials/AlarmMaterial");
|
||||
|
||||
@@ -62,6 +62,17 @@ namespace AibisDream.FixSystem
|
||||
heatMapController = transform.Find("HeatMapController").GetComponent<HeatMapController>();
|
||||
}
|
||||
|
||||
// 初始化 BodyModules 列表
|
||||
BodyModules = new List<BodyModule>();
|
||||
if (_bodyModuleGroup != null)
|
||||
{
|
||||
BodyModules.AddRange(_bodyModuleGroup.GetComponentsInChildren<BodyModule>());
|
||||
}
|
||||
if (_headModuleGroup != null)
|
||||
{
|
||||
BodyModules.AddRange(_headModuleGroup.GetComponentsInChildren<BodyModule>());
|
||||
}
|
||||
|
||||
FixSystemCenter.SystemDic.Register(this);
|
||||
|
||||
// 注册Camera
|
||||
|
||||
@@ -2,6 +2,7 @@ using System.Collections;
|
||||
using AibisDream.FixSystem;
|
||||
using AibisDream.Framework;
|
||||
using Yarn.Unity;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AibisDream
|
||||
{
|
||||
@@ -111,7 +112,14 @@ namespace AibisDream
|
||||
public static void ModuleHightLight(string moduleName, bool enable)
|
||||
{
|
||||
var targetModule = BodyModuleSystem.FindBodyModuleByName(moduleName);
|
||||
targetModule.Highlight(enable);
|
||||
if (targetModule != null)
|
||||
{
|
||||
targetModule.Highlight(enable);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"Module {moduleName} not found!");
|
||||
}
|
||||
}
|
||||
|
||||
[YarnCommand("ModuleAlarm")]
|
||||
|
||||
@@ -12,6 +12,7 @@ namespace AibisDream.FixSystem
|
||||
private Transform _end;
|
||||
|
||||
private LineRenderer _lineRenderer;
|
||||
private LineRenderer _stiffLineRenderer; // 用于渲染刚性部分
|
||||
private Vector3[] _points;
|
||||
|
||||
private CableSystem _cableSystem;
|
||||
@@ -30,7 +31,22 @@ namespace AibisDream.FixSystem
|
||||
_cableSystem = transform.parent.GetComponent<CableSystem>();
|
||||
|
||||
_lineRenderer = GetComponent<LineRenderer>();
|
||||
_lineRenderer.positionCount = config.resolution;
|
||||
_lineRenderer.positionCount = config.resolution - config.stiffCount;
|
||||
|
||||
// 创建第二个LineRenderer用于刚性部分
|
||||
GameObject stiffObj = new GameObject("StiffLineRenderer");
|
||||
stiffObj.transform.SetParent(transform);
|
||||
_stiffLineRenderer = stiffObj.AddComponent<LineRenderer>();
|
||||
_stiffLineRenderer.positionCount = config.stiffCount;
|
||||
|
||||
// 复制第一个LineRenderer的属性到第二个
|
||||
_stiffLineRenderer.startWidth = _lineRenderer.startWidth;
|
||||
_stiffLineRenderer.endWidth = _lineRenderer.startWidth;
|
||||
_stiffLineRenderer.material = _lineRenderer.material;
|
||||
_stiffLineRenderer.startColor = _lineRenderer.startColor;
|
||||
_stiffLineRenderer.endColor = _lineRenderer.startColor;
|
||||
_stiffLineRenderer.sortingLayerName = _cableSystem.CableReelRef.GetComponent<SpriteRenderer>().sortingLayerName;
|
||||
_stiffLineRenderer.sortingOrder = _cableSystem.CableReelRef.GetComponent<SpriteRenderer>().sortingOrder - 1;
|
||||
|
||||
// 连线起止点
|
||||
_start = _cableSystem.CableRootPos;
|
||||
@@ -55,31 +71,54 @@ namespace AibisDream.FixSystem
|
||||
void DrawLine()
|
||||
{
|
||||
var points = UpdateRope();
|
||||
for (int i = 0; i < config.resolution; i++)
|
||||
|
||||
// 渲染刚性部分
|
||||
for (int i = 0; i < config.stiffCount; i++)
|
||||
{
|
||||
_lineRenderer.SetPosition(i, points[i] + Vector3.forward * -.3f);
|
||||
_stiffLineRenderer.SetPosition(i, points[i] + Vector3.forward * -.3f);
|
||||
}
|
||||
|
||||
// 渲染非刚性部分
|
||||
for (int i = 0; i < config.resolution - config.stiffCount; i++)
|
||||
{
|
||||
_lineRenderer.SetPosition(i, points[i + config.stiffCount] + Vector3.forward * -.3f);
|
||||
}
|
||||
}
|
||||
|
||||
Vector3[] UpdateRope()
|
||||
{
|
||||
|
||||
float t = Mathf.InverseLerp(config.dstMin, config.dstMax, (_start.position - _end.position).magnitude);
|
||||
float F = Mathf.Lerp(config.forceMin, config.forceMax, t);
|
||||
_points[0] = _start.position;
|
||||
_points[^1] = _end.position;
|
||||
for (int ik = 0; ik < config.k; ik++)
|
||||
|
||||
// 计算出线方向(从转盘出线口到起始点)
|
||||
Vector3 dir = (_start.position - _cableSystem.CableReelRef.transform.position).normalized;
|
||||
|
||||
// 设置刚性部分
|
||||
for (int i = 0; i < config.stiffCount; i++)
|
||||
{
|
||||
for (int i = 1; i < _points.Length - 1; i++)
|
||||
float t_stiff = (float)i / (config.stiffCount - 1);
|
||||
_points[i] = _start.position + dir * (config.stiffLength * t_stiff);
|
||||
}
|
||||
|
||||
// 设置末端点
|
||||
_points[^1] = _end.position;
|
||||
|
||||
// 只对非刚性部分进行物理模拟
|
||||
for (int ik = 0; ik < config.k; ik++)
|
||||
{
|
||||
// 确保非刚性部分的第一个点与刚性部分的最后一个点重合
|
||||
_points[config.stiffCount] = _points[config.stiffCount - 1];
|
||||
|
||||
for (int i = config.stiffCount + 1; i < _points.Length - 1; i++)
|
||||
{
|
||||
Vector3 offsetPrev = _points[i - 1] - _points[i];
|
||||
Vector3 offsetNext = _points[i + 1] - _points[i];
|
||||
Vector3 velocity = offsetPrev.normalized * (offsetPrev.magnitude * F) +
|
||||
offsetNext.normalized * (offsetNext.magnitude * F);
|
||||
_points[i] += velocity * Time.deltaTime / config.k;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 1; i < _points.Length - 1; i++)
|
||||
for (int i = config.stiffCount + 1; i < _points.Length - 1; i++)
|
||||
{
|
||||
_points[i] += Vector3.down * (9.8f * Time.deltaTime) / config.k;
|
||||
}
|
||||
@@ -98,7 +137,7 @@ namespace AibisDream.FixSystem
|
||||
}
|
||||
public Vector3 GetDirection()
|
||||
{
|
||||
return _points[^1]-_points[_points.Length-1];
|
||||
return _points[^1] - _points[^2];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,6 +150,8 @@ namespace AibisDream.FixSystem
|
||||
public float forceMin;
|
||||
public float forceMax;
|
||||
public int k;
|
||||
public int stiffCount; // 刚性部分点数
|
||||
public float stiffLength; // 刚性部分长度
|
||||
|
||||
public static CableConfig GeneDefaultConfig()
|
||||
{
|
||||
@@ -121,7 +162,9 @@ namespace AibisDream.FixSystem
|
||||
dstMax = 1.0f,
|
||||
forceMin = 0.1f,
|
||||
forceMax = 150f,
|
||||
k = 10
|
||||
k = 10,
|
||||
stiffCount = 3,
|
||||
stiffLength = 0.1f
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace AibisDream.FixSystem
|
||||
{
|
||||
public class CableReel : MonoBehaviour
|
||||
{
|
||||
[Header("转盘设置")]
|
||||
[SerializeField] private float torqueStrength = 200f; // 扭矩强度
|
||||
[SerializeField] private float angularDamping = 5f; // 角速度阻尼
|
||||
|
||||
private CableSystem cableSystem;
|
||||
private float angularVelocity; // 角速度
|
||||
private float currentAngle; // 当前角度
|
||||
public Vector3 outletPos { get; private set; } // 出线口位置
|
||||
private SpriteRenderer spriteRenderer;
|
||||
private float reelRadius; // 转盘半径
|
||||
|
||||
private void Start()
|
||||
{
|
||||
// 获取CableSystem引用
|
||||
cableSystem = transform.parent.GetComponent<CableSystem>();
|
||||
currentAngle = transform.eulerAngles.z;
|
||||
|
||||
// 获取SpriteRenderer并计算半径
|
||||
spriteRenderer = GetComponent<SpriteRenderer>();
|
||||
if (spriteRenderer != null)
|
||||
{
|
||||
// 使用sprite的bounds来计算半径
|
||||
reelRadius = spriteRenderer.bounds.extents.y; // 使用y方向的半高作为半径
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("CableReel没有SpriteRenderer组件!");
|
||||
reelRadius = 0.5f; // 默认值
|
||||
}
|
||||
|
||||
// 初始化出线口位置
|
||||
UpdateOutletPosition();
|
||||
}
|
||||
|
||||
private void UpdateOutletPosition()
|
||||
{
|
||||
// 更新出线口位置(圆形本地坐标 (0, -radius) 转成世界坐标)
|
||||
outletPos = transform.TransformPoint(new Vector3(0, -reelRadius, 0));
|
||||
}
|
||||
|
||||
public void ResetRotation()
|
||||
{
|
||||
// 重置角度和角速度
|
||||
currentAngle = 0;
|
||||
angularVelocity = 0;
|
||||
transform.rotation = Quaternion.identity;
|
||||
UpdateOutletPosition();
|
||||
}
|
||||
|
||||
public void UpdateRotation()
|
||||
{
|
||||
// 如果PhysicCable激活,不进行旋转
|
||||
if (cableSystem == null || cableSystem.PlugRef == null ||
|
||||
cableSystem.PhysicCableRef.GetComponent<LineRenderer>().enabled) return;
|
||||
|
||||
Vector2 center = transform.position;
|
||||
|
||||
// 使用当前出线口位置
|
||||
Vector2 outlet = outletPos;
|
||||
|
||||
// 力向量(线缆拉力方向)
|
||||
Vector2 force = (Vector2)cableSystem.PlugRef.transform.position - outlet;
|
||||
|
||||
// 杆臂向量(力作用点相对于圆心的位置)
|
||||
Vector2 r = outlet - center;
|
||||
|
||||
// 计算 2D 扭矩:r × F
|
||||
float torque = (r.x * force.y - r.y * force.x);
|
||||
|
||||
// 应用扭矩 + 阻尼
|
||||
angularVelocity += torque * torqueStrength * Time.deltaTime;
|
||||
angularVelocity *= Mathf.Exp(-angularDamping * Time.deltaTime); // 简易阻尼
|
||||
|
||||
// 应用旋转
|
||||
currentAngle += angularVelocity * Time.deltaTime;
|
||||
transform.rotation = Quaternion.Euler(0, 0, currentAngle);
|
||||
|
||||
// 更新出线口位置
|
||||
UpdateOutletPosition();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 81da72a4f38d7f94784a80abb68a0ddd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -11,6 +11,7 @@ namespace AibisDream.FixSystem
|
||||
|
||||
public PhysicCable PhysicCableRef { get; private set; }
|
||||
public Transform CableRootPos { get; private set; }
|
||||
public CableReel CableReelRef { get; private set; }
|
||||
|
||||
public BodyModuleSystem BodyModuleSystem { get; private set; }
|
||||
|
||||
@@ -52,7 +53,8 @@ namespace AibisDream.FixSystem
|
||||
{
|
||||
PlugRef = transform.Find("Plug").GetComponent<Plug>();
|
||||
CableRef = transform.Find("Cable").GetComponent<Cable>();
|
||||
CableRootPos = transform.Find("Cable Root Pos").transform;
|
||||
CableReelRef = transform.Find("CableReel").GetComponent<CableReel>();
|
||||
CableRootPos = CableReelRef.transform.Find("Cable Root Pos").transform;
|
||||
PhysicCableRef = transform.Find("PhysicCable").GetComponent<PhysicCable>();
|
||||
|
||||
BodyModuleSystem = transform.parent.GetComponent<BodyModuleSystem>();
|
||||
@@ -64,6 +66,15 @@ namespace AibisDream.FixSystem
|
||||
PlugRef.InitPlug();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// 获取线缆方向并更新转盘旋转
|
||||
if (CableRef != null && CableReelRef != null)
|
||||
{
|
||||
CableReelRef.UpdateRotation();
|
||||
}
|
||||
}
|
||||
|
||||
public void ResetPlug()
|
||||
{
|
||||
// 不在初始插孔里,就从当前插孔拔出来,插回初始插孔
|
||||
|
||||
@@ -179,7 +179,10 @@ namespace AibisDream.FixSystem
|
||||
|
||||
public void ReturnToStartPosition()
|
||||
{
|
||||
transform.DOMove(_startPos, 0.1f).OnComplete(SwitchToPhysicCableState);
|
||||
transform.DOMove(_startPos, 0.1f).OnComplete(() => {
|
||||
_cableSystem.CableReelRef.ResetRotation();
|
||||
SwitchToPhysicCableState();
|
||||
});
|
||||
}
|
||||
|
||||
private void AdjustPlugPos()
|
||||
|
||||
@@ -29,6 +29,7 @@ namespace AibisDream
|
||||
if (targetSpriteRenderer != null)
|
||||
{
|
||||
targetSpriteRenderer.color = new Color(1f, 1f, 1f, 0f);
|
||||
targetSpriteRenderer.enabled = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -38,6 +39,7 @@ namespace AibisDream
|
||||
if (backupSpriteRenderer != null)
|
||||
{
|
||||
backupSpriteRenderer.color = new Color(1f, 1f, 1f, 0f);
|
||||
backupSpriteRenderer.enabled = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -97,6 +99,8 @@ namespace AibisDream
|
||||
|
||||
private IEnumerator StartFlash(Sprite flashSprite, float fadeDuration = 0.5f)
|
||||
{
|
||||
targetSpriteRenderer.enabled = true;
|
||||
backupSpriteRenderer.enabled = true;
|
||||
Debug.Log("开始Sprite闪烁");
|
||||
isFading = true;
|
||||
targetSpriteRenderer.sprite = flashSprite;
|
||||
@@ -124,6 +128,8 @@ namespace AibisDream
|
||||
Debug.Log("闪烁效果完成");
|
||||
isFading = false;
|
||||
currentSequence = null;
|
||||
targetSpriteRenderer.enabled = false;
|
||||
backupSpriteRenderer.enabled = false;
|
||||
DialogController.Instance.StartDialogNode("闪回完成");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user