Skip to content

Instantly share code, notes, and snippets.

@general-games
Created January 7, 2023 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save general-games/ef43743c3fcfa45c13fa12f3580cc8f6 to your computer and use it in GitHub Desktop.
Save general-games/ef43743c3fcfa45c13fa12f3580cc8f6 to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Linq;
using Gridr.Datastructures;
using Gridr.Gameplay;
using Scripts.Gameplay.Interactive.Sequence.Attack;
using UnityEngine;
namespace Grid.Adw
{
[CreateAssetMenu(menuName = "Gridr/Sequences/ADW/Attack Area Sequence")]
public class InflictAreaDamageSequence : AttackSequence
{
[SerializeField] private AnimationBank animationBank;
[SerializeField] private GameObject visualEffect;
private Animator _animator;
private float length;
private int aoeRange = 2;
public override IEnumerator Run(GridEntity attackingEntity, IAttackTarget attackTarget, OnSequenceStarted onSequenceStarted,
OnSequenceCompleted onSequenceCompleted)
{
onSequenceStarted?.Invoke();
SetAttackSpriteDirection();
GameObject explosion;
var attackAction = (AttackAction)attackingEntity.FindAction(typeof(AttackAction));
var inRangeCells = attackAction.gameGrid.cells.Where(cell =>
InRange(cell.gridPosition, attackTarget.GetCell().gridPosition, aoeRange));
if (attackAction)
{
_animator = attackingEntity.GetComponent<Animator>();
_animator.CrossFade(animationBank.GetAttackClip(PropertyUtil.GetTeam(attackingEntity).team).name, 0, 0);
if (visualEffect)
{
explosion = Instantiate(visualEffect, attackTarget.GetCell().transform.position, Quaternion.identity);
Destroy(explosion, _animator.GetCurrentAnimatorClipInfo(0).Length);
foreach (var areaCell in inRangeCells)
{
var areaExplosion = Instantiate(visualEffect, areaCell.transform.position, Quaternion.identity);
Destroy(areaExplosion, _animator.GetCurrentAnimatorClipInfo(0).Length);
}
}
yield return new WaitForSeconds(_animator.GetCurrentAnimatorClipInfo(0).Length);
attackTarget.Damage(attackAction.attackData.damageAmount);
foreach (var areaCell in inRangeCells)
{
foreach (var entity in areaCell.Occupants)
{
var areaTarget = PropertyUtil.GetAttackTarget(entity);
areaTarget?.Damage(attackAction.attackData.damageAmount);
}
}
}
_animator.CrossFade(animationBank.GetIdleClip(PropertyUtil.GetTeam(attackingEntity).team).name, 0, 0);
SetSpriteDirection();
onSequenceCompleted?.Invoke();
void SetAttackSpriteDirection() => attackingEntity.GetComponent<SpriteRenderer>().flipX = (attackingEntity.Cell.gridPosition - attackTarget.GetCell().gridPosition).position.x > 0;
void SetSpriteDirection() => attackingEntity.GetComponent<SpriteRenderer>().flipX = PropertyUtil.GetTeam(attackingEntity).team.teamDirection == TeamDirection.Left;
}
public bool InRange(GridPosition gp, GridPosition other, int threshold)
{
var difference = other - gp;
return !(gp == other) &&
Mathf.Abs(difference.position.y) < threshold &&
Mathf.Abs(difference.position.z) < threshold &&
Mathf.Abs(difference.position.y) + MathF.Abs(difference.position.x) + Mathf.Abs(difference.position.z) < threshold;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment