You are on page 1of 4

using System.

Collections;
using System.Collections.Generic;
using UnityEngine;

public class JM_Boss_AI : MonoBehaviour


{

public float stunDuration;


public float chargeCooldown;
public float walkSpeed;
public float chargeSpeed;
public float hitDamage = 1;

public GameObject Player;

public AudioSource attackSound;

private Rigidbody2D rb_NPC;


private CapsuleCollider2D collider_NPC;
private SpriteRenderer sprite_NPC;
private Animator anim_NPC;

private bool stunned;


private float stunTime;
private bool didChargeHit;
private float chargeDirection;
private float chargeTimer;
private bool charging;
private bool disableCharge;

public GameObject projectile;

private void Start()


{
rb_NPC = GetComponent<Rigidbody2D>();
collider_NPC = GetComponent<CapsuleCollider2D>();
sprite_NPC = GetComponent<SpriteRenderer>();
anim_NPC = GetComponent<Animator>();

chargeTimer = chargeCooldown;
}

void Update()
{
anim_NPC.SetFloat("Speed", Mathf.Abs(rb_NPC.velocity.x));

HitPlayer();

if (stunned) // Are we stunned?


{
stunTime -= Time.deltaTime; // Decrease stun timer
if (stunTime <= 0) // When stun is finished...
{
stunned = false; // set stunned to false.
anim_NPC.SetBool("Stunned", false);
}
else
{
return;
}
}

if (charging) // Are we charging?


{
ContinueCharge();
return;
}

if (ShouldCharge()) // Should we charge?


{
StartCharge();
}

WalkToPlayer();
}

void HitPlayer()
{
Vector2 center = new Vector2(transform.position.x + collider_NPC.offset.x,
transform.position.y + collider_NPC.offset.y); // Get the center of the boss
Collider2D hitPlayer = Physics2D.OverlapBox(center, collider_NPC.size, 0, 1
<< Player.layer); // Check if the boss touches the player

if(hitPlayer) // Did we hit the player?


{
if(charging) // Is the boss charging?
{
didChargeHit = true; // The boss just hit the player
}

var player_health = hitPlayer.GetComponent<JM_2D_Health>();


if(player_health)
{
player_health.Damage(hitDamage);
}
}
}

void WalkToPlayer()
{
float speed = walkSpeed; // Set Boss's
speed.

if (Player.transform.position.x < transform.position.x) // Is the player to


the left...
{
speed *= -1; // walk left.
}

sprite_NPC.flipX = (speed > 0); // Flip the boss's


sprite, to the right. To Do, make boss face right by default in sprite.

rb_NPC.velocity = new Vector2(speed, 0); // Apply the speed


to the rigidbody.
}

bool ShouldCharge() // Check if charge is off


cooldown.
{
chargeTimer -= Time.deltaTime; // Decrease charge time by
a lapsed time.
return chargeTimer <= 0 && !disableCharge; // Return is the charge off
cooldown and enabled.
}

void StartCharge() // Charge towards


the player
{
if (Player.transform.position.x < transform.position.x) // Check player
position
{
chargeDirection = -1; // charge left...
}
else
{
chargeDirection = 1; // charge right.
}

didChargeHit = false;
charging = true;
}

void ContinueCharge()
{
float speed = chargeSpeed * chargeDirection; // Set chargeSpeed to
negative (Left) or positive (Right)

sprite_NPC.flipX = (speed > 0); // Flip the boss's sprite


to the right.

rb_NPC.velocity = new Vector2(speed, 0); // Set the velocity speed.


}

void CounterAttack(Vector2 position) // The position of the player


bullet obtained from OnTriggerEnter.
{
Vector3 direction = transform.right; // Fire to the right
Quaternion rotation = Quaternion.identity; // Default no rotation
if (position.x < transform.position.x) // Is the bullet to the left...
{
rotation = Quaternion.Euler(0, 180, 0); // flip the boss's bullet to
the left.
direction *= -1; // Fire to the left.
}

var offset = new Vector3(direction.x * 1.5f, -0.43f, 0);


Instantiate(projectile, transform.position + offset, rotation); //
Instantiate a bullet

anim_NPC.SetTrigger("Attack");

if(attackSound)
{
attackSound.Play();
}
}
private void OnCollisionExit2D(Collision2D collision) // When the boss leaves
the wall, enable charge.
{
if (collision.collider.tag == "BossStun")
{
disableCharge = false;
}
}

void OnCollisionEnter2D(Collision2D collision)


{
if (collision.otherCollider.isTrigger) return; // Ignore the big trigger
collider around the boss.

if (collision.collider.tag == "BossStun") // Did the object we


collide with have the tag BossStun...
{
disableCharge = true; // Disbale charge, so
doesn't get stuck in the wall.

if (charging) // On collsion if the boss


is charging
{
charging = false; // is so, stop charging.
if (!didChargeHit) // If the charge did not
hit the player..
{
stunned = true; // Stun the boss
stunTime = stunDuration; // Set the stun timer
didChargeHit = false; // Reset didChargeHit

anim_NPC.SetBool("Stunned", true);
}
chargeTimer = chargeCooldown; // Reset the charge timer.
}
}
}

void OnTriggerEnter2D(Collider2D collider)


{
if (collider.tag == "PlayerBullet") // If the players
bullet enters the trigger...
{
if(!stunned && !charging) // and the boss is
not stunned or charging...
{
CounterAttack(collider.transform.position); // pass the
position of the collider into counterAttack.
}
}
}
}

You might also like