Dr.Jack
That's Cap'n to you!
- Reaction score
- 109
Configuerable Sliding System - CSS
Well I needed a sliding system. Simple one. Up to date one. Comfortable one. Flexible enough to fit almost all my needs. And so SSS was born. Originally made for personal use. And then I noticed we don't have any up to data sliding systems here on TH.net. So with a small "what the heck" I posted it. After that with slight improvement I posted ASS (advanced sliding system) that basically did the same thing but used one timer instead of one timer per instance.
Well, SSS and ASS were really basic. Purely made to save some typing time. And I didn't like that. I wanted to create a system that could do more than that but still save its simplicity, lightness and user-friendlieness. So CSS was created instead of S/ASS.
It works similarly to ASS. CSS as hinted in its name is more configuerable. There are more function, more options and it is more optimized.
So what is CSS?
How does it work? Quite simply. There is one timer and one group creates. The timer is executed every so much time. Every unit that slides is added to the group that is looped through every time execution. The code calculates where the unit should go to and moves it to that location. That's it.
Enjoy. I would love to hear comments. :thup:
Well I needed a sliding system. Simple one. Up to date one. Comfortable one. Flexible enough to fit almost all my needs. And so SSS was born. Originally made for personal use. And then I noticed we don't have any up to data sliding systems here on TH.net. So with a small "what the heck" I posted it. After that with slight improvement I posted ASS (advanced sliding system) that basically did the same thing but used one timer instead of one timer per instance.
Well, SSS and ASS were really basic. Purely made to save some typing time. And I didn't like that. I wanted to create a system that could do more than that but still save its simplicity, lightness and user-friendlieness. So CSS was created instead of S/ASS.
It works similarly to ASS. CSS as hinted in its name is more configuerable. There are more function, more options and it is more optimized.
So what is CSS?
- A simple sliding system able to slide a unit to a certain point - calculated by distance and angle, by coordinates or by unit's facing (updated).
- Speed is configurable.
- One timer controls the whole system.
- A very light, simple, user-friendly system.
- Requires NewGen version 1.5 or better.
- Can be used easily by GUI users.
- MUI of course, if there was any doubt.
- Any slide can be stopped at any moment.
- Slide's parameter can be changed while its execution.
- Can execute a trigger at the end of the slide. (Added by request)
- Can check terminate slides if unit reach unpathable location.
- Includes documentation and comments.
- Uses PUI. Thanks Cohadar!
- Uses Events. Thanks Jesus4Lyf
JASS:
library CSS initializer init needs PUI, Event
globals
private real PERIOD = 0.025 // How often the timer runs
private boolean PATHCHECK = true // False if you don't want to check pathability
private pathingtype PATH = PATHING_TYPE_WALKABILITY // What kind of pathability to check?
// DO NOT MESS WITH
private timer CORE
private group LIST // *Shrug* Groups are comfortable.
endglobals
struct CSS
real ang
real tck
real ts
real x
real y
real cX
real cY
Event e
unit u
boolean fac = false // Facing based?
endstruct
private function coreFunc takes nothing returns nothing
local CSS data
local real x
local real y
local real tx
local real ty
local real tck
local real fac
local unit u
local group l = CreateGroup()
call GroupAddGroup(LIST, l) // *GASP* a BJ
loop
set u = FirstOfGroup(l)
exitwhen u == null
set data = GetUnitIndex(u)
set tck = data.tck
if data.fac then // Need to update angle.
set fac = GetUnitFacing(u)
if (data.ang != fac) then
set data.cX = data.ts * Cos(fac * bj_DEGTORAD)
set data.cY = data.ts * Sin(fac * bj_DEGTORAD)
endif
endif
set tx = data.x + data.cX
set ty = data.y + data.cY
if PATHCHECK and IsTerrainPathable(tx,ty, PATH) then
set tck = 0
else
call SetUnitPosition(u,tx,ty)
endif
set tck = tck -1
if tck <= 0 then // Unit reached its destinatin.
call GroupRemoveUnit(LIST, u)
set data.fac = false
call data.destroy()
call data.e.fire()
call data.e.chainDestroy()
endif
set data.x = tx
set data.y = ty
set data.tck = tck
call GroupRemoveUnit(l, u)
endloop
// No leaks KTHXBYE.
call DestroyGroup(l)
set l = null
set u = null
endfunction
// Start a slide based on distance & angle.
public function Start takes real dis, real ang, real spd, unit unt returns integer
local CSS data = GetUnitIndex(unt) // Structs are integer so we can safely use
// PUI to create the struct.
set data.ang = ang // Ang is just for easier calculations, not really needed.
set data.ts = spd * PERIOD // Distance the unit will move every timer run
set data.tck = (dis/spd)/PERIOD // How many timer runs before the process ends.
set data.x = GetUnitX(unt)
set data.y = GetUnitY(unt)
set data.cX = data.ts * Cos(ang * bj_DEGTORAD) // Shortens calculations
set data.cY = data.ts * Sin(ang * bj_DEGTORAD)
set data.u = unt
call GroupAddUnit(LIST, unt)
return GetUnitIndex(unt)
endfunction
// Start a slide based on unit's facing.
public function FacingStart takes real dis, real spd, unit unt returns integer
local CSS data = GetUnitIndex(unt)
set data.fac = true
call Start(dis, GetUnitFacing(unt), spd, unt)
return GetUnitIndex(unt)
endfunction
// Start a slide by giving coords.
public function CoordsStart takes real x, real y, real spd, unit unt returns integer
local real ux = GetUnitX(unt)
local real uy = GetUnitY(unt)
local real ang = bj_RADTODEG * Atan2(y - uy, x - ux)
local real dis
set dis = SquareRoot((ux - x) * (ux - x) + (uy - y) * (uy-y))
call Start(dis, ang, spd, unt)
return GetUnitIndex(unt)
endfunction
// Terminate current slide
public function Stop takes integer i returns nothing
local CSS data = i
set data.fac = false
call GroupRemoveUnit(LIST, data.u)
call data.destroy()
endfunction
// Modify Speed
public function ModSpeed takes integer i, real spd returns nothing
local CSS data = i
set data.ts = spd * PERIOD
set data.cX = data.ts * Cos(data.ang * bj_DEGTORAD)
set data.cY = data.ts * Sin(data.ang * bj_DEGTORAD)
endfunction
// Disables unit facing. Modifies sliding angle.
public function ModAngle takes integer i, real ang returns nothing
local CSS data = i
set data.ang = ang
set data.cX = data.ts * Cos(data.ang * bj_DEGTORAD)
set data.cY = data.ts * Sin(data.ang * bj_DEGTORAD)
set data.fac = false
endfunction
// Modify the distance (new one from current position)
public function ModDistance takes integer i, real dis returns nothing
local CSS data = i
local real spd = data.ts / PERIOD
set data.tck = (dis/spd)/PERIOD
endfunction
// Executes and trigger at the end of the slide.
public function SetEvent takes integer i, trigger t returns nothing
local Event e = Event.create()
local CSS data = i
set data.e = e
call e.register(t)
endfunction
// In case you don't want to deal with PUI...
public function U2I takes unit u returns integer
return GetUnitIndex(u)
endfunction
// Create timer and group.
private function init takes nothing returns nothing
set CORE = CreateTimer()
set LIST = CreateGroup()
call TimerStart(CORE, PERIOD, true, function coreFunc)
endfunction
endlibrary
How does it work? Quite simply. There is one timer and one group creates. The timer is executed every so much time. Every unit that slides is added to the group that is looped through every time execution. The code calculates where the unit should go to and moves it to that location. That's it.
Enjoy. I would love to hear comments. :thup: