AStarHeightmapGrid / Assets / PlatypusIdeas / AirPath / Runtime / Events / PathfindingEvents.cs
PathfindingEvents.cs
Raw
using System;
using System.Collections.Generic;
using PlatypusIdeas.AirPath.Runtime.Configuration;
using PlatypusIdeas.AirPath.Runtime.Modes;
using UnityEngine;

namespace PlatypusIdeas.AirPath.Runtime.Events {
    /// <summary>
    /// Base class for all pathfinding events
    /// </summary>
    public abstract class PathfindingEventBase {
        public float Timestamp { get; }
        public object Sender { get; }
        
        protected PathfindingEventBase(object sender = null) {
            Timestamp = Time.time;
            Sender = sender;
        }
    }
    
    /// <summary>
    /// Event fired when a new path is requested
    /// </summary>
    public class PathRequestedEvent : PathfindingEventBase {
        public PathRequest Request { get; }
        public PathfindingModeType RequestingMode { get; }
        
        public PathRequestedEvent(PathRequest request, PathfindingModeType mode, object sender = null) 
            : base(sender) {
            Request = request;
            RequestingMode = mode;
        }
    }
    
    /// <summary>
    /// Event fired when a path calculation is completed
    /// </summary>
    public class PathCalculatedEvent : PathfindingEventBase {
        public PathResult Result { get; }
        public List<Vector3> WorldPath { get; }
        public float CalculationTime { get; }
        public bool Success { get; }
        
        public PathCalculatedEvent(PathResult result, List<Vector3> worldPath, object sender = null) 
            : base(sender) {
            Result = result;
            WorldPath = worldPath;
            CalculationTime = result?.CalculationTime ?? 0f;
            Success = result?.Success ?? false;
        }
    }
    
    /// <summary>
    /// Event fired when the pathfinding mode changes
    /// </summary>
    public class PathfindingModeChangedEvent : PathfindingEventBase {
        public PathfindingModeType PreviousMode { get; }
        public PathfindingModeType NewMode { get; }
        public IPathfindingMode ModeInstance { get; }
        
        public PathfindingModeChangedEvent(
            PathfindingModeType previousMode, 
            PathfindingModeType newMode,
            IPathfindingMode modeInstance,
            object sender = null) : base(sender) {
            PreviousMode = previousMode;
            NewMode = newMode;
            ModeInstance = modeInstance;
        }
    }
    
    /// <summary>
    /// Event for visualization updates
    /// </summary>
    public class VisualizationUpdateEvent : PathfindingEventBase {
        public enum UpdateType {
            PathLineUpdated,
            MarkersUpdated,
            GridCellsUpdated,
            DebugVisualizationToggled,
            VisualizationCleared
        }
        
        public UpdateType Type { get; }
        public object Data { get; }
        
        public VisualizationUpdateEvent(UpdateType type, object data = null, object sender = null) 
            : base(sender) {
            Type = type;
            Data = data;
        }
    }
    
    /// <summary>
    /// Event fired when a boundary violation occurs
    /// </summary>
    public class BoundaryViolationEvent : PathfindingEventBase {
        public Vector2Int OriginalPosition { get; }
        public Vector2Int ClampedPosition { get; }
        public string ViolationType { get; }
        public bool WasAutoClamped { get; }
        
        public BoundaryViolationEvent(
            Vector2Int original, 
            Vector2Int clamped, 
            string violationType,
            bool wasAutoClamped,
            object sender = null) : base(sender) {
            OriginalPosition = original;
            ClampedPosition = clamped;
            ViolationType = violationType;
            WasAutoClamped = wasAutoClamped;
        }
    }
    
    /// <summary>
    /// Event for bird swarm updates
    /// </summary>
    public class SwarmUpdateEvent : PathfindingEventBase {
        public enum UpdateType {
            BirdsSpawned,
            BirdsStartedPath,
            BirdReachedTarget,
            AllBirdsReachedTarget,
            SwarmPositionUpdated
        }
        
        public UpdateType Type { get; }
        public Vector3 Position { get; }
        public int BirdCount { get; }
        public object AdditionalData { get; }
        
        public SwarmUpdateEvent(
            UpdateType type, 
            Vector3 position, 
            int birdCount = 0,
            object additionalData = null,
            object sender = null) : base(sender) {
            Type = type;
            Position = position;
            BirdCount = birdCount;
            AdditionalData = additionalData;
        }
    }
    
    
    /// <summary>
    /// Event for configuration changes
    /// </summary>
    public class ConfigurationChangedEvent : PathfindingEventBase {
        public ConfigurationBase Configuration { get; }
        public string ConfigurationType { get; }
        public bool RequiresRecalculation { get; }
        
        public ConfigurationChangedEvent(
            ConfigurationBase config, 
            bool requiresRecalc = false,
            object sender = null) : base(sender) {
            Configuration = config;
            ConfigurationType = config.GetType().Name;
            RequiresRecalculation = requiresRecalc;
        }
    }
}