b6804e2986
the console should finally be message free! except for the unity memory leak lol but that one's not on us
115 lines
4.3 KiB
C#
115 lines
4.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class TambourineBehavior : MonoBehaviour {
|
|
|
|
private Rigidbody2D rb;
|
|
// private float timer;
|
|
|
|
private Animator animator;
|
|
|
|
private GameObject collidedObject;
|
|
private float timeLerped = 0.0f;
|
|
private float timeToLerp = 0.2f;
|
|
|
|
private GameObject player;
|
|
|
|
public bool pinned = false;
|
|
public AudioSource tambourineHitSound;
|
|
|
|
private bool returnToPlayer = false;
|
|
|
|
|
|
void Awake() {
|
|
this.gameObject.GetComponent<CircleCollider2D>().enabled = true;
|
|
rb = this.gameObject.GetComponent<Rigidbody2D>();
|
|
animator = this.gameObject.transform.GetChild(0).gameObject.GetComponent<Animator>();
|
|
player = GameObject.FindGameObjectWithTag("Player");
|
|
}
|
|
|
|
|
|
void Start() {
|
|
this.gameObject.transform.localScale = new Vector2(transform.localScale.x * player.GetComponent<PlayerBehavior>().forward, transform.localScale.y);
|
|
StartCoroutine(CheckToDestroy());
|
|
}
|
|
|
|
void Update() {
|
|
if (player == null) {
|
|
DestroySelf();
|
|
} else {
|
|
if (collidedObject != null && collidedObject.tag != "grappleSurface" && !returnToPlayer) {
|
|
rb.constraints = RigidbodyConstraints2D.FreezeAll;
|
|
timeLerped += Time.deltaTime;
|
|
this.gameObject.transform.position = Vector2.Lerp(this.gameObject.transform.position, collidedObject.transform.position, timeLerped/timeToLerp);
|
|
if (this.gameObject.transform.position.x == collidedObject.transform.position.x && this.gameObject.transform.position.y == collidedObject.transform.position.y && !animator.GetBool("pinned")) {
|
|
pinned = true;
|
|
animator.SetBool("pinned", true);
|
|
tambourineHitSound.Play();
|
|
}
|
|
} else if (returnToPlayer) {
|
|
Destroy(rb);
|
|
Destroy(this.gameObject.GetComponent<CircleCollider2D>());
|
|
animator.SetBool("pinned", false);
|
|
pinned = false;
|
|
|
|
timeLerped += Time.deltaTime;
|
|
this.gameObject.transform.position = Vector2.Lerp(this.gameObject.transform.position, player.transform.position, timeLerped/0.1f);
|
|
if (this.gameObject.transform.position.x == player.transform.position.x && this.gameObject.transform.position.y == player.transform.position.y) {
|
|
player.GetComponent<PlayerBehavior>().SetHasTambourine(true);
|
|
Destroy(this.gameObject);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void OnTriggerEnter2D(Collider2D col) {
|
|
if (col.tag == "Enemy") {
|
|
this.gameObject.GetComponent<BoxCollider2D>().enabled = false;
|
|
collidedObject = col.gameObject;
|
|
this.gameObject.GetComponent<CircleCollider2D>().enabled = false;
|
|
collidedObject.GetComponent<EnemyPatrol>().TogglePin(true, this);
|
|
} else if (col.tag == "Projectile") {
|
|
this.gameObject.GetComponent<BoxCollider2D>().enabled = false;
|
|
collidedObject = col.gameObject;
|
|
this.gameObject.GetComponent<CircleCollider2D>().enabled = false;
|
|
collidedObject.GetComponent<ProjectileBehavior>().Pin();
|
|
}
|
|
}
|
|
|
|
void OnCollisionEnter2D(Collision2D col) {
|
|
if (col.gameObject.tag == "wall") {
|
|
DestroySelf();
|
|
}
|
|
}
|
|
|
|
IEnumerator CheckToDestroy() {
|
|
yield return new WaitForSeconds(3f);
|
|
if (!player.GetComponent<PlayerBehavior>().grapplingRope.isGrappling) {
|
|
DestroySelf();
|
|
}
|
|
}
|
|
|
|
public void DestroySelf() {
|
|
if (player != null) {
|
|
timeLerped = 0.0f;
|
|
returnToPlayer = true;
|
|
}
|
|
|
|
if (collidedObject != null && collidedObject.tag == "Enemy") {
|
|
EnemyPatrol enemy = collidedObject.GetComponent<EnemyPatrol>();
|
|
if (!enemy.isPlayingDefeatAnimation) {
|
|
enemy.TogglePin(false, this);
|
|
}
|
|
} else if (collidedObject != null && collidedObject.tag == "Projectile") {
|
|
collidedObject.GetComponent<ProjectileBehavior>().Explode();
|
|
}
|
|
|
|
if (player == null) {
|
|
Destroy(this.gameObject);
|
|
} else {
|
|
player.GetComponent<PlayerBehavior>().grapplingGun.ReleaseGrapple();
|
|
}
|
|
}
|
|
}
|