70 lines
3.0 KiB
C#
70 lines
3.0 KiB
C#
using System;
|
|
using Actions;
|
|
using UnityEngine;
|
|
using UnityEngine.Serialization;
|
|
|
|
public class UnitAnimator : MonoBehaviour {
|
|
private const float UNIT_SHOULDER_HEIGHT = 1.7f;
|
|
private static readonly int isWalking = Animator.StringToHash("IsWalking");
|
|
private static readonly int shoot = Animator.StringToHash("Shoot");
|
|
private static readonly int swordSlash = Animator.StringToHash("SwordSlash");
|
|
private static readonly int jumpUp = Animator.StringToHash("JumpUp");
|
|
private static readonly int jumpDown = Animator.StringToHash("JumpDown");
|
|
|
|
[SerializeField] private Animator animator;
|
|
[SerializeField] private Transform bulletProjectilePrefab;
|
|
[SerializeField] private Transform rifleTransform;
|
|
[SerializeField] private Transform swordTransform;
|
|
|
|
[FormerlySerializedAs("shootPoint")] [SerializeField]
|
|
private Transform shootPointTransform;
|
|
|
|
private void Awake() {
|
|
if (TryGetComponent(out MoveAction moveAction)) {
|
|
moveAction.OnStartMoving += MoveAction_OnStartMoving;
|
|
moveAction.OnStopMoving += MoveAction_OnStopMoving;
|
|
moveAction.OnChangeFloorsStarted += MoveAction_OnChangeFloorsStarted;
|
|
}
|
|
|
|
if (TryGetComponent(out ShootAction shootAction)) {
|
|
shootAction.OnShoot += ShootAction_OnShoot;
|
|
}
|
|
|
|
if (TryGetComponent(out SwordAction swordAction)) {
|
|
swordAction.OnSwordActionStarted += SwordAction_OnSwordActionStarted;
|
|
swordAction.OnSwordActionCompleted += SwordAction_OnSwordActionCompleted;
|
|
}
|
|
}
|
|
|
|
private void Start() => EquipRifle();
|
|
|
|
private void MoveAction_OnChangeFloorsStarted(object sender, MoveAction.ChangeFloorsStartedEventArgs e) => animator.SetTrigger(e.targetGridPosition.Floor > e.unitGridPosition.Floor ? jumpUp : jumpDown);
|
|
|
|
private void SwordAction_OnSwordActionStarted(object sender, EventArgs e) {
|
|
EquipSword();
|
|
animator.SetTrigger(swordSlash);
|
|
}
|
|
|
|
private void SwordAction_OnSwordActionCompleted(object sender, EventArgs e) => EquipRifle();
|
|
|
|
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.ShootEventArgs e) {
|
|
animator.SetTrigger(shoot);
|
|
Transform bulletProjectPrefab = Instantiate(bulletProjectilePrefab, shootPointTransform.position, Quaternion.identity);
|
|
Vector3 targetUnitShootAtPosition = e.TargetUnit.GetWorldPosition();
|
|
targetUnitShootAtPosition.y = UNIT_SHOULDER_HEIGHT;
|
|
bulletProjectPrefab.GetComponent<BulletProjectile>().Setup(targetUnitShootAtPosition);
|
|
}
|
|
|
|
private void EquipSword() {
|
|
swordTransform.gameObject.SetActive(true);
|
|
rifleTransform.gameObject.SetActive(false);
|
|
}
|
|
|
|
private void EquipRifle() {
|
|
rifleTransform.gameObject.SetActive(true);
|
|
swordTransform.gameObject.SetActive(false);
|
|
}
|
|
} |