Skip to content
Snippets Groups Projects
Commit 212d889e authored by Jordan's avatar Jordan
Browse files

Changes to enemy AI

DECISIONS TREE CLASS:
- Made lots of members private to remove Unity warnings
- Removed parameters we didn't need from a few functions

ENEMYAI:
- Few structural changes, BuildDecisionTree seems to work in start() now
- Now denoting actions with "a_ACTIONNAME" and decision with
  "d_DECISIONNAME".  Easier to read!
parent 47db5729
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -21,7 +21,7 @@ public class ArenaCombatControl : MonoBehaviour {
weapons[currentWeapon].attackSpeed = 1f;
Player = GetComponent<Rigidbody2D>();
myAnimator = GetComponent<Animator>(); //required for animation *******
SetWeaponStats();
//SetWeaponStats();
}
// Update is called once per frame
......
......@@ -4,17 +4,23 @@ using System.Collections;
public class DecisionTree {
public delegate bool Decision();
public Decision decision;
public Decision decision;
public delegate void Action();
public Action action;
public DecisionTree rightLeaf;
public DecisionTree leftLeaf;
private DecisionTree right;
private DecisionTree left;
public DecisionTree() {
rightLeaf = null;
leftLeaf = null;
right = null;
left = null;
decision = null;
action = null;
}
public bool isLeaf() {
return (right == null && left == null);
}
public void setDecision(Decision d) {
decision = d;
}
......@@ -24,34 +30,22 @@ public class DecisionTree {
}
public void setLeft(DecisionTree node) {
if (decision() == true) {
leftLeaf = node;
}
left = node;
}
public void setRight(DecisionTree node) {
if (decision() == false) {
rightLeaf = node;
}
right = node;
}
public bool isLeaf(DecisionTree node) {
if (rightLeaf == null && leftLeaf == null) {
return true;
}
return false;
}
public void search(DecisionTree node) {
if (isLeaf(node)) {
node.action();
public void search() {
if (isLeaf()) {
action();
}
else if (node.decision() == false) {
rightLeaf.search(node.rightLeaf);
else if (decision() == false) {
left.search ();
}
else if (node.decision() == true) {
leftLeaf.search(node.leftLeaf);
else if (decision() == true) {
right.search ();
}
}
......
......@@ -4,7 +4,7 @@ using System.Collections;
public class EnemyAI : MonoBehaviour {
public DecisionTree root;
public Transform target;
private Transform target;
private Rigidbody2D Enemy;
private Animator myAnimator;
private bool LookRight = false;
......@@ -20,34 +20,27 @@ public class EnemyAI : MonoBehaviour {
myAnimator = GetComponent<Animator>(); //required for animation *******
Enemy = GetComponent<Rigidbody2D>();
BuildDecisionTree();
}
void Update() {
root.search(root);
root.search();
}
public bool inAttackRange() {
public bool a_inAttackRange() {
if (Vector3.Distance(transform.position, target.position) < 1.5f) {
myAnimator.SetBool("speed", false);
return true;
return false;
}
else {
myAnimator.SetBool("block", false);
myAnimator.SetBool("attack", false);
// Debug.Log("In Zone- not");
return false;
return true;
}
}
public bool isPlayerAttacking() {
public bool a_isPlayerAttacking() {
if (Input.GetKeyDown(KeyCode.Space) && Vector3.Distance(transform.position, target.position) < 5f) {
return false;
}
......@@ -65,16 +58,17 @@ public class EnemyAI : MonoBehaviour {
}
}
*/
public void attack() {
// Debug.Log("attacking");
// DECISIONS
public void d_attack() {
Debug.Log("attacking");
// myAnimator.SetBool("speed", false);
myAnimator.SetBool("attack", true);
Enemy.velocity = Vector2.zero;
}
public void run() {
public void d_run() {
Debug.Log("run");
// float option = Random.Range(0, 2);
// if (option == 1) {
myAnimator.SetBool("block", true);
......@@ -87,8 +81,8 @@ public class EnemyAI : MonoBehaviour {
}
public void moveToPlayer() {
// Debug.Log("moving");
public void d_moveToPlayer() {
Debug.Log("moving");
//rotate to look at the player
//transform.LookAt(target.position);
myAnimator.SetBool("speed", true);
......@@ -109,36 +103,32 @@ public class EnemyAI : MonoBehaviour {
public void BuildDecisionTree() {
//Left SubTree
//Actions
DecisionTree Run = new DecisionTree();
Run.setAction(run);
Run.setAction(d_run);
DecisionTree MoveToPlayer = new DecisionTree();
MoveToPlayer.setAction(moveToPlayer);
MoveToPlayer.setAction(d_moveToPlayer);
DecisionTree Attack = new DecisionTree();
Attack.setAction(attack);
/*
DecisionTree HpLevel = new DecisionTree();
HpLevel.setDecision(hpLevel);
HpLevel.setLeft(Attack);
HpLevel.setRight(Retreat);
*/
Attack.setAction(d_attack);
// Decisions
DecisionTree GettingAttacked = new DecisionTree();
GettingAttacked.setDecision(isPlayerAttacking);
GettingAttacked.setDecision(a_isPlayerAttacking);
GettingAttacked.setLeft(Run);
GettingAttacked.setRight(Attack);
//Right Subtree
root.setDecision(inAttackRange);
root.setDecision(a_inAttackRange);
root.setLeft(GettingAttacked);
root.setRight(MoveToPlayer);
/*
DecisionTree HpLevel = new DecisionTree();
HpLevel.setDecision(hpLevel);
HpLevel.setLeft(Attack);
HpLevel.setRight(Retreat);
*/
}
......
......@@ -33,8 +33,8 @@ public class PlayerMovement : MonoBehaviour
Movement(h, i);
FlipPlayer(h);
RotatePlayerZaxis(h);
Attack();
Block();
attack();
block();
}
//controlling the characters movement
......@@ -50,7 +50,7 @@ public class PlayerMovement : MonoBehaviour
}
private void Attack() {
private void attack() {
attackSpeedCooldown -= Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Space) && attackSpeedCooldown <= 0) {
attackSpeedCooldown = weapons[currentWeapon].attackSpeed;
......@@ -61,7 +61,7 @@ public class PlayerMovement : MonoBehaviour
}
}
private void Block()
private void block()
{
if (Input.GetKeyDown(KeyCode.E))
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment