齿轮
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using AibisDream;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Framework.Core
|
||||
@@ -100,11 +99,6 @@ namespace Framework.Core
|
||||
{
|
||||
foreach (var component in components)
|
||||
{
|
||||
if (attribute.goName == "Min Pointer")
|
||||
{
|
||||
Debug.Log(component.name);
|
||||
}
|
||||
|
||||
if (component.name != attribute.goName) continue;
|
||||
field.SetValue(mono, component);
|
||||
break;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using AibisDream.Framework;
|
||||
using DG.Tweening;
|
||||
using UnityEngine;
|
||||
@@ -17,10 +18,13 @@ namespace AibisDream
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
gearController = transform.parent.parent.GetComponent<GearController>();
|
||||
circleCollider = GetComponent<CircleCollider2D>();
|
||||
spriteRenderer = GetComponent<SpriteRenderer>();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
gearController = transform.parent.parent.GetComponent<GearController>();
|
||||
// 给齿轮赋值
|
||||
InitGearModel();
|
||||
}
|
||||
@@ -28,12 +32,13 @@ namespace AibisDream
|
||||
/// <summary>
|
||||
/// 设置齿轮信息
|
||||
/// </summary>
|
||||
/// <param name="gearModel">齿轮信息</param>
|
||||
public void InitGearModel()
|
||||
{
|
||||
// 给齿轮赋值
|
||||
spriteRenderer.sprite = gearSprite;
|
||||
circleCollider.radius = radius;
|
||||
|
||||
spriteRenderer.size = new Vector2(radius * 2, radius * 2);
|
||||
}
|
||||
|
||||
public void StartMove()
|
||||
@@ -54,7 +59,19 @@ namespace AibisDream
|
||||
public void Move(Vector2 targetPos)
|
||||
{
|
||||
transform.position = targetPos;
|
||||
targetShaft = CheckTargetShaft();
|
||||
var tempShaft = gearController.CheckClosestShaft(transform.position, radius);
|
||||
if (!tempShaft) return;
|
||||
|
||||
targetShaft = tempShaft;
|
||||
|
||||
if (gearController.CheckGearSuitable(tempShaft, radius))
|
||||
{
|
||||
targetShaft = tempShaft;
|
||||
}
|
||||
else
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private Shaft CheckTargetShaft()
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace AibisDream
|
||||
{
|
||||
#region 索引
|
||||
|
||||
private GameObject _gearPrefab;
|
||||
[SerializeField] private GameObject _gearPrefab;
|
||||
private Shaft[] _shafts;
|
||||
|
||||
#endregion
|
||||
@@ -76,6 +76,17 @@ namespace AibisDream
|
||||
{
|
||||
return _shaftGraph.QuerySnapShaft(pos, gearRadius);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断齿轮能不能放进轴内
|
||||
/// </summary>
|
||||
/// <param name="sourceShaft">当前轴</param>
|
||||
/// <param name="gearRadius">齿轮半径</param>
|
||||
/// <returns>是否可用</returns>
|
||||
public bool CheckGearSuitable(Shaft sourceShaft, float gearRadius)
|
||||
{
|
||||
return _shaftGraph.CheckGearSuitable(sourceShaft, gearRadius);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成齿轮
|
||||
|
||||
@@ -9,18 +9,20 @@ namespace AibisDream
|
||||
[SerializeField] private int id;
|
||||
|
||||
[SerializeField] public ShaftType shaftType;
|
||||
|
||||
|
||||
// 初始齿轮
|
||||
[SerializeField] private float initRadius;
|
||||
[SerializeField] private Sprite initSprite;
|
||||
[SerializeField] private bool hasGear;
|
||||
|
||||
private GameObject childGear;
|
||||
|
||||
[SerializeField] private GameObject childGear;
|
||||
|
||||
// 初始角速度
|
||||
[SerializeField] private float initialSpeed;
|
||||
|
||||
// 目标角速度
|
||||
[SerializeField] private float targetSpeed;
|
||||
|
||||
// 当前角速度
|
||||
public float speed;
|
||||
|
||||
@@ -73,10 +75,14 @@ namespace AibisDream
|
||||
private GameObject InitStaticGear()
|
||||
{
|
||||
GameObject staticGear = transform.GetChild(0).gameObject;
|
||||
|
||||
|
||||
// 添加齿轮图片
|
||||
staticGear.SetActive(true);
|
||||
staticGear.GetComponent<SpriteRenderer>().sprite = initSprite;
|
||||
var staticGearRenderer = staticGear.GetComponent<SpriteRenderer>();
|
||||
staticGearRenderer.sprite = initSprite;
|
||||
staticGearRenderer.size = new Vector2(initRadius * 2, initRadius * 2);
|
||||
|
||||
// 如果有初速,就要开始转动了
|
||||
if (!CommonUtil.IsZero(initialSpeed))
|
||||
{
|
||||
@@ -89,17 +95,21 @@ namespace AibisDream
|
||||
|
||||
private GameObject InitPluggableGear()
|
||||
{
|
||||
return gearController.InitGear(transform.position, initRadius, initSprite, initialSpeed);
|
||||
var gear = gearController.InitGear(transform.position, initRadius, initSprite, initialSpeed);
|
||||
gear.GetComponent<Gear>().targetShaft = this;
|
||||
return gear;
|
||||
}
|
||||
|
||||
public void PlugInGear(Gear gear)
|
||||
{
|
||||
childGear = gear.gameObject;
|
||||
gearController.ChangeGraphVertex(GetInstanceID());
|
||||
}
|
||||
|
||||
public void PlugOutGear()
|
||||
{
|
||||
childGear = null;
|
||||
gearController.ChangeGraphVertex(GetInstanceID());
|
||||
}
|
||||
|
||||
public void UpdateSpeed()
|
||||
@@ -127,15 +137,23 @@ namespace AibisDream
|
||||
|
||||
// 更新动画
|
||||
childGear.transform.DOKill();
|
||||
childGear.transform.DORotate(new Vector3(0, 0, 360), 1 / speed, RotateMode.LocalAxisAdd)
|
||||
.SetEase(Ease.Linear).SetLoops(-1, LoopType.Incremental);
|
||||
if (speed > 0)
|
||||
{
|
||||
childGear.transform.DORotate(new Vector3(0, 0, 360), 1 / speed, RotateMode.LocalAxisAdd)
|
||||
.SetEase(Ease.Linear).SetLoops(-1, LoopType.Incremental);
|
||||
}
|
||||
else
|
||||
{
|
||||
childGear.transform.DORotate(new Vector3(0, 0, -360), 1 / -speed, RotateMode.LocalAxisAdd)
|
||||
.SetEase(Ease.Linear).SetLoops(-1, LoopType.Incremental);
|
||||
}
|
||||
}
|
||||
|
||||
private void CheckGameState()
|
||||
{
|
||||
if (shaftType == ShaftType.Target)
|
||||
{
|
||||
if (targetSpeed <= speed)
|
||||
if (targetSpeed <= Mathf.Abs(speed))
|
||||
{
|
||||
gearController.Succeed();
|
||||
}
|
||||
|
||||
@@ -207,19 +207,31 @@ namespace AibisDream
|
||||
return minDistance < gearRadius ? closestShaft : null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断齿轮能不能放进轴内
|
||||
/// </summary>
|
||||
/// <param name="sourceShaft">当前轴</param>
|
||||
/// <param name="gearRadius">齿轮半径</param>
|
||||
/// <returns>是否可用</returns>
|
||||
public bool CheckGearSuitable(Shaft sourceShaft, float gearRadius)
|
||||
{
|
||||
// 获取目标idx
|
||||
int idx = TranInstanceIDToIdx(sourceShaft.GetInstanceID());
|
||||
|
||||
bool isShaftAvailable = true;
|
||||
// 寻找所有临接轴
|
||||
for (int i = 0; i < _vertexSize; i++)
|
||||
{
|
||||
if (i != idx) continue; // 不需要计算和自身的关系
|
||||
float availableDis = Shaft.CalcShaftAvailableDis(sourceShaft.transform.position, _vertexArray[i]);
|
||||
if (availableDis < gearRadius - 0.15f)
|
||||
{
|
||||
isShaftAvailable = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return isShaftAvailable;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user