106 lines
3.2 KiB
C#
106 lines
3.2 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
|
|
public class CharacterController : MonoBehaviour
|
|
{
|
|
public int CharacterId { get; set; }
|
|
public bool IsMoving { get; private set; }
|
|
public bool IsAnimationComplete { get; private set; }
|
|
[SerializeField]
|
|
public int blockSubroutine { get; set; }
|
|
|
|
private float _moveSpeed = 200f;
|
|
private Vector3 _originalPosition;
|
|
|
|
void Start()
|
|
{
|
|
_originalPosition = transform.position;
|
|
}
|
|
|
|
public void MoveToGameCoordinates(uint gameX, uint gameZ, uint endAction, uint speed)
|
|
{
|
|
IsMoving = true;
|
|
IsAnimationComplete = false;
|
|
|
|
// Êîíâåðòèðóåì èãðîâûå êîîðäèíàòû â ìèðîâûå
|
|
float x = ResourceManager.ConvertGameCoordinate(gameX);
|
|
float z = ResourceManager.ConvertGameCoordinate(gameZ);
|
|
float y = transform.position.y;
|
|
|
|
Vector3 targetPosition = new Vector3(x, y, z);
|
|
float speedMultiplier = speed == 1 ? 2f : 1f;
|
|
|
|
StartCoroutine(MoveCoroutine(targetPosition, speedMultiplier, endAction));
|
|
}
|
|
|
|
private IEnumerator MoveCoroutine(Vector3 targetPosition, float speedMultiplier, uint endAction)
|
|
{
|
|
float currentSpeed = _moveSpeed * speedMultiplier;
|
|
float journeyLength = Vector3.Distance(transform.position, targetPosition);
|
|
float startTime = Time.time;
|
|
|
|
Vector3 startPosition = transform.position;
|
|
|
|
while (Vector3.Distance(transform.position, targetPosition) > 0.1f)
|
|
{
|
|
float distCovered = (Time.time - startTime) * currentSpeed;
|
|
float fractionOfJourney = distCovered / journeyLength;
|
|
|
|
transform.position = Vector3.Lerp(startPosition, targetPosition, fractionOfJourney);
|
|
yield return null;
|
|
}
|
|
|
|
transform.position = targetPosition;
|
|
|
|
// Ïðèìåíÿåì êîíå÷íûé ïîâîðîò
|
|
ApplyEndAction(endAction);
|
|
|
|
IsMoving = false;
|
|
IsAnimationComplete = true;
|
|
}
|
|
|
|
private void ApplyEndAction(uint endAction)
|
|
{
|
|
switch (endAction)
|
|
{
|
|
case 1: // Ïîâîðîò íà 90°
|
|
transform.rotation = Quaternion.Euler(0, 90f, 0);
|
|
break;
|
|
case 2: // Ïîâîðîò íà 180°
|
|
transform.rotation = Quaternion.Euler(0, 180f, 0);
|
|
break;
|
|
case 3: // Ïîâîðîò íà 270°
|
|
transform.rotation = Quaternion.Euler(0, 270f, 0);
|
|
break;
|
|
}
|
|
}
|
|
|
|
public void PlayAnimation(int animationId, int repeatCount)
|
|
{
|
|
IsAnimationComplete = false;
|
|
StartCoroutine(AnimationCoroutine(animationId, repeatCount));
|
|
}
|
|
|
|
private IEnumerator AnimationCoroutine(int animationId, int repeatCount)
|
|
{
|
|
Vector3 startPosition = transform.position;
|
|
|
|
for (int i = 0; i < Mathf.Max(1, repeatCount); i++)
|
|
{
|
|
// Ïðîñòàÿ àíèìàöèÿ - ïîäíèìàåì è îïóñêàåì îáúåêò
|
|
transform.position = startPosition + Vector3.up * 50f;
|
|
yield return new WaitForSeconds(0.1f);
|
|
transform.position += Vector3.down * 50f;
|
|
yield return new WaitForSeconds(0.1f);
|
|
}
|
|
|
|
IsAnimationComplete = true;
|
|
}
|
|
|
|
public void StopAllActions()
|
|
{
|
|
StopAllCoroutines();
|
|
IsMoving = false;
|
|
IsAnimationComplete = true;
|
|
}
|
|
} |