ofb/Assets/Scripts/EnemyPatrol.cs

93 lines
3.0 KiB
C#
Raw Normal View History

2023-04-11 19:55:24 -07:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyPatrol : MonoBehaviour {
[HideInInspector] public bool pinned = false;
[Header("Horizontal")]
public bool isHorizontal;
public float rangeHorizontal;
2023-04-11 19:55:24 -07:00
public float xLeft;
public float xRight;
public Vector2 movementVectorHorizontal = Vector2.right;
[Header("Vertical")]
public bool isVertical;
public float rangeVertical;
public float yUp;
public float yDown;
public Vector2 movementVectorVertical = Vector2.up;
[Header("General")]
2023-04-11 19:55:24 -07:00
public float moveSpeed;
Animator animator;
void Awake() {
animator = GetComponent<Animator>();
}
2023-04-11 19:55:24 -07:00
// Start is called before the first frame update
void Start() {
xLeft = transform.position.x - rangeHorizontal;
xRight = transform.position.x + rangeHorizontal;
yDown = transform.position.y - rangeVertical;
yUp = transform.position.y + rangeVertical;
movementVectorHorizontal *= moveSpeed;
movementVectorVertical *= moveSpeed;
2023-04-11 19:55:24 -07:00
}
// Update is called once per frame
void Update() {
if (!pinned) {
if (isHorizontal) {
if (transform.position.x >= xRight || transform.position.x <= xLeft) {
movementVectorHorizontal = -movementVectorHorizontal;
GetComponent<SpriteRenderer>().flipX = !GetComponent<SpriteRenderer>().flipX;
}
transform.position += new Vector3(movementVectorHorizontal.x, 0, 0) * Time.deltaTime;
}
if (isVertical) {
if (transform.position.y >= yUp || transform.position.y <= yDown) {
movementVectorVertical = -movementVectorVertical;
}
transform.position += new Vector3(0, movementVectorVertical.y, 0) * Time.deltaTime;
2023-04-11 19:55:24 -07:00
}
}
}
public void TogglePin(bool isPinned) {
if (isPinned) {
animator.speed = 0;
} else {
animator.speed = 1;
}
}
public void DefeatEnemy() {
StartCoroutine(Defeat());
}
IEnumerator Defeat() {
this.gameObject.GetComponent<BoxCollider2D>().enabled = false;
animator.Play("Explosion");
yield return new WaitForSeconds(animator.GetCurrentAnimatorStateInfo(0).length);
this.gameObject.GetComponent<BoxCollider2D>().enabled = true;
this.gameObject.SetActive(false);
}
void OnDrawGizmos() {
Gizmos.color = Color.green;
if (isHorizontal){
Gizmos.DrawLine(new Vector3(transform.position.x - rangeHorizontal, transform.position.y, transform.position.z), new Vector3(transform.position.x + rangeHorizontal, transform.position.y, transform.position.z));
}
if (isVertical) {
Gizmos.DrawLine(new Vector3(transform.position.x, transform.position.y - rangeVertical, transform.position.z), new Vector3(transform.position.x, transform.position.y + rangeVertical, transform.position.z));
}
}
2023-04-11 19:55:24 -07:00
}