Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using Unity.VisualScripting;
- using UnityEngine;
- using UnityEngine.Rendering;
- using UnityEngine.TextCore.Text;
- using static CharecterAnimator;
- public enum Teams
- {
- Blue,
- Red,
- Yellow
- }
- public class BattleNpcMove : MonoBehaviour, ITeam
- {
- public bool shouldRotate;
- public Creature creature;
- public float checkRadius, attackRadius;
- public float speed;
- [SerializeField] GameObject target;
- public CharecterAnimator anim;
- private Rigidbody2D therb;
- private Vector2 movement;
- Vector3 dir;
- public bool isInAttackRange;
- public Animator theAnim;
- // Start is called before the first frame update
- [SerializeField] Teams curTeam;
- [SerializeField] FacingDirection defaultDir = FacingDirection.Down;
- // Refrences
- SpriteRenderer spriteRenderer;
- public Teams CurrentTeam { get => curTeam; }
- public GameObject getGameObject => gameObject;
- SpriteAnimator currentAnim;
- bool wasPreviouslyMoving;
- public float MoveX { get; set; }
- public float MoveY { get; set; }
- public bool IsMoving { get; set; }
- void Start()
- {
- therb = GetComponent<Rigidbody2D>();
- anim = GetComponent<CharecterAnimator>();
- creature = GetComponentInParent<Party>().creatures[0];
- spriteRenderer = GetComponent<SpriteRenderer>();
- anim.SetFacingDir(defaultDir);
- }
- // Update is called once per frame
- void Update()
- {
- var prevAnim = currentAnim;
- float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
- dir.Normalize();
- if (target == null)
- {
- FindTarget();
- } else
- {
- // var facingDir = new Vector3(anim.MoveX, anim.MoveY);
- // var interactPos = transform.position + facingDir;
- isInAttackRange = (Vector2.Distance(transform.position, target.transform.position) <= attackRadius);
- transform.position = Vector2.MoveTowards(transform.position, target.transform.position, speed * Time.deltaTime);
- transform.rotation = Quaternion.Euler(Vector3.forward * angle);
- if (isInAttackRange)
- {
- speed = 0;
- SetattackFacingDir(defaultDir);
- StartCoroutine(Damage());
- }
- }
- movement = dir;
- if (shouldRotate)
- {
- }
- if (creature.HP == 0)
- {
- Destroy(this.gameObject);
- }
- }
- private void FixedUpdate()
- {
- if (!isInAttackRange)
- {
- speed = 3;
- MoveCharecter(movement);
- }
- }
- private void MoveCharecter(Vector2 dir)
- {
- therb.MovePosition((Vector2)transform.position + (dir * speed * Time.deltaTime));
- }
- private IEnumerator Damage()
- {
- yield return new WaitForSeconds(5f);
- creature.DecreaseHP(creature.MaxHP/creature.MaxHP);
- }
- public void FindTarget()
- {
- Collider2D[] potenTar = Physics2D.OverlapCircleAll(transform.position, checkRadius);
- foreach (Collider2D potenEnemy in potenTar)
- {
- ITeam tempCollidable = potenEnemy.GetComponent<ITeam>();
- if (tempCollidable != null)
- {
- if(tempCollidable.CurrentTeam == curTeam)
- {
- continue;
- }
- GameObject potentailTar = tempCollidable.getGameObject;
- if (target != null)
- {
- float disA = Vector2.Distance(transform.position, target.transform.position);
- float disB = Vector2.Distance(transform.position, potentailTar.transform.position);
- if (disA <= disB)
- {
- continue;
- }
- }
- target = potentailTar;
- }
- }
- }
- public void SetattackFacingDir(FacingDirection dir)
- {
- // if(isInAttackRange)
- if (dir == FacingDirection.Right)
- currentAnim = anim.attackRightAnim;
- else if (dir == FacingDirection.Left)
- currentAnim = anim.attackLeftAnim;
- else if (dir == FacingDirection.Down)
- currentAnim = anim.attackDownAnim;
- else if (dir == FacingDirection.Up)
- currentAnim = anim.attackUpAnim;
- }
- // Vector2 getRoamingPos()
- // {
- // return startingPos + getRandomDir() * UnityEngine.Random.Range(10f, 30f);
- // }
- // private Vector2 getRandomDir()
- // {
- // return new Vector2(UnityEngine.Random.Range(-1f, 1f), UnityEngine.Random.Range(-1f, 1f)).normalized;
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement