You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
2.8 KiB
C#
113 lines
2.8 KiB
C#
2 weeks ago
|
using UnityEngine;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
|
||
|
public class Mole : MonoBehaviour
|
||
|
{
|
||
|
public float visibleDuration = 1.0f;
|
||
|
public float moveSpeed = 5f;
|
||
|
private Vector3 hiddenPosition;
|
||
|
private Vector3 visiblePosition;
|
||
|
private bool isVisible = false;
|
||
|
private bool isMoving = false;
|
||
|
|
||
|
private GameManager gameManager;
|
||
|
|
||
|
void Start()
|
||
|
{
|
||
|
hiddenPosition = transform.position;
|
||
|
visiblePosition = hiddenPosition + Vector3.up * 1.0f;
|
||
|
gameManager = FindObjectOfType<GameManager>();
|
||
|
HideInstant(); // start hidden
|
||
|
}
|
||
|
|
||
|
public void Show()
|
||
|
{
|
||
|
if (!isMoving)
|
||
|
StartCoroutine(MoveTo(visiblePosition, () =>
|
||
|
{
|
||
|
isVisible = true;
|
||
|
Invoke(nameof(Hide), visibleDuration);
|
||
|
}));
|
||
|
}
|
||
|
|
||
|
public void Hide()
|
||
|
{
|
||
|
if (!isMoving)
|
||
|
StartCoroutine(MoveTo(hiddenPosition, () => isVisible = false));
|
||
|
}
|
||
|
private IEnumerator HitReaction()
|
||
|
{
|
||
|
Vector3 originalScale = transform.localScale;
|
||
|
Vector3 squashed = new Vector3(1.2f, 0.6f, 1.2f);
|
||
|
|
||
|
transform.localScale = squashed;
|
||
|
yield return new WaitForSeconds(0.1f);
|
||
|
|
||
|
transform.localScale = originalScale;
|
||
|
}
|
||
|
private IEnumerator MoveTo(Vector3 targetPos, System.Action onComplete)
|
||
|
{
|
||
|
isMoving = true;
|
||
|
float t = 0;
|
||
|
Vector3 startPos = transform.position;
|
||
|
float duration = Vector3.Distance(startPos, targetPos) / moveSpeed;
|
||
|
|
||
|
while (t < duration)
|
||
|
{
|
||
|
t += Time.deltaTime;
|
||
|
float progress = Mathf.Clamp01(t / duration);
|
||
|
float eased = EaseOutBounce(progress);
|
||
|
transform.position = Vector3.Lerp(startPos, targetPos, eased);
|
||
|
yield return null;
|
||
|
}
|
||
|
|
||
|
transform.position = targetPos;
|
||
|
isMoving = false;
|
||
|
onComplete?.Invoke();
|
||
|
}
|
||
|
|
||
|
private void HideInstant()
|
||
|
{
|
||
|
transform.position = hiddenPosition;
|
||
|
isVisible = false;
|
||
|
}
|
||
|
|
||
|
private void OnMouseDown()
|
||
|
{
|
||
|
if (isVisible)
|
||
|
{
|
||
|
gameManager.AddScore(1);
|
||
|
StartCoroutine(HitReaction());
|
||
|
Hide();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Bounce easing function (EaseOutBounce)
|
||
|
private float EaseOutBounce(float x)
|
||
|
{
|
||
|
const float n1 = 7.5625f;
|
||
|
const float d1 = 2.75f;
|
||
|
|
||
|
if (x < 1 / d1)
|
||
|
{
|
||
|
return n1 * x * x;
|
||
|
}
|
||
|
else if (x < 2 / d1)
|
||
|
{
|
||
|
x -= 1.5f / d1;
|
||
|
return n1 * x * x + 0.75f;
|
||
|
}
|
||
|
else if (x < 2.5f / d1)
|
||
|
{
|
||
|
x -= 2.25f / d1;
|
||
|
return n1 * x * x + 0.9375f;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
x -= 2.625f / d1;
|
||
|
return n1 * x * x + 0.984375f;
|
||
|
}
|
||
|
}
|
||
|
}
|