• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[vJASS] ToggleAbility

JASS:
library ToggleAbility/*
*************************************************************************************
*
*   Simple extension for ChargedAbility to provide Toggleable abilities
*
*   States start at 0
*
*************************************************************************************
*
*   */ requires /*
*
*       */ ChargedAbility /* hiveworkshop.com/forums/submissions-414/chargedability-276136/
*
*************************************************************************************
*
*   API
*
*       static method create takes integer base returns thistype
*           - Creates an instance where base is the starting ability
*
*       method pushState takes integer abil returns nothing
*           - Pushes toggle state
*
*       method popState takes nothing returns nothing
*           - Remove toggle state(recently added)
*
*       method getState takes unit u returns integer
*           - Returns the current toggle state the unit has
*
*       method add takes unit u, integer level returns nothing
*           - Add ToggleAbility to unit 
*
*       method toggle takes unit u, integer level returns nothing
*           - Toggles ability on unit(sets next state)
*
*       method reset takes unit u, integer level returns nothing
*           - Reset the toggle (returns to base ability)
*
*       method setLevel takes unit u, integer level returns nothing
*           - Set ToggleAbility level to unit 
*
*************************************************************************************
*/
    struct ToggleAbility
        private ChargedAbility a
        private integer b
        
        static method create takes integer base returns thistype
            local thistype this = allocate()
            set a = ChargedAbility.create(base)
            call a.pushState(base)
            set b = base
            return this
        endmethod
        
        method popState takes nothing returns nothing
            call a.popState()
            call a.popState()
            call a.pushState(b)
        endmethod
        
        method pushState takes integer abil returns nothing
            call a.popState()
            call a.pushState(abil)
            call a.pushState(b)
        endmethod
        
        method getState takes unit u returns integer
            return a.getCharge(u)
        endmethod
        
        method add takes unit u, integer level returns nothing
            call a.add(u, 0, level)
        endmethod
        
        method reset takes unit u, integer level returns nothing
            call a.emptyCharges(u, level)
        endmethod
        
        method setLevel takes unit u, integer level returns nothing
            call a.setChargeLevel(u, level)
        endmethod
        
        method toggle takes unit u, integer level returns nothing
            call a.incCharge(u, level)
        endmethod
    endstruct
endlibrary
Example:
JASS:
struct Tester extends array

    static ToggleAbility states
    
    private static method onCast takes nothing returns boolean
        local unit u = GetTriggerUnit()
        local integer state
        /*
        *   Toggle current state
        */
        call states.toggle(u, 1)
        set state = states.getState(u)
        call BJDebugMsg("Current state : " + I2S(state))
        if state == 0 then
            call FogEnable(true)
        elseif state == 1 then
            call FogEnable(false)
        elseif state == 2 then
            call BJDebugMsg("There is no maybe")
        endif
        set u = null
        return false
    endmethod
    
    private static method onInit takes nothing returns nothing
        local trigger t = CreateTrigger()
        /*
        *   Create an instance called "states" with 'sta1' as starting toggle state
        */
        set states = ToggleAbility.create('sta1')
        /*
        *   let 'sta2' represent the next state
        */
        call states.pushState('sta2')
        /*
        *   let 'sta3' represent the next and final state
        */
        call states.pushState('sta3')
         /*
        *   add ability to unit.
        */
        call states.add(gg_unit_Hpal_0001,  1)
        call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_EFFECT )
        call TriggerAddCondition(t, Condition(function Tester.onCast))
        call FogMaskEnable(false)
        call FogEnable(false)
    endmethod
endstruct
 

Attachments

  • ToggleAbility.w3x
    34 KB · Views: 49
Last edited:

Bannar

Code Reviewer
Level 26
Joined
Mar 19, 2008
Messages
3,140
It doesn't, really. However when one resource is a wrapper (not a stand alone snippet) and is highly dependant on another library (especially on functionality level), I don't see why they shouldn't be merged into same thread - it's easier for user to find possible implementations for header library.

Just imagen RegisterNativeEvent being split into 5 separate threads or Dialog being a separate class from Button.
On the other hand, Vector<T> even though a wrapper for Table, the functionality it provides is distinct from core resource (also the authors nicks don't match, hehe).
 
Top