-
-
Save e-sarkis/4b29a43c4e6fa3a2e1547da299649ade to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class EnemyController : MonoBehaviour | |
{ | |
[HideInInspector] public GameObject gm; // reference to the scene's gamemanager object | |
[HideInInspector] public GameController gc; // reference to the scene's gamemanager object's gamecontroller component | |
[HideInInspector] public ColliderManager cm; | |
[HideInInspector] public HealthPoints hp; | |
[HideInInspector] public Animator anim; | |
[HideInInspector] public LevelEventController lec; | |
public bool startZRotationFrozen = true; | |
public bool startXYMovementFrozen = true; | |
public float moveSpeed = 0.01f; | |
public Vector2 destination; | |
public bool activeMovement = false; | |
public bool activeHitbox = false; | |
public bool activeDamage = false; | |
public bool movingDestination = false; | |
public Vector2 simpleMovementVector = new Vector2 (0, 0); | |
public int damageOnContact = 1; | |
private Rigidbody2D rb; | |
private Collider2D c; | |
private SpriteRenderer sr; | |
[HideInInspector] public bool atDestinationX; | |
[HideInInspector] public bool atDestinationY; | |
public int stageNum = -1; | |
public int stageEnemyNum = -1; | |
[HideInInspector] public bool removed = false; | |
public int deathBarrier = 20; | |
public float TimeUntilFadingAfterDeath = 0f; | |
private bool BeganFade = false; | |
void Awake() | |
{ | |
rb = GetComponent<Rigidbody2D>(); | |
c = GetComponent<Collider2D>(); | |
sr = GetComponent<SpriteRenderer>(); | |
cm = GetComponent<ColliderManager>(); | |
hp = GetComponent<HealthPoints>(); | |
anim = GetComponent<Animator>(); | |
gc = GameObject.Find("GameController").GetComponent<GameController>(); | |
lec = GameObject.Find("GameController").GetComponent<LevelEventController>(); | |
if (startXYMovementFrozen) | |
{ | |
rb.constraints = RigidbodyConstraints2D.FreezePosition; | |
} | |
if (startZRotationFrozen) | |
{ | |
rb.freezeRotation = true; | |
} | |
} | |
void FixedUpdate() | |
{ | |
if (activeMovement) | |
{ | |
if (movingDestination) { DestinationMovement(); } | |
else { VectorMovement(); } | |
} | |
if(hp.isZero || PassedDeathBarrier()) | |
{ | |
Die(); | |
} | |
c.enabled = gc.isOnStage(this.gameObject); | |
cm.enabled = c.enabled; | |
} | |
public void SetDestination(Vector2 newDestination, float newMoveSpeed = 0f) | |
{ | |
if (newMoveSpeed == 0) { newMoveSpeed = moveSpeed; } | |
destination = newDestination; | |
anim.SetBool("isMoving", true); | |
} | |
public void SetMovementVector(float vectorX, float vectorY) | |
{ | |
simpleMovementVector.x = vectorX; | |
simpleMovementVector.y = vectorY; | |
} | |
public void EnableDestinationMovement() | |
{ | |
movingDestination = true; | |
} | |
public void EnableVectorMovement() | |
{ | |
movingDestination = false; | |
} | |
void DestinationMovement() | |
{ | |
Vector2 newPosition = new Vector2(transform.position.x, transform.position.y); | |
if (!atDestinationX) | |
{ | |
if (transform.position.x < destination.x) | |
{ | |
transform.position = new Vector3(transform.position.x + moveSpeed, transform.position.y, transform.position.z); | |
} else if (transform.position.x > destination.x) | |
{ | |
transform.position = new Vector3(transform.position.x - moveSpeed, transform.position.y, transform.position.z); | |
} | |
if ( Mathf.Abs(transform.position.x - destination.x) < moveSpeed ) | |
{ | |
atDestinationX = true; | |
} | |
} | |
if (!atDestinationY) | |
{ | |
if (transform.position.y < destination.y) | |
{ | |
transform.position = new Vector3(transform.position.x, transform.position.y + moveSpeed, transform.position.z); | |
} else if (transform.position.y > destination.y) | |
{ | |
transform.position = new Vector3(transform.position.x, transform.position.y - moveSpeed, transform.position.z); | |
} | |
if ( Mathf.Abs(transform.position.y - destination.y) < moveSpeed ) | |
{ | |
atDestinationY = true; | |
} | |
} | |
if (atDestinationX && atDestinationY) | |
{ | |
movingDestination = false; | |
anim.SetBool("isMoving", false); | |
} | |
} | |
void VectorMovement() | |
{ | |
if ( Mathf.Abs(simpleMovementVector.x) > 0 || Mathf.Abs(simpleMovementVector.y) > 0 ) | |
{ | |
anim.SetBool("isMoving", true); | |
} else | |
{ | |
anim.SetBool("isMoving", false); | |
} | |
transform.position = new Vector3(transform.position.x + (simpleMovementVector.x * moveSpeed), | |
transform.position.y + (simpleMovementVector.y * moveSpeed), | |
transform.position.z); | |
} | |
void OnCollisionEnter2D(Collision2D coll) | |
{ | |
if (coll.gameObject.tag == "Floor" && hp.isZero) | |
{ | |
Physics2D.IgnoreCollision(c, coll.collider, true); | |
AudioController.instance.PlaySound("Kiss"); | |
return; | |
} | |
if (coll.gameObject.tag == "Enemy") | |
{ | |
if (!hp.isZero) { Physics2D.IgnoreCollision(c, coll.collider, true); } | |
else { Physics2D.IgnoreCollision(c, coll.collider, true); } | |
return; | |
} | |
if (coll.gameObject.tag == "Player") | |
{ | |
if (hp.isZero || coll.gameObject.GetComponent<HealthPoints>().isInvincible) | |
{ | |
Physics2D.IgnoreCollision(c, coll.collider, true); | |
} else | |
{ | |
cm.DisableColliderTemporarily(); | |
} | |
} | |
} | |
void OnTriggerEnter2D(Collider2D coll) | |
{ | |
if (coll.gameObject.tag == "Bullet" && !hp.isZero) | |
{ | |
BulletController Bullet = coll.gameObject.GetComponent<BulletController>(); | |
hp.ApplyDamage(Bullet.Damage); | |
if (hp.isZero) | |
{ | |
ImpactReciever imp = GetComponent<ImpactReciever>(); | |
imp.SetAndApplyImpact(Bullet.gameObject.transform.right * Bullet.ImpactStrength, Bullet.gameObject.transform.position); | |
AudioController.instance.PlaySound("Whoosh1"); | |
AudioController.instance.PlaySound("Impact0"); | |
AudioController.instance.PlaySound("DeadSplat"); | |
AudioController.instance.PlaySound("Slap1"); | |
gc.comboCount++; | |
gc.ResetComboTimer(); | |
StartCoroutine("dissapear"); | |
} | |
Bullet.Die(); | |
} | |
} | |
public void SetLocation2D(float x, float y) | |
{ | |
Vector3 newPosition = new Vector3(x, y, transform.position.z); | |
transform.position = newPosition; | |
} | |
public void SetLocation3D(float x, float y, float z) | |
{ | |
Vector3 newPosition = new Vector3(x, y, z); | |
transform.position = newPosition; | |
} | |
public void Die() | |
{ | |
if (!removed) | |
{ | |
transform.parent = null; | |
removed = true; | |
} | |
sr.color = Color.white; | |
anim.SetBool("isDead", true); | |
DeactivateDamage(); | |
DeactivateMovement(); | |
Physics2D.IgnoreCollision(GetComponent<Collider2D>(), GameObject.Find("Player").GetComponent<Collider2D>(), true); | |
} | |
public bool PassedDeathBarrier() | |
{ | |
return ( Mathf.Abs(transform.position.x) > deathBarrier | |
|| Mathf.Abs(transform.position.y) > deathBarrier); | |
} | |
void ActivateMovement() | |
{ | |
activeMovement = true; | |
} | |
void ActivateHitbox() | |
{ | |
activeHitbox = true; | |
} | |
void ActivateDamage() | |
{ | |
activeDamage = true; | |
} | |
void DeactivateMovement() | |
{ | |
activeMovement = false; | |
} | |
void DeactivateHitbox() | |
{ | |
activeHitbox = false; | |
} | |
void DeactivateDamage() | |
{ | |
activeDamage = false; | |
} | |
IEnumerator dissapear() | |
{ | |
yield return new WaitForSeconds(10); | |
foreach (SpriteRenderer s in GetComponents<SpriteRenderer>()) | |
{ | |
s.enabled = false; | |
} | |
foreach (SpriteRenderer s in GetComponentsInChildren<SpriteRenderer>()) | |
{ | |
s.enabled = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment