JASS:
function Trig_Sonic_Wave_Actions takes nothing returns nothing
local unit caster = GetTriggerUnit()
local location targetloc = GetSpellTargetLoc()
local real targetx = GetLocationX(targetloc)
local real targety = GetLocationY(targetloc)
local real casterx = GetUnitX(caster)
local real castery = GetUnitY(caster)
local real dx = targetx - casterx
local real dy = targety - castery
local real angle = Atan2(dy, dx)
local real distance = SquareRoot(dx * dx + dy * dy)
...
endfunction
I'm sure most JASS users have seen something like this before. A mess of code for calculating something simple, like an offset, just to avoid the use of locations. In an attempt to shorten and simplify that whole process...
JASS:
library CoordinatesType
struct coordinates
real sourcex
real sourcey
real destx
real desty
real dist
real angle
real x
real y
static method create takes real sourcex, real sourcey, real destx, real desty returns coordinates
local coordinates c = coordinates.allocate()
local real dx = destx - sourcex
local real dy = desty - sourcey
set c.sourcex = sourcex
set c.sourcey = sourcey
set c.destx = destx
set c.desty = desty
set c.dist = SquareRoot(dx * dx + dy * dy)
set c.angle = Atan2(dy, dx)
return c
endmethod
// This offsets the sourcex/y coordinates and stores the new ones in x/y.
method offset takes real offset, real angle returns nothing
set .x = .sourcex + offset * Cos(angle)
set .y = .sourcey + offset * Sin(angle)
endmethod
// This updates dist and angle.
method update takes nothing returns nothing
local real dx = .destx - .sourcex
local real dy = .desty - .sourcey
set .dist = SquareRoot(dx * dx + dy * dy)
set .angle = Atan2(dy, dx)
endmethod
// This function takes new source coordinates and basically resets the struct.
method updatewith takes real sourcex, real sourcey, real destx, real desty returns nothing
set .sourcex = sourcex
set .sourcey = sourcey
set .destx = destx
set .desty = desty
call .update()
endmethod
endstruct
endlibrary
The code is pretty straightforward; it's in essence a means to put all that untidy code away somewhere. Consider it a type like the location (or rather, a struct, in standard programming), with some extended syntax and without the leaks and stuff.
Requires:
- vJass
- JASS knowledge.
Implementation:
-Copy the code/trigger into a blank text trigger, or into some other utility library. Voila.
What is it for?
Handling coordinates. Simplifies everything. All that's required is a create() function call to take coordinates, and the struct members are assigned and do the rest.
It's not very user-friendly, admittedly, but then it's used pretty much like a normal struct:
JASS:
function Weird takes nothing returns nothing
local unit caster = GetTriggerUnit()
local location targetloc = GetSpellTargetLoc()
local coordinates c = coordinates.create(GetUnitX(caster), GetUnitY(caster), GetLocationX(targetloc), GetLocationY(targetloc))
// Moves "caster" 600 units forward in the direction of casting, a la Blink:
call c.offset(600., c.angle)
call SetUnitPosition(caster, c.x, c.y)
// Sets "caster"'s hitpoints to 20% of the (remaining) distance between it and the targeted point, a la Spear of Holy Calculations
set c.sourcex = GetUnitX(caster)
set c.sourcey = GetUnitY(caster)
call c.update()
call SetUnitState(caster, UNIT_STATE_LIFE, c.dist * .2)
// Like all structs, coordinates must be destroyed. They don't leak, but they needlessly take up struct instances.
call c.destroy()
call RemoveLocation(targetloc)
set targetloc = null
set caster = null
endfunction
A demo map in which this snippet is used can be found here, if it's needed; it's actually for my Wait System.
Comments and suggestions for improvement are appreciated.