GoodNoodle

CharecterAnimator.cs

Aug 16th, 2022 (edited)
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.99 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UIElements;
  5.  
  6. public class CharecterAnimator : MonoBehaviour
  7. {
  8.     [SerializeField] List<Sprite> walkDownSprites;
  9.     [SerializeField] List<Sprite> walkUpSprites;
  10.     [SerializeField] List<Sprite> walkRightSprites;
  11.     [SerializeField] List<Sprite> walkLeftSprites;
  12.  
  13.  
  14.     [SerializeField] List<Sprite> torchDownSprites;
  15.     [SerializeField] List<Sprite> torchUpSprites;
  16.     [SerializeField] List<Sprite> torchRightSprites;
  17.     [SerializeField] List<Sprite> torchLeftSprites;
  18.  
  19.     [SerializeField] List<Sprite> attackDownSprites;
  20.     [SerializeField] List<Sprite> attackUpSprites;
  21.     [SerializeField] List<Sprite> attackRightSprites;
  22.     [SerializeField] List<Sprite> attackLeftSprites;
  23.  
  24.  
  25.     [SerializeField] List<Sprite> surfSprites;
  26.     [SerializeField] FacingDirection defaultDir = FacingDirection.Down;
  27.  
  28.     BattleNpcMove battlenpc;
  29.  
  30.    public bool isBattlenpc;
  31.  
  32.     // Parameters
  33.     public float MoveX { get; set; }
  34.     public float MoveY { get; set; }
  35.     public bool IsMoving { get; set; }
  36.     public bool IsJumping { get; set; }
  37.  
  38.     public bool IsSurfing { get; set; }
  39.  
  40.     public bool IsSHoldingTorch{ get; set; }
  41.  
  42.     public bool IsSHoldingFreezeItem { get; set; }
  43.  
  44.  
  45.  
  46.  
  47.     // States
  48.     [Header("Walk")]
  49.     SpriteAnimator walkDownAnim;
  50.     SpriteAnimator walkUpAnim;
  51.     SpriteAnimator walkRightAnim;
  52.     SpriteAnimator walkLeftAnim;
  53.    
  54.     [Header("Attack")]
  55.     SpriteAnimator attackDownAnim;
  56.     SpriteAnimator attackUpAnim;
  57.     SpriteAnimator attackRightAnim;
  58.     SpriteAnimator attackLeftAnim;
  59.  
  60.     [Header("Torch")]
  61.     SpriteAnimator torchDownAnim;
  62.     SpriteAnimator torchUpAnim;
  63.     SpriteAnimator torchRightAnim;
  64.     SpriteAnimator torchLeftAnim;
  65.  
  66.     [Header("Freeze Item")]
  67.     SpriteAnimator freezeDownAnim;
  68.     SpriteAnimator freezeUpAnim;
  69.     SpriteAnimator freezeRightAnim;
  70.     SpriteAnimator freezeLeftAnim;
  71.  
  72.  
  73.     SpriteAnimator currentAnim;
  74.     bool wasPreviouslyMoving;
  75.  
  76.     // Refrences
  77.     SpriteRenderer spriteRenderer;
  78.  
  79.     private void Start()
  80.     {
  81.  
  82.         spriteRenderer = GetComponent<SpriteRenderer>();
  83.         walkDownAnim = new SpriteAnimator(walkDownSprites, spriteRenderer);
  84.         walkUpAnim = new SpriteAnimator(walkUpSprites, spriteRenderer);
  85.         walkRightAnim = new SpriteAnimator(walkRightSprites, spriteRenderer);
  86.         walkLeftAnim = new SpriteAnimator(walkLeftSprites, spriteRenderer);
  87.  
  88.         torchDownAnim = new SpriteAnimator(torchDownSprites, spriteRenderer);
  89.         torchUpAnim = new SpriteAnimator(torchUpSprites, spriteRenderer);
  90.         torchRightAnim = new SpriteAnimator(torchRightSprites, spriteRenderer);
  91.         torchLeftAnim = new SpriteAnimator(torchLeftSprites, spriteRenderer);
  92.  
  93.         attackUpAnim = new SpriteAnimator(attackUpSprites,spriteRenderer);
  94.         attackDownAnim = new SpriteAnimator(attackDownSprites,spriteRenderer);
  95.         attackLeftAnim = new SpriteAnimator(attackLeftSprites,spriteRenderer);
  96.         attackRightAnim = new SpriteAnimator(attackRightSprites,spriteRenderer);
  97.  
  98.         battlenpc = GetComponent<BattleNpcMove>();
  99.  
  100.         SetFacingDir(defaultDir);
  101.  
  102.         currentAnim = walkDownAnim;
  103.     }
  104.  
  105.     private void Update()
  106.     {
  107.        
  108.         var prevAnim = currentAnim;
  109.  
  110.         if(!IsSurfing)
  111.         {
  112.             if (MoveX == 1)
  113.                 currentAnim = walkRightAnim;
  114.             else if (MoveX == -1)
  115.                 currentAnim = walkLeftAnim;
  116.             else if (MoveY == 1)
  117.                 currentAnim = walkUpAnim;
  118.             else if (MoveY == -1)
  119.                 currentAnim = walkDownAnim;
  120.  
  121.             if (currentAnim != prevAnim || IsMoving != wasPreviouslyMoving)
  122.                 currentAnim.Start();
  123.  
  124.             if (IsMoving)
  125.                 currentAnim.HandleUpdate();
  126.             else
  127.                 spriteRenderer.sprite = currentAnim.Frames[0];
  128.         } else
  129.         {
  130.             if (MoveX == 1)
  131.                 spriteRenderer.sprite = surfSprites[2];
  132.             else if (MoveX == -1)
  133.                 spriteRenderer.sprite = surfSprites[3];
  134.             else if (MoveY == 1)
  135.                 spriteRenderer.sprite = surfSprites[1];
  136.             else if (MoveY == -1)
  137.                 spriteRenderer.sprite = surfSprites[0];
  138.         }
  139.  
  140.         if(IsSHoldingTorch)
  141.         if (MoveX == 1)
  142.             currentAnim = torchRightAnim;
  143.         else if (MoveX == -1)
  144.             currentAnim = torchLeftAnim;
  145.         else if (MoveY == 1)
  146.             currentAnim = torchUpAnim;
  147.         else if (MoveY == -1)
  148.             currentAnim = torchDownAnim;
  149.  
  150.         if (IsSHoldingFreezeItem)
  151.             if (MoveX == 1)
  152.                 currentAnim = freezeRightAnim;
  153.             else if (MoveX == -1)
  154.                 currentAnim = freezeLeftAnim;
  155.             else if (MoveY == 1)
  156.                 currentAnim = freezeUpAnim;
  157.             else if (MoveY == -1)
  158.                 currentAnim = freezeDownAnim;
  159.  
  160.         if (isBattlenpc == true)
  161.         {
  162.             if (battlenpc.isInAttackRange)
  163.                 if (MoveX == 1)
  164.                     currentAnim = attackRightAnim;
  165.                 else if (MoveX == -1)
  166.                     currentAnim = attackLeftAnim;
  167.                 else if (MoveY == 1)
  168.                     currentAnim = attackUpAnim;
  169.                 else if (MoveY == -1)
  170.                     currentAnim = attackDownAnim;
  171.         }
  172.  
  173.         wasPreviouslyMoving = IsMoving;
  174.     }
  175.  
  176.     public void SetFacingDir(FacingDirection dir)
  177.     {
  178.         if (dir == FacingDirection.Right)
  179.             MoveX = 1f;
  180.         else if (dir == FacingDirection.Left)
  181.             MoveX = -1f;
  182.         else if (dir == FacingDirection.Down)
  183.             MoveY = -1f;
  184.         else if (dir == FacingDirection.Up)
  185.             MoveY = 1f;
  186.     }
  187.     public FacingDirection DefultDirection
  188.     {
  189.         get => defaultDir;
  190.     }
  191.     public enum FacingDirection
  192.     {
  193.         Up,
  194.         Down,
  195.         Left,
  196.         Right
  197.     }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment