140 lines
4.2 KiB
C#
140 lines
4.2 KiB
C#
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
using System;
|
|
|
|
public class AnimationControllerAPM : MonoBehaviour
|
|
{
|
|
private Animator animator;
|
|
private AnimationClip animationUClip;
|
|
private AnimationClip animationDClip;
|
|
|
|
// Tham chiếu đến pallet và fork
|
|
public Transform pallet; // Gán GameObject của pallet trong Inspector
|
|
public Transform fork; // Gán GameObject của fork trong Inspector
|
|
|
|
public float AnimationUDuration => animationUClip != null ? animationUClip.length : 0f;
|
|
public float AnimationDDuration => animationDClip != null ? animationDClip.length : 0f;
|
|
|
|
public Action<string, string> OnAnimationStateChanged { get; set; } // ActionType, State (RUNNING/FINISHED)
|
|
|
|
private string currentActionType; // Theo dõi ActionType hiện tại (pick/drop)
|
|
|
|
private void Awake()
|
|
{
|
|
animator = GetComponent<Animator>();
|
|
animator.SetBool("U", false);
|
|
animator.SetBool("D", false);
|
|
|
|
InitializeAnimationClips();
|
|
}
|
|
|
|
private void InitializeAnimationClips()
|
|
{
|
|
if (animator == null || animator.runtimeAnimatorController == null)
|
|
{
|
|
Debug.LogError("Animator hoặc AnimatorController không được gán!");
|
|
return;
|
|
}
|
|
|
|
var controller = animator.runtimeAnimatorController;
|
|
var clips = controller.animationClips;
|
|
|
|
foreach (var clip in clips)
|
|
{
|
|
if (clip.name.Contains("Up")) // Thay bằng tên thực tế của clip U
|
|
{
|
|
animationUClip = clip;
|
|
}
|
|
else if (clip.name.Contains("Down")) // Thay bằng tên thực tế của clip D
|
|
{
|
|
animationDClip = clip;
|
|
}
|
|
}
|
|
|
|
if (animationUClip == null || animationDClip == null)
|
|
{
|
|
Debug.LogWarning("Không tìm thấy animation clip cho U hoặc D. Kiểm tra tên clip hoặc AnimatorController.");
|
|
}
|
|
}
|
|
|
|
// Kích hoạt animation "Down"
|
|
public void OnKey1()
|
|
{
|
|
DetachPallet(); // Tách pallet khỏi fork
|
|
animator.SetBool("U", false);
|
|
animator.SetBool("D", true);
|
|
currentActionType = "drop";
|
|
}
|
|
|
|
// Kích hoạt animation "Up"
|
|
public void OnKey2()
|
|
{
|
|
AttachPalletToFork(); // Gắn pallet vào fork
|
|
animator.SetBool("U", true);
|
|
animator.SetBool("D", false);
|
|
currentActionType = "pick";
|
|
}
|
|
|
|
// Phương thức gắn pallet vào fork
|
|
public void AttachPalletToFork()
|
|
{
|
|
if (pallet != null && fork != null)
|
|
{
|
|
pallet.SetParent(fork);
|
|
Debug.Log("Pallet đã được gắn vào fork.");
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("Pallet hoặc fork không được gán trong Inspector.");
|
|
}
|
|
}
|
|
|
|
// Phương thức tách pallet khỏi fork
|
|
public void DetachPallet()
|
|
{
|
|
if (pallet != null)
|
|
{
|
|
pallet.SetParent(null);
|
|
Debug.Log("Pallet đã được tách khỏi fork.");
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("Pallet không được gán trong Inspector.");
|
|
}
|
|
}
|
|
|
|
public float GetAnimationDuration(string actionType)
|
|
{
|
|
switch (actionType)
|
|
{
|
|
case "pick":
|
|
return AnimationUDuration;
|
|
case "drop":
|
|
return AnimationDDuration;
|
|
default:
|
|
Debug.LogWarning($"ActionType {actionType} không được hỗ trợ.");
|
|
return 0f;
|
|
}
|
|
}
|
|
|
|
// Gọi bởi Animation Event khi animation bắt đầu
|
|
public void OnAnimationStart()
|
|
{
|
|
if (!string.IsNullOrEmpty(currentActionType))
|
|
{
|
|
Debug.Log($"Animation bắt đầu: {currentActionType}");
|
|
OnAnimationStateChanged?.Invoke(currentActionType, "RUNNING");
|
|
}
|
|
}
|
|
|
|
// Gọi bởi Animation Event khi animation kết thúc
|
|
public void OnAnimationEnd()
|
|
{
|
|
if (!string.IsNullOrEmpty(currentActionType))
|
|
{
|
|
Debug.Log($"Animation kết thúc: {currentActionType}");
|
|
OnAnimationStateChanged?.Invoke(currentActionType, "FINISHED");
|
|
currentActionType = null; // Reset sau khi hoàn tất
|
|
}
|
|
}
|
|
} |