Skip to content

Commit

Permalink
Refactoring code to be able to user it for the AI
Browse files Browse the repository at this point in the history
Issue: #2
- AI should now chase the enemy if player gets in range. Also starts to attack it.
- Focusing more on GameObject rather then CombatTarget. (Player doesn't have a combatTarget component, cause its not clickable.
  • Loading branch information
Remgax committed Jan 19, 2022
1 parent 57f11f8 commit 521921b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ PrefabInstance:
propertyPath: m_Name
value: Enemy
objectReference: {fileID: 0}
- target: {fileID: 7156413487468441685, guid: 54314722e8eb32848a6b2550fc09f23d, type: 3}
propertyPath: m_Speed
value: 4
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 54314722e8eb32848a6b2550fc09f23d, type: 3}
--- !u!1 &6078929623773163940 stripped
Expand Down Expand Up @@ -109,7 +113,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: babd31d5949ad5247941c96f4bc959e0, type: 3}
m_Name:
m_EditorClassIdentifier:
distance: 5
catchDistance: 5
--- !u!1001 &3393030800166862411
PrefabInstance:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ private void Update()
else
{
GetComponent<Move>().Cancel();

AttackBehaviour();
}
}
Expand Down Expand Up @@ -54,12 +53,12 @@ private bool GetIsInRange()
return Vector3.Distance(transform.position, target.transform.position) < weaponRange;
}

public void Attack(CombatTarget combatTarget)
public void Attack(GameObject combatTarget)
{
GetComponent<ActionScheduler>().startAction(this);
target = combatTarget.GetComponent<Health>(); ;
}
public bool CanAttackTarget(CombatTarget combatTarget)
public bool CanAttackTarget(GameObject combatTarget)
{
if (combatTarget == null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
using System.Collections;
using System.Collections.Generic;
using WorldOfEronia.Combat;
using UnityEngine;

namespace WorldOfEronia.Control
{
public class AIController : MonoBehaviour
{
[SerializeField] float catchDistance = 5f;
Fighter AI;
GameObject player;
private void Start()
{
AI = GetComponent<Fighter>();
player = GameObject.FindWithTag("Player");
}
private void Update()
{

if (DistanceToPlayer() < catchDistance)
if (InAttackRangeOfPlayer() && AI.CanAttackTarget(player))
{
// Debug.Log(gameObject.name + " will move towards the player. Position:" + gameObject.transform.position);
GetComponent<Fighter>().Attack(player);
}
else
{
Debug.Log(gameObject.name + " will move towards the player. Position:" + gameObject.transform.position);
AI.Cancel();
}
}

private float DistanceToPlayer()
private bool InAttackRangeOfPlayer()
{
GameObject player = GameObject.FindWithTag("Player");
return Vector3.Distance(player.transform.position, transform.position);
return Vector3.Distance(player.transform.position, transform.position) < catchDistance;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ private bool InteractWithCombat()
foreach (RaycastHit hit in hits)
{
CombatTarget target = hit.transform.GetComponent<CombatTarget>();
if (!GetComponent<Fighter>().CanAttackTarget(target)) continue;
if (target == null) continue;

if (!GetComponent<Fighter>().CanAttackTarget(target.gameObject)) continue;
if (Input.GetMouseButtonDown(0))
{
GetComponent<Fighter>().Attack(target);
GetComponent<Fighter>().Attack(target.gameObject);
}
return true;
}
Expand Down

0 comments on commit 521921b

Please sign in to comment.