using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UnitAnimator : MonoBehaviour
{
[SerializeField] private Animator unitAnimator;
[SerializeField] private Transform bulletProjectilePrefab;
[SerializeField] private Transform grenadeProjectilePrefab;
[SerializeField] private Transform shootPointTransform;
[SerializeField] private Transform rifleTransform;
[SerializeField] private Transform swordTransform;
private void Start() {
EquipRifle();
}
private void Awake() {
if (TryGetComponent<MoveAction>(out MoveAction moveAction)) {
moveAction.OnStartMoving += moveAction_OnStartMoving;
moveAction.OnStopMoving += moveAction_OnStopMoving;
}
if (TryGetComponent<ShootAction>(out ShootAction shootAction)) {
shootAction.onShoot += shootAction_onShoot;
}
if (TryGetComponent<GrenadeAction>(out GrenadeAction grenadeAction)) {
grenadeAction.onGrenadeThrow += grenadeAction_onGrenadeThrow;
}
if (TryGetComponent<SwordAction>(out SwordAction swordAction)) {
swordAction.onSwordActionStarted += swordAction_onSwordActionStarted;
swordAction.onSwordActionCompleted += swordAction_onSwordActionCompleted;
}
}
private void swordAction_onSwordActionCompleted(object sender, EventArgs e)
{
EquipRifle();
}
private void swordAction_onSwordActionStarted(object sender, EventArgs e)
{
EquipSword();
unitAnimator.SetTrigger("SwordSlash");
}
private void grenadeAction_onGrenadeThrow(object sender, GrenadeAction.onThrowEventArgs grenadeEvent)
{
unitAnimator.SetTrigger("Shoot");
Transform projectileTransform =
Instantiate(grenadeProjectilePrefab, shootPointTransform.position, Quaternion.identity);
GrenadeProjectile projectile = projectileTransform.GetComponent<GrenadeProjectile>();
Vector3 targetUnitShootAtPosition = LevelGrid.Instance.GetWorldPosition(grenadeEvent.targetGridPosition);
targetUnitShootAtPosition.y = 0;
projectile.Setup(targetUnitShootAtPosition, grenadeEvent.grenadeExplodeAction);
}
private void moveAction_OnStartMoving(object sender, EventArgs empty) {
unitAnimator.SetBool("IsWalking",true);
}
private void moveAction_OnStopMoving(object sender, EventArgs empty) {
unitAnimator.SetBool("IsWalking",false);
}
private void shootAction_onShoot(object sender, ShootAction.onShootEventArgs shootEvent) {
unitAnimator.SetTrigger("Shoot");
Transform projectileTransform =
Instantiate(bulletProjectilePrefab, shootPointTransform.position, Quaternion.identity);
BulletProjectile bulletProjectile = projectileTransform.GetComponent<BulletProjectile>();
Vector3 targetUnitShootAtPosition = shootEvent.targetUnit.GetWorldPosition();
targetUnitShootAtPosition.y = bulletProjectile.transform.position.y;
bulletProjectile.Setup(targetUnitShootAtPosition, EmptyFunc);
}
private void EmptyFunc() {}
private void EquipSword() {
rifleTransform.gameObject.SetActive(false);
swordTransform.gameObject.SetActive(true);
}
private void EquipRifle() {
swordTransform.gameObject.SetActive(false);
rifleTransform.gameObject.SetActive(true);
}
}