Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Pathfinding;
- public class BoxEnemy : MonoBehaviour
- {
- private GameObject playerP;
- private GameObject player;
- [SerializeField] private Transform target;
- [SerializeField] private GameObject proj;
- [SerializeField] private Transform FirePoint;
- [SerializeField] private ParticleSystem p_damage;
- [SerializeField] private ParticleSystem p_die;
- [SerializeField] private SpriteRenderer sp;
- [SerializeField] private float speed = 200f;
- [SerializeField] private float jumpSpeed = 200f;
- [SerializeField] private float jumpCooldown;
- [SerializeField] private float counterMovement;
- [SerializeField] private float nextWaypointDistance = 3f;
- [SerializeField] private float health = 10;
- [SerializeField] private Color damageColor;
- [SerializeField] private float fireRate;
- [SerializeField] private bool IsGrounded;
- private float fireTime;
- Path path;
- int cw = 0;
- bool reachedEndOfPath = false;
- Seeker seeker;
- Rigidbody2D rb;
- Color oc;
- void Awake()
- {
- playerP = GameObject.Find("PlayerParent");
- player = playerP.transform.Find("Player").gameObject;
- seeker = GetComponent<Seeker>();
- rb = GetComponent<Rigidbody2D>();
- oc = sp.color;
- InvokeRepeating("UpdatePath", 0f, .1f);
- }
- void UpdatePath()
- {
- if(seeker.IsDone())
- seeker.StartPath(rb.position, target.position, OnPathComplete);
- }
- void Shoot()
- {
- if(fireTime <= 0)
- {
- Vector3 mousePos2;
- mousePos2 = player.transform.position;
- mousePos2.z = 5.23f;
- Vector3 objectPos2 = transform.position;
- mousePos2.x = mousePos2.x - objectPos2.x;
- mousePos2.y = mousePos2.y - objectPos2.y;
- float a = Mathf.Atan2(mousePos2.y, mousePos2.x) * Mathf.Rad2Deg;
- Quaternion dr2 = Quaternion.Euler(new Vector3(0, 0, a));
- fireTime = fireRate;
- Instantiate(proj, FirePoint.position, dr2);
- }
- }
- void OnPathComplete(Path p)
- {
- if(!p.error)
- {
- path = p;
- cw = 0;
- }
- }
- bool died = false;
- [SerializeField] Vector2 force;
- void Update()
- {
- if (IsGrounded)
- {
- rb.drag = counterMovement;
- }
- else
- {
- rb.drag = 0;
- }
- if(fireTime > 0)
- {
- fireTime -= Time.deltaTime;
- }
- if(health <= 0 && died == false)
- {
- died = true;
- StartCoroutine(Die());
- }
- if(died)
- return;
- if(path == null)
- return;
- if(cw >= path.vectorPath.Count || path.vectorPath.Count < 1)
- {
- reachedEndOfPath = true;
- return;
- } else
- {
- reachedEndOfPath = false;
- }
- float distance = Vector3.Distance(transform.position, player.transform.position);
- if(distance <= 2)
- {
- force = Vector2.zero;
- Shoot();
- return;
- }
- if(distance <= 5)
- {
- Shoot();
- }
- Vector2 dir = ((Vector2)path.vectorPath[cw] - rb.position).normalized;
- force = dir * speed * Time.deltaTime;
- float dist = Vector2.Distance(rb.position, path.vectorPath[cw]);
- if(dist < nextWaypointDistance)
- {
- cw++;
- }
- }
- bool jumpCoolDown;
- private void FixedUpdate()
- {
- if(force.x >= 0.01f && IsGrounded)
- {
- rb.AddForce(new Vector2(1 * speed, 0));
- }
- else if(force.x <= -0.01f && IsGrounded)
- {
- rb.AddForce(new Vector2(-1 * speed, 0));
- }
- else if(force.y >= 0.03f && IsGrounded)
- {
- Jump();
- }
- if(force.x >= 0.01f && !IsGrounded)
- {
- rb.velocity = new Vector2(1 * speed / 2, rb.velocity.y);
- }
- else if(force.x <= -0.01f && !IsGrounded)
- {
- rb.velocity = new Vector2(-1 * speed / 2, rb.velocity.y);
- }
- }
- private void Jump()
- {
- // Makes it so the player cannot spam jumping.
- jumpCoolDown = true;
- StartCoroutine(JumpReset());
- // Adds force upward.
- rb.velocity = new Vector2(rb.velocity.x, jumpSpeed);
- }
- public IEnumerator JumpReset()
- {
- // Wait for time: 0.2 seconds.
- yield return new WaitForSeconds(jumpCooldown);
- // Sets the jumpCoolDown var to true.
- jumpCoolDown = false;
- }
- private void OnCollisionEnter2D(Collision2D other)
- {
- if(other.transform.tag == "Bullet")
- {
- Damage(1);
- }
- if(other.transform.tag == "Ground")
- {
- IsGrounded = true;
- }
- }
- private void OnCollisionExit2D(Collision2D other)
- {
- if(other.transform.tag == "Ground")
- {
- IsGrounded = false;
- }
- }
- void Damage(float amount)
- {
- p_damage.Play();
- health -= amount;
- StartCoroutine(DamageIE());
- }
- IEnumerator DamageIE()
- {
- sp.color = damageColor;
- yield return new WaitForSeconds(0.1f);
- sp.color = oc;
- }
- IEnumerator Die()
- {
- CancelInvoke();
- rb.simulated = false;
- GetComponent<BoxCollider2D>().enabled = false;
- sp.enabled = false;
- p_die.Play();
- yield return new WaitForSeconds(3);
- Destroy(gameObject);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement