Advertisement
Guest User

Untitled

a guest
May 4th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.09 KB | None | 0 0
  1. local time = 0
  2. local bullets = {}
  3. local targetTable = {} -- table of target projectiles
  4. local newestTarget = 0 -- what's the newest spawned target?
  5. local numT = 0
  6. Encounter.SetVar("wavetimer",5.0)
  7. Arena.Resize(300,300)
  8. function Update()
  9.     time = time + 1
  10.     if time <= 105 then
  11.         if time%15 == 0 then
  12.             local t = CreateProjectile('Target', Player.x + math.random(35,-35), Player.y + math.random(35,-35))
  13.             numT = numT + 1
  14.             if newestTarget == 0 then
  15.                 newestTarget = newestTarget + 1 -- increment newestTarget so we know what to remove
  16.             else
  17.                 newestTarget = newestTarget + 4 -- increment newestTarget so we know what to remove
  18.             end
  19.             table.insert(targetTable, t) -- insert target to table, so we can delete it later
  20.             t.SetVar('tNum', numT)
  21.             Audio.PlaySound("targetsound")
  22.             --DEBUG("Created some bullets")
  23.             --DEBUG("newestTarget = " .. newestTarget)
  24.             end
  25.     else
  26.         for _,t in ipairs(targetTable) do
  27.             function CheckNumberx(bulletnum) -- this function will take your bullet's target number, and try to pair it up with its target.
  28.                 local target = nil
  29.                 for t,v in pairs (targetTable) do
  30.                     target = targetTable[t]
  31.                 end
  32.                 if target ~= nil then
  33.                     DEBUG("Target's X Is:"..tostring(target.x)) -- tells you where your target is. if this fails, let me know.
  34.                     return target.x
  35.                 end
  36.             end
  37.             function CheckNumbery(bulletnum)-- same here, just swapped coordinates.
  38.                 local target = nil
  39.                 for t,v in pairs (targetTable) do
  40.                     target = targetTable[t]
  41.                 end
  42.                 if target ~= nil then
  43.                     DEBUG("Target's Y Is:"..tostring(target.y))
  44.                     return target.y
  45.                 end
  46.             end
  47.             for k=1,2 do
  48.                 for l=1,2 do
  49.                     b = CreateProjectile('SeekerBullet', Arena.width*(2*k-3), Arena.height*(2*l-3))
  50.                     table.insert(bullets, b)
  51.                     b.SetVar('TX', CheckNumberx(b.GetVar('bulletnum'))) -- both of these are going to use the functions above to get the specified target. (based off of the number)
  52.                     b.SetVar('TY', CheckNumbery(b.GetVar('bulletnum')))
  53.                     b.SetVar('pastx', TX)
  54.                     b.SetVar('pasty', TY)
  55.                     b.SetVar('bpastx', b.x)
  56.                     b.SetVar('bpasty', b.y)
  57.                     b.SetVar('xspeed', 0)
  58.                     b.SetVar('yspeed', 0)
  59.                 end
  60.             end
  61.         end
  62.     end
  63.     --for _,b in ipairs(bullets) do -- not sure WHAT the hell this is doing, so it's gone
  64.     --    if b.isactive then
  65.     --        DEBUG("There are "..#bullets.." bullets")
  66.     --        local pastx = b.GetVar('pastx')
  67.     --        local pasty = b.GetVar('pasty')
  68.     --        local bpastx = b.GetVar('bpastx')
  69.     --        local bpasty = b.GetVar('bpasty')
  70.     --        local dx = pastx - bpastx
  71.     --        local dy = pasty - bpasty
  72.     --        local xspeed = b.GetVar('xspeed') / 2 + dx / 75
  73.     --        local yspeed = b.GetVar('yspeed') / 2 + dy / 75
  74.     --        b.Move(xspeed, yspeed)
  75.     --        b.SetVar('xspeed', xspeed)
  76.     --        b.SetVar('yspeed', yspeed)
  77.     --        if b.x >= pastx - 8 and b.x <= pastx + 8 then
  78.     --            if b.y >= pasty - 8 and b.x <= pasty + 8 then
  79.     --                b.Remove()
  80.     --            end
  81.     --        end
  82.     --    end
  83.     --end
  84.    
  85.    
  86.     for i = 1, #bullets do
  87.         local bullet = bullets[i] -- get the current bullet we're manipulating
  88.         local pastx = b.GetVar('pastx') -- get the target's X
  89.         local pasty = b.GetVar('pasty') -- get the target's Y
  90.         local bpastx = b.GetVar('bpastx') -- get the bullet's original X
  91.         local bpasty = b.GetVar('bpasty') -- get the bullet's original Y
  92.         local dx = pastx - bpastx   -- calculate difference
  93.         local dy = pasty - bpasty   -- ^
  94.         local xspeed = bullet.GetVar('xspeed') / 2 + dx / 50 -- get the x speed
  95.         local yspeed = bullet.GetVar('yspeed') / 2 + dy / 50 -- get the y speed
  96.  
  97.         if bullet.isactive then -- is this bullet even active?
  98.             bullet.Move(xspeed, yspeed) -- move the bullet
  99.             bullet.SetVar('xspeed', xspeed) -- new X speed
  100.             bullet.SetVar('yspeed', yspeed) -- new Y speed
  101.             if distanceGeneric(pastx, bullet.x, pasty, bullet.y) < 7 then -- are we closer than 5 pixels to the center?
  102.                 bullet.Remove() -- if so, delete bullet
  103.                 if targetTable[newestTarget].isactive then -- here we are assuming that there will never be more than one target on the screen
  104.                     targetTable[newestTarget].Remove()     -- at the same time, so we remove the latest target once any bullet 'collides' with its center
  105.                 end
  106.             end
  107.         else -- we've already deleted the bullet?
  108.             -- don't do anything to it
  109.         end
  110.     end
  111. end
  112.  
  113. function distanceToSpr(spr1, spr2) -- sprite 1, sprite 2; returns distance between two sprites/objects/the player given x and y positions
  114.     local distance = (math.sqrt(math.pow(spr2.x - spr1.x, 2) + math.pow(spr2.y - spr1.y, 2))) -- actually unused but useful anyway
  115.     return distance
  116. end
  117.  
  118. function distanceGeneric(no1, no2, no1b, no2b) -- x1, x2, y1, y2; returns distance between two objects given x and y positions
  119.     local distance = (math.sqrt(math.pow(no2 - no1, 2) + math.pow(no2b - no1b, 2)))
  120.     return distance
  121. end
  122.  
  123. function OnHit(bullet)
  124.     local atk = GetGlobal("atk")
  125.     if bullet.GetVar('xspeed') ~= nil then -- bullet?
  126.         Player.Hurt(atk)
  127.     end -- must be target, so do nothing
  128. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement