Skip to content

Instantly share code, notes, and snippets.

@TinkerWorX
Created April 27, 2018 12:18
Show Gist options
  • Save TinkerWorX/7ef9d51a1072522ed5bc4583d0285582 to your computer and use it in GitHub Desktop.
Save TinkerWorX/7ef9d51a1072522ed5bc4583d0285582 to your computer and use it in GitHub Desktop.
library itemscatter initializer init
globals
// config
private real RADIUS = 32.00
private real PERIOD = 1.00 / 64.00
// internal
private item current = null
endglobals
private function Abs takes real r returns real
if (r < 0.00) then
return -r
endif
return r
endfunction
private function filter takes nothing returns boolean
return true
endfunction
private function callbackB takes nothing returns nothing
local real sr = RADIUS
local real cx
local real cy
local real ox
local real oy
local real dx
local real dy
local real a
local real mx
local real my
local item other = GetEnumItem()
if (current == other) then
return
endif
set cx = GetItemX(current)
set cy = GetItemY(current)
set ox = GetItemX(other)
set oy = GetItemY(other)
set dx = cx - ox
set dy = cy - oy
if (Abs(dx) < 0.01) then
set dx = GetRandomReal(-0.10, 0.10)
endif
if (Abs(dy) < 0.01) then
set dy = GetRandomReal(-0.10, 0.10)
endif
if (dx * dx + dy * dy < sr * sr) then // we're too close
set mx = 0.50 * (cx + ox)
set my = 0.50 * (cy + oy)
set a = Atan2(dy, dx)
set cx = mx - Cos(a) * RADIUS
set cy = my - Sin(a) * RADIUS
set ox = mx + Cos(a) * RADIUS
set oy = my + Sin(a) * RADIUS
call SetItemPosition(current, cx, cy)
call SetItemPosition(other, ox, oy)
endif
set other = null
endfunction
private function callbackA takes nothing returns nothing
set current = GetEnumItem()
call EnumItemsInRect(GetWorldBounds(), function filter, function callbackB)
endfunction
private function action takes nothing returns nothing
call EnumItemsInRect(GetWorldBounds(), function filter, function callbackA)
endfunction
// remove this
private function test takes nothing returns nothing
local integer i = 0
loop
call CreateItem('afac', 0.00, -3328.00)
set i = i + 1
exitwhen i == 16
endloop
endfunction
private function init takes nothing returns nothing
local trigger t = CreateTrigger()
call TriggerAddAction(t, function action)
call TriggerRegisterTimerEvent(t, PERIOD, true)
call test()
endfunction
endlibrary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment