Created
March 1, 2023 23:05
-
-
Save mrkybe/576d11ff4363b6eb6ca3a3535589c78c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Animancer; | |
using Assets; | |
using System; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public struct InputState | |
{ | |
public Vector2 MovementDirection; | |
public bool SprintPressed; | |
public bool MovementKeyPressed; | |
public bool MovementKeyWasCancelled; | |
public bool BasicAttackPressed; | |
public bool StrongAttackPressed; | |
public bool KickAttackPressed; | |
public bool ThrowAttackPressed; | |
} | |
public abstract class EntController : MonoBehaviour | |
{ | |
protected Vector2 moveDirection; | |
protected Rigidbody2D rb; | |
protected AnimancerComponent animController; | |
protected AnimancerState animState; | |
protected SimpleEventReceiver _EventReceiver; | |
protected SpriteRenderer spriteRenderer; | |
public bool IsFalling { get; protected set; } | |
public bool FlipX { get; protected set; } | |
protected AnimationClip LastPlayedAnimation { get; private set; } | |
private InputState _inputState; | |
protected ref InputState InputState { get { return ref _inputState; } } | |
protected int ZDepthOffset { get; set; } | |
protected int AnimationPriorityLevel { get; set; } | |
protected Action InterruptCurrentAnimationCallback; | |
protected bool CanMove { get { return !CantMove; } } | |
protected bool CantMove | |
{ | |
get | |
{ | |
foreach (var f in CantMoveCauses) | |
{ | |
if (f.Invoke()) return true; | |
} | |
return false; | |
} | |
} | |
protected bool CantAttack | |
{ | |
get | |
{ | |
foreach (var f in CantAttackCauses) | |
{ | |
if (f.Invoke()) return true; | |
} | |
return false; | |
} | |
} | |
protected bool IsAttacking | |
{ | |
get | |
{ | |
foreach (var f in IsAttackingCauses) | |
{ | |
if (f.Invoke()) return true; | |
} | |
return false; | |
} | |
} | |
protected bool IsMovingOrIdle | |
{ | |
get | |
{ | |
foreach (var f in IsMovingOrIdleCauses) | |
{ | |
if (f.Invoke()) return true; | |
} | |
return false; | |
} | |
} | |
protected List<Func<bool>> CantMoveCauses { get; private set; } | |
protected List<Func<bool>> CantAttackCauses { get; private set; } | |
protected List<Func<bool>> IsAttackingCauses { get; private set; } | |
protected List<Func<bool>> IsMovingOrIdleCauses { get; private set; } | |
public enum FlipMode { MovementDirection, NearestEnemy, Velocity, Locked } | |
protected FlipMode flipMode { get; private set; } | |
protected void Awake() | |
{ | |
ActiveDamageBoxes = new HashSet<DamageBox>(); | |
animController = GetComponentInChildren<AnimancerComponent>(); | |
spriteRenderer = GetComponentInChildren<SpriteRenderer>(); | |
_EventReceiver = GetComponentInChildren<SimpleEventReceiver>(); | |
_inputState = new InputState(); | |
CantMoveCauses = new List<Func<bool>>(); | |
CantAttackCauses = new List<Func<bool>>(); | |
IsAttackingCauses = new List<Func<bool>>(); | |
IsMovingOrIdleCauses = new List<Func<bool>>(); | |
flipMode = FlipMode.MovementDirection; | |
rb = GetComponent<Rigidbody2D>(); | |
} | |
protected void OnTriggerEnter2D(Collider2D collision) | |
{ | |
if (collision.gameObject.CompareTag("FallZone")) | |
{ | |
IsFalling = true; | |
rb.gravityScale = 1f; | |
rb.AddForce(new Vector2(Mathf.Sign(moveDirection.x) * 10f, 100f)); | |
var hp = GetComponent<CharHealth>(); | |
hp.Hurt(hp.HP, new FallingDamage()); | |
gameObject.SetLayerRecursively(Common.NoCollideAllLayer); | |
} | |
} | |
protected void Update() | |
{ | |
GetMovementAndAttacksInput(); | |
ApplyMovementAndAttacks(); | |
spriteRenderer.sortingOrder = (int)(transform.position.y * -100f) + 200 + ZDepthOffset; | |
} | |
protected void FixedUpdate() | |
{ | |
transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.y); | |
} | |
protected void FlipXDirection() | |
{ | |
FlipX = !FlipX; | |
transform.localScale = new Vector3(transform.localScale.x * -1, transform.localScale.y, transform.localScale.z); | |
} | |
private void FlipXDirectionChildren(Transform parent) | |
{ | |
foreach (Transform t in parent) | |
{ | |
FlipXDirectionChildren(t); | |
t.transform.localPosition = new Vector3(t.transform.localPosition.x * -1f, t.transform.localPosition.y, 0); | |
} | |
} | |
protected abstract void GetMovementAndAttacksInput(); | |
protected abstract void ApplyMovementAndAttacks(); | |
protected void UpdateAnimator() | |
{ | |
if (flipMode == FlipMode.MovementDirection) | |
{ | |
if (moveDirection.x < -0.1f && FlipX == false) | |
{ | |
FlipXDirection(); | |
} | |
else if (moveDirection.x > 0.1f && FlipX == true) | |
{ | |
FlipXDirection(); | |
} | |
} | |
else if (flipMode == FlipMode.Velocity) | |
{ | |
if (rb.velocity.x < -0.1f && FlipX == false) | |
{ | |
FlipXDirection(); | |
} | |
else if (rb.velocity.x > 0.1f && FlipX == true) | |
{ | |
FlipXDirection(); | |
} | |
} | |
} | |
protected abstract void MyHealth_TookDamageEvent(object sender, CharHealth.TookDamageArgs e); | |
protected abstract void MyHealth_DiedEvent(object sender, CharHealth.DiedArgs e); | |
protected AnimancerState PlayAnimation(AnimationClip animationClip, float speed = 1f, bool resetIfAlreadyPlaying = false) | |
{ | |
if (resetIfAlreadyPlaying) | |
{ | |
animController.States.Current.NormalizedTime = 0; | |
} | |
if (InterruptCurrentAnimationCallback != null) | |
{ | |
var temp = InterruptCurrentAnimationCallback; | |
InterruptCurrentAnimationCallback = null; | |
temp.Invoke(); | |
} | |
animState = animController.Play(animationClip); | |
animController.States.Current.Speed = speed; | |
LastPlayedAnimation = animationClip; | |
return animState; | |
} | |
private FlipMode oldflipMode; | |
protected void LockFlipMode() | |
{ | |
oldflipMode = flipMode; | |
flipMode = FlipMode.Locked; | |
} | |
protected void UnlockFlipMode() | |
{ | |
flipMode = oldflipMode; | |
} | |
public void SetFlipMode(FlipMode flipMode) | |
{ | |
if (flipMode != FlipMode.Locked) | |
{ | |
this.flipMode = flipMode; | |
} | |
else | |
{ | |
oldflipMode = flipMode; | |
} | |
} | |
protected HashSet<DamageBox> ActiveDamageBoxes { get; private set; } | |
public virtual void RegisterDamageBoxEnabled(DamageBox dmgBox) | |
{ | |
ActiveDamageBoxes.Add(dmgBox); | |
} | |
public virtual void RegisterDamageBoxDisabled(DamageBox dmgBox) | |
{ | |
ActiveDamageBoxes.Remove(dmgBox); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment