34 lines
1.5 KiB
C#
34 lines
1.5 KiB
C#
using System;
|
|
using Actions;
|
|
using UnityEngine;
|
|
using UnityEngine.Serialization;
|
|
|
|
public class UnitAnimator : MonoBehaviour {
|
|
[SerializeField] private Animator animator;
|
|
[SerializeField] private Transform bulletProjectilePrefab;
|
|
[FormerlySerializedAs("shootPoint")] [SerializeField] private Transform shootPointTransform;
|
|
private static readonly int isWalking = Animator.StringToHash("IsWalking");
|
|
private static readonly int shoot = Animator.StringToHash("Shoot");
|
|
|
|
private void Awake() {
|
|
if (TryGetComponent(out MoveAction moveAction)) {
|
|
moveAction.OnStartMoving += MoveAction_OnStartMoving;
|
|
moveAction.OnStopMoving += MoveAction_OnStopMoving;
|
|
}
|
|
|
|
if (TryGetComponent(out ShootAction shootAction)) {
|
|
shootAction.OnShoot += ShootAction_OnShoot;
|
|
}
|
|
}
|
|
|
|
private void MoveAction_OnStartMoving(object sender, EventArgs e) => animator.SetBool(isWalking, true);
|
|
private void MoveAction_OnStopMoving(object sender, EventArgs e) => animator.SetBool(isWalking, false);
|
|
private void ShootAction_OnShoot(object sender, ShootAction.OnShootEventArgs e) {
|
|
animator.SetTrigger(shoot);
|
|
Transform bulletProjectPrefab = Instantiate(bulletProjectilePrefab, shootPointTransform.position, Quaternion.identity);
|
|
Vector3 targetUnitShootAtPosition = e.TargetUnit.GetWorldPosition();
|
|
targetUnitShootAtPosition.y = shootPointTransform.position.y;
|
|
bulletProjectPrefab.GetComponent<BulletProjectile>().Setup(targetUnitShootAtPosition);
|
|
}
|
|
}
|