Source file unity-csharp/patterns/feedback-layer/PlayerEvents.cs from the GDnD code examples. Download raw file
// GDnD wiki example
// Demonstrates: the gameplay-event half of a feedback layer. Player states (or any gameplay code)
// announce WHAT happened through these events; they never call hit stop, camera shake,
// particles or sound themselves. FeedbackPlayer.cs listens and decides how each moment feels.
// Related pages: [[state-pattern]], [[game-feel]], [[observer-pattern]], [[scriptableobject-event-channels]]
// Unity 6 (6000.x). No packages required.
//
// Setup: add to the player GameObject next to PlayerStateController (patterns/PlayerStateController.cs).
// States reach it through the context, e.g. player.Events.RaiseLanded(player.transform.position, fallSpeed).
// For listeners in other scenes or spawned prefabs, swap these C# events for ScriptableObject
// event channels; the rest of the design is unchanged.
using System;
using UnityEngine;
namespace GDnD.Patterns.Feedback
{
public class PlayerEvents : MonoBehaviour
{
public event Action<Vector3> Jumped; // where the jump started
public event Action<Vector3, float> Landed; // where, and how fast the player was falling
public event Action<Vector3, int> HitLanded; // where the hit connected, and its damage
public void RaiseJumped(Vector3 at) => Jumped?.Invoke(at);
public void RaiseLanded(Vector3 at, float fallSpeed) => Landed?.Invoke(at, fallSpeed);
public void RaiseHitLanded(Vector3 at, int damage) => HitLanded?.Invoke(at, damage);
}
}