Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local time = 0
- local bullets = {}
- local targetTable = {} -- table of target projectiles
- local newestTarget = 0 -- what's the newest spawned target?
- local numT = 0
- Encounter.SetVar("wavetimer",5.0)
- Arena.Resize(300,300)
- function Update()
- time = time + 1
- if time <= 105 then
- if time%15 == 0 then
- local t = CreateProjectile('Target', Player.x + math.random(35,-35), Player.y + math.random(35,-35))
- numT = numT + 1
- if newestTarget == 0 then
- newestTarget = newestTarget + 1 -- increment newestTarget so we know what to remove
- else
- newestTarget = newestTarget + 4 -- increment newestTarget so we know what to remove
- end
- table.insert(targetTable, t) -- insert target to table, so we can delete it later
- t.SetVar('tNum', numT)
- Audio.PlaySound("targetsound")
- --DEBUG("Created some bullets")
- --DEBUG("newestTarget = " .. newestTarget)
- end
- else
- for _,t in ipairs(targetTable) do
- function CheckNumberx(bulletnum) -- this function will take your bullet's target number, and try to pair it up with its target.
- local target = nil
- for t,v in pairs (targetTable) do
- target = targetTable[t]
- end
- if target ~= nil then
- DEBUG("Target's X Is:"..tostring(target.x)) -- tells you where your target is. if this fails, let me know.
- return target.x
- end
- end
- function CheckNumbery(bulletnum)-- same here, just swapped coordinates.
- local target = nil
- for t,v in pairs (targetTable) do
- target = targetTable[t]
- end
- if target ~= nil then
- DEBUG("Target's Y Is:"..tostring(target.y))
- return target.y
- end
- end
- for k=1,2 do
- for l=1,2 do
- b = CreateProjectile('SeekerBullet', Arena.width*(2*k-3), Arena.height*(2*l-3))
- table.insert(bullets, b)
- 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)
- b.SetVar('TY', CheckNumbery(b.GetVar('bulletnum')))
- b.SetVar('pastx', TX)
- b.SetVar('pasty', TY)
- b.SetVar('bpastx', b.x)
- b.SetVar('bpasty', b.y)
- b.SetVar('xspeed', 0)
- b.SetVar('yspeed', 0)
- end
- end
- end
- end
- --for _,b in ipairs(bullets) do -- not sure WHAT the hell this is doing, so it's gone
- -- if b.isactive then
- -- DEBUG("There are "..#bullets.." bullets")
- -- local pastx = b.GetVar('pastx')
- -- local pasty = b.GetVar('pasty')
- -- local bpastx = b.GetVar('bpastx')
- -- local bpasty = b.GetVar('bpasty')
- -- local dx = pastx - bpastx
- -- local dy = pasty - bpasty
- -- local xspeed = b.GetVar('xspeed') / 2 + dx / 75
- -- local yspeed = b.GetVar('yspeed') / 2 + dy / 75
- -- b.Move(xspeed, yspeed)
- -- b.SetVar('xspeed', xspeed)
- -- b.SetVar('yspeed', yspeed)
- -- if b.x >= pastx - 8 and b.x <= pastx + 8 then
- -- if b.y >= pasty - 8 and b.x <= pasty + 8 then
- -- b.Remove()
- -- end
- -- end
- -- end
- --end
- for i = 1, #bullets do
- local bullet = bullets[i] -- get the current bullet we're manipulating
- local pastx = b.GetVar('pastx') -- get the target's X
- local pasty = b.GetVar('pasty') -- get the target's Y
- local bpastx = b.GetVar('bpastx') -- get the bullet's original X
- local bpasty = b.GetVar('bpasty') -- get the bullet's original Y
- local dx = pastx - bpastx -- calculate difference
- local dy = pasty - bpasty -- ^
- local xspeed = bullet.GetVar('xspeed') / 2 + dx / 50 -- get the x speed
- local yspeed = bullet.GetVar('yspeed') / 2 + dy / 50 -- get the y speed
- if bullet.isactive then -- is this bullet even active?
- bullet.Move(xspeed, yspeed) -- move the bullet
- bullet.SetVar('xspeed', xspeed) -- new X speed
- bullet.SetVar('yspeed', yspeed) -- new Y speed
- if distanceGeneric(pastx, bullet.x, pasty, bullet.y) < 7 then -- are we closer than 5 pixels to the center?
- bullet.Remove() -- if so, delete bullet
- if targetTable[newestTarget].isactive then -- here we are assuming that there will never be more than one target on the screen
- targetTable[newestTarget].Remove() -- at the same time, so we remove the latest target once any bullet 'collides' with its center
- end
- end
- else -- we've already deleted the bullet?
- -- don't do anything to it
- end
- end
- end
- function distanceToSpr(spr1, spr2) -- sprite 1, sprite 2; returns distance between two sprites/objects/the player given x and y positions
- local distance = (math.sqrt(math.pow(spr2.x - spr1.x, 2) + math.pow(spr2.y - spr1.y, 2))) -- actually unused but useful anyway
- return distance
- end
- function distanceGeneric(no1, no2, no1b, no2b) -- x1, x2, y1, y2; returns distance between two objects given x and y positions
- local distance = (math.sqrt(math.pow(no2 - no1, 2) + math.pow(no2b - no1b, 2)))
- return distance
- end
- function OnHit(bullet)
- local atk = GetGlobal("atk")
- if bullet.GetVar('xspeed') ~= nil then -- bullet?
- Player.Hurt(atk)
- end -- must be target, so do nothing
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement