- Joined
- Sep 6, 2013
- Messages
- 6,742
Description is in code.
Code
Code
JASS:
library GetUnitScale /* v1.3
Provides functionality to get a unit's current and default scale.
The system expects the default scale value to be exactly 1.00.
If you modified the value in the object editor for a specific unit,
you have to adapt it to the system. (see module DefaultScale)
*/ requires /*
*/ UnitDex /* hiveworkshop.com/threads/system-unitdex-unit-indexer.248209/
*/ Table /* http://www.hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/
** API
**
**
** function GetUnitScale (unit) returns real
** Returns the current scale of the unit.
**
** function GetUnitDefaultScale (unit) returns real
** Returns the default scale of the unit.
***************************************************************************
**
** Credits to guys named in brackets.
**
** Requires:
** - Table (Bribe)
** - UnitDex (TriggerHappy)
**
**************************************************************************/
// Here you can define the correct default scale, it the value is not "1" in object editor.
private module DefaultScale
private static method onInit takes nothing returns nothing
// Example: call apply('hfoo', 1.60) // -> Register footman with default scale 1.60.
call RegisterUnitIndexEvent(Condition(function thistype.onIndex), EVENT_UNIT_INDEX)
endmethod
endmodule
private struct UnitScale extends array
private static key k
readonly static Table table = k
readonly real defaultScale
readonly static real array currentScale
private static thistype this = 0
static method apply takes integer uType, real scale returns nothing
if not thistype.table.has(uType) then
set this = this + 1
set thistype.table[uType] = this
endif
set this.defaultScale = scale
endmethod
implement DefaultScale
static method operator [] takes integer uType returns thistype
return thistype.table[uType]
endmethod
// onIndex we initalisize the scale value.
private static method onIndex takes nothing returns boolean
local integer id = GetIndexedUnitId()
local integer uType = GetUnitTypeId(GetIndexedUnit())
// Check if default scale is registered for this unit type.
if not thistype.table.has(uType) then
set thistype.currentScale[id] = 1.
else
set thistype.currentScale[id] = thistype[uType].defaultScale
endif
return false
endmethod
// hook methods
static method DataUpdate takes unit u, real x, real y, real z returns nothing
set thistype.currentScale[GetUnitUserData(u)] = x
endmethod
static method DataUpdatePercent takes unit u, real x, real y, real z returns nothing
set thistype.currentScale[GetUnitUserData(u)] = x*0.01
endmethod
endstruct
hook SetUnitScale UnitScale.DataUpdate
hook SetUnitScalePercent UnitScale.DataUpdatePercent
// API functions
function GetUnitScale takes unit u returns real
return UnitScale.currentScale[GetUnitUserData(u)]
endfunction
function GetUnitDefaultScale takes unit u returns real
if UnitScale.table.has(GetUnitTypeId(u)) then
return UnitScale[GetUnitTypeId(u)].defaultScale
endif
return 1.
endfunction
endlibrary
Last edited: