Skip to content
Snippets Groups Projects
EnemyAI.cs 27.6 KiB
Newer Older
Jordan's avatar
Jordan committed
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

Jordan's avatar
Jordan committed

public class EnemyAI : MonoBehaviour {

    public DecisionTree root; //root of the decision tree which is searched for the AI to function
    public GameObject bananaPrefab;
Jordan's avatar
Jordan committed
    private Transform target;
    private Rigidbody2D Player;
Graham Solie's avatar
Graham Solie committed
    private Rigidbody2D Enemy;
    private Animator myAnimator;
    //Variables for keeping track of the Enemies health
    public float enemyHealth; //current health
    public float original; //health he spawned with
    //Variables for keeping track of the Players health
    public float playerHealth; //current health
    public float playerHealthOriginal; //players health they entered the arena with
    public static bool LookRight = true; //check which way the enemy is facing
    private bool set;
    private float speed = 2; //movement speed of enemy
    private float attackSpeed; //attack speed
    private float randomSpeed; //random speed which the atttack speed takes
Graham Solie's avatar
Graham Solie committed
    float spaceBarCounter = 0; //check for how aggresive the player is being 
    private float coolDown = 0; //coolDown for Enemy attack
    private bool actionDelay = false; //sets a delay for the search of the tree during an enemy action
    private float searchDelay = 0.2f;
    private float searchCoolDown = 0;
    public static bool isEnemyKicking = false; //check for the player
    public static bool isEnemyPushing = false; //check for the player
    public static bool isEnemyDead = false; //check for the player
    private float kickCoolDown = 0; //cooldown on enemy kicks
    private float kickTimer;    //hold the max time between enemy kicks
    private float aggressiveTimer; //time which countdowns, if player spams spacebar before it end, they will be seen as "Aggressive"
    private bool aggressiveCheck = false; //checks if the player is aggressive
    private bool isAggressive = false;
    private bool isPlayerAttacking = false; //Check for AI
    private bool isPlayerBlocking = false; //Check for AI
    private bool isPlayerKicking = false; //Check for AI
    private bool isPlayerPushing = false; //Check for AI
    private float playerPushingTimer; //Timer which makes AI jump after being pushed 'x' times
    private float throwCoolDown = 0; //timer to slow amount the AI can throw
    private float throwTimer;
    private bool canJump = false; //check to see if the AI can jump, to avoid clipping
    private int playerPushingCounter = 0; //check for how many times the AI has been pushed by the player
    private float randomActionCoolDown = 0; //cooldown for random actions
    private float randomAction; //holds the randomActionSpeed
    private float randomAcioionSpeed; //random speed set for random actions
    private int healthPotion;
    public int enemyMax;
    public int enemyMin;
    public bool dash = true;
    public float dashCoolDown = 3;
Jordan's avatar
Jordan committed

Graham Solie's avatar
Graham Solie committed
    void Start() {
Graham Solie's avatar
Graham Solie committed

        healthPotion = 1;
        throwTimer = 2;
        throwCoolDown = 1;
Graham Solie's avatar
Graham Solie committed
        playerPushingTimer = 2;
        aggressiveTimer = 3;
        kickTimer = 5;
        randomSpeed = Random.Range(0, 1f);
Graham Solie's avatar
Graham Solie committed
        attackSpeed = randomSpeed;
        randomAcioionSpeed = Random.Range(0, 2f);
Graham Solie's avatar
Graham Solie committed
        randomAction = randomAcioionSpeed;
Graham Solie's avatar
Graham Solie committed
        setEnemyHp();
        playerHealth = PlayerStats.getHealth();
Graham Solie's avatar
Graham Solie committed
        playerHealthOriginal = playerHealth;
        isEnemyDead = false;
Graham Solie's avatar
Graham Solie committed

Graham Solie's avatar
Graham Solie committed
        root = new DecisionTree();
Graham Solie's avatar
Graham Solie committed
        //  Debug.Log("start");
        target = GameObject.FindGameObjectWithTag("Player").transform; //look at the player
Graham Solie's avatar
Graham Solie committed
        Player = GameObject.FindGameObjectWithTag("Player").GetComponent<Rigidbody2D>();
Graham Solie's avatar
Graham Solie committed
        myAnimator = GetComponent<Animator>(); //required for animation *******
        Enemy = GetComponent<Rigidbody2D>();
Graham Solie's avatar
Graham Solie committed
        BuildDecisionTree();
Jordan's avatar
Jordan committed

Graham Solie's avatar
Graham Solie committed
    }
Jordan's avatar
Jordan committed

Graham Solie's avatar
Graham Solie committed
    void Update() {
        //calls the all functions which need to be checked every frame
Graham Solie's avatar
Graham Solie committed
        AggressiveCoolDown();
        BlockCheck();
        AttackCheck();
        KickCheck();
Graham Solie's avatar
Graham Solie committed
        PushCheck();
        HpCheck();
Graham Solie's avatar
Graham Solie committed
        getPushed();

        //cool downs for various actions
        throwCoolDown -= Time.deltaTime;
        coolDown -= Time.deltaTime; //for attacking
        kickCoolDown -= Time.deltaTime;
Graham Solie's avatar
Graham Solie committed
        randomActionCoolDown -= Time.deltaTime;
Graham Solie's avatar
Graham Solie committed

        //If the player can jump this will allow him to do so without getting stuck in the player
Graham Solie's avatar
Graham Solie committed
        if (canJump == true) {
            playerPushingTimer -= Time.deltaTime;
            if (playerPushingTimer <= 0) {
                JumpPlayer();
                playerPushingTimer = 1.5f;
Graham Solie's avatar
Graham Solie committed
                canJump = false;
            }
        }

        if (dash == true) {
            dashCoolDown -= Time.deltaTime;
            if (dashCoolDown <= 0) {
                dashCoolDown = 3;
        //sets a delay to the search of the tree so actions can not be spammed out by the AI
Graham Solie's avatar
Graham Solie committed
        if (actionDelay == true) {
            searchCoolDown -= Time.deltaTime;
            if (searchCoolDown <= 0) {
                searchCoolDown = searchDelay;
                root.search(); //search the decision tree
Graham Solie's avatar
Graham Solie committed
            }

        }
        else {
            root.search(); //search the decision tree
Graham Solie's avatar
Graham Solie committed
            actionDelay = false;
        }

Graham Solie's avatar
Graham Solie committed
    }
Graham Solie's avatar
Graham Solie committed

    public void setEnemyHp() {
        if (ArenaCombatControl.winCount <= 2) {
            enemyHealth = enemyHealth = Random.Range(20, 40);
Graham Solie's avatar
Graham Solie committed
            original = enemyHealth;
        }
        else {
            enemyHealth = enemyHealth = Random.Range(15 * (int)(ArenaCombatControl.winCount / 1.1f + 1), 20 * (int)(ArenaCombatControl.winCount / 1.1f + 1));
            original = enemyHealth;
    /*
     * getPushed()
     * Takes Input from the user and checks whether or not the enemy is being pushed
     * Checks which direction the enemy is facing and applies the relative force
     * If the enemy has been pushed too many times he will then jump over the player and switch up the combat
     */
Graham Solie's avatar
Graham Solie committed
    public void getPushed() {
        if (Vector3.Distance(transform.position, target.position) < 2.5f && isPlayerPushing == true) {
            myAnimator.SetBool("knockBack", true);
            myAnimator.SetBool("kick", false);
            myAnimator.SetBool("attack", false);
            myAnimator.SetBool("block", false);
Graham Solie's avatar
Graham Solie committed
            playerPushingCounter++;
            Invoke("StopKnockAnimation", 0.5f);
Graham Solie's avatar
Graham Solie committed
            if (LookRight == true) {
                Enemy.AddForce(new Vector2(-500, 0));
            }
            else {
                Enemy.AddForce(new Vector2(500, 0));
            }
Graham Solie's avatar
Graham Solie committed

            if (playerPushingCounter >= 2) {
Graham Solie's avatar
Graham Solie committed
                playerPushingCounter = 0;
                canJump = true;
            }
Graham Solie's avatar
Graham Solie committed
        }
    }
    //fixes infinate loop of knockBack animation
    public void StopKnockAnimation() {
        myAnimator.SetBool("knockBack", false);
    }

    //forces to the Enemy to jump over the player without getting stuck
    //relative forces are applied by checking the direction the enemy is facing
Graham Solie's avatar
Graham Solie committed
    public void JumpPlayer() {
Graham Solie's avatar
Graham Solie committed
        if (Vector3.Distance(transform.position, target.position) < 6f) {
Graham Solie's avatar
Graham Solie committed
            if (LookRight == true) {
Graham Solie's avatar
Graham Solie committed
                Enemy.AddForce(new Vector2(200, 1000));
Graham Solie's avatar
Graham Solie committed
            }
            else {
Graham Solie's avatar
Graham Solie committed
                Enemy.AddForce(new Vector2(-200, 1000));
    /*
     * AggreiveCoolDown()
     * Takes input from the player and checks how much the spacebar is being pressed
     * if the spacebar is spammed, the enemy will push the player away 
     */

Graham Solie's avatar
Graham Solie committed
    public void AggressiveCoolDown() {
        if (Input.GetKeyDown(KeyCode.Space)) {
            spaceBarCounter++;
            // Debug.Log(spaceBarCounter);
            aggressiveCheck = true;
        }
        if (aggressiveCheck == true) {
            aggressiveTimer -= Time.deltaTime;
            // Debug.Log(agressiveTimer);
        }

        if (aggressiveTimer <= 0) {
            aggressiveCheck = false;
            spaceBarCounter = 0;
            aggressiveTimer = 5;
        }
    }
Graham Solie's avatar
Graham Solie committed


    //Check from user imput for AI, if the player is blocking or not
    public void BlockCheck() {
        isPlayerBlocking = ArenaCombatControl.block;
        //Debug.Log(isPlayerBlocking + "Enemy");

        if (isPlayerBlocking == false) {
            myAnimator.SetBool("kick", false);
        }
    //Check from user imput for AI, if the player is atacking or not
    public void AttackCheck() {
        isPlayerAttacking = ArenaCombatControl.attack;
    }

    //Check from user imput for AI, if the player is kicking or not
    public void KickCheck() {
        isPlayerKicking = ArenaCombatControl.kick;
    }

    //Check from player stats for AI, keeps track of the players HP
    public void HpCheck() {
        playerHealth = PlayerStats.curHealth;
    }

    //Check from user imput for AI, if the player is pushing or not
Graham Solie's avatar
Graham Solie committed
    public void PushCheck() {
        isPlayerPushing = ArenaCombatControl.push;
    }

    /*
	 * Damage dealt against the enemy from the player
Jordan's avatar
Jordan committed
    public void Damage(int amount) {
        enemyHealth -= amount;
Graham Solie's avatar
Graham Solie committed
        if (enemyHealth <= 0) {
            enemyHealth = 0;
Michael LaFreniere's avatar
Michael LaFreniere committed
            ArenaCombatControl.winCount++;
            isEnemyDead = true;
            myAnimator.SetBool("death", true);
            Enemy.velocity = Vector2.zero;
            Invoke("EnemyDeath", 2.5f);
            Invoke("ExitArena", 2.4f);
Graham Solie's avatar
Graham Solie committed
        }
Graham Solie's avatar
Graham Solie committed

    //destory the enemy when hes dead
    public void EnemyDeath() {
        Destroy(gameObject);
    }

    //exit the arena after the enemy had died
    public void ExitArena() {
        isEnemyDead = false;
    /*
	 * Damage dealt to the player from the enemy
    public int getDamageOutput() {
        enemyMax = 8 + (int)(ArenaCombatControl.winCount * 1.25f); //BALANCE THIS
        enemyMin = 1 + (int)(ArenaCombatControl.winCount * 1.1f);
        int final = Random.Range(enemyMin, enemyMax);
        Debug.Log(final);
Jordan's avatar
Jordan committed
        // calculate here! remember player defense is factored in later
Graham Solie's avatar
Graham Solie committed

    //Decision for the Decision Tree

    //is the player attacking?
Graham Solie's avatar
Graham Solie committed
    public bool d_isPlayerAttacking() {
        if (isPlayerAttacking == true) {
            return true;
        }
        //   Debug.Log("Player Not Attacking");
        return false;
    }

    //is the player kicking?
    public bool d_isPlayerKicking() {
        if (isPlayerKicking == true) {
Graham Solie's avatar
Graham Solie committed
            return true;
Graham Solie's avatar
Graham Solie committed
        }
Graham Solie's avatar
Graham Solie committed
        return false;
    }

    //is the enemy able to attack?
    //if yes he will attack
    //if no he will move towards the player
Graham Solie's avatar
Graham Solie committed
    public bool d_inAttackRange() {
        isEnemyKicking = false;
Graham Solie's avatar
Graham Solie committed
        isEnemyPushing = false;
        myAnimator.SetBool("drink", false);
        if (Vector3.Distance(transform.position, target.position) < 1.5f && Vector3.Distance(transform.position, target.position) > 1f) {
            myAnimator.SetFloat("speed", 0);
Graham Solie's avatar
Graham Solie committed
            return true;
Graham Solie's avatar
Graham Solie committed
        }
Graham Solie's avatar
Graham Solie committed

Graham Solie's avatar
Graham Solie committed
        else {
            myAnimator.SetBool("kick", false);
Graham Solie's avatar
Graham Solie committed
            myAnimator.SetBool("attack", false);
Graham Solie's avatar
Graham Solie committed
            myAnimator.SetBool("block", false);
            //   myAnimator.SetBool("knockBack", false);
            myAnimator.SetBool("push", false);
Graham Solie's avatar
Graham Solie committed
            // Debug.Log("In Zone- not");
Graham Solie's avatar
Graham Solie committed
            return false;
    //is the player too close?
    //if no the enemy moves towards
    //if yes the enemy moves backwards
Graham Solie's avatar
Graham Solie committed
    public bool d_isPlayerTooClose() {
Graham Solie's avatar
Graham Solie committed

Graham Solie's avatar
Graham Solie committed
        if (Vector3.Distance(transform.position, target.position) < 1f) {
            myAnimator.SetFloat("speed", 1f);
Graham Solie's avatar
Graham Solie committed
            return false;
        }
        else {
Graham Solie's avatar
Graham Solie committed
            return true;
        }
    }

    //if the player too far
    //if yes the enemy throws object to taunt you
    public bool d_isPlayerTooFar() {
        if (Vector3.Distance(transform.position, target.position) > 7.5f && Vector3.Distance(transform.position, target.position) < 12f) {
            return true;
        }
        else {
            return false;
        }
    }

Graham Solie's avatar
Graham Solie committed

    //is the player aggressive, checks to see if the user is spamming attack
Graham Solie's avatar
Graham Solie committed
    public bool d_isPlayerAggresive() {
        if (spaceBarCounter > 7 && aggressiveTimer > 0) {
Graham Solie's avatar
Graham Solie committed
            isAggressive = true;
            //  Debug.Log("Aggressive");
Graham Solie's avatar
Graham Solie committed
            return true;
        }
Graham Solie's avatar
Graham Solie committed

Graham Solie's avatar
Graham Solie committed
        else {
            isAggressive = false;
            //    Debug.Log("Not Aggresive");
Graham Solie's avatar
Graham Solie committed
        }
Graham Solie's avatar
Graham Solie committed

    //is the player blocking
Graham Solie's avatar
Graham Solie committed
    public bool d_isPlayerBlocking() {
        if (isPlayerBlocking == true) {
            return true;
Graham Solie's avatar
Graham Solie committed
        }
        return false;
    //checks the Players HP
    //if players HP reaches below a certain threshold AI descions will be altered
Graham Solie's avatar
Graham Solie committed
    public bool d_isPlayerHpLow() {
        if ((playerHealthOriginal * 0.2f) > playerHealth) {
Graham Solie's avatar
Graham Solie committed
            return true;
Graham Solie's avatar
Graham Solie committed
        }
Graham Solie's avatar
Graham Solie committed
        return false;
    }

    //checks the Enemies HP
    //if Enemis HP reaches below a certain threshold AI descions will be altered
Graham Solie's avatar
Graham Solie committed
    public bool d_isHpLow() {
        myAnimator.SetBool("drink", false);
Graham Solie's avatar
Graham Solie committed
        if ((original / 2) > enemyHealth) {
            if (healthPotion >= 1) {
                myAnimator.SetBool("drink", true);
                enemyHealth += (int)(original * 0.5);
                if (enemyHealth > original) {
                    enemyHealth = original;
                }
                healthPotion--;
            }
Graham Solie's avatar
Graham Solie committed
            return true;
        }
Graham Solie's avatar
Graham Solie committed
        return false;
Graham Solie's avatar
Graham Solie committed
    }
Graham Solie's avatar
Graham Solie committed

    //makes a random decision with a 75% true return and a 25% false return
Graham Solie's avatar
Graham Solie committed
    public bool d_randomDecision75() {
Graham Solie's avatar
Graham Solie committed
        if (x == 1) {
            // Debug.Log("Random");
Graham Solie's avatar
Graham Solie committed

Graham Solie's avatar
Graham Solie committed

    //Actions for the end of the Decision Tree
Graham Solie's avatar
Graham Solie committed

    //Enemies attack, with cooldown so he can spam and kill you too fast
Graham Solie's avatar
Graham Solie committed
    public void a_attack() {
Graham Solie's avatar
Graham Solie committed
        actionDelay = true;
Graham Solie's avatar
Graham Solie committed
        myAnimator.SetFloat("speed", 0f);
Graham Solie's avatar
Graham Solie committed
        if (coolDown <= 0) {
            coolDown = attackSpeed;
            //     Debug.Log("attacking");
Graham Solie's avatar
Graham Solie committed
            myAnimator.SetBool("attack", true);
            Enemy.velocity = Vector2.zero;
        }
        else {
            myAnimator.SetBool("attack", false);
        }
Graham Solie's avatar
Graham Solie committed
    }
Graham Solie's avatar
Graham Solie committed

    //Jump over the Player
Graham Solie's avatar
Graham Solie committed
    public void a_jumpOverPlayer() {
        actionDelay = true;
        if (LookRight == true) {
            //     Debug.Log("Over Yo HEAD");
Graham Solie's avatar
Graham Solie committed
            Enemy.AddForce(new Vector2(100, 1000));
        }
        else {
            //     Debug.Log("Over Yo HEAD");
Graham Solie's avatar
Graham Solie committed
            Enemy.AddForce(new Vector2(-100, 1000));
        }
    }

    //Push the player away 
    public void a_push() {
        actionDelay = true;
        aggressiveTimer = 5;
        spaceBarCounter = 0;
        myAnimator.SetBool("push", true);
        //  Debug.Log("Pushing");
        Invoke("enemyPushing", 0.1f);
    }

    //Check for the Player, if the enemy is pushing then the player gets knocked back
    public void enemyPushing() {
Graham Solie's avatar
Graham Solie committed
        isEnemyPushing = true;
    //Rouge attack, if the players HP gets really low the enemy goes insane and gets super aggressive
Graham Solie's avatar
Graham Solie committed
    public void a_superAttack() {
        myAnimator.SetFloat("speed", 0f);
        Debug.Log("SUPER!!!!");
Graham Solie's avatar
Graham Solie committed
        myAnimator.SetBool("attack", true);
    }

    //Jump back away from combat, dodging attacks
Graham Solie's avatar
Graham Solie committed
    public void a_jumpBack() {
        actionDelay = true;
Graham Solie's avatar
Graham Solie committed
        if (LookRight == true) {
            Enemy.AddForce(new Vector2(-250, 500));
        }
        else {
            Enemy.AddForce(new Vector2(250, 500));
        }
Graham Solie's avatar
Graham Solie committed
    }

    //Casual jump
    public void a_jump() {
        actionDelay = true;
        Enemy.AddForce(new Vector2(0, 500));
    }

    //Block, will defend off the player reducing hits
Graham Solie's avatar
Graham Solie committed
    public void a_block() {
        //    Debug.Log("Block");
Graham Solie's avatar
Graham Solie committed
        actionDelay = true;
Graham Solie's avatar
Graham Solie committed
        myAnimator.SetBool("attack", false);
        myAnimator.SetBool("block", true);
        Enemy.velocity = Vector2.zero;

    }

    //Kick, kick will disarm the player if they are blocking
    public void a_kick() {
        actionDelay = true;
        if (kickCoolDown <= 0) {
            isEnemyKicking = true;
            kickCoolDown = kickTimer;
            //        Debug.Log("kicking");
            myAnimator.SetBool("kick", true);
            Enemy.velocity = Vector2.zero;
        }
        else {
            myAnimator.SetBool("kick", false);
            myAnimator.SetBool("attack", true);
        }


    }

    //Throw - throws objects at the player
    public void a_throw() {
        actionDelay = true;
        myAnimator.SetFloat("speed", 0f);
        if (throwCoolDown <= 0) {
            throwCoolDown = throwTimer;
            //     Debug.Log("throwing");
            myAnimator.SetBool("throw", true);
            Invoke("bananaToss", 0.2f);
        }
        else {
            myAnimator.SetBool("throw", false);
    //Spawns the object which is thrown at the player
    public void bananaToss() {
        Vector3 offset = transform.rotation * new Vector3(0.5f, 0.5f, 0); //fire from the front of the player
        Instantiate(bananaPrefab, transform.position + offset, transform.rotation);
        myAnimator.SetBool("throw", false);
    //change up the movement so its not so predictable
    public void a_walkOrDash() {
        if (dash == false) {
            a_dashToPlayer();
            dash = true;
        }

        else {
            a_moveToPlayer();
        }

    }
    //Will allow the enemy it either attack the player or preform a random action
    //50% chance of either happening
    public void a_AttackOrRandom() {
        //   Debug.Log("Action");
        float x = Random.Range(0, 2);
        actionDelay = true;

        if (x == 1) {
            a_attack();
        }

        if (x == 2) {
    //Random Actions too keep the Enemy always moving 
    //Each action is given a differnt activation %
    //Push 5%
    //Jump Back 15%
    //Jump Over Player 20%
    //Attack 40%
    //Kick 20%
Graham Solie's avatar
Graham Solie committed
    public void a_randomAction() {
        //  Debug.Log("Action");
        float x = Random.Range(0, 500);
Graham Solie's avatar
Graham Solie committed

Graham Solie's avatar
Graham Solie committed
        actionDelay = true;
        if (randomActionCoolDown <= 0) {
            //Debug.Log(x);
Graham Solie's avatar
Graham Solie committed
            randomActionCoolDown = randomAction;
Graham Solie's avatar
Graham Solie committed

            if (x < 25) {
Graham Solie's avatar
Graham Solie committed
            }

            if (x > 25 && x < 125) {
                a_jumpOverPlayer();
            if (x > 225 && x <= 300) {
Graham Solie's avatar
Graham Solie committed
                a_jumpBack();
            }

            if (x >= 125 && x <= 225) {
                a_attack();
            }
            if (x > 300 && x <= 400) {
Graham Solie's avatar
Graham Solie committed
        }
Graham Solie's avatar
Graham Solie committed

Graham Solie's avatar
Graham Solie committed
    }
Graham Solie's avatar
Graham Solie committed

    //Random Block so the enemy doesnt just hold his shield every time
    //each block is given a certain % activation
    public void a_randomBlock() {
Graham Solie's avatar
Graham Solie committed

        float x = Random.Range(0, 350);

        actionDelay = true;
        if (randomActionCoolDown <= 0) {
            //Debug.Log(x);
            randomActionCoolDown = randomAction;

            if (x < 125) {
            }
            if (x >= 125 && x <= 200) {
                a_jumpBack();
            }

            if (x > 200 && x < 250) {
                a_push();
            }

            if (x > 250) {
                a_block();
    //Random Attack
    //Enemy will still use basic attack more often then any action 
    //this random action gives variety 
Graham Solie's avatar
Graham Solie committed
    public void a_randomAttack() {

        float x = Random.Range(0, 250);

        actionDelay = true;
        if (randomActionCoolDown <= 0) {
            //Debug.Log(x);
            randomActionCoolDown = randomAction;

            if (x < 125) {
                a_attack();
            }
            if (x >= 125 && x <= 200) {
                a_kick();
            }

            if (x > 200) {
Graham Solie's avatar
Graham Solie committed
    }

    //Chance to Kick the shield down off the player if they are blocking
Graham Solie's avatar
Graham Solie committed
    public void a_disarmSheild() {
        actionDelay = true;
        float x = Random.Range(0, 3);
        if (x == 1) {
            a_kick();
        }

        else {
            a_randomAttack();
        }
Graham Solie's avatar
Graham Solie committed

Graham Solie's avatar
Graham Solie committed
    public void a_moveToPlayer() {
        //   Debug.Log("moving");
Graham Solie's avatar
Graham Solie committed
        //rotate to look at the player
Graham Solie's avatar
Graham Solie committed
        //transform.LookAt(target.position);
Graham Solie's avatar
Graham Solie committed
        myAnimator.SetFloat("speed", 1); // This should probbaly reflect the actual speed
Graham Solie's avatar
Graham Solie committed
        transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
Graham Solie's avatar
Graham Solie committed
        if (target.transform.position.x <= transform.position.x) //players spot in world space as opposed to enemy "self" spot 
        {
Graham Solie's avatar
Graham Solie committed
            LookRight = false;
Graham Solie's avatar
Graham Solie committed
            transform.rotation = new Quaternion(0, 180, 0, 0); // flips enemy around to face the player on x axis only
Graham Solie's avatar
Graham Solie committed
        }
Graham Solie's avatar
Graham Solie committed
        else if (target.transform.position.x >= transform.position.x) //players spot in world space as opposed to enemy "self" spot
        {
Graham Solie's avatar
Graham Solie committed
            LookRight = true;
Graham Solie's avatar
Graham Solie committed
            transform.rotation = new Quaternion(0, 0, 0, 0); // flips enemy around to face the player on x axis only }
        }
Graham Solie's avatar
Graham Solie committed
    }
Graham Solie's avatar
Graham Solie committed

    public void a_dashToPlayer() {
        //   Debug.Log("moving");
        //rotate to look at the player
        //transform.LookAt(target.position);
        myAnimator.SetFloat("speed", 1); // This should probbaly reflect the actual speed

        if (target.transform.position.x <= transform.position.x) //players spot in world space as opposed to enemy "self" spot 
        {
            LookRight = false;
            transform.rotation = new Quaternion(0, 180, 0, 0); // flips enemy around to face the player on x axis only
            Enemy.AddForce(new Vector2(-350, Player.velocity.y * 5));
        }
        else if (target.transform.position.x >= transform.position.x) //players spot in world space as opposed to enemy "self" spot
        {
            LookRight = true;
            transform.rotation = new Quaternion(0, 0, 0, 0); // flips enemy around to face the player on x axis only }
            Enemy.AddForce(new Vector2(350, Player.velocity.y * 5));
        }
    }




Graham Solie's avatar
Graham Solie committed
    public void a_moveFromPlayer() {
        //  Debug.Log("movingAWAY");
Graham Solie's avatar
Graham Solie committed
        //rotate to look at the player
        //transform.LookAt(target.position);
Graham Solie's avatar
Graham Solie committed
        myAnimator.SetFloat("speed", 1); // This should probbaly reflect the actual speed
        transform.position = Vector2.MoveTowards(transform.position, target.position, (-speed * 2) * Time.deltaTime);
Graham Solie's avatar
Graham Solie committed

Graham Solie's avatar
Graham Solie committed
        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 }
        }
Graham Solie's avatar
Graham Solie committed


    /*
     * BuildDecisionTree()
     * each action and decision above are used in this function to create the DecisionTree
     */
Graham Solie's avatar
Graham Solie committed
    public void BuildDecisionTree() {

Jordan's avatar
Jordan committed
        //Actions
        DecisionTree Dash = new DecisionTree();
        Dash.setAction(a_dashToPlayer);

        DecisionTree RandomWalk = new DecisionTree();
        RandomWalk.setAction(a_walkOrDash);

Graham Solie's avatar
Graham Solie committed
        DecisionTree RandomAction = new DecisionTree();
        RandomAction.setAction(a_randomAction);

        DecisionTree RandomBlock = new DecisionTree();
        RandomBlock.setAction(a_randomBlock);

        DecisionTree RandomAttack = new DecisionTree();
        RandomAttack.setAction(a_randomAction);

        DecisionTree AttackOrRandom = new DecisionTree();
Graham Solie's avatar
Graham Solie committed
        AttackOrRandom.setAction(a_AttackOrRandom);
Graham Solie's avatar
Graham Solie committed
        DecisionTree Push = new DecisionTree();
        Push.setAction(a_push);

        DecisionTree Rouge = new DecisionTree();
        Rouge.setAction(a_superAttack);

        DecisionTree Kick = new DecisionTree();
        Kick.setAction(a_kick);

Graham Solie's avatar
Graham Solie committed
        DecisionTree DisarmShield = new DecisionTree();
        DisarmShield.setAction(a_disarmSheild);

Graham Solie's avatar
Graham Solie committed
        DecisionTree JumpBack = new DecisionTree();
        JumpBack.setAction(a_jumpBack);

Graham Solie's avatar
Graham Solie committed
        DecisionTree Block = new DecisionTree();
        Block.setAction(a_block);
Graham Solie's avatar
Graham Solie committed

        DecisionTree MoveToPlayer = new DecisionTree();
Graham Solie's avatar
Graham Solie committed
        MoveToPlayer.setAction(a_moveToPlayer);
Graham Solie's avatar
Graham Solie committed

        DecisionTree Attack = new DecisionTree();
Graham Solie's avatar
Graham Solie committed
        Attack.setAction(a_attack);
Graham Solie's avatar
Graham Solie committed
        DecisionTree MoveAway = new DecisionTree();
        MoveAway.setAction(a_moveFromPlayer);

Graham Solie's avatar
Graham Solie committed
        DecisionTree JumpOverPlayer = new DecisionTree();
        JumpOverPlayer.setAction(a_jumpOverPlayer);

        DecisionTree Throw = new DecisionTree();
        Throw.setAction(a_throw);

Graham Solie's avatar
Graham Solie committed
        // Decisions
        DecisionTree isPlayerBlocking = new DecisionTree();
        isPlayerBlocking.setDecision(d_isPlayerBlocking);
Graham Solie's avatar
Graham Solie committed
        isPlayerBlocking.setLeft(DisarmShield);
        isPlayerBlocking.setRight(Push);
        DecisionTree isPlayerBlocking2 = new DecisionTree();
        isPlayerBlocking2.setDecision(d_isPlayerBlocking);
        isPlayerBlocking2.setLeft(DisarmShield);
        isPlayerBlocking2.setRight(AttackOrRandom);
Graham Solie's avatar
Graham Solie committed
        DecisionTree JumpPlayer = new DecisionTree();
        JumpPlayer.setDecision(d_jumpOverPlayer);
        JumpPlayer.setLeft(JumpOverPlayer);
        JumpPlayer.setRight(RandomAction);
Graham Solie's avatar
Graham Solie committed

Graham Solie's avatar
Graham Solie committed
        DecisionTree PlayerHP2 = new DecisionTree();
        PlayerHP2.setDecision(d_isPlayerHpLow);
        PlayerHP2.setLeft(AttackOrRandom);
        PlayerHP2.setRight(isPlayerBlocking2);
Graham Solie's avatar
Graham Solie committed

        DecisionTree PlayerHP3 = new DecisionTree();
        PlayerHP3.setDecision(d_isPlayerHpLow);
        PlayerHP3.setLeft(Rouge);
        PlayerHP3.setRight(isPlayerBlocking2);
Graham Solie's avatar
Graham Solie committed

        DecisionTree EnemyHP = new DecisionTree();
        EnemyHP.setDecision(d_isHpLow);
        EnemyHP.setLeft(PlayerHP2);
        EnemyHP.setRight(PlayerHP3);
Graham Solie's avatar
Graham Solie committed

        DecisionTree GettingAttacked = new DecisionTree();
        GettingAttacked.setDecision(d_isPlayerAttacking);
        GettingAttacked.setLeft(RandomBlock);
        GettingAttacked.setRight(EnemyHP);

        DecisionTree PlayerHP = new DecisionTree();
        PlayerHP.setDecision(d_isPlayerHpLow);
        PlayerHP.setLeft(Attack);
        PlayerHP.setRight(isPlayerBlocking);
Graham Solie's avatar
Graham Solie committed
        DecisionTree JumpPlayer2 = new DecisionTree();
        JumpPlayer2.setDecision(d_jumpOverPlayer);
        JumpPlayer2.setLeft(JumpOverPlayer);
        JumpPlayer2.setRight(EnemyHP);
Graham Solie's avatar
Graham Solie committed

        DecisionTree GettingAttacked2 = new DecisionTree();
        GettingAttacked2.setDecision(d_isPlayerAttacking);
        GettingAttacked2.setLeft(RandomBlock);
        GettingAttacked2.setRight(EnemyHP);
Graham Solie's avatar
Graham Solie committed

Graham Solie's avatar
Graham Solie committed
        //Random Action Trees
        DecisionTree Random01 = new DecisionTree();
        Random01.setDecision(d_randomDecision75);
        Random01.setLeft(PlayerHP);
        Random01.setRight(Attack);
Graham Solie's avatar
Graham Solie committed

        //Random Action Trees
        DecisionTree Random02 = new DecisionTree();
        Random02.setDecision(d_randomDecision75);
        Random02.setLeft(GettingAttacked2);
        Random02.setRight(AttackOrRandom);
Graham Solie's avatar
Graham Solie committed

        DecisionTree Aggressiveness = new DecisionTree();
        Aggressiveness.setDecision(d_isPlayerAggresive);
        Aggressiveness.setLeft(Random01);
        Aggressiveness.setRight(Random02);
Graham Solie's avatar
Graham Solie committed

        DecisionTree IsPlayerTooFar = new DecisionTree();
        IsPlayerTooFar.setDecision(d_isPlayerTooFar);
        IsPlayerTooFar.setLeft(Throw);
        IsPlayerTooFar.setRight(RandomWalk);
Graham Solie's avatar
Graham Solie committed
        DecisionTree IsPlayerTooClose = new DecisionTree();
        IsPlayerTooClose.setDecision(d_isPlayerTooClose);
        IsPlayerTooClose.setLeft(IsPlayerTooFar);
Graham Solie's avatar
Graham Solie committed
        IsPlayerTooClose.setRight(MoveAway);
Graham Solie's avatar
Graham Solie committed
        root.setDecision(d_inAttackRange);
Graham Solie's avatar
Graham Solie committed
        root.setLeft(Aggressiveness);
Graham Solie's avatar
Graham Solie committed
        root.setRight(IsPlayerTooClose);