121 lines
3.3 KiB
C#
121 lines
3.3 KiB
C#
using DG.Tweening;
|
|
using UnityEngine;
|
|
|
|
public class Chip : MonoBehaviour
|
|
{
|
|
public Team team;
|
|
[SerializeField] float acceleration = 200f;
|
|
[SerializeField] float maxFallingRotationSpeed = 500;
|
|
|
|
[SerializeField] private SpriteRenderer spriteRenderer;
|
|
[SerializeField] private SpriteMask mask;
|
|
[SerializeField] private Transform _trailParticles;
|
|
|
|
[Header("Победная анимация")]
|
|
[SerializeField] private Transform winDots;
|
|
[SerializeField] private float winAnimationDuration = 1;
|
|
|
|
private float chipVelocity = 0;
|
|
private float rotationSpeed = 0;
|
|
|
|
private Tween winTween;
|
|
private WinDotsSpriteAnimation _winDotsAnimation;
|
|
private float _trailAngle;
|
|
|
|
public int WinAnimationFrameCount => _winDotsAnimation.FrameCount;
|
|
|
|
private void Awake()
|
|
{
|
|
_winDotsAnimation = winDots.GetComponent<WinDotsSpriteAnimation>();
|
|
}
|
|
|
|
private void LateUpdate()
|
|
{
|
|
if (_trailParticles != null)
|
|
_trailParticles.rotation = Quaternion.Euler(0f, 0f, _trailAngle);
|
|
}
|
|
|
|
public void Configure(Sprite sprite, Team team)
|
|
{
|
|
spriteRenderer.sprite = sprite;
|
|
mask.sprite = sprite;
|
|
this.team = team;
|
|
|
|
mask.enabled = false;
|
|
winDots.gameObject.SetActive(false);
|
|
_trailAngle = 0f;
|
|
}
|
|
|
|
public virtual void RemoveFromField()
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
|
|
public bool MoveToLinear(Vector2 target, float speed)
|
|
{
|
|
Vector2 previousPosition = transform.position;
|
|
Vector2 nextPosition = Vector2.MoveTowards(previousPosition, target, speed * Time.deltaTime);
|
|
transform.position = nextPosition;
|
|
UpdateTrailDirection(nextPosition - previousPosition);
|
|
|
|
if ((Vector2)transform.position == target)
|
|
return true;
|
|
|
|
return false;
|
|
}
|
|
|
|
public bool MoveToAccelerated(Vector2 target)
|
|
{
|
|
Vector2 previousPosition = transform.position;
|
|
Vector2 nextPosition = Vector2.MoveTowards(previousPosition, target, chipVelocity * Time.deltaTime);
|
|
transform.position = nextPosition;
|
|
UpdateTrailDirection(nextPosition - previousPosition);
|
|
chipVelocity += acceleration * Time.deltaTime;
|
|
|
|
if ((Vector2)transform.position == target)
|
|
{
|
|
acceleration = 0;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public void SetRandomRotationSpeed() => rotationSpeed = Random.Range(-maxFallingRotationSpeed, maxFallingRotationSpeed);
|
|
|
|
public void Rotate() => transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime);
|
|
|
|
public void WinAnimation()
|
|
{
|
|
if(winTween != null)
|
|
{
|
|
winTween.Kill();
|
|
winTween = null;
|
|
|
|
}
|
|
|
|
mask.enabled = true;
|
|
winDots.localPosition = Vector3.zero;
|
|
winDots.localScale = Vector3.one;
|
|
winDots.gameObject.SetActive(true);
|
|
|
|
winTween = DOVirtual.DelayedCall(winAnimationDuration, () =>
|
|
{
|
|
mask.enabled = false;
|
|
winDots.gameObject.SetActive(false);
|
|
}, false);
|
|
winTween.PlayForward();
|
|
}
|
|
|
|
public void SetWinAnimationFrame(int frameIndex)
|
|
{
|
|
_winDotsAnimation.SetFrame(frameIndex);
|
|
}
|
|
|
|
private void UpdateTrailDirection(Vector2 movement)
|
|
{
|
|
if (movement.sqrMagnitude > 0f)
|
|
_trailAngle = Vector2.SignedAngle(Vector2.up, movement);
|
|
}
|
|
}
|