Advertisement
crabb

keiki_example

Jul 5th, 2020
1,172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.39 KB | None | 0 0
  1. local keiki = require("keiki")
  2.  
  3. --[[
  4.     * a usual (slow) game's code goes something like this:
  5.     *
  6.     * list = keiki.new(100)
  7.     * while wait() do
  8.     *  list:iter(list_update_function)
  9.     * end
  10. --]]
  11.  
  12.  
  13. --> a list of bullets. each has a 2D position, speed, and angle.
  14. local list_bullet = keiki.new(512,function(may,i,o)
  15.     o.position = Vector2.new()
  16.     o.speed = 0
  17.     o.angle = 0
  18. end)
  19.  
  20. --> loops through all bullets in the list, to move them all at once.
  21. local function bullet_update(list,i,o)
  22.     -- if the current object is dead, then don't update it!
  23.     if not o.dead then
  24.         local angle = o.angle
  25.         local speed = o.speed
  26.         local velocity = speed * Vector2.new(math.cos(angle),math.sin(angle))
  27.        
  28.         o.position = o.position + velocity
  29.     end
  30. end
  31.  
  32. --> creates a new bullet in the list.
  33. local function bullet_add(pos,spd,ang)
  34.     -- > 0 is the first argument, since there aren't
  35.     -- >>any other types of bullets.
  36.     list_bullet:add(0,function(list,i,o)
  37.         o.position = pos
  38.         o.speed = spd
  39.         o.angle = ang
  40.     end)
  41. end
  42.  
  43. --> now let's actually shoot some damn bullets for once
  44. local timer = 0
  45. game:GetService("RunService").Heartbeat:Connect(function(dt)
  46.     -- every 0.5 seconds, add a new bullet.
  47.     if timer>=0.5 then
  48.         timer = 0
  49.         bullet_add( Vector2.new(0,0),4,math.rad(timer*10) )
  50.     end
  51.     -- now, update the bullets.
  52.     list_bullet:iter( bullet_update )
  53.     -- wait and add to the timer
  54.     timer = timer + dt
  55. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement