Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class EnemyBase : MonoBehaviour
- {
- [SerializeField]
- protected float health;
- [SerializeField]
- protected float speed;
- [SerializeField]
- protected float idleTime;
- [SerializeField]
- protected Transform [] waypoints;
- [SerializeField]
- protected int WpID;
- protected Transform targetWp;
- [SerializeField]
- protected Transform target;
- protected Animator anim;
- protected SpriteRenderer sprite;
- protected bool isHit = false;
- //Player position variable
- [SerializeField]
- protected PlayerControl player_script;
- //Distance check variable.
- [SerializeField]
- protected float d_Check;
- protected float d_ToTarget;
- protected float player_dist;
- [SerializeField]
- protected float aggroDist;
- protected Vector3 t_Direction;
- protected BoxCollider2D boxCollider2D;
- public bool isDead = false;
- [SerializeField]
- protected bool isFacingLeft;
- protected bool inCombat = false;
- protected bool inReverse;
- protected bool isAggroed;
- [SerializeField]
- protected bool isRanged;
- public bool isFlipped;
- public float deathTimer;
- public float Health{get;set;}
- //
- //public GameObject[] _lootTable;
- //public Vector3 _dropOffset;
- //protected Diamond _diamondScript;
- public virtual void Init()
- {
- anim = GetComponentInChildren<Animator>();
- sprite = GetComponentInChildren<SpriteRenderer>();
- boxCollider2D = GetComponent<BoxCollider2D>();
- inReverse = false;
- isFlipped = false;
- WpID = 0;
- if(waypoints.Length >= 2)
- {
- targetWp = waypoints[WpID];
- }
- else if(waypoints.Length < 2)
- {
- Debug.Log("Not Enough waypoints");
- }
- player_script = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerControl>();
- }
- // Start is called before the first frame update
- void Start()
- {
- Init();
- }
- // Update is called once per frame
- public virtual void Update()
- {
- /* if((anim.GetCurrentAnimatorStateInfo(0).IsName("Idle"))&& (anim.GetBool("InCombat") == false))
- {
- return;
- } */
- AILogic();
- DeathCheck();
- }
- void AILogic()
- {
- //runs code based on the bool variable "isRanged"
- if(isRanged == false)
- {
- MeleeAILogic();
- }
- else if(isRanged == true)
- {
- RangeAILogic();
- }
- }
- void RangeAILogic()
- {
- if(isDead == false)
- {
- FaceTarget();
- TargetDistanceCheck();
- if(inCombat == false)
- {
- RangedPatrolLogic();
- }
- }
- }
- void MeleeAILogic()
- {
- if(isDead == false)
- {
- if((inCombat == false)&&(waypoints.Length >= 2))
- {
- if(isAggroed == false)
- {
- if(isDead == false)
- {
- PatrolLogic();
- }
- anim.SetBool("InCombat",false);
- target = targetWp;
- }
- }
- else if(inCombat == true)
- {
- target = player_script.transform;
- anim.SetBool("InCombat",true);
- FaceTarget();
- }
- if((isAggroed == true)&&(inCombat == false))
- {
- AggroLogic();
- }
- FaceTarget();
- TargetDistanceCheck();
- }
- }
- public virtual void DeathCheck()
- {
- if(Health == 0)
- {
- isDead = true;
- }
- if(isDead == true)
- {
- //boxCollider2D.enabled = false;
- anim.SetTrigger("Death");
- Destroy(this.gameObject,deathTimer);
- //play death anim
- //destroy this game object
- }
- }
- void AggroLogic()
- {
- FaceTarget();
- target = player_script.transform;
- if(transform.position.x != target.position.x)
- {
- transform.position = Vector3.MoveTowards(transform.position,target.position,speed * Time.deltaTime);
- anim.SetBool("IsWalking",true);
- }
- }
- public void TargetDistanceCheck()
- {
- player_dist = Vector3.Distance(transform.localPosition,player_script.transform.localPosition);
- //Debug.Log("distance to player:" + player_dist);
- if(player_dist <= d_Check)
- {
- //Debug.Log("Player target found");
- target = player_script.transform;
- FaceTarget();
- inCombat = true;
- anim.SetBool("InCombat",true);
- }
- else if(player_dist > d_Check)
- {
- //Debug.Log("Target lost");
- //target = targetWp;
- FaceTarget();
- inCombat = false;
- anim.SetBool("InCombat",false);
- }
- AggroDistanceCheck(player_dist);
- }
- void AggroDistanceCheck(float t_distance)
- {
- if(t_distance <= aggroDist)
- {
- isAggroed = true;
- //Debug.Log("Aggro'd:" + isAggroed);
- }
- else if(t_distance > aggroDist)
- {
- isAggroed = false;
- //Debug.Log("Aggro'd:" + isAggroed);
- }
- d_ToTarget = t_distance;
- }
- void RangedPatrolLogic()
- {
- }
- void PatrolLogic()
- {
- if((transform.position.x != targetWp.position.x)&&(isHit == false))
- {
- if(inCombat == false)
- {
- anim.SetBool("IsWalking",true);
- transform.position = Vector3.MoveTowards(transform.position,targetWp.position,speed * Time.deltaTime);
- }
- }
- if((transform.position.x == targetWp.position.x)&&(isHit == false))
- {
- if((inCombat == false)&&(inReverse == false))
- {
- if(WpID < waypoints.Length - 1)
- {
- WpID++;
- targetWp = waypoints[WpID];
- target = targetWp;
- }
- else if(WpID == waypoints.Length - 1)
- {
- inReverse = true;
- }
- }
- else if((inCombat == false)&&(inReverse == true))
- {
- if(WpID > 0)
- {
- WpID --;
- targetWp = waypoints[WpID];
- target = targetWp;
- }
- else if(WpID == 0)
- {
- inReverse = false;
- }
- }
- }
- }
- public virtual void FaceTarget()
- {
- //determine where the target is and it's distance to this object
- //determine if the target is either on the left or right of this object
- //check if this object's sprite is facing left
- //flip the sprite to face target
- if((targetWp != null)&&(isAggroed == false))
- {
- target = targetWp;
- }
- else if((targetWp == null)||(isAggroed == true))
- {
- target = player_script.transform;
- }
- t_Direction = target.localPosition - transform.localPosition;
- //Debug.Log("target direction:" + t_Direction);
- if(isFacingLeft == true)
- {
- if(t_Direction.x > 0)
- {
- //180 rotation
- isFlipped = true;
- Vector3 eulerRotation = transform.rotation.eulerAngles;
- transform.rotation = Quaternion.Euler(eulerRotation.x,180,eulerRotation.z);
- }
- else if(t_Direction.x < 0)
- {
- //0 rotation
- isFlipped = false;
- Vector3 eulerRotation = transform.rotation.eulerAngles;
- transform.rotation = Quaternion.Euler(eulerRotation.x,0,eulerRotation.z);
- }
- }
- else if(isFacingLeft == false)
- {
- if(t_Direction.x > 0)
- {
- //0 rotation
- isFlipped = false;
- Vector3 eulerRotation = transform.rotation.eulerAngles;
- transform.rotation = Quaternion.Euler(eulerRotation.x,0,eulerRotation.z);
- }
- else if(t_Direction.x < 0)
- {
- //180 rotation
- isFlipped = true;
- Vector3 eulerRotation = transform.rotation.eulerAngles;
- transform.rotation = Quaternion.Euler(eulerRotation.x,180,eulerRotation.z);
- }
- }
- //float distance = Vector3.Distance(transform.localPosition,player_script.transform.localPosition);
- //Vector3 direction = player_script.transform.localPosition - transform.localPosition;
- }
- public void Damage()
- {
- if(isDead != true)
- {
- //Enemy takes damage
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement