I understand your challenge - you're designing a complex 2D combat platformer with a state-based system, modular actions, and animation-synchronized hitboxes. Let me outline a scalable framework approach:
Core Architecture
Start with a state machine pattern combined with a component-based system:
public abstract class CharacterState
{
protected Character Owner { get; private set; }
public virtual void Enter() {}
public virtual void Exit() {}
public virtual void Update() {}
public virtual void FixedUpdate() {}
public virtual void Initialize(Character owner)
{
Owner = owner;
}
// Dictionary to store state-specific actions
protected Dictionary<string, CharacterAction> ActionMap = new Dictionary<string, CharacterAction>();
public bool TryGetAction(string actionKey, out CharacterAction action)
{
return ActionMap.TryGetValue(actionKey, out action);
}
}
public abstract class CharacterAction
{
protected Character Owner { get; private set; }
protected CharacterState CurrentState { get; private set; }
public virtual void Initialize(Character owner, CharacterState state)
{
Owner = owner;
CurrentState = state;
}
public virtual bool CanExecute() { return true; }
public virtual void Execute() {}
public virtual void Update() {}
public virtual void End() {}
}
Animation Frame Tracking
For precise frame data without relying on animation events:
public class FrameData
{
public int FrameIndex { get; private set; }
public float Duration { get; private set; }
public List<HitboxData> Hitboxes { get; private set; } = new List<HitboxData>();
public List<HurtboxData> Hurtboxes { get; private set; } = new List<HurtboxData>();
}
public class AnimationTracker
{
private float _timer = 0f;
private int _currentFrame = 0;
private List<FrameData> _frames = new List<FrameData>();
private Animator _animator;
public void Update(float deltaTime)
{
_timer += deltaTime;
if (_currentFrame < _frames.Count && _timer >= _frames[_currentFrame].Duration)
{
_timer -= _frames[_currentFrame].Duration;
_currentFrame = (_currentFrame + 1) % _frames.Count;
UpdateHitHurtBoxes();
}
// Sync animator
_animator.Play("AnimationName", 0, GetNormalizedTime());
}
private float GetNormalizedTime()
{
// Calculate normalized time for animator sync
float totalDuration = _frames.Sum(f => f.Duration);
float elapsedTime = 0;
for (int i = 0; i < _currentFrame; i++)
elapsedTime += _frames[i].Duration;
elapsedTime += _timer;
return elapsedTime / totalDuration;
}
private void UpdateHitHurtBoxes()
{
// Apply current frame's hitbox/hurtbox data
}
}
Input Handling with New Input System
public class InputHandler : MonoBehaviour
{
private PlayerInput _playerInput;
private Character _character;
private void Awake()
{
_playerInput = GetComponent<PlayerInput>();
_character = GetComponent<Character>();
}
public void OnAttack(InputAction.CallbackContext context)
{
if (!context.performed) return;
Vector2 direction = _playerInput.actions["Move"].ReadValue<Vector2>();
string actionKey = "Attack";
if (direction.y > 0.5f) actionKey = "UpTiltAttack";
else if (direction.y < -0.5f) actionKey = "DownTiltAttack";
else if (Mathf.Abs(direction.x) > 0.5f) actionKey = "SideTiltAttack";
_character.TryExecuteAction(actionKey);
}
public void OnDash(InputAction.CallbackContext context)
{
if (!context.performed) return;
_character.TryExecuteAction("Dash");
}
// Other input handlers
}
Character Base Class with Generic Extensions
public abstract class Character : MonoBehaviour
{
protected CharacterState CurrentState { get; private set; }
protected Dictionary<System.Type, CharacterState> States = new Dictionary<System.Type, CharacterState>();
public T GetState<T>() where T : CharacterState
{
if (States.TryGetValue(typeof(T), out CharacterState state))
return (T)state;
return null;
}
public void ChangeState<T>() where T : CharacterState
{
if (CurrentState != null)
CurrentState.Exit();
CurrentState = GetState<T>();
CurrentState?.Enter();
}
public bool TryExecuteAction(string actionKey)
{
if (CurrentState == null) return false;
if (CurrentState.TryGetAction(actionKey, out CharacterAction action))
{
if (action.CanExecute())
{
action.Execute();
return true;
}
}
return false;
}
protected virtual void Update()
{
CurrentState?.Update();
}
protected virtual void FixedUpdate()
{
CurrentState?.FixedUpdate();
}
}
// Type-specific extension
public class Player : Character
{
public float Stamina { get; private set; }
protected override void Awake()
{
base.Awake();
// Initialize player-specific states
States[typeof(PlayerGroundState)] = new PlayerGroundState();
States[typeof(PlayerAirState)] = new PlayerAirState();
// Initialize all states
foreach (var state in States.Values)
state.Initialize(this);
// Set initial state
ChangeState<PlayerGroundState>();
}
}
State-Specific Actions
public class PlayerGroundState : CharacterState
{
public override void Initialize(Character owner)
{
base.Initialize(owner);
// Register ground-specific actions
ActionMap["Dash"] = new PlayerGroundDash();
ActionMap["Attack"] = new PlayerGroundAttack();
ActionMap["UpTiltAttack"] = new PlayerGroundUpTiltAttack();
// Initialize all actions
foreach (var action in ActionMap.Values)
action.Initialize(owner, this);
}
}
public class PlayerAirState : CharacterState
{
public override void Initialize(Character owner)
{
base.Initialize(owner);
// Register air-specific actions
ActionMap["Dash"] = new PlayerAirDash();
ActionMap["Attack"] = new PlayerAirAttack();
// Initialize all actions
foreach (var action in ActionMap.Values)
action.Initialize(owner, this);
}
}
ScriptableObject-Based Action Configuration
For drag-and-drop modularity:
[CreateAssetMenu(fileName = "NewActionData", menuName = "Combat/Action Data")]
public class ActionData : ScriptableObject
{
public string ActionName;
public float Cooldown;
public AnimationClip Animation;
public List<FrameData> Frames;
// Other configuration parameters
}
// Factory to create actions from data
public static class ActionFactory
{
public static CharacterAction CreateAction(ActionData data, System.Type actionType)
{
CharacterAction action = System.Activator.CreateInstance(actionType) as CharacterAction;
// Configure action with data
return action;
}
}
This framework gives you:
- A state machine for character states (ground, air, etc.)
- State-specific action mapping for different inputs
- Manual frame tracking for precise hitbox/hurtbox timing
- Type-specific extensions without duplicating core logic
- ScriptableObject-based configuration for drag-and-drop modularity
The key is separating the concerns: states manage overall character behavior, actions handle specific abilities, and animation tracking precisely controls hitboxes/hurtboxes. This approach scales well as you add more characters, states, and actions.
Would you like me to explain or break down any part of this code in more detail?