Unfortunately, now I'm realizing that this script's main weakness is that it parents to the NPC, rather than something neutral like a map layer. If the NPC moves, the positioning of the projectile moves too, which would make no sense.
So I'm doing this instead:
Code: Select all
set parent (proj, lookup slice (sl:obsolete overhead))
set slice x (proj, npc pixel x(ref) + (slice width(proj) --20) / -2)
set slice y (proj, npc pixel y(ref) + (slice height(proj) --20) / -2)
It took me a damn long time to suss out the proper formula to do faux centering of a 20x20 pixel NPC. Math is not my strong point.
So here's (I hope) the final form of this script:
Code: Select all
script, shoot projectile, ref, type, speed=5, begin
variable (proj, move distance, xdir, ydir, dist)
proj := type
set parent (proj, lookup slice (sl:obsolete overhead))
set slice x (proj, npc pixel x(ref) + (slice width(proj) --20) / -2)
set slice y (proj, npc pixel y(ref) + (slice height(proj) --20) / -2)
move distance := 300
xdir := hero x (me) -- npc x (ref)
ydir := hero y (me) -- npc y (ref)
dist := sqrt(100 * xdir^2 + 100 * ydir^2)
# Scale the (xdir, ydir) vector to length movedistance
xdir := move distance * xdir * 10 / dist
ydir := move distance * ydir * 10 / dist
move slice by (proj, xdir, ydir, move distance / speed)
set slice extra (get npc slice(ref), extra 0, 100)
end