Superfighters Deluxe
ElevatorHandlerHelper Class Reference

Helper class to handle a single elevator instance with objects named with elevator prefix and floor suffix in the following pattern:
{elePrefix}_BtnUp - Button to make the elevator go up one floor.
{elePrefix}_BtnDown - Button to make the elevator go down one floor.
{elePrefix}_BtnFloor_{floorLetter} - Button to make the elevator go to the destinated PathJoint with the same floorLetter.
{elePrefix}_PathJoint_{floorLetter} - PathJoint per floor.
{elePrefix}_EleJoint - Elevator attachment joint.
{elePrefix}_PathCon_{floorLetter} - PathNodeConnection per floor.

More...

Public Member Functions

 ElevatorHandlerHelper (IGame game, string elevatorPrefix, string[] elevatorFloorLetters, string startingFloorLetter)
 Helper class to handle a single elevator instance with objects named with elevator prefix and floor suffix in the following pattern:

More...
 
void ButtonPressed (string btnPressedID)
 Handle elevator button pressed More...
 
void ElevatorArrived (string floorPathJointID)
 Will enable and disable correct buttons based on which floor the elevator arrived at More...
 

Static Public Member Functions

static void HandleElevatorArrived (TriggerArgs args)
 Helper to handle multiple elevator instances when an elevator has arrived at its destination. More...
 
static void HandleElevatorButtonPress (TriggerArgs args)
 Helper to handle multiple elevator instances when you press a button connected to an elevator. More...
 
static ElevatorHandlerHelper Register (IGame game, string elevatorPrefix, string[] elevatorFloorLetters, string startingFloorLetter)
 Helper to handle multiple elevator instances with objects named with elevator prefix and floor suffix in the following pattern:

More...
 
static void Unregister (ElevatorHandlerHelper elevatorHandlerHelper)
 Unregisteres a registered elevator. More...
 

Detailed Description

Helper class to handle a single elevator instance with objects named with elevator prefix and floor suffix in the following pattern:
{elePrefix}_BtnUp - Button to make the elevator go up one floor.
{elePrefix}_BtnDown - Button to make the elevator go down one floor.
{elePrefix}_BtnFloor_{floorLetter} - Button to make the elevator go to the destinated PathJoint with the same floorLetter.
{elePrefix}_PathJoint_{floorLetter} - PathJoint per floor.
{elePrefix}_EleJoint - Elevator attachment joint.
{elePrefix}_PathCon_{floorLetter} - PathNodeConnection per floor.

This helper class was based on the following SFD Script code and can be modified if you like for your own script:
See official maps for examples how to use this helper class.

*/
/*
 public class ElevatorHandlerHelper
 {
    private class ElevatorFloor
    {
        public IObjectTrigger[] FloorButtons = null;
        public IObjectElevatorPathJoint ElevatorPathJoint = null;
        public string FloorLetter = "";
        public IObjectPathNodeConnection[] PathNodeConnections = null;
        public int FloorIndex;
    }

    private IGame Game;
    private IObjectElevatorAttachmentJoint ElevatorAttachmentJoint = null;
    private IObjectTrigger ElevatorButtonUp = null;
    private IObjectTrigger ElevatorButtonDown = null;
    private string ElevatorCurrentFloorLetter = "";
    private float ElevatorMotorSpeed = 0f;

    // Index 0 is bottom floor, 1 is second floor and so on...
    private List<ElevatorFloor> ElevatorFloors = null;

    /// <param name="elevatorPrefix">Prefix for the elevator</param>
    /// <param name="elevatorFloorLetters">Floors starting from bottom to top in order</param>
    /// <param name="startingFloorLetter">Starting floor the elevator attachment joint spawns at</param>
    public ElevatorHandlerHelper(IGame game, string elevatorPrefix, string[] elevatorFloorLetters, string startingFloorLetter)
    {

        // prepare data
        this.Game = game;
        this.ElevatorFloors = new List<ElevatorFloor>();

        // fetch objects
        if (elevatorFloorLetters != null)
        {
            foreach (string floorLetter in elevatorFloorLetters)
            {
                ElevatorFloor floor = new ElevatorFloor();
                floor.FloorLetter = floorLetter;
                floor.FloorButtons = Game.GetObjectsByCustomID<IObjectTrigger>(string.Format("{0}_BtnFloor_{1}", elevatorPrefix, floorLetter));
                floor.ElevatorPathJoint = Game.GetSingleObjectByCustomID<IObjectElevatorPathJoint>(string.Format("{0}_PathJoint_{1}", elevatorPrefix, floorLetter));
                floor.PathNodeConnections = Game.GetObjectsByCustomID<IObjectPathNodeConnection>(string.Format("{0}_PathCon_{1}", elevatorPrefix, floorLetter));
                floor.FloorIndex = this.ElevatorFloors.Count;
                this.ElevatorFloors.Add(floor);
            }
        }

        // should only be one elevatorAttachmentJoint
        IObject[] elevatorAttachmentJoint = Game.GetObjectsByCustomID(string.Format("{0}_EleJoint", elevatorPrefix));
        if ((elevatorAttachmentJoint.Length == 1) && (elevatorAttachmentJoint[0] is IObjectElevatorAttachmentJoint))
        {
            this.ElevatorAttachmentJoint = (IObjectElevatorAttachmentJoint)elevatorAttachmentJoint[0];
            this.ElevatorMotorSpeed = ElevatorAttachmentJoint.GetMotorSpeed(); // store motorspeed set in the editor
        }
        else
        {
            Game.WriteToConsole(string.Format("Can't find ElevatorAttachmentJoint '{0}_EleJoint'", elevatorPrefix));
        }

        //{elePrefix}_BtnUp
        //{elePrefix}_BtnDown
        this.ElevatorButtonUp = Game.GetSingleObjectByCustomID<IObjectTrigger>(string.Format("{0}_BtnUp", elevatorPrefix));
        this.ElevatorButtonDown = Game.GetSingleObjectByCustomID<IObjectTrigger>(string.Format("{0}_BtnDown", elevatorPrefix));

        // start with negative speed to make elevator reset itself to its starting position
        if ((this.ElevatorAttachmentJoint != null) && (this.ElevatorAttachmentJoint.GetMotorSpeed() > 0f))
        {
            this.ElevatorAttachmentJoint.SetMotorSpeed(-this.ElevatorAttachmentJoint.GetMotorSpeed());
        }

        Game.WriteToConsole("Elevator Initialized");
    }

    public void ButtonPressed(string btnPressedID)
    {
        if (this.ElevatorAttachmentJoint != null)
        {
            ElevatorFloor destinationElevatorFloor = GetElevatorDestinationFloorByButtonID(btnPressedID);
            if (destinationElevatorFloor != null)
            {
                // Check that the button pressed belong to another floor. In that case move the elevator to that floor and disable buttons.
                if (this.ElevatorCurrentFloorLetter != destinationElevatorFloor.FloorLetter)
                {
                    if (destinationElevatorFloor.ElevatorPathJoint != null) // failsafe
                    {
                        ElevatorFloor currentElevatorFloor = GetElevatorFloorByFloorLetter(this.ElevatorCurrentFloorLetter);
                        if (currentElevatorFloor != null) // failsafe
                        {
                            if ((currentElevatorFloor.ElevatorPathJoint != null) && (destinationElevatorFloor.ElevatorPathJoint != null)) // failsafe
                            {
                                // Clear destination's next PathJoint so the elevator will stop there and not automatically continue it's path
                                for (int i = 0; i < ElevatorFloors.Count; i++)
                                {
                                    if (ElevatorFloors[i].ElevatorPathJoint != null)
                                    {
                                        ElevatorFloors[i].ElevatorPathJoint.SetNextPathJoint(null);
                                    }
                                }
                                // Set up path-joints connection
                                currentElevatorFloor.ElevatorPathJoint.SetNextPathJoint(destinationElevatorFloor.ElevatorPathJoint);
                                // Re-initialize elevatorAttachmentJoint
                                this.ElevatorAttachmentJoint.SetElevatorPathJoint(currentElevatorFloor.ElevatorPathJoint);
                                this.ElevatorAttachmentJoint.SetMotorSpeed(this.ElevatorMotorSpeed);
                                // Disable buttons
                                ButtonsSetEnabled(false);
                                // Reconfigure PathConnections
                                PathNodeConnectionEnableForFloor(destinationElevatorFloor);
                            }
                        }
                    }
                }
            }
        }
    }

    public void ElevatorArrived(string floorPathJointID)
    {
        if (this.ElevatorAttachmentJoint != null)
        {
            ElevatorFloor elevatorFloor = GetElevatorFloorByPathJointID(floorPathJointID);
            if (elevatorFloor != null)
            {
                this.ElevatorCurrentFloorLetter = elevatorFloor.FloorLetter;
                // Play sound
                Game.PlaySound("ElevatorDing", ElevatorAttachmentJoint.GetWorldPosition(), 1f);
                // Enable buttons
                ButtonsSetEnabled(true);
                // Reconfigure PathConnections
                PathNodeConnectionEnableForFloor(elevatorFloor);
            }
            else
            {
                Game.WriteToConsole(string.Format("ElevatorArrived ({0}) - PathJointNotFound", floorPathJointID));
            }
        }
    }

    private void ButtonsSetEnabled(bool value)
    {
        // Enable buttons
        for (int i = 0; i < this.ElevatorFloors.Count; i++)
        {
            ElevatorFloor floor = this.ElevatorFloors[i];
            if (floor.FloorButtons != null)
            {
                for (int j = 0; j < floor.FloorButtons.Length; j++)
                {
                    floor.FloorButtons[j].SetEnabled(value);
                }
            }
        }
        if (this.ElevatorButtonUp != null)
        {
            this.ElevatorButtonUp.SetEnabled(value);
        }
        if (this.ElevatorButtonDown != null)
        {
            this.ElevatorButtonDown.SetEnabled(value);
        }
    }

    // Sets current PathNodeConnections to be enabled to the specified floor while disabling all others.
    private void PathNodeConnectionEnableForFloor(ElevatorFloor activeFloor)
    {
        if (activeFloor == null)
        {
            return;
        }
        for (int i = 0; i < this.ElevatorFloors.Count; i++)
        {
            ElevatorFloor floor = this.ElevatorFloors[i];
            bool enabled = (activeFloor.FloorIndex == floor.FloorIndex);
            if (floor.PathNodeConnections != null)
            {
                // Up/Down buttons always valid activators (if it's the right direction!)
                // Each floor's call buttons are also valid activators.
                // Destination floor has connection enabled!
                List<IObjectActivateTrigger> enableWithActivators = new List<IObjectActivateTrigger>();
                if (floor.FloorButtons != null)
                {
                    foreach (IObjectTrigger trigger in floor.FloorButtons)
                    {
                        if (trigger is IObjectActivateTrigger)
                        {
                            enableWithActivators.Add((IObjectActivateTrigger)trigger);
                        }
                    }
                }
                if (floor.FloorIndex < activeFloor.FloorIndex && this.ElevatorButtonDown != null && this.ElevatorButtonDown is IObjectActivateTrigger)
                {
                    enableWithActivators.Add((IObjectActivateTrigger)this.ElevatorButtonDown);
                }
                else if (floor.FloorIndex > activeFloor.FloorIndex && this.ElevatorButtonUp != null && this.ElevatorButtonUp is IObjectActivateTrigger)
                {
                    enableWithActivators.Add((IObjectActivateTrigger)this.ElevatorButtonUp);
                }
                for (int j = 0; j < floor.PathNodeConnections.Length; j++)
                {
                    floor.PathNodeConnections[j].SetConnectionEnabled(enabled);
                    floor.PathNodeConnections[j].SetEnableWithActivatorObjects(enableWithActivators);
                }
            }
        }
    }

    // Helper method to find ElevatorFloor by PathJoinCustomID
    private ElevatorFloor GetElevatorFloorByPathJointID(string floorPathJointID)
    {
        for (int i = 0; i < this.ElevatorFloors.Count; i++)
        {
            ElevatorFloor floor = this.ElevatorFloors[i];
            if ((floor.ElevatorPathJoint != null) && (floor.ElevatorPathJoint.CustomID == floorPathJointID))
            {
                return floor;
            }
        }
        return null;
    }

    // Helper method to find destination ElevatorFloor based on button pressed.
    private ElevatorFloor GetElevatorDestinationFloorByButtonID(string buttonID)
    {
        for (int i = 0; i < this.ElevatorFloors.Count; i++)
        {
            ElevatorFloor floor = this.ElevatorFloors[i];
            if (floor.FloorButtons != null)
            {
                for (int j = 0; j < floor.FloorButtons.Length; j++)
                {
                    if (floor.FloorButtons[j].CustomID == buttonID)
                    {
                        return floor;
                    }
                }
            }
        }
        // Check if buttonUp or buttonDown has been pressed aswell and find out the next ElevatorFloor based on that
        if ((this.ElevatorButtonDown != null) && (this.ElevatorButtonDown.CustomID == buttonID))
        {
            // Floors starting from bottom to top in order
            for (int i = 1; i < this.ElevatorFloors.Count; i++)
            {
                if (this.ElevatorFloors[i].FloorLetter == this.ElevatorCurrentFloorLetter)
                {
                    return this.ElevatorFloors[i - 1];
                }
            }
        }
        if ((this.ElevatorButtonUp != null) && (this.ElevatorButtonUp.CustomID == buttonID))
        {
            // Floors starting from bottom to top in order
            for (int i = 0; i < this.ElevatorFloors.Count - 1; i++)
            {
                if (this.ElevatorFloors[i].FloorLetter == this.ElevatorCurrentFloorLetter)
                {
                    return this.ElevatorFloors[i + 1];
                }
            }
        }

        return null;
    }

    // Helper method to find ElevatorFloor based on floor letter
    private ElevatorFloor GetElevatorFloorByFloorLetter(string floorLetter)
    {
        for (int i = 0; i < this.ElevatorFloors.Count; i++)
        {
            if (this.ElevatorFloors[i].FloorLetter == floorLetter)
            {
                return this.ElevatorFloors[i];
            }
        }
        return null;
    }

    private static List<ElevatorHandlerHelper> m_elevatorHandlerHelpers = null;
    
    // <param name="elevatorPrefix">Prefix for the elevator</param>
    // <param name="elevatorFloorLetters">Floors starting from bottom to top in order</param>
    // <param name="startingFloorLetter">Starting floor the elevator attachment joint spawns at</param>
    public static ElevatorHandlerHelper Register(IGame game, string elevatorPrefix, string[] elevatorFloorLetters, string startingFloorLetter)
    {
        if (m_elevatorHandlerHelpers == null)
        {
            m_elevatorHandlerHelpers = new List<ElevatorHandlerHelper>();
        }
        ElevatorHandlerHelper helper = new ElevatorHandlerHelper(game, elevatorPrefix, elevatorFloorLetters, startingFloorLetter);
        m_elevatorHandlerHelpers.Add(helper);
        return helper;
    }

    // Unregisteres a registered elevator.
    public static void Unregister(ElevatorHandlerHelper elevatorHandlerHelper)
    {
        if (m_elevatorHandlerHelpers != null)
        {
            m_elevatorHandlerHelpers.Remove(elevatorHandlerHelper);
            if (m_elevatorHandlerHelpers.Count == 0)
            {
                m_elevatorHandlerHelpers = null;
            }
        }
    }

    // Helper to handle multiple elevator instances when you press a button connected to an elevator.
    // <param name="args">TriggerArgs from the button press event</param>
    public static void HandleElevatorButtonPress(TriggerArgs args)
    {
        if (m_elevatorHandlerHelpers != null)
        {
            // Find out which button was pressed
            if ((args.Caller != null) && (args.Caller is IObject))
            {
                IObject btnPressed = (IObject)args.Caller;
                foreach (ElevatorHandlerHelper elevator in m_elevatorHandlerHelpers)
                {
                    elevator.ButtonPressed(btnPressed.CustomId);
                }
            }
        }
    }

    // Helper to handle multiple elevator instances when an elevator has arrived at its destination.
    // <param name="args">TriggerArgs from the event</param>
    public static void HandleElevatorArrived(TriggerArgs args)
    {
        if (m_elevatorHandlerHelpers != null)
        {
            // args.Caller is the elevatorAttachmentJoint, args.Sender is the elevatorPathJoint arrived at in this case
            if ((args.Sender != null) && (args.Sender is IObject))
            {
                IObject pathJoint = (IObject)args.Sender;
                foreach (ElevatorHandlerHelper elevator in m_elevatorHandlerHelpers)
                {
                    elevator.ElevatorArrived(pathJoint.CustomId);
                }
            }
        }
    }

}
*/
/**

Constructor & Destructor Documentation

◆ ElevatorHandlerHelper()

ElevatorHandlerHelper ( IGame  game,
string  elevatorPrefix,
string[]  elevatorFloorLetters,
string  startingFloorLetter 
)

Helper class to handle a single elevator instance with objects named with elevator prefix and floor suffix in the following pattern:

{elePrefix}_BtnUp - Button to make the elevator go up one floor.
{elePrefix}_BtnDown - Button to make the elevator go down one floor.
{elePrefix}_BtnFloor_{floorLetter} - Button to make the elevator go to the destinated PathJoint with the same floorLetter.
{elePrefix}_PathJoint_{floorLetter} - PathJoint per floor.
{elePrefix}_EleJoint - Elevator attachment joint.
{elePrefix}_PathCon_{floorLetter} - PathNodeConnection per floor.

Parameters
game
elevatorPrefixPrefix for the elevator
elevatorFloorLettersFloors starting from bottom to top in order
startingFloorLetterStarting floor the elevator attachment joint spawns at

Member Function Documentation

◆ ButtonPressed()

void ButtonPressed ( string  btnPressedID)

Handle elevator button pressed

Parameters
btnPressedID

◆ ElevatorArrived()

void ElevatorArrived ( string  floorPathJointID)

Will enable and disable correct buttons based on which floor the elevator arrived at

Parameters
floorPathJointID

◆ HandleElevatorArrived()

static void HandleElevatorArrived ( TriggerArgs  args)
static

Helper to handle multiple elevator instances when an elevator has arrived at its destination.

Parameters
argsTriggerArgs from the event

◆ HandleElevatorButtonPress()

static void HandleElevatorButtonPress ( TriggerArgs  args)
static

Helper to handle multiple elevator instances when you press a button connected to an elevator.

Parameters
argsTriggerArgs from the button press event

◆ Register()

static ElevatorHandlerHelper Register ( IGame  game,
string  elevatorPrefix,
string[]  elevatorFloorLetters,
string  startingFloorLetter 
)
static

Helper to handle multiple elevator instances with objects named with elevator prefix and floor suffix in the following pattern:

{elePrefix}_BtnUp - Button to make the elevator go up one floor.
{elePrefix}_BtnDown - Button to make the elevator go down one floor.
{elePrefix}_BtnFloor_{floorLetter} - Button to make the elevator go to the destinated PathJoint with the same floorLetter.
{elePrefix}_PathJoint_{floorLetter} - PathJoint per floor.
{elePrefix}_EleJoint - Elevator attachment joint.

Parameters
elevatorPrefixPrefix for the elevator
elevatorFloorLettersFloors starting from bottom to top in order
startingFloorLetterStarting floor the elevator attachment joint spawns at

◆ Unregister()

static void Unregister ( ElevatorHandlerHelper  elevatorHandlerHelper)
static

Unregisteres a registered elevator.

Parameters
elevatorHandlerHelperElevatorHandlerHelper to unregister