Skip to content

Commit

Permalink
Rat attack damage bound to animation frame
Browse files Browse the repository at this point in the history
This commit also fixes issues with layer mask in enemy sensor. Refactor of attack behavour. Move detail in player ragdoll ...
  • Loading branch information
Avokadoen committed Oct 18, 2020
1 parent a942302 commit c01c330
Show file tree
Hide file tree
Showing 12 changed files with 2,279 additions and 2,385 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ public class OAAttackStats : ScriptableObject
public int damage;
// How many seconds it takes to do one attack
public float attackSpeed;
public float attackCooldown;
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

enum AttackState
{
Attacking,
WindingUp
}
using UnityEngine;

public class OAEnemyAttackingBehaviour : StateMachineBehaviour
{
private OAEnemySensors sensors;
private Rigidbody2D rb;

private float duration = 0;


// OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
Expand All @@ -27,21 +16,6 @@ override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo
rb = sensors.Rb;

rb.velocity = new Vector2(0, rb.velocity.y);
duration = 0;
animator.SetBool("isAttackCompleteReady", false);
animator.speed = sensors.AttackStats.attackSpeed;
}

// OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
duration += Time.deltaTime;
if (duration > sensors.AttackStats.attackSpeed)
{
// player.TakeDamage(sensors.AttackStats.damage); TODO: Check colliders in a radius around attack and apply damage
duration = 0;
animator.SetBool("isAttackCompleteReady", true);
return;
}
}
}
35 changes: 30 additions & 5 deletions Assets/ProjectAssets/Characters/Common/Scripts/OAEnemySensors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class OAEnemySensors : MonoBehaviour

[SerializeField]
private Transform ratHead;
public Transform RatHead { get => ratHead; }

[SerializeField]
private OAMovingEntity moveStats = null;
Expand All @@ -37,7 +38,10 @@ public class OAEnemySensors : MonoBehaviour
[SerializeField]
private OAKillable selfKillable;

private LayerMask buildingMask;
private LayerMask playerAndBuldingLayer;
private Vector2 range;
// We limit attack testing to 4 object. This might be more, or it migth be enough with less
private RaycastHit2D[] hits = new RaycastHit2D[4];

public float targetDistance;
private Vector3 front;
Expand All @@ -63,7 +67,8 @@ void Start()
if (!selfKillable)
selfKillable = GetComponent<OAKillable>();

buildingMask = LayerMask.NameToLayer("Building");
playerAndBuldingLayer = LayerMask.GetMask(new string[] { "Player", "Building" });
range = new Vector2(AttackStats.range * 1.2f, AttackStats.range);
}

// Update is called once per frame
Expand All @@ -73,8 +78,28 @@ void FixedUpdate()
front.x = targetDistance;
front.Normalize();
bool isInTargetRange = Mathf.Abs(targetDistance) < attackStats.range;
var hit = Physics2D.Raycast(transform.position, front, attackStats.range, buildingMask);
animator.SetBool("isInAttackRange", isInTargetRange || hit.collider);
animator.SetInteger("health", selfKillable.Health);
var hitCount = Physics2D.RaycastNonAlloc(ratHead.position, front, hits, attackStats.range, playerAndBuldingLayer);
Debug.DrawRay(ratHead.position, front);
animator.SetBool("isInAttackRange", isInTargetRange || hitCount > 0);
}

void CommitAttack()
{
int hitCount = Physics2D.CapsuleCastNonAlloc(
RatHead.position,
range,
CapsuleDirection2D.Horizontal,
0,
front,
hits,
Mathf.Infinity,
playerAndBuldingLayer.value
);

for (int i = 0; i < hitCount; i++)
{
hits[i].collider.gameObject.SendMessage("ApplyDamage", AttackStats.damage, SendMessageOptions.DontRequireReceiver); // TODO: this is probably terribly slow?
// TODO: apply damage
}
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit c01c330

Please sign in to comment.