89 lines
2.4 KiB
C#
89 lines
2.4 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;
|
|
|
|
[Header("Победная анимация")]
|
|
[SerializeField] private Transform winDots;
|
|
[SerializeField] private float winAnimationDuration = 1;
|
|
|
|
private float chipVelocity = 0;
|
|
private float rotationSpeed = 0;
|
|
|
|
private float winDotsPosition = 1.5f;
|
|
private Tween winTween;
|
|
|
|
public void Configure(Sprite sprite, Team team)
|
|
{
|
|
spriteRenderer.sprite = sprite;
|
|
mask.sprite = sprite;
|
|
this.team = team;
|
|
|
|
mask.enabled = false;
|
|
winDots.gameObject.SetActive(false);
|
|
}
|
|
|
|
public virtual void RemoveFromField()
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
|
|
public bool MoveToLinear(Vector2 target, float speed)
|
|
{
|
|
transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
|
|
|
|
if ((Vector2)transform.position == target)
|
|
return true;
|
|
|
|
return false;
|
|
}
|
|
|
|
public bool MoveToAccelerated(Vector2 target)
|
|
{
|
|
transform.position = Vector2.MoveTowards(transform.position, target, chipVelocity * Time.deltaTime);
|
|
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.gameObject.SetActive(true);
|
|
|
|
Vector2 startPosition = new Vector2(winDotsPosition, winDotsPosition);
|
|
winDots.localPosition = startPosition;
|
|
winTween = winDots.DOLocalMove(-startPosition, winAnimationDuration).SetEase(Ease.Linear);
|
|
|
|
winTween.onComplete = () =>
|
|
{
|
|
mask.enabled = false;
|
|
winDots.gameObject.SetActive(false);
|
|
};
|
|
winTween.PlayForward();
|
|
}
|
|
} |