126 lines
3.3 KiB
C#
126 lines
3.3 KiB
C#
using AibisDream.FixSystem;
|
|
using AibisDream.Framework;
|
|
using DG.Tweening;
|
|
using UnityEngine;
|
|
|
|
namespace AibisDream
|
|
{
|
|
public class Gear : MonoBehaviour, IMoveable
|
|
{
|
|
private const int ShakeAngle = 15;
|
|
|
|
public float radius;
|
|
public Sprite gearSprite;
|
|
public bool isBroken;
|
|
public bool isDisc;
|
|
public bool isConstant;
|
|
|
|
public float speed;
|
|
private Tweener _rotationTweener;
|
|
|
|
#region 引用
|
|
|
|
private GearSystem _gearSystem;
|
|
private CircleCollider2D _circleCollider;
|
|
private SpriteRenderer _spriteRenderer;
|
|
|
|
#endregion
|
|
|
|
public Shaft targetShaft;
|
|
|
|
public bool IsBlock { get; }
|
|
|
|
private void Awake()
|
|
{
|
|
_circleCollider = GetComponent<CircleCollider2D>();
|
|
_spriteRenderer = GetComponent<SpriteRenderer>();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
_gearSystem = FixSystemCenter.SystemDic.Get<GearSystem>();
|
|
// 给齿轮赋值
|
|
InitGearModel();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置齿轮信息
|
|
/// </summary>
|
|
private void InitGearModel()
|
|
{
|
|
// 给齿轮赋值
|
|
_spriteRenderer.sprite = gearSprite;
|
|
_circleCollider.radius = radius;
|
|
|
|
_spriteRenderer.size = new Vector2(radius * 2, radius * 2);
|
|
}
|
|
|
|
public void UpdateRotation(float newSpeed, float speedScale)
|
|
{
|
|
// 初始化速度
|
|
speed = Mathf.Abs(newSpeed);
|
|
var target = newSpeed >= 0 ? new Vector3(0, 0, 360) : new Vector3(0, 0, -360);
|
|
|
|
_rotationTweener?.Kill();
|
|
// 打开动画
|
|
if (isBroken)
|
|
{
|
|
_rotationTweener = transform.DORotate(new Vector3(0, 0, ShakeAngle), ShakeAngle / speed / 360,
|
|
RotateMode.LocalAxisAdd)
|
|
.SetEase(Ease.Linear)
|
|
.SetLoops(-1, LoopType.Yoyo)
|
|
.SetDelay(0.1f);
|
|
}
|
|
else
|
|
{
|
|
_rotationTweener = transform.DORotate(target, 1 / Mathf.Abs(speed), RotateMode.LocalAxisAdd)
|
|
.SetEase(Ease.Linear)
|
|
.SetLoops(-1, LoopType.Incremental);
|
|
}
|
|
|
|
_rotationTweener.timeScale = speedScale;
|
|
}
|
|
|
|
public void UpdateSpeedScale(float speedScale)
|
|
{
|
|
_rotationTweener.timeScale = speedScale;
|
|
}
|
|
|
|
#region 移动相关
|
|
|
|
public void StartMove()
|
|
{
|
|
if (isConstant) return;
|
|
|
|
targetShaft?.PlugOutGear();
|
|
transform.DOPause();
|
|
}
|
|
|
|
public void StopMove()
|
|
{
|
|
if (isConstant) return;
|
|
|
|
if (targetShaft)
|
|
{
|
|
transform.position = targetShaft.transform.position;
|
|
targetShaft.PlugInGear(this);
|
|
}
|
|
}
|
|
|
|
public void Move(Vector2 targetPos)
|
|
{
|
|
if (isConstant) return;
|
|
|
|
transform.position = targetPos;
|
|
var tempShaft = _gearSystem.CheckClosestShaft(transform.position, radius);
|
|
if (!tempShaft) return;
|
|
|
|
if (_gearSystem.CheckGearSuitable(tempShaft, radius))
|
|
{
|
|
targetShaft = tempShaft;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |