| 01-29-2009, 06:34 PM | #1 |
This is my jumping system, i'm not submitting it because i'm afraid it's not going to be worthy for the database, and probably isn't. Anyways code improvements! JASS:llibrary Jump globals private real smoothness = 0.02 // How smooth the system will run, 0.03 should be fine. private integer crow = 'Amrf' // The crow form ability, generally not changed. private string animation_end = "stand" // The animation to be played once the jump is over private string start_order = "stop" // The order the unit is given when spell is casted endglobals private struct dat unit u real z real s real x real y real startx real starty boolean b static method create takes unit u, real s, real x, real y returns dat local dat d = dat.allocate() set d.u = u set d.s = s set d.x = x set d.y = y set d.b = false set d.startx = GetUnitX(u) set d.starty = GetUnitY(u) return d endmethod endstruct globals private timer t = CreateTimer() private integer instances = 0 endglobals private constant function Parabola takes real d, real h, real x returns real return ( 4 * h / d ) * ( d - x ) * ( x / d ) endfunction private function mapbounds takes real x, real y returns boolean return x < GetRectMaxX(bj_mapInitialPlayableArea)-64.00 and x > GetRectMinX(bj_mapInitialPlayableArea)+64.00 and y < GetRectMaxY(bj_mapInitialPlayableArea)-64.00 and y > GetRectMinY(bj_mapInitialPlayableArea)+64.00 endfunction private function move takes nothing returns nothing local real x local real y local real x1 local real y1 local real dx local real dy local real d2 local real dx2 local real dy2 local real p local real v local integer i = 1 local dat d = 1 loop exitwhen i > instances if d.u != null then set dx = d.x - GetUnitX(d.u) set dy = d.y - GetUnitY(d.u) set d2 = SquareRoot(dx * dx + dy * dy) set x = GetUnitX(d.u) + d.s * dx / d2 set y = GetUnitY(d.u) + d.s * dy / d2 call SetUnitX(d.u, x) call SetUnitY(d.u, y) set dx2 = x - d.x set dy2 = y - d.y set p = SquareRoot(dx2 * dx2 + dy2 * dy2) if p <= 20 then call PauseUnit(d.u, false) call SetUnitFlyHeight(d.u, 0, 1000) call UnitRemoveAbility(d.u, crow) call SetUnitAnimation(d.u, animation_end) set d.u = null if i == instances then set instances = instances - 1 if instances <= 0 then call PauseTimer(t) endif endif call d.destroy() set i = i - 1 endif set dx2 = d.startx - d.x set dy2 = d.starty - d.y set v = SquareRoot(dx2 * dx2 + dy2 * dy2) call SetUnitFlyHeight(d.u, Parabola( v, v * 0.4, p ), 0) endif set i = i + 1 set d = i endloop endfunction function UnitJump takes unit u, real speed, real x, real y returns nothing local dat d if mapbounds(x,y) then set d = dat.create(u,speed,x,y) set instances = instances + 1 call IssueImmediateOrder(u, start_order) call UnitAddAbility(u, crow) call SetUnitFlyHeight(u, 1000000, speed*50) call PauseUnit(u, true) if instances == 1 then call TimerStart(t, smoothness, true, function move) endif endif endfunction function UnitJumpLoc takes unit u, real speed, location l returns nothing call UnitJump(u, speed, GetLocationX(l), GetLocationY(l)) endfunction endlibrary download |
| 01-29-2009, 10:02 PM | #2 |
Your math is very confusing, I'm sure there are better ways to do the unit movement. You don't account for varying terrain heights either at the start and end of the jump or inbetween. The way you loop through the structs is kinda ugly (not to mention oyu're doing it wrong so instances can increase more and more as the game goes on), you should maintain an array of active structs instead. things like the height of the jump aren't calibratable when they should be. |
| 01-29-2009, 10:08 PM | #3 | |
Sorry updated my code to version 1.3, i'll work on those suggestions. Also any reason why adding exitwhen i > instances and d.active == true to the loop makes it lag incredibly? EDIT: Quote:
Doesn't matter, if d.u == null does the same thing as d.active. |
| 01-29-2009, 10:46 PM | #4 |
I already posted the improvements on the thread at clan mapz >> |
