Framework Design for 2D Combat Platformer
To create a scalable framework for your 2D combat platformer in Unity, you can follow this structured approach:
-
State Machine: Implement a state machine to manage different states (e.g., ground, air). Each state will handle inputs and transitions.
-
States: Define states as classes that inherit from a base State class. Each state will have a mapping of inputs to actions and their associated animations.
-
Actions: Create modular actions that can be executed. Each action can have associated logic and animations.
-
Animation Controller: Develop an AnimationController to handle playing animations and syncing hitboxes/hurtboxes with animation frames. This involves manually tracking frames to ensure precise control.
-
Input Handling: Use Unity's new input system to map inputs to actions based on the current state.
-
Hitbox/Hurtbox System: Manage activation/deactivation of hitboxes and hurtboxes based on the current animation frame.
Example Implementation
Here's a high-level implementation outline:
State Machine
public class StateMachine<T> where T : class, IStateOwner
{
private State<T> currentState;
private T owner;
private InputHandler inputHandler;
private AnimationController animationController;
public StateMachine(T owner, InputHandler inputHandler, AnimationController animationController)
{
this.owner = owner;
this.inputHandler = inputHandler;
this.animationController = animationController;
this.inputHandler.OnInput += HandleInput;
}
public void ChangeState(State<T> newState)
{
currentState?.Exit();
currentState = newState;
currentState?.Enter();
}
public void Update()
{
currentState?.Update();
animationController.Update();
}
private void HandleInput(InputAction input)
{
currentState?.HandleInput(input);
}
}
State
public abstract class State<T> where T : class, IStateOwner
{
protected T owner;
protected AnimationController animationController;
public State(T owner, AnimationController animationController)
{
this.owner = owner;
this.animationController = animationController;
}
public abstract void Enter();
public abstract void Exit();
public abstract void Update();
public abstract void HandleInput(InputAction input);
}
public class GroundState<T> : State<T> where T : class, IStateOwner
{
private Dictionary<InputAction, (Action action, string animationName)> actionMap;
public GroundState(T owner, AnimationController animationController) : base(owner, animationController)
{
actionMap = new Dictionary<InputAction, (Action action, string animationName)>
{
{ InputAction.Dash, (new DashAction(), "GroundDash") },
{ InputAction.Attack, (new AttackAction(), "GroundAttack") }
};
}
public override void Enter() { /* ... */ }
public override void Exit() { /* ... */ }
public override void Update() { /* ... */ }
public override void HandleInput(InputAction input)
{
if (actionMap.TryGetValue(input, out var actionData))
{
actionData.action.Execute();
animationController.PlayAnimation(actionData.animationName);
}
}
}
Action
public abstract class Action
{
public abstract void Execute();
}
public class DashAction : Action
{
public override void Execute()
{
// Dash logic here
}
}
Animation Controller
public class AnimationController
{
private Animator animator;
private Dictionary<string, AnimationClipData> animations;
private float currentTime;
private AnimationClipData currentClipData;
private bool isPlaying;
private int currentFrame;
public AnimationController(Animator animator)
{
this.animator = animator;
this.animations = new Dictionary<string, AnimationClipData>();
}
public void AddAnimation(string name, AnimationClipData clipData)
{
animations[name] = clipData;
}
public void PlayAnimation(string name)
{
if (animations.TryGetValue(name, out var clipData))
{
currentClipData = clipData;
currentTime = 0;
isPlaying = true;
currentFrame = 0;
}
}
public void Update()
{
if (isPlaying && currentClipData != null)
{
currentTime += Time.deltaTime;
if (currentTime >= currentClipData.Clip.length)
{
currentTime = 0;
isPlaying = false;
}
// Calculate current frame
float frameTime = currentClipData.Clip.length / currentClipData.Clip.frameRate;
int newFrame = (int)(currentTime / frameTime);
if (newFrame != currentFrame)
{
currentFrame = newFrame;
// Deactivate previous frame's hitboxes and hurtboxes
var prevFrameData = currentClipData.FrameDataList.Find(fd => fd.Frame == currentFrame - 1);
if (prevFrameData != null)
{
foreach (var hitbox in prevFrameData.Hitboxes)
{
hitbox.Deactivate();
}
foreach (var hurtbox in prevFrameData.Hurtboxes)
{
hurtbox.Deactivate();
}
}
// Activate current frame's hitboxes and hurtboxes
var frameData = currentClipData.FrameDataList.Find(fd => fd.Frame == currentFrame);
if (frameData != null)
{
foreach (var hitbox in frameData.Hitboxes)
{
hitbox.Activate();
}
foreach (var hurtbox in frameData.Hurtboxes)
{
hurtbox.Activate();
}
}
}
// Update the animator's playback time manually
animator.Play(currentClipData.Clip.name, 0, currentTime / currentClipData.Clip.length);
}
}
}
Input Handler
public class InputHandler
{
public event Action<InputAction> OnInput;
public void HandleInput(InputAction input)
{
OnInput?.Invoke(input);
}
}
Example Usage
public class Player : MonoBehaviour, IStateOwner
{
private StateMachine<Player> stateMachine;
private InputHandler inputHandler;
private AnimationController animationController;
private void Awake()
{
inputHandler = new InputHandler();
animationController = new AnimationController(GetComponent<Animator>());
stateMachine = new StateMachine<Player>(this, inputHandler, animationController);
// Add animations
var groundDashClipData = new AnimationClipData
{
Clip = Resources.Load<AnimationClip>("GroundDash"),
FrameDataList = new List<FrameData>
{
new FrameData { Frame = 0, Hitboxes = new List<Hitbox>(), Hurtboxes = new List<Hurtbox>() },
// Add more frame data as needed
}
};
animationController.AddAnimation("GroundDash", groundDashClipData);
// Set initial state
stateMachine.ChangeState(new GroundState<Player>(this, animationController));
}
private void Update()
{
stateMachine.Update();
}
}
Key Points
- State Machine: Manages transitions between states and delegates input handling to the current state.
- States: Define available actions and handle inputs. Each state can have its own set of actions and animations.
- Actions: Modular and reusable. Each action can be associated with an animation.
- Animation Controller: Handles playing animations and syncing hitboxes/hurtboxes with animation frames by manually tracking frames.
- Input Handling: Maps inputs to actions based on the current state.
- Hitbox/Hurtbox System: Manages activation/deactivation based on the current animation frame.
This framework provides a scalable and modular approach to handling states, actions, and animations in your 2D combat platformer. It allows for flexibility in defining what each input does in each state and ensures that hitboxes and hurtboxes are synchronized with animation frames.