Skip to content

Commit

Permalink
implementation of counter visuals + initial circling enemy movement
Browse files Browse the repository at this point in the history
  • Loading branch information
cardosoandre committed Jul 12, 2021
1 parent 7ee493d commit 49bf8dd
Show file tree
Hide file tree
Showing 17 changed files with 17,593 additions and 12,160 deletions.
50 changes: 27 additions & 23 deletions Assets/CombatScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class CombatScript : MonoBehaviour

//Events
public UnityEvent<EnemyScript> OnHit;
public UnityEvent<EnemyScript> OnCounterAttack;

void Start()
{
Expand All @@ -52,21 +53,23 @@ void CounterCheck()
if (!AnEnemyIsPreparingAttack())
return;

transform.DOLookAt(ClosestCounterEnemy().transform.position,.2f);
lockedTarget = ClosestCounterEnemy();
OnCounterAttack.Invoke(lockedTarget);

transform.DOLookAt(lockedTarget.transform.position,.2f);
transform.DOMove(transform.position - transform.forward, duration);

//StartCoroutine(Test(duration));
StartCoroutine(CounterCoroutine(duration));

//IEnumerator Test(float duration)
//{
// isCountering = true;
// movementInput.enabled = false;
// yield return new WaitForSeconds(duration);
// Attack(ClosestCounterEnemy(), TargetDistance(lockedTarget));
// movementInput.enabled = true;
// isCountering = false;
IEnumerator CounterCoroutine(float duration)
{
isCountering = true;
movementInput.enabled = false;
yield return new WaitForSeconds(duration);
Attack(lockedTarget, TargetDistance(lockedTarget));
isCountering = false;

//}
}
}

void AttackCheck()
Expand Down Expand Up @@ -180,7 +183,7 @@ public Vector3 TargetOffset(Transform target)

public void HitEvent()
{
if (enemyDetection.CurrentTarget() == null || lockedTarget == null)
if (lockedTarget == null)
return;

OnHit.Invoke(lockedTarget);
Expand All @@ -196,7 +199,7 @@ bool AnEnemyIsPreparingAttack()
{
foreach (EnemyScript enemyScript in enemyDetection.targets)
{
if (enemyScript.preparingAttack)
if (enemyScript.IsPreparingAttack())
{
return true;
}
Expand All @@ -213,7 +216,7 @@ EnemyScript ClosestCounterEnemy()
{
EnemyScript enemy = enemyDetection.targets[i];

if (enemy.preparingAttack)
if (enemy.IsPreparingAttack())
{
if (Vector3.Distance(transform.position, enemy.transform.position) < minDistance)
{
Expand All @@ -233,25 +236,26 @@ void LerpCharacterAcceleration()
DOVirtual.Float(0, 1, .6f, ((acceleration)=> movementInput.acceleration = acceleration));
}

bool isLastBlow()
{
if (lockedTarget == null)
return false;

return enemyDetection.targets.Count <= 1 && lockedTarget.health <= 1;
}

#region Input

public void OnCounter()
private void OnCounter()
{
CounterCheck();
}

public void OnAttack()
private void OnAttack()
{
AttackCheck();
}

#endregion

bool isLastBlow()
{
if (lockedTarget == null)
return false;

return enemyDetection.targets.Count <= 1 && lockedTarget.health <= 1;
}
}
85 changes: 79 additions & 6 deletions Assets/EnemyScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,44 @@

public class EnemyScript : MonoBehaviour
{
public int health = 3;

Animator animator;
CombatScript playerCombat;
EnemyDetection enemyDetection;
CharacterController characterController;

[Header("Stats")]
public int health = 3;

[Header("States")]
[SerializeField] private bool isPreparingAttack;
[SerializeField] private bool isRecovering;

public bool preparingAttack;
[Header("Polish")]
[SerializeField] private ParticleSystem counterParticle;

void Start()
{
animator = GetComponent<Animator>();
characterController = GetComponent<CharacterController>();
playerCombat = FindObjectOfType<CombatScript>();
enemyDetection = FindObjectOfType<EnemyDetection>();
playerCombat.OnHit.AddListener((x) => OnHit(x));
playerCombat.OnCounterAttack.AddListener((x) => OnCounter(x));

StartCoroutine(PrepareAttackCoroutine());
}

void Update()
{

Vector3 dir = (playerCombat.transform.position - transform.position).normalized;
Vector3 pDir = Quaternion.AngleAxis(90, Vector3.up) * dir; //Vector perpendicular to direction
Vector3 movedir = Vector3.zero;

movedir += pDir * Time.deltaTime;

characterController.Move(movedir);

transform.LookAt(new Vector3(playerCombat.transform.position.x, transform.position.y, playerCombat.transform.position.z));
}

Expand All @@ -34,15 +56,66 @@ void OnHit(EnemyScript target)
{
animator.SetTrigger("Death");

FindObjectOfType<EnemyDetection>().RemoveEnemy(this);
FindObjectOfType<EnemyDetection>().SetCurrentTarget(null);
GetComponent<CharacterController>().enabled = false;
enemyDetection.RemoveEnemy(this);
enemyDetection.SetCurrentTarget(null);
characterController.enabled = false;
this.enabled = false;
return;
}

animator.SetTrigger("Hit");
transform.DOMove(transform.position - (transform.forward/2), .3f).SetDelay(.1f);

if (isPreparingAttack)
PrepareAttack(false);
}

IEnumerator HitCoroutine()
{
yield return new WaitForSeconds(1);

}
}

void OnCounter(EnemyScript target)
{
if(target == this)
{
PrepareAttack(false);
}
}

IEnumerator PrepareAttackCoroutine()
{
yield return new WaitForSeconds(1);
PrepareAttack(true);
yield return new WaitForSeconds(1);

}

void PrepareAttack(bool active)
{
isPreparingAttack = active;

if (active)
{
counterParticle.Play();
}
else
{
counterParticle.Clear();
counterParticle.Stop();
}
}

public bool IsAttackable()
{
return !isRecovering;
}

public bool IsPreparingAttack()
{
return isPreparingAttack;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ AnimatorStateTransition:
serializedVersion: 3
m_TransitionDuration: 0.25
m_TransitionOffset: 0
m_ExitTime: 0.82558143
m_ExitTime: 0.65
m_HasExitTime: 1
m_HasFixedDuration: 1
m_InterruptionSource: 0
Expand Down Expand Up @@ -369,37 +369,37 @@ AnimatorController:
m_DefaultFloat: 0
m_DefaultInt: 0
m_DefaultBool: 0
m_Controller: {fileID: 0}
m_Controller: {fileID: 9100000}
- m_Name: InputX
m_Type: 1
m_DefaultFloat: 0
m_DefaultInt: 0
m_DefaultBool: 0
m_Controller: {fileID: 0}
m_Controller: {fileID: 9100000}
- m_Name: InputY
m_Type: 1
m_DefaultFloat: 0
m_DefaultInt: 0
m_DefaultBool: 0
m_Controller: {fileID: 0}
m_Controller: {fileID: 9100000}
- m_Name: AirPunch
m_Type: 9
m_DefaultFloat: 0
m_DefaultInt: 0
m_DefaultBool: 0
m_Controller: {fileID: 0}
m_Controller: {fileID: 9100000}
- m_Name: Hit
m_Type: 9
m_DefaultFloat: 0
m_DefaultInt: 0
m_DefaultBool: 0
m_Controller: {fileID: 0}
m_Controller: {fileID: 9100000}
- m_Name: Death
m_Type: 9
m_DefaultFloat: 0
m_DefaultInt: 0
m_DefaultBool: 0
m_Controller: {fileID: 0}
m_Controller: {fileID: 9100000}
m_AnimatorLayers:
- serializedVersion: 5
m_Name: Base Layer
Expand Down Expand Up @@ -481,9 +481,9 @@ AnimatorStateTransition:
m_Mute: 0
m_IsExit: 0
serializedVersion: 3
m_TransitionDuration: 0.25
m_TransitionOffset: 0
m_ExitTime: 0.75
m_TransitionDuration: 0.13310504
m_TransitionOffset: 0.0231221
m_ExitTime: 0.70542145
m_HasExitTime: 0
m_HasFixedDuration: 1
m_InterruptionSource: 0
Expand All @@ -506,9 +506,9 @@ AnimatorStateTransition:
m_Mute: 0
m_IsExit: 0
serializedVersion: 3
m_TransitionDuration: 0.25
m_TransitionDuration: 0.08447501
m_TransitionOffset: 0
m_ExitTime: 0.75
m_ExitTime: 0.17073937
m_HasExitTime: 0
m_HasFixedDuration: 1
m_InterruptionSource: 0
Expand All @@ -522,7 +522,7 @@ AnimatorState:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: HeadHity
m_Speed: 1
m_Speed: 1.4
m_CycleOffset: 0
m_Transitions:
- {fileID: -6900291363712344859}
Expand Down Expand Up @@ -623,6 +623,6 @@ AnimatorStateMachine:
m_StateMachineBehaviours: []
m_AnyStatePosition: {x: 260, y: 110, z: 0}
m_EntryPosition: {x: -160, y: 200, z: 0}
m_ExitPosition: {x: 480, y: 120, z: 0}
m_ExitPosition: {x: 460, y: 150, z: 0}
m_ParentStateMachinePosition: {x: 800, y: 20, z: 0}
m_DefaultState: {fileID: 6559091717608044577}
8 changes: 8 additions & 0 deletions Assets/Prefabs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 49bf8dd

Please sign in to comment.