Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UIElements;
- public class CharecterAnimator : MonoBehaviour
- {
- [SerializeField] List<Sprite> walkDownSprites;
- [SerializeField] List<Sprite> walkUpSprites;
- [SerializeField] List<Sprite> walkRightSprites;
- [SerializeField] List<Sprite> walkLeftSprites;
- [SerializeField] List<Sprite> torchDownSprites;
- [SerializeField] List<Sprite> torchUpSprites;
- [SerializeField] List<Sprite> torchRightSprites;
- [SerializeField] List<Sprite> torchLeftSprites;
- [SerializeField] List<Sprite> attackDownSprites;
- [SerializeField] List<Sprite> attackUpSprites;
- [SerializeField] List<Sprite> attackRightSprites;
- [SerializeField] List<Sprite> attackLeftSprites;
- [SerializeField] List<Sprite> surfSprites;
- [SerializeField] FacingDirection defaultDir = FacingDirection.Down;
- BattleNpcMove battlenpc;
- public bool isBattlenpc;
- // Parameters
- public float MoveX { get; set; }
- public float MoveY { get; set; }
- public bool IsMoving { get; set; }
- public bool IsJumping { get; set; }
- public bool IsSurfing { get; set; }
- public bool IsSHoldingTorch{ get; set; }
- public bool IsSHoldingFreezeItem { get; set; }
- // States
- [Header("Walk")]
- SpriteAnimator walkDownAnim;
- SpriteAnimator walkUpAnim;
- SpriteAnimator walkRightAnim;
- SpriteAnimator walkLeftAnim;
- [Header("Attack")]
- SpriteAnimator attackDownAnim;
- SpriteAnimator attackUpAnim;
- SpriteAnimator attackRightAnim;
- SpriteAnimator attackLeftAnim;
- [Header("Torch")]
- SpriteAnimator torchDownAnim;
- SpriteAnimator torchUpAnim;
- SpriteAnimator torchRightAnim;
- SpriteAnimator torchLeftAnim;
- [Header("Freeze Item")]
- SpriteAnimator freezeDownAnim;
- SpriteAnimator freezeUpAnim;
- SpriteAnimator freezeRightAnim;
- SpriteAnimator freezeLeftAnim;
- SpriteAnimator currentAnim;
- bool wasPreviouslyMoving;
- // Refrences
- SpriteRenderer spriteRenderer;
- private void Start()
- {
- spriteRenderer = GetComponent<SpriteRenderer>();
- walkDownAnim = new SpriteAnimator(walkDownSprites, spriteRenderer);
- walkUpAnim = new SpriteAnimator(walkUpSprites, spriteRenderer);
- walkRightAnim = new SpriteAnimator(walkRightSprites, spriteRenderer);
- walkLeftAnim = new SpriteAnimator(walkLeftSprites, spriteRenderer);
- torchDownAnim = new SpriteAnimator(torchDownSprites, spriteRenderer);
- torchUpAnim = new SpriteAnimator(torchUpSprites, spriteRenderer);
- torchRightAnim = new SpriteAnimator(torchRightSprites, spriteRenderer);
- torchLeftAnim = new SpriteAnimator(torchLeftSprites, spriteRenderer);
- attackUpAnim = new SpriteAnimator(attackUpSprites,spriteRenderer);
- attackDownAnim = new SpriteAnimator(attackDownSprites,spriteRenderer);
- attackLeftAnim = new SpriteAnimator(attackLeftSprites,spriteRenderer);
- attackRightAnim = new SpriteAnimator(attackRightSprites,spriteRenderer);
- battlenpc = GetComponent<BattleNpcMove>();
- SetFacingDir(defaultDir);
- currentAnim = walkDownAnim;
- }
- private void Update()
- {
- var prevAnim = currentAnim;
- if(!IsSurfing)
- {
- if (MoveX == 1)
- currentAnim = walkRightAnim;
- else if (MoveX == -1)
- currentAnim = walkLeftAnim;
- else if (MoveY == 1)
- currentAnim = walkUpAnim;
- else if (MoveY == -1)
- currentAnim = walkDownAnim;
- if (currentAnim != prevAnim || IsMoving != wasPreviouslyMoving)
- currentAnim.Start();
- if (IsMoving)
- currentAnim.HandleUpdate();
- else
- spriteRenderer.sprite = currentAnim.Frames[0];
- } else
- {
- if (MoveX == 1)
- spriteRenderer.sprite = surfSprites[2];
- else if (MoveX == -1)
- spriteRenderer.sprite = surfSprites[3];
- else if (MoveY == 1)
- spriteRenderer.sprite = surfSprites[1];
- else if (MoveY == -1)
- spriteRenderer.sprite = surfSprites[0];
- }
- if(IsSHoldingTorch)
- if (MoveX == 1)
- currentAnim = torchRightAnim;
- else if (MoveX == -1)
- currentAnim = torchLeftAnim;
- else if (MoveY == 1)
- currentAnim = torchUpAnim;
- else if (MoveY == -1)
- currentAnim = torchDownAnim;
- if (IsSHoldingFreezeItem)
- if (MoveX == 1)
- currentAnim = freezeRightAnim;
- else if (MoveX == -1)
- currentAnim = freezeLeftAnim;
- else if (MoveY == 1)
- currentAnim = freezeUpAnim;
- else if (MoveY == -1)
- currentAnim = freezeDownAnim;
- if (isBattlenpc == true)
- {
- if (battlenpc.isInAttackRange)
- if (MoveX == 1)
- currentAnim = attackRightAnim;
- else if (MoveX == -1)
- currentAnim = attackLeftAnim;
- else if (MoveY == 1)
- currentAnim = attackUpAnim;
- else if (MoveY == -1)
- currentAnim = attackDownAnim;
- }
- wasPreviouslyMoving = IsMoving;
- }
- public void SetFacingDir(FacingDirection dir)
- {
- if (dir == FacingDirection.Right)
- MoveX = 1f;
- else if (dir == FacingDirection.Left)
- MoveX = -1f;
- else if (dir == FacingDirection.Down)
- MoveY = -1f;
- else if (dir == FacingDirection.Up)
- MoveY = 1f;
- }
- public FacingDirection DefultDirection
- {
- get => defaultDir;
- }
- public enum FacingDirection
- {
- Up,
- Down,
- Left,
- Right
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment