using System; using System.Collections.Generic; using UnityEngine; using Framework.Services; using Framework.Support; namespace Framework.Selection { /// Tracks the objects selected by the player /// /// /// Many genres have a need to store selections. A strategy game may have a set of /// currently selected units to which commands are issued, while an action adventure /// might mark an object as highlighted so the "use" button can perform an action on it, /// without caring if the object was selected by touch, gaze or mouse hover. /// /// /// The selection tracker is added to an actor and tracks which objects are currently /// selected by it. The component is added to an individual actor so it can be looked /// up by other components executing actions on behalf of that actor. /// /// /// Highlighted objects are stored separately from selected objects in the expectation /// that these are the only two channels of selections a typical game needs. Future /// implementations may offer named sets to which items can be added or removed while /// keeping the and /// properties around for easy access. /// /// public class SelectionTracker : ScriptComponent { /// Triggered when the CanSelect property changes its value public Action CanSelectChanged; /// Triggered when the CanHighlight property changes its value public Action CanHighlightChanged; /// Whether the actor is currently allowed to select objects public bool CanSelect = true; /// Whether the actor is currently allowed to highlight objects public bool CanHighlight = true; #if false /// Whether the actor is currently allowed to select objects public bool CanSelect { get { return this.canSelect; } set { if (value != this.canSelect) { this.canSelect = value; OnCanSelectChanged(); } } } /// Whether the actor is currently allowed to highlight objects public bool CanHighlight { get { return this.canHighlight; } set { if (value != this.canHighlight) { this.canHighlight = value; OnCanHighlightChanged(); } } } #endif /// Objects that are currently selected public ICollection SelectedObjects { get { if(this.selectedObjects == null) { this.selectedObjects = new ObservableCollection(); } return this.selectedObjects; } } /// Objects that are currently highlighted public ICollection HighlightedObjects { get { if(this.highlightedObjects == null) { this.highlightedObjects = new ObservableCollection(); } return this.highlightedObjects; } } /// Fires the event protected virtual void OnCanSelectChanged() { if (CanSelectChanged != null) { CanSelectChanged(); } } /// Fires the event protected virtual void OnCanHighlightChanged() { if (CanHighlightChanged != null) { CanHighlightChanged(); } } /// Stores the currently selected objects private ObservableCollection selectedObjects; /// Stores the currently highlighted objects private ObservableCollection highlightedObjects; #if false /// Whether the actor is currently allowed to select objects private bool canSelect = true; /// Whether the actor is currently allowed to highlight objects private bool canHighlight = true; #endif } } // namespace Framework.Selection