-
Michael LaFreniere authoredMichael LaFreniere authored
EnemyAI.cs 6.17 KiB
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
public DecisionTree root;
private Transform target;
private Rigidbody2D Enemy;
private Animator myAnimator;
public float enemyHealth, original;
float playerHealth;
private bool LookRight = false;
public bool set;
private float speed = 1;
void Start() {
enemyHealth = Random.Range(50, 500);
original = enemyHealth;
playerHealth = PlayerStats.getHealth();
root = new DecisionTree();
// Debug.Log("start");
target = GameObject.FindGameObjectWithTag("Player").transform; //look at the player
myAnimator = GetComponent<Animator>(); //required for animation *******
Enemy = GetComponent<Rigidbody2D>();
BuildDecisionTree();
}
void Update() {
root.search();
}
public void ChangeHealthBare(int damage)
{
GameObject healthbar = GameObject.Find("healthbarFulle");
float newsize = healthbar.transform.localScale.x - ((damage / original)*0.3f);
healthbar.transform.localScale = new Vector3(newsize, 0.3f, 1f);
if (enemyHealth <= 0)
{
healthbar.GetComponent<SpriteRenderer>().enabled = false;
}
}
public void ApplyDamageE(int damage)
{
enemyHealth -= damage;
if (enemyHealth <= 0)
{
Destroy(gameObject);
}
}
public bool d_inAttackRange() {
if (Vector3.Distance(transform.position, target.position) < 1.5f) {
myAnimator.SetBool("speed", false);
return true;
}
else {
myAnimator.SetBool("block", false);
myAnimator.SetBool("attack", false);
// Debug.Log("In Zone- not");
return false;
}
}
public bool d_isPlayerAttacking() {
if (Input.GetKeyDown(KeyCode.Space) && Vector3.Distance(transform.position, target.position) < 6f) {
return false;
}
return true;
}
public bool d_isPlayerHpLow() {
if ((PlayerStats.getHealth()/2) < playerHealth) {
return true;
}
return false;
}
public bool d_isHpLow() {
if ((original/2) < enemyHealth) {
return true;
}
return false;
}
//makes a random decision with a 75% true return and a 25% false return
public bool randomDecision75()
{
int x = Random.Range(1, 4);
if (x == 1)
{
return false;
}
else return true;
}
// Actions
public void a_attack() {
Debug.Log("attacking");
myAnimator.SetFloat("speed", 0f);
myAnimator.SetBool("attack", true);
Enemy.velocity = Vector2.zero;
}
public void a_block() {
Debug.Log("run");
float option = Random.Range(0, 2);
if (option == 1) {
myAnimator.SetBool("attack", false);
myAnimator.SetBool("block", true);
Enemy.velocity = Vector2.zero;
}
if (option == 2) {
Enemy.AddForce(new Vector2(-50, 500));
}
}
//template for a 'poison' special attack. Will slowly and gradually drain players health over time.
public void poison()
{
Debug.Log("Performing Poison Attack");
}
public void a_moveToPlayer() {
Debug.Log("moving");
//rotate to look at the player
//transform.LookAt(target.position);
myAnimator.SetFloat("speed", 1); // This should probbaly reflect the actual speed
transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
if (target.transform.position.x <= transform.position.x) //players spot in world space as opposed to enemy "self" spot
{
transform.rotation = new Quaternion(0, 180, 0, 0); // flips enemy around to face the player on x axis only
}
else if (target.transform.position.x >= transform.position.x) //players spot in world space as opposed to enemy "self" spot
{
transform.rotation = new Quaternion(0, 0, 0, 0); // flips enemy around to face the player on x axis only }
}
}
public void a_moveFromPlayer() {
Debug.Log("moving");
//rotate to look at the player
//transform.LookAt(target.position);
myAnimator.SetFloat("speed", 1); // This should probbaly reflect the actual speed
transform.position = Vector2.MoveTowards(-transform.position, target.position, speed * Time.deltaTime);
if (target.transform.position.x <= transform.position.x) //players spot in world space as opposed to enemy "self" spot
{
transform.rotation = new Quaternion(0, 180, 0, 0); // flips enemy around to face the player on x axis only
}
else if (target.transform.position.x >= transform.position.x) //players spot in world space as opposed to enemy "self" spot
{
transform.rotation = new Quaternion(0, 0, 0, 0); // flips enemy around to face the player on x axis only }
}
}
public void BuildDecisionTree() {
//Actions
DecisionTree Block = new DecisionTree();
Block.setAction(a_block);
DecisionTree MoveToPlayer = new DecisionTree();
MoveToPlayer.setAction(a_moveToPlayer);
DecisionTree Attack = new DecisionTree();
Attack.setAction(a_attack);
DecisionTree MoveAway = new DecisionTree();
MoveAway.setAction(a_moveFromPlayer);
// Decisions
DecisionTree EnemyHP = new DecisionTree();
EnemyHP.setDecision(d_isHpLow);
EnemyHP.setLeft(MoveAway);
EnemyHP.setRight(Attack);
DecisionTree PlayerHP = new DecisionTree();
PlayerHP.setDecision(d_isPlayerHpLow);
PlayerHP.setLeft(Attack);
PlayerHP.setRight(EnemyHP);
DecisionTree GettingAttacked = new DecisionTree();
GettingAttacked.setDecision(d_isPlayerAttacking);
GettingAttacked.setLeft(Attack);
GettingAttacked.setRight(Block);
root.setDecision(d_inAttackRange);
root.setLeft(GettingAttacked);
root.setRight(MoveToPlayer);
}
}