library Wolf
globals
planet array PLANET
comet array COMET
integer PCOUNT = 0
integer CCOUNT = 0
timer PTIMER = CreateTimer()
timer CTIMER = CreateTimer()
string array PNAME
string array CNAME
constant integer MAX_FOLLOW = 5 // change this to change the maximum number of instances following the commet
endglobals
struct planet
unit self
unit father
real distance
real rotationspeed
real circlingspeed
real angle
string name
endstruct
struct comet
unit self
unit array follow[MAX_FOLLOW]
integer countfollow
real speed
real angle
string name
endstruct
function CometDestroy takes comet c returns nothing
local integer i = 0
loop
exitwhen i > c.countfollow
call RemoveUnit(c.follow[i])
set i = i + 1
endloop
call RemoveUnit(c.self)
call c.destroy()
endfunction
function OutofMap takes real x, real y returns boolean
if x < GetCameraBoundMinX() or x > GetCameraBoundMaxX() or y < GetCameraBoundMinY() or y > GetCameraBoundMaxY() then
return true
endif
return false
endfunction
function MoveComets takes nothing returns nothing
local comet c
local integer i = 0
local integer int = 0
local unit u
local unit un
loop
exitwhen i > CCOUNT
set c = COMET[i]
set u = c.self
call SetUnitX(u, GetUnitX(u) + c.speed / 25 * Cos(c.angle * bj_DEGTORAD))
call SetUnitY(u, GetUnitY(u) + c.speed / 25 * Sin(c.angle * bj_DEGTORAD))
if OutofMap(GetUnitX(u), GetUnitY(u)) then
call CometDestroy(c)
endif
loop
exitwhen int > c.countfollow
set un = c.follow[int]
call SetUnitX(un, GetUnitX(un) + c.speed / 25 * Cos(c.angle * bj_DEGTORAD))
call SetUnitY(un, GetUnitY(un) + c.speed / 25 * Sin(c.angle * bj_DEGTORAD))
set int = int + 1
endloop
set u = null
set i = i + 1
endloop
if CCOUNT == 0 then
call PauseTimer(CTIMER)
endif
endfunction
function MovePlanets takes nothing returns nothing
local planet p
local integer i = 0
loop
exitwhen i > PCOUNT
set p = PLANET[i]
set p.angle = p.angle + p.circlingspeed
call SetUnitX(p.self, GetUnitX(p.father) + p.distance * Cos(p.angle * bj_DEGTORAD))
call SetUnitY(p.self, GetUnitY(p.father) + p.distance * Sin(p.angle * bj_DEGTORAD))
call SetUnitFacing(p.self, GetUnitFacing(p.self) + p.rotationspeed)
set i = i + 1
endloop
if PCOUNT == 0 then
call PauseTimer(PTIMER)
endif
endfunction
function MainCreate takes real x, real y, real angle, real rotationspeed, real size, integer model, integer pnum returns nothing
local planet mainplanet = planet.create()
local unit u = CreateUnit(Player(pnum), model, x, y, angle)
call SetUnitScale(u, size, size, size)
set mainplanet.self = u
set mainplanet.rotationspeed = 14.4 * rotationspeed
set mainplanet.angle = angle
set PCOUNT = PCOUNT + 1
set PLANET[PCOUNT - 1] = mainplanet
if PCOUNT - 1 == 0 then
call TimerStart(PTIMER, 0.04, true, function MovePlanets)
endif
set u = null
endfunction
function MoonCreate takes planet p, real distance, real angle, real rotationspeed, real circlingspeed, real size, integer model, integer pnum returns nothing
local planet moon = planet.create()
local unit u = CreateUnit(Player(pnum), model, GetUnitX(p.self) + distance * Cos(angle * bj_DEGTORAD), GetUnitY(p.self) + distance * Sin(angle * bj_DEGTORAD), 0)
if p == null then
call BJDebugMsg("Error: planet p is null")
endif
call SetUnitScale(u, size, size, size)
set moon.self = u
set moon.father = p.self
set moon.distance = distance
set moon.rotationspeed = 14.4 * rotationspeed
set moon.circlingspeed = 14.4 * circlingspeed
set moon.angle = angle
set PCOUNT = PCOUNT + 1
set PLANET[PCOUNT - 1] = moon
set u = null
endfunction
function DestroyPlanet takes planet d returns nothing
local planet p
local integer i = 0
loop
exitwhen i > PCOUNT
set p = PLANET[i]
if p.father == d.self then
set PLANET[PCOUNT] = PLANET[PCOUNT-1]
set PCOUNT = PCOUNT -1
call KillUnit(p.self)
call p.destroy()
endif
set i = i + 1
endloop
set PLANET[PCOUNT] = PLANET[PCOUNT-1]
set PCOUNT = PCOUNT -1
call KillUnit(d.self)
call d.destroy()
endfunction
function CometCreate takes real x, real y, real angle, real speed, real size, integer model, integer pnum, integer fnumber, real fdistance returns nothing
local comet c = comet.create()
local unit u = CreateUnit(Player(pnum), model, x, y, angle)
local unit follow
local integer i = 1
loop
exitwhen i > fnumber
set follow = CreateUnit(Player(pnum), model, x - fdistance * i * Cos(angle * bj_DEGTORAD), y - fdistance * i * Sin(angle * bj_DEGTORAD), angle)
call SetUnitVertexColor(follow, 100, 100, 100, 100 * -i)
call SetUnitScale(follow, 100 - 10 * i, 100 - 10 * i, 100 - 10 * i)
set c.follow[c.countfollow] = follow
set c.countfollow = c.countfollow + 1
set c.follow[c.countfollow - 1] = follow
set i = i + 1
endloop
set c.self = u
set c.speed = speed
set c.angle = angle
call SetUnitScale(u, size, size, size)
if CCOUNT == 0 then
call TimerStart(CTIMER, 0.04, true, function MoveComets)
endif
set CCOUNT = CCOUNT + 1
set COMET[CCOUNT - 1] = c
set u = null
endfunction
//===================== Helper functions =======================
function LastCreatedPlanet takes nothing returns planet
return PLANET[PCOUNT - 1]
endfunction
function LastCreatedComet takes nothing returns comet
return COMET[CCOUNT - 1]
endfunction
//===================== Setting planet properties functions =======================
function SetPlanetName takes planet p, string s returns nothing
set p.name = s
endfunction
function SetPlanetRotationSpeed takes planet p, real rotationspeed returns nothing
set p.rotationspeed = rotationspeed
endfunction
function SetPlanetDistance takes planet p, real distance returns nothing
set p.distance = distance
endfunction
function SetPlanetCirclingSpeed takes planet p, real circlingspeed returns nothing
set p.circlingspeed = circlingspeed
endfunction
function SetPlanetSize takes planet p, real size returns nothing
call SetUnitScale(p.self, size, size, size)
endfunction
function SetPlanetOwner takes planet p, integer pnum returns nothing
call SetUnitOwner(p.self, Player(pnum), true)
endfunction
//===================== Setting comet properties functions =======================
function SetCometName takes comet c, string s returns nothing
set c.name = s
endfunction
function SetCometSpeed takes comet c, real s returns nothing
set c.speed = s
endfunction
function SetCometSize takes comet c, real size returns nothing
call SetUnitScale(c.self, size, size, size)
endfunction
function SetCometOwner takes comet c, integer pnum returns nothing
call SetUnitOwner(c.self, Player(pnum), true)
endfunction
endlibrary