13 years
edited 8 years
The documentation has a brief explanation as to why I made this.
Code
Demo
What do you think?
Code
/*************************************
*
* FlyHeight
* v1.0.0.1
* By Magtheridon96
*
* - Warcraft III movement types are pretty
* bugged. It would be much better to write
* them from scratch. This system allows
* you to create flying units with changeable
* heights.
*
* All units must have Movement Type Foot.
*
* Requires:
* ---------
*
* - UnitIndexer by Nestharus
* - https://wc3modding.info/4607/unitindexer/
*
* Optional:
* ---------
*
* - Table by Bribe
* - https://wc3modding.info/4611/snippet-new-table/
*
* API:
* ----
*
* - struct FlyHeight extends array
*
* - static method operator [] takes unit u returns thistype
* - Returns an instance of the struct given a unit
*
* - static method setDefault takes integer unitType, real flyHeight returns nothing
* - When a unit of a certain type enters the map, his height will be set
* to a default value input by the user using this function.
*
* - method operator height takes nothing returns real
* - Returns the height of a unit
*
* - method operator height= takes real newHeight returns nothing
* - Sets the height of a unit
*
*************************************/
library FlyHeight requires UnitIndexer, optional Table
struct FlyHeight extends array
private static real array h
static if LIBRARY_Table then
private static key defaultK
private static Table default = defaultK
else
private static hashtable default = InitHashtable()
endif
method operator height takes nothing returns real
return h[this]
endmethod
static method setDefault takes integer unitType, real defaultHeight returns nothing
static if LIBRARY_Table then
set default.real[unitType] = defaultHeight
else
call SaveReal(default, unitType, 0, defaultHeight)
endif
endmethod
private static method haveDefault takes integer unitType returns boolean
static if LIBRARY_Table then
return default.real.has(unitType)
else
return HaveSavedReal(default, unitType, 0)
endif
endmethod
private static method index takes nothing returns nothing
local integer unitType = GetUnitTypeId(GetIndexedUnit())
if haveDefault(unitType) then
static if not LIBRARY_AutoFly then
if UnitAddAbility(GetIndexedUnit(), 'Amrf') then
call UnitRemoveAbility(GetIndexedUnit(), 'Amrf')
endif
endif
static if LIBRARY_Table then
set h[GetIndexedUnitId()] = default.real[unitType]
else
set h[GetIndexedUnitId()] = LoadReal(default, unitType, 0)
endif
call SetUnitFlyHeight(GetIndexedUnit(), h[GetIndexedUnitId()], 0)
endif
endmethod
implement UnitIndexStruct
method operator height= takes real newHeight returns nothing
if h[this] != newHeight then
set h[this] = newHeight
call SetUnitFlyHeight(this.unit, newHeight, 0)
endif
endmethod
endstruct
endlibraryDemo
struct Demo extends array
private static method onInit takes nothing returns nothing
local unit peasant
local unit hawk
call FlyHeight.setDefault('hpea', 200)
call FlyHeight.setDefault('hdhw', 150)
set peasant = CreateUnit(Player(0), 'hpea', -256, 0, 270)
set hawk = CreateUnit(Player(0), 'hdhw', 256, 0, 270)
call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, R2SW(FlyHeight[peasant].height, 5, 1))
call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, R2SW(FlyHeight[hawk].height, 5, 1))
call TriggerSleepAction(5)
set FlyHeight[peasant].height = 900
set FlyHeight[hawk].height = 2
call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, R2SW(FlyHeight[peasant].height, 5, 1))
call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, R2SW(FlyHeight[hawk].height, 5, 1))
set peasant = null
set hawk = null
endmethod
endstructWhat do you think?