Jump to content
Sign in to follow this  
ShadowDraconix

Automatic BarGate Open Script

Recommended Posts

I'm totally lost, I've been trying for several days to get my script to work to open and close the BarGates automatically. I based myself on the doorOpener script present in reforger, but impossible to find how to adapt it. I managed to reimplement the door detection, but impossible to get the player detection to work. I'll leave you my script here if you have a solution I'm interested. Thanks in advance
 

[EntityEditorProps(category: "GameScripted/Utility", description: "Opens door in certain radius.")]

class ATM_DoorOpenerEntityClass : GenericEntityClass

{

};



// doorComp.GetOwner() would solve the need for this

class ATMDoorStruct

{

    IEntity owner;

    DoorComponent component;



    void ATMDoorStruct(IEntity e, DoorComponent comp)

    {

        this.owner = e;

        this.component = comp;

    }

};



class ATM_DoorOpenerEntity : GenericEntity

{

    [Attribute(defvalue: "10", uiwidget: UIWidgets.Slider, desc: "Radius in which to open door.", "0 1000 1")]

    protected float m_fRadius;



    [Attribute(defvalue: "0", uiwidget: UIWidgets.Slider, desc: "Control value to manage door state.", "0 1 0.01")]

    protected float m_fControlValue;



    private ref array<ref ATMDoorStruct> m_aQueriedDoors;

    private ref array<IEntity> m_aQueriedCharacters;

   

    #ifdef WORKBENCH    

    private bool m_bVisualize;

    #endif

   

    // Check for characters in radius

    private bool QueryCharacters(IEntity unit)

    {

        if (unit)

        {

            m_aQueriedCharacters.Insert(unit);

        }



        return true;

    }



    // Query the area for characters

    private void GetCharacters(float radius)

    {

        BaseWorld world = GetWorld();

        world.QueryEntitiesBySphere(GetOrigin(), radius, QueryCharacters);

    }



    // Query the area for doors

    private bool QueryEntities(IEntity e)

    {

        DoorComponent door = DoorComponent.Cast(e.FindComponent(DoorComponent));

        if (door)

        {

            m_aQueriedDoors.Insert(new ATMDoorStruct(e, door));

        }



        return true;

    }



    private void GetDoors(float radius)

    {

        BaseWorld world = GetWorld();

        world.QueryEntitiesBySphere(GetOrigin(), radius, QueryEntities);

    }



    // Update the door control based on the presence of characters

    private void UpdateDoorControl()

    {

        m_aQueriedCharacters = {};

        GetCharacters(m_fRadius);



        if (m_aQueriedCharacters.Count() > 0)

        {

            m_fControlValue = 1.0;  // A character is in the radius

        }

        else

        {

            m_fControlValue = 0.0;  // No characters in the radius

        }



        // Update the doors accordingly

        foreach (ATMDoorStruct door : m_aQueriedDoors)

        {

            door.component.SetControlValue(m_fControlValue);

        }

    }



    // Initialization method

    override void EOnInit(IEntity owner)

    {

        // server only

        RplComponent rplComponent = RplComponent.Cast(owner.FindComponent(RplComponent));

        if (rplComponent && !rplComponent.IsMaster())

            return;



        // capture doors around

        m_aQueriedDoors = {};

        GetDoors(m_fRadius);

       

        // Start updating the door control based on the presence of characters

        SetEventMask(EntityEvent.FRAME);

    }



    // Called every frame

    override void EOnFrame(IEntity owner, float timeSlice)

    {

        UpdateDoorControl();  // Check and update the door control

    }



    // Constructor

    void SCR_DoorOpenerEntity(IEntitySource src, IEntity parent)

    {

        if (SCR_Global.IsEditMode(this))

            return;



        SetEventMask(EntityEvent.INIT | EntityEvent.FRAME);

    }



    void ~ATM_DoorOpenerEntity()

    {

    }



    #ifdef WORKBENCH



    private void _CaptureDoors()

    {

        m_aQueriedDoors = {};

        GetDoors(m_fRadius);

    }



    override bool _WB_OnKeyChanged(BaseContainer src, string key, BaseContainerList ownerContainers, IEntity parent)

    {

        _CaptureDoors();

        return super._WB_OnKeyChanged(src, key, ownerContainers, parent);

    }



    override void _WB_SetExtraVisualiser(EntityVisualizerType type, IEntitySource src)

    {

        m_bVisualize = false;

        switch (type)

        {

            case EntityVisualizerType.EVT_NONE:

                return;



            case EntityVisualizerType.EVT_NORMAL:

                return;

        }



        m_bVisualize = true;

        _CaptureDoors();

        super._WB_SetExtraVisualiser(type, src);

    }



    override void _WB_AfterWorldUpdate(float timeSlice)

    {

        if (m_bVisualize)

        {

            auto origin = GetOrigin();

            auto radiusShape = Shape.CreateSphere(COLOR_YELLOW, ShapeFlags.WIREFRAME | ShapeFlags.ONCE, origin, m_fRadius);



            foreach (ATMDoorStruct door : m_aQueriedDoors)

            {

                auto arrowShape = Shape.CreateArrow(origin, door.owner.GetOrigin(), 0.1, COLOR_GREEN, ShapeFlags.ONCE);

            }

        }



        super._WB_AfterWorldUpdate(timeSlice);

    }



    #endif

};

 

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×