Advertisement
GoodNoodle

Battle NPC

Dec 21st, 2023 (edited)
1,034
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.91 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Unity.VisualScripting;
  5. using UnityEngine;
  6. using UnityEngine.Rendering;
  7. using UnityEngine.TextCore.Text;
  8. using static CharecterAnimator;
  9.  
  10. public enum Teams
  11. {
  12.     Blue,
  13.     Red,
  14.     Yellow
  15. }
  16.  
  17.  
  18.  
  19. public class BattleNpcMove : MonoBehaviour, ITeam
  20. {
  21.  
  22.     public bool shouldRotate;
  23.  
  24.     public Creature creature;
  25.  
  26.     public float checkRadius, attackRadius;
  27.  
  28.     public float speed;
  29.  
  30.  
  31.     [SerializeField] GameObject target;
  32.  
  33.     public CharecterAnimator anim;
  34.  
  35.     private Rigidbody2D therb;
  36.  
  37.     private Vector2 movement;
  38.  
  39.     Vector3 dir;
  40.  
  41.     public bool  isInAttackRange;
  42.  
  43.     public Animator theAnim;
  44.  
  45.     // Start is called before the first frame update
  46.  
  47.     [SerializeField] Teams curTeam;
  48.  
  49.     [SerializeField] FacingDirection defaultDir = FacingDirection.Down;
  50.  
  51.     // Refrences
  52.     SpriteRenderer spriteRenderer;
  53.  
  54.     public Teams CurrentTeam { get => curTeam; }
  55.  
  56.    
  57.  
  58.     public GameObject getGameObject => gameObject;
  59.  
  60.     SpriteAnimator currentAnim;
  61.     bool wasPreviouslyMoving;
  62.  
  63.     public float MoveX { get; set; }
  64.     public float MoveY { get; set; }
  65.  
  66.     public bool IsMoving { get; set; }
  67.  
  68.  
  69.  
  70.     void Start()
  71.     {
  72.         therb = GetComponent<Rigidbody2D>();
  73.  
  74.         anim = GetComponent<CharecterAnimator>();
  75.  
  76.         creature = GetComponentInParent<Party>().creatures[0];
  77.  
  78.         spriteRenderer = GetComponent<SpriteRenderer>();
  79.  
  80.         anim.SetFacingDir(defaultDir);
  81.  
  82.     }
  83.  
  84.     // Update is called once per frame
  85.     void Update()
  86.     {
  87.  
  88.      
  89.  
  90.         var prevAnim = currentAnim;
  91.  
  92.         float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
  93.         dir.Normalize();
  94.  
  95.         if (target == null)
  96.         {
  97.             FindTarget();
  98.         } else
  99.         {
  100.            // var facingDir = new Vector3(anim.MoveX, anim.MoveY);
  101.            // var interactPos = transform.position + facingDir;
  102.  
  103.             isInAttackRange = (Vector2.Distance(transform.position, target.transform.position) <= attackRadius);
  104.  
  105.             transform.position = Vector2.MoveTowards(transform.position, target.transform.position, speed * Time.deltaTime);
  106.             transform.rotation = Quaternion.Euler(Vector3.forward * angle);
  107.  
  108.             if (isInAttackRange)
  109.             {
  110.                 speed = 0;
  111.                 SetattackFacingDir(defaultDir);
  112.                 StartCoroutine(Damage());
  113.             }
  114.         }      
  115.         movement = dir;
  116.         if (shouldRotate)
  117.         {
  118.  
  119.         }
  120.  
  121.         if (creature.HP == 0)
  122.         {
  123.             Destroy(this.gameObject);
  124.         }
  125.  
  126.     }
  127.  
  128.     private void FixedUpdate()
  129.     {
  130.         if (!isInAttackRange)
  131.         {
  132.             speed = 3;
  133.             MoveCharecter(movement);
  134.         }
  135.  
  136.      
  137.     }
  138.     private void MoveCharecter(Vector2 dir)
  139.     {
  140.         therb.MovePosition((Vector2)transform.position + (dir * speed * Time.deltaTime));
  141.     }
  142.  
  143.     private IEnumerator Damage()
  144.     {
  145.         yield return new WaitForSeconds(5f);
  146.         creature.DecreaseHP(creature.MaxHP/creature.MaxHP);
  147.        
  148.     }
  149.  
  150.     public void FindTarget()
  151.     {
  152.         Collider2D[] potenTar = Physics2D.OverlapCircleAll(transform.position, checkRadius);
  153.      
  154.  
  155.         foreach (Collider2D potenEnemy in potenTar)
  156.         {
  157.             ITeam tempCollidable = potenEnemy.GetComponent<ITeam>();
  158.  
  159.             if (tempCollidable != null)
  160.             {
  161.              if(tempCollidable.CurrentTeam == curTeam)
  162.                 {
  163.                     continue;
  164.                 }
  165.                     GameObject potentailTar = tempCollidable.getGameObject;
  166.  
  167.                 if (target != null)
  168.                 {
  169.  
  170.                     float disA = Vector2.Distance(transform.position, target.transform.position);
  171.                     float disB = Vector2.Distance(transform.position, potentailTar.transform.position);
  172.  
  173.                     if (disA <= disB)
  174.                     {
  175.                         continue;
  176.                     }
  177.                 }
  178.              
  179.                         target = potentailTar;
  180.             }
  181.         }
  182.  
  183.     }
  184.  
  185.     public void SetattackFacingDir(FacingDirection dir)
  186.     {
  187.        // if(isInAttackRange)
  188.         if (dir == FacingDirection.Right)
  189.             currentAnim = anim.attackRightAnim;
  190.         else if (dir == FacingDirection.Left)
  191.             currentAnim = anim.attackLeftAnim;
  192.         else if (dir == FacingDirection.Down)
  193.             currentAnim = anim.attackDownAnim;
  194.         else if (dir == FacingDirection.Up)
  195.             currentAnim = anim.attackUpAnim;
  196.     }
  197.  
  198.     // Vector2 getRoamingPos()
  199.     // {
  200.     //    return startingPos + getRandomDir() * UnityEngine.Random.Range(10f, 30f);
  201.     // }
  202.     // private Vector2 getRandomDir()
  203.     //  {
  204.     //     return new Vector2(UnityEngine.Random.Range(-1f, 1f), UnityEngine.Random.Range(-1f, 1f)).normalized;
  205.     // }
  206.  
  207. }
  208.  
  209.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement